PageRenderTime 27ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src/router/php/Zend/zend_objects_API.c

https://gitlab.com/envieidoc/advancedtomato2
C | 423 lines | 296 code | 74 blank | 53 comment | 41 complexity | 9e4317cb290a560a66e0e6484baf7644 MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2014 Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@zend.com> |
  16. | Zeev Suraski <zeev@zend.com> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. #include "zend.h"
  21. #include "zend_globals.h"
  22. #include "zend_variables.h"
  23. #include "zend_API.h"
  24. #include "zend_objects_API.h"
  25. #define ZEND_DEBUG_OBJECTS 0
  26. ZEND_API void zend_objects_store_init(zend_objects_store *objects, zend_uint init_size)
  27. {
  28. objects->object_buckets = (zend_object_store_bucket *) emalloc(init_size * sizeof(zend_object_store_bucket));
  29. objects->top = 1; /* Skip 0 so that handles are true */
  30. objects->size = init_size;
  31. objects->free_list_head = -1;
  32. memset(&objects->object_buckets[0], 0, sizeof(zend_object_store_bucket));
  33. }
  34. ZEND_API void zend_objects_store_destroy(zend_objects_store *objects)
  35. {
  36. efree(objects->object_buckets);
  37. objects->object_buckets = NULL;
  38. }
  39. ZEND_API void zend_objects_store_call_destructors(zend_objects_store *objects TSRMLS_DC)
  40. {
  41. zend_uint i = 1;
  42. for (i = 1; i < objects->top ; i++) {
  43. if (objects->object_buckets[i].valid) {
  44. struct _store_object *obj = &objects->object_buckets[i].bucket.obj;
  45. if (!objects->object_buckets[i].destructor_called) {
  46. objects->object_buckets[i].destructor_called = 1;
  47. if (obj->dtor && obj->object) {
  48. obj->refcount++;
  49. obj->dtor(obj->object, i TSRMLS_CC);
  50. obj = &objects->object_buckets[i].bucket.obj;
  51. obj->refcount--;
  52. if (obj->refcount == 0) {
  53. /* in case gc_collect_cycle is triggered before free_storage */
  54. GC_REMOVE_ZOBJ_FROM_BUFFER(obj);
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }
  61. ZEND_API void zend_objects_store_mark_destructed(zend_objects_store *objects TSRMLS_DC)
  62. {
  63. zend_uint i;
  64. if (!objects->object_buckets) {
  65. return;
  66. }
  67. for (i = 1; i < objects->top ; i++) {
  68. if (objects->object_buckets[i].valid) {
  69. objects->object_buckets[i].destructor_called = 1;
  70. }
  71. }
  72. }
  73. ZEND_API void zend_objects_store_free_object_storage(zend_objects_store *objects TSRMLS_DC)
  74. {
  75. zend_uint i = 1;
  76. for (i = 1; i < objects->top ; i++) {
  77. if (objects->object_buckets[i].valid) {
  78. struct _store_object *obj = &objects->object_buckets[i].bucket.obj;
  79. GC_REMOVE_ZOBJ_FROM_BUFFER(obj);
  80. objects->object_buckets[i].valid = 0;
  81. if (obj->free_storage) {
  82. obj->free_storage(obj->object TSRMLS_CC);
  83. }
  84. /* Not adding to free list as we are shutting down anyway */
  85. }
  86. }
  87. }
  88. /* Store objects API */
  89. ZEND_API zend_object_handle zend_objects_store_put(void *object, zend_objects_store_dtor_t dtor, zend_objects_free_object_storage_t free_storage, zend_objects_store_clone_t clone TSRMLS_DC)
  90. {
  91. zend_object_handle handle;
  92. struct _store_object *obj;
  93. if (EG(objects_store).free_list_head != -1) {
  94. handle = EG(objects_store).free_list_head;
  95. EG(objects_store).free_list_head = EG(objects_store).object_buckets[handle].bucket.free_list.next;
  96. } else {
  97. if (EG(objects_store).top == EG(objects_store).size) {
  98. EG(objects_store).size <<= 1;
  99. EG(objects_store).object_buckets = (zend_object_store_bucket *) erealloc(EG(objects_store).object_buckets, EG(objects_store).size * sizeof(zend_object_store_bucket));
  100. }
  101. handle = EG(objects_store).top++;
  102. }
  103. obj = &EG(objects_store).object_buckets[handle].bucket.obj;
  104. EG(objects_store).object_buckets[handle].destructor_called = 0;
  105. EG(objects_store).object_buckets[handle].valid = 1;
  106. EG(objects_store).object_buckets[handle].apply_count = 0;
  107. obj->refcount = 1;
  108. GC_OBJ_INIT(obj);
  109. obj->object = object;
  110. obj->dtor = dtor?dtor:(zend_objects_store_dtor_t)zend_objects_destroy_object;
  111. obj->free_storage = free_storage;
  112. obj->clone = clone;
  113. obj->handlers = NULL;
  114. #if ZEND_DEBUG_OBJECTS
  115. fprintf(stderr, "Allocated object id #%d\n", handle);
  116. #endif
  117. return handle;
  118. }
  119. ZEND_API zend_uint zend_objects_store_get_refcount(zval *object TSRMLS_DC)
  120. {
  121. zend_object_handle handle = Z_OBJ_HANDLE_P(object);
  122. return EG(objects_store).object_buckets[handle].bucket.obj.refcount;
  123. }
  124. ZEND_API void zend_objects_store_add_ref(zval *object TSRMLS_DC)
  125. {
  126. zend_object_handle handle = Z_OBJ_HANDLE_P(object);
  127. EG(objects_store).object_buckets[handle].bucket.obj.refcount++;
  128. #if ZEND_DEBUG_OBJECTS
  129. fprintf(stderr, "Increased refcount of object id #%d\n", handle);
  130. #endif
  131. }
  132. /*
  133. * Add a reference to an objects store entry given the object handle.
  134. */
  135. ZEND_API void zend_objects_store_add_ref_by_handle(zend_object_handle handle TSRMLS_DC)
  136. {
  137. EG(objects_store).object_buckets[handle].bucket.obj.refcount++;
  138. }
  139. #define ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST() \
  140. EG(objects_store).object_buckets[handle].bucket.free_list.next = EG(objects_store).free_list_head; \
  141. EG(objects_store).free_list_head = handle; \
  142. EG(objects_store).object_buckets[handle].valid = 0;
  143. ZEND_API void zend_objects_store_del_ref(zval *zobject TSRMLS_DC)
  144. {
  145. zend_object_handle handle;
  146. handle = Z_OBJ_HANDLE_P(zobject);
  147. Z_ADDREF_P(zobject);
  148. zend_objects_store_del_ref_by_handle_ex(handle, Z_OBJ_HT_P(zobject) TSRMLS_CC);
  149. Z_DELREF_P(zobject);
  150. GC_ZOBJ_CHECK_POSSIBLE_ROOT(zobject);
  151. }
  152. /*
  153. * Delete a reference to an objects store entry given the object handle.
  154. */
  155. ZEND_API void zend_objects_store_del_ref_by_handle_ex(zend_object_handle handle, const zend_object_handlers *handlers TSRMLS_DC) /* {{{ */
  156. {
  157. struct _store_object *obj;
  158. int failure = 0;
  159. if (!EG(objects_store).object_buckets) {
  160. return;
  161. }
  162. obj = &EG(objects_store).object_buckets[handle].bucket.obj;
  163. /* Make sure we hold a reference count during the destructor call
  164. otherwise, when the destructor ends the storage might be freed
  165. when the refcount reaches 0 a second time
  166. */
  167. if (EG(objects_store).object_buckets[handle].valid) {
  168. if (obj->refcount == 1) {
  169. if (!EG(objects_store).object_buckets[handle].destructor_called) {
  170. EG(objects_store).object_buckets[handle].destructor_called = 1;
  171. if (obj->dtor) {
  172. if (handlers && !obj->handlers) {
  173. obj->handlers = handlers;
  174. }
  175. zend_try {
  176. obj->dtor(obj->object, handle TSRMLS_CC);
  177. } zend_catch {
  178. failure = 1;
  179. } zend_end_try();
  180. }
  181. }
  182. /* re-read the object from the object store as the store might have been reallocated in the dtor */
  183. obj = &EG(objects_store).object_buckets[handle].bucket.obj;
  184. if (obj->refcount == 1) {
  185. GC_REMOVE_ZOBJ_FROM_BUFFER(obj);
  186. if (obj->free_storage) {
  187. zend_try {
  188. obj->free_storage(obj->object TSRMLS_CC);
  189. } zend_catch {
  190. failure = 1;
  191. } zend_end_try();
  192. }
  193. ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST();
  194. }
  195. }
  196. }
  197. obj->refcount--;
  198. #if ZEND_DEBUG_OBJECTS
  199. if (obj->refcount == 0) {
  200. fprintf(stderr, "Deallocated object id #%d\n", handle);
  201. } else {
  202. fprintf(stderr, "Decreased refcount of object id #%d\n", handle);
  203. }
  204. #endif
  205. if (failure) {
  206. zend_bailout();
  207. }
  208. }
  209. /* }}} */
  210. ZEND_API zend_object_value zend_objects_store_clone_obj(zval *zobject TSRMLS_DC)
  211. {
  212. zend_object_value retval;
  213. void *new_object;
  214. struct _store_object *obj;
  215. zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
  216. obj = &EG(objects_store).object_buckets[handle].bucket.obj;
  217. if (obj->clone == NULL) {
  218. zend_error(E_CORE_ERROR, "Trying to clone uncloneable object of class %s", Z_OBJCE_P(zobject)->name);
  219. }
  220. obj->clone(obj->object, &new_object TSRMLS_CC);
  221. obj = &EG(objects_store).object_buckets[handle].bucket.obj;
  222. retval.handle = zend_objects_store_put(new_object, obj->dtor, obj->free_storage, obj->clone TSRMLS_CC);
  223. retval.handlers = Z_OBJ_HT_P(zobject);
  224. EG(objects_store).object_buckets[handle].bucket.obj.handlers = retval.handlers;
  225. return retval;
  226. }
  227. ZEND_API void *zend_object_store_get_object(const zval *zobject TSRMLS_DC)
  228. {
  229. zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
  230. return EG(objects_store).object_buckets[handle].bucket.obj.object;
  231. }
  232. /*
  233. * Retrieve an entry from the objects store given the object handle.
  234. */
  235. ZEND_API void *zend_object_store_get_object_by_handle(zend_object_handle handle TSRMLS_DC)
  236. {
  237. return EG(objects_store).object_buckets[handle].bucket.obj.object;
  238. }
  239. /* zend_object_store_set_object:
  240. * It is ONLY valid to call this function from within the constructor of an
  241. * overloaded object. Its purpose is to set the object pointer for the object
  242. * when you can't possibly know its value until you have parsed the arguments
  243. * from the constructor function. You MUST NOT use this function for any other
  244. * weird games, or call it at any other time after the object is constructed.
  245. * */
  246. ZEND_API void zend_object_store_set_object(zval *zobject, void *object TSRMLS_DC)
  247. {
  248. zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
  249. EG(objects_store).object_buckets[handle].bucket.obj.object = object;
  250. }
  251. /* Called when the ctor was terminated by an exception */
  252. ZEND_API void zend_object_store_ctor_failed(zval *zobject TSRMLS_DC)
  253. {
  254. zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
  255. zend_object_store_bucket *obj_bucket = &EG(objects_store).object_buckets[handle];
  256. obj_bucket->bucket.obj.handlers = Z_OBJ_HT_P(zobject);;
  257. obj_bucket->destructor_called = 1;
  258. }
  259. /* Proxy objects workings */
  260. typedef struct _zend_proxy_object {
  261. zval *object;
  262. zval *property;
  263. } zend_proxy_object;
  264. static zend_object_handlers zend_object_proxy_handlers;
  265. ZEND_API void zend_objects_proxy_destroy(zend_object *object, zend_object_handle handle TSRMLS_DC)
  266. {
  267. }
  268. ZEND_API void zend_objects_proxy_free_storage(zend_proxy_object *object TSRMLS_DC)
  269. {
  270. zval_ptr_dtor(&object->object);
  271. zval_ptr_dtor(&object->property);
  272. efree(object);
  273. }
  274. ZEND_API void zend_objects_proxy_clone(zend_proxy_object *object, zend_proxy_object **object_clone TSRMLS_DC)
  275. {
  276. *object_clone = emalloc(sizeof(zend_proxy_object));
  277. (*object_clone)->object = object->object;
  278. (*object_clone)->property = object->property;
  279. zval_add_ref(&(*object_clone)->property);
  280. zval_add_ref(&(*object_clone)->object);
  281. }
  282. ZEND_API zval *zend_object_create_proxy(zval *object, zval *member TSRMLS_DC)
  283. {
  284. zend_proxy_object *pobj = emalloc(sizeof(zend_proxy_object));
  285. zval *retval;
  286. pobj->object = object;
  287. zval_add_ref(&pobj->object);
  288. ALLOC_ZVAL(pobj->property);
  289. INIT_PZVAL_COPY(pobj->property, member);
  290. zval_copy_ctor(pobj->property);
  291. MAKE_STD_ZVAL(retval);
  292. Z_TYPE_P(retval) = IS_OBJECT;
  293. Z_OBJ_HANDLE_P(retval) = zend_objects_store_put(pobj, (zend_objects_store_dtor_t)zend_objects_proxy_destroy, (zend_objects_free_object_storage_t) zend_objects_proxy_free_storage, (zend_objects_store_clone_t) zend_objects_proxy_clone TSRMLS_CC);
  294. Z_OBJ_HT_P(retval) = &zend_object_proxy_handlers;
  295. return retval;
  296. }
  297. ZEND_API void zend_object_proxy_set(zval **property, zval *value TSRMLS_DC)
  298. {
  299. zend_proxy_object *probj = zend_object_store_get_object(*property TSRMLS_CC);
  300. if (Z_OBJ_HT_P(probj->object) && Z_OBJ_HT_P(probj->object)->write_property) {
  301. Z_OBJ_HT_P(probj->object)->write_property(probj->object, probj->property, value, 0 TSRMLS_CC);
  302. } else {
  303. zend_error(E_WARNING, "Cannot write property of object - no write handler defined");
  304. }
  305. }
  306. ZEND_API zval* zend_object_proxy_get(zval *property TSRMLS_DC)
  307. {
  308. zend_proxy_object *probj = zend_object_store_get_object(property TSRMLS_CC);
  309. if (Z_OBJ_HT_P(probj->object) && Z_OBJ_HT_P(probj->object)->read_property) {
  310. return Z_OBJ_HT_P(probj->object)->read_property(probj->object, probj->property, BP_VAR_R, 0 TSRMLS_CC);
  311. } else {
  312. zend_error(E_WARNING, "Cannot read property of object - no read handler defined");
  313. }
  314. return NULL;
  315. }
  316. ZEND_API zend_object_handlers *zend_get_std_object_handlers(void)
  317. {
  318. return &std_object_handlers;
  319. }
  320. static zend_object_handlers zend_object_proxy_handlers = {
  321. ZEND_OBJECTS_STORE_HANDLERS,
  322. NULL, /* read_property */
  323. NULL, /* write_property */
  324. NULL, /* read dimension */
  325. NULL, /* write_dimension */
  326. NULL, /* get_property_ptr_ptr */
  327. zend_object_proxy_get, /* get */
  328. zend_object_proxy_set, /* set */
  329. NULL, /* has_property */
  330. NULL, /* unset_property */
  331. NULL, /* has_dimension */
  332. NULL, /* unset_dimension */
  333. NULL, /* get_properties */
  334. NULL, /* get_method */
  335. NULL, /* call_method */
  336. NULL, /* get_constructor */
  337. NULL, /* get_class_entry */
  338. NULL, /* get_class_name */
  339. NULL, /* compare_objects */
  340. NULL, /* cast_object */
  341. NULL, /* count_elements */
  342. };
  343. /*
  344. * Local variables:
  345. * tab-width: 4
  346. * c-basic-offset: 4
  347. * indent-tabs-mode: t
  348. * End:
  349. */