You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

393 lines
12 KiB

22 years ago
22 years ago
22 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2006 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->refcount--;
  51. }
  52. }
  53. }
  54. }
  55. }
  56. ZEND_API void zend_objects_store_mark_destructed(zend_objects_store *objects TSRMLS_DC)
  57. {
  58. zend_uint i;
  59. if (!objects->object_buckets) {
  60. return;
  61. }
  62. for (i = 1; i < objects->top ; i++) {
  63. if (objects->object_buckets[i].valid) {
  64. objects->object_buckets[i].destructor_called = 1;
  65. }
  66. }
  67. }
  68. ZEND_API void zend_objects_store_free_object_storage(zend_objects_store *objects TSRMLS_DC)
  69. {
  70. zend_uint i = 1;
  71. for (i = 1; i < objects->top ; i++) {
  72. if (objects->object_buckets[i].valid) {
  73. struct _store_object *obj = &objects->object_buckets[i].bucket.obj;
  74. objects->object_buckets[i].valid = 0;
  75. if (obj->free_storage) {
  76. obj->free_storage(obj->object TSRMLS_CC);
  77. }
  78. /* Not adding to free list as we are shutting down anyway */
  79. }
  80. }
  81. }
  82. /* Store objects API */
  83. 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)
  84. {
  85. zend_object_handle handle;
  86. struct _store_object *obj;
  87. if (EG(objects_store).free_list_head != -1) {
  88. handle = EG(objects_store).free_list_head;
  89. EG(objects_store).free_list_head = EG(objects_store).object_buckets[handle].bucket.free_list.next;
  90. } else {
  91. if (EG(objects_store).top == EG(objects_store).size) {
  92. EG(objects_store).size <<= 1;
  93. EG(objects_store).object_buckets = (zend_object_store_bucket *) erealloc(EG(objects_store).object_buckets, EG(objects_store).size * sizeof(zend_object_store_bucket));
  94. }
  95. handle = EG(objects_store).top++;
  96. }
  97. obj = &EG(objects_store).object_buckets[handle].bucket.obj;
  98. EG(objects_store).object_buckets[handle].destructor_called = 0;
  99. EG(objects_store).object_buckets[handle].valid = 1;
  100. obj->refcount = 1;
  101. obj->object = object;
  102. obj->dtor = dtor?dtor:(zend_objects_store_dtor_t)zend_objects_destroy_object;
  103. obj->free_storage = free_storage;
  104. obj->clone = clone;
  105. #if ZEND_DEBUG_OBJECTS
  106. fprintf(stderr, "Allocated object id #%d\n", handle);
  107. #endif
  108. return handle;
  109. }
  110. ZEND_API zend_uint zend_objects_store_get_refcount(zval *object TSRMLS_DC)
  111. {
  112. zend_object_handle handle = Z_OBJ_HANDLE_P(object);
  113. return EG(objects_store).object_buckets[handle].bucket.obj.refcount;
  114. }
  115. ZEND_API void zend_objects_store_add_ref(zval *object TSRMLS_DC)
  116. {
  117. zend_object_handle handle = Z_OBJ_HANDLE_P(object);
  118. EG(objects_store).object_buckets[handle].bucket.obj.refcount++;
  119. #if ZEND_DEBUG_OBJECTS
  120. fprintf(stderr, "Increased refcount of object id #%d\n", handle);
  121. #endif
  122. }
  123. /*
  124. * Add a reference to an objects store entry given the object handle.
  125. */
  126. ZEND_API void zend_objects_store_add_ref_by_handle(zend_object_handle handle TSRMLS_DC)
  127. {
  128. EG(objects_store).object_buckets[handle].bucket.obj.refcount++;
  129. }
  130. #define ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST() \
  131. EG(objects_store).object_buckets[handle].bucket.free_list.next = EG(objects_store).free_list_head; \
  132. EG(objects_store).free_list_head = handle; \
  133. EG(objects_store).object_buckets[handle].valid = 0;
  134. ZEND_API void zend_objects_store_del_ref(zval *zobject TSRMLS_DC)
  135. {
  136. zend_object_handle handle;
  137. handle = Z_OBJ_HANDLE_P(zobject);
  138. zobject->refcount++;
  139. zend_objects_store_del_ref_by_handle(handle TSRMLS_CC);
  140. zobject->refcount--;
  141. }
  142. /*
  143. * Delete a reference to an objects store entry given the object handle.
  144. */
  145. ZEND_API void zend_objects_store_del_ref_by_handle(zend_object_handle handle TSRMLS_DC)
  146. {
  147. struct _store_object *obj;
  148. int failure = 0;
  149. if (!EG(objects_store).object_buckets) {
  150. return;
  151. }
  152. obj = &EG(objects_store).object_buckets[handle].bucket.obj;
  153. /* Make sure we hold a reference count during the destructor call
  154. otherwise, when the destructor ends the storage might be freed
  155. when the refcount reaches 0 a second time
  156. */
  157. if (EG(objects_store).object_buckets[handle].valid) {
  158. if (obj->refcount == 1) {
  159. if (!EG(objects_store).object_buckets[handle].destructor_called) {
  160. EG(objects_store).object_buckets[handle].destructor_called = 1;
  161. if (obj->dtor) {
  162. zend_try {
  163. obj->dtor(obj->object, handle TSRMLS_CC);
  164. } zend_catch {
  165. failure = 1;
  166. } zend_end_try();
  167. }
  168. }
  169. if (obj->refcount == 1) {
  170. if (obj->free_storage) {
  171. zend_try {
  172. obj->free_storage(obj->object TSRMLS_CC);
  173. } zend_catch {
  174. failure = 1;
  175. } zend_end_try();
  176. }
  177. ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST();
  178. }
  179. }
  180. }
  181. obj->refcount--;
  182. #if ZEND_DEBUG_OBJECTS
  183. if (obj->refcount == 0) {
  184. fprintf(stderr, "Deallocated object id #%d\n", handle);
  185. } else {
  186. fprintf(stderr, "Decreased refcount of object id #%d\n", handle);
  187. }
  188. #endif
  189. if (failure) {
  190. zend_bailout();
  191. }
  192. }
  193. ZEND_API zend_object_value zend_objects_store_clone_obj(zval *zobject TSRMLS_DC)
  194. {
  195. zend_object_value retval;
  196. void *new_object;
  197. struct _store_object *obj;
  198. zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
  199. obj = &EG(objects_store).object_buckets[handle].bucket.obj;
  200. if (obj->clone == NULL) {
  201. zend_error(E_CORE_ERROR, "Trying to clone uncloneable object of class %s", Z_OBJCE_P(zobject)->name);
  202. }
  203. obj->clone(obj->object, &new_object TSRMLS_CC);
  204. retval.handle = zend_objects_store_put(new_object, obj->dtor, obj->free_storage, obj->clone TSRMLS_CC);
  205. retval.handlers = Z_OBJ_HT_P(zobject);
  206. return retval;
  207. }
  208. ZEND_API void *zend_object_store_get_object(zval *zobject TSRMLS_DC)
  209. {
  210. zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
  211. return EG(objects_store).object_buckets[handle].bucket.obj.object;
  212. }
  213. /*
  214. * Retrieve an entry from the objects store given the object handle.
  215. */
  216. ZEND_API void *zend_object_store_get_object_by_handle(zend_object_handle handle TSRMLS_DC)
  217. {
  218. return EG(objects_store).object_buckets[handle].bucket.obj.object;
  219. }
  220. /* zend_object_store_set_object:
  221. * It is ONLY valid to call this function from within the constructor of an
  222. * overloaded object. Its purpose is to set the object pointer for the object
  223. * when you can't possibly know its value until you have parsed the arguments
  224. * from the constructor function. You MUST NOT use this function for any other
  225. * weird games, or call it at any other time after the object is constructed.
  226. * */
  227. ZEND_API void zend_object_store_set_object(zval *zobject, void *object TSRMLS_DC)
  228. {
  229. zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
  230. EG(objects_store).object_buckets[handle].bucket.obj.object = object;
  231. }
  232. /* Called when the ctor was terminated by an exception */
  233. ZEND_API void zend_object_store_ctor_failed(zval *zobject TSRMLS_DC)
  234. {
  235. zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
  236. EG(objects_store).object_buckets[handle].destructor_called = 1;
  237. }
  238. /* Proxy objects workings */
  239. typedef struct _zend_proxy_object {
  240. zval *object;
  241. zval *property;
  242. } zend_proxy_object;
  243. static zend_object_handlers zend_object_proxy_handlers;
  244. ZEND_API void zend_objects_proxy_free_storage(zend_proxy_object *object TSRMLS_DC)
  245. {
  246. zval_ptr_dtor(&object->object);
  247. zval_ptr_dtor(&object->property);
  248. efree(object);
  249. }
  250. ZEND_API void zend_objects_proxy_clone(zend_proxy_object *object, zend_proxy_object **object_clone TSRMLS_DC)
  251. {
  252. *object_clone = emalloc(sizeof(zend_proxy_object));
  253. (*object_clone)->object = object->object;
  254. (*object_clone)->property = object->property;
  255. zval_add_ref(&(*object_clone)->property);
  256. zval_add_ref(&(*object_clone)->object);
  257. }
  258. ZEND_API zval *zend_object_create_proxy(zval *object, zval *member TSRMLS_DC)
  259. {
  260. zend_proxy_object *pobj = emalloc(sizeof(zend_proxy_object));
  261. zval *retval;
  262. pobj->object = object;
  263. pobj->property = member;
  264. zval_add_ref(&pobj->property);
  265. zval_add_ref(&pobj->object);
  266. MAKE_STD_ZVAL(retval);
  267. Z_TYPE_P(retval) = IS_OBJECT;
  268. Z_OBJ_HANDLE_P(retval) = zend_objects_store_put(pobj, NULL, (zend_objects_free_object_storage_t) zend_objects_proxy_free_storage, (zend_objects_store_clone_t) zend_objects_proxy_clone TSRMLS_CC);
  269. Z_OBJ_HT_P(retval) = &zend_object_proxy_handlers;
  270. return retval;
  271. }
  272. ZEND_API void zend_object_proxy_set(zval **property, zval *value TSRMLS_DC)
  273. {
  274. zend_proxy_object *probj = zend_object_store_get_object(*property TSRMLS_CC);
  275. if (Z_OBJ_HT_P(probj->object) && Z_OBJ_HT_P(probj->object)->write_property) {
  276. Z_OBJ_HT_P(probj->object)->write_property(probj->object, probj->property, value TSRMLS_CC);
  277. } else {
  278. zend_error(E_WARNING, "Cannot write property of object - no write handler defined");
  279. }
  280. }
  281. ZEND_API zval* zend_object_proxy_get(zval *property TSRMLS_DC)
  282. {
  283. zend_proxy_object *probj = zend_object_store_get_object(property TSRMLS_CC);
  284. if (Z_OBJ_HT_P(probj->object) && Z_OBJ_HT_P(probj->object)->read_property) {
  285. return Z_OBJ_HT_P(probj->object)->read_property(probj->object, probj->property, BP_VAR_R TSRMLS_CC);
  286. } else {
  287. zend_error(E_WARNING, "Cannot read property of object - no read handler defined");
  288. }
  289. return NULL;
  290. }
  291. ZEND_API zend_object_handlers *zend_get_std_object_handlers()
  292. {
  293. return &std_object_handlers;
  294. }
  295. static zend_object_handlers zend_object_proxy_handlers = {
  296. ZEND_OBJECTS_STORE_HANDLERS,
  297. NULL, /* read_property */
  298. NULL, /* write_property */
  299. NULL, /* read dimension */
  300. NULL, /* write_dimension */
  301. NULL, /* get_property_ptr_ptr */
  302. zend_object_proxy_get, /* get */
  303. zend_object_proxy_set, /* set */
  304. NULL, /* has_property */
  305. NULL, /* unset_property */
  306. NULL, /* has_dimension */
  307. NULL, /* unset_dimension */
  308. NULL, /* get_properties */
  309. NULL, /* get_method */
  310. NULL, /* call_method */
  311. NULL, /* get_constructor */
  312. NULL, /* get_class_entry */
  313. NULL, /* get_class_name */
  314. NULL, /* compare_objects */
  315. NULL, /* cast_object */
  316. NULL, /* count_elements */
  317. };
  318. /*
  319. * Local variables:
  320. * tab-width: 4
  321. * c-basic-offset: 4
  322. * indent-tabs-mode: t
  323. * End:
  324. */