zend_objects_API.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2018 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@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. | Dmitry Stogov <dmitry@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  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. ZEND_API void ZEND_FASTCALL zend_objects_store_init(zend_objects_store *objects, uint32_t init_size)
  26. {
  27. objects->object_buckets = (zend_object **) emalloc(init_size * sizeof(zend_object*));
  28. objects->top = 1; /* Skip 0 so that handles are true */
  29. objects->size = init_size;
  30. objects->free_list_head = -1;
  31. memset(&objects->object_buckets[0], 0, sizeof(zend_object*));
  32. }
  33. ZEND_API void ZEND_FASTCALL zend_objects_store_destroy(zend_objects_store *objects)
  34. {
  35. efree(objects->object_buckets);
  36. objects->object_buckets = NULL;
  37. }
  38. ZEND_API void ZEND_FASTCALL zend_objects_store_call_destructors(zend_objects_store *objects)
  39. {
  40. EG(flags) |= EG_FLAGS_OBJECT_STORE_NO_REUSE;
  41. if (objects->top > 1) {
  42. uint32_t i;
  43. for (i = 1; i < objects->top; i++) {
  44. zend_object *obj = objects->object_buckets[i];
  45. if (IS_OBJ_VALID(obj)) {
  46. if (!(OBJ_FLAGS(obj) & IS_OBJ_DESTRUCTOR_CALLED)) {
  47. GC_ADD_FLAGS(obj, IS_OBJ_DESTRUCTOR_CALLED);
  48. if (obj->handlers->dtor_obj
  49. && (obj->handlers->dtor_obj != zend_objects_destroy_object
  50. || obj->ce->destructor)) {
  51. GC_ADDREF(obj);
  52. obj->handlers->dtor_obj(obj);
  53. GC_DELREF(obj);
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }
  60. ZEND_API void ZEND_FASTCALL zend_objects_store_mark_destructed(zend_objects_store *objects)
  61. {
  62. if (objects->object_buckets && objects->top > 1) {
  63. zend_object **obj_ptr = objects->object_buckets + 1;
  64. zend_object **end = objects->object_buckets + objects->top;
  65. do {
  66. zend_object *obj = *obj_ptr;
  67. if (IS_OBJ_VALID(obj)) {
  68. GC_ADD_FLAGS(obj, IS_OBJ_DESTRUCTOR_CALLED);
  69. }
  70. obj_ptr++;
  71. } while (obj_ptr != end);
  72. }
  73. }
  74. ZEND_API void ZEND_FASTCALL zend_objects_store_free_object_storage(zend_objects_store *objects, zend_bool fast_shutdown)
  75. {
  76. zend_object **obj_ptr, **end, *obj;
  77. if (objects->top <= 1) {
  78. return;
  79. }
  80. /* Free object contents, but don't free objects themselves, so they show up as leaks */
  81. end = objects->object_buckets + 1;
  82. obj_ptr = objects->object_buckets + objects->top;
  83. if (fast_shutdown) {
  84. do {
  85. obj_ptr--;
  86. obj = *obj_ptr;
  87. if (IS_OBJ_VALID(obj)) {
  88. if (!(OBJ_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
  89. GC_ADD_FLAGS(obj, IS_OBJ_FREE_CALLED);
  90. if (obj->handlers->free_obj && obj->handlers->free_obj != zend_object_std_dtor) {
  91. GC_ADDREF(obj);
  92. obj->handlers->free_obj(obj);
  93. GC_DELREF(obj);
  94. }
  95. }
  96. }
  97. } while (obj_ptr != end);
  98. } else {
  99. do {
  100. obj_ptr--;
  101. obj = *obj_ptr;
  102. if (IS_OBJ_VALID(obj)) {
  103. if (!(OBJ_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
  104. GC_ADD_FLAGS(obj, IS_OBJ_FREE_CALLED);
  105. if (obj->handlers->free_obj) {
  106. GC_ADDREF(obj);
  107. obj->handlers->free_obj(obj);
  108. GC_DELREF(obj);
  109. }
  110. }
  111. }
  112. } while (obj_ptr != end);
  113. }
  114. }
  115. /* Store objects API */
  116. ZEND_API void ZEND_FASTCALL zend_objects_store_put(zend_object *object)
  117. {
  118. int handle;
  119. /* When in shutdown sequence - do not reuse previously freed handles, to make sure
  120. * the dtors for newly created objects are called in zend_objects_store_call_destructors() loop
  121. */
  122. if (EG(objects_store).free_list_head != -1 && EXPECTED(!(EG(flags) & EG_FLAGS_OBJECT_STORE_NO_REUSE))) {
  123. handle = EG(objects_store).free_list_head;
  124. EG(objects_store).free_list_head = GET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[handle]);
  125. } else {
  126. if (EG(objects_store).top == EG(objects_store).size) {
  127. uint32_t new_size = 2 * EG(objects_store).size;
  128. EG(objects_store).object_buckets = (zend_object **) erealloc(EG(objects_store).object_buckets, new_size * sizeof(zend_object*));
  129. /* Assign size after realloc, in case it fails */
  130. EG(objects_store).size = new_size;
  131. }
  132. handle = EG(objects_store).top++;
  133. }
  134. object->handle = handle;
  135. EG(objects_store).object_buckets[handle] = object;
  136. }
  137. ZEND_API void ZEND_FASTCALL zend_objects_store_del(zend_object *object) /* {{{ */
  138. {
  139. ZEND_ASSERT(GC_REFCOUNT(object) == 0);
  140. /* GC might have released this object already. */
  141. if (UNEXPECTED(GC_TYPE(object) == IS_NULL)) {
  142. return;
  143. }
  144. /* Make sure we hold a reference count during the destructor call
  145. otherwise, when the destructor ends the storage might be freed
  146. when the refcount reaches 0 a second time
  147. */
  148. if (!(OBJ_FLAGS(object) & IS_OBJ_DESTRUCTOR_CALLED)) {
  149. GC_ADD_FLAGS(object, IS_OBJ_DESTRUCTOR_CALLED);
  150. if (object->handlers->dtor_obj
  151. && (object->handlers->dtor_obj != zend_objects_destroy_object
  152. || object->ce->destructor)) {
  153. GC_ADDREF(object);
  154. object->handlers->dtor_obj(object);
  155. GC_DELREF(object);
  156. }
  157. }
  158. if (GC_REFCOUNT(object) == 0) {
  159. uint32_t handle = object->handle;
  160. void *ptr;
  161. ZEND_ASSERT(EG(objects_store).object_buckets != NULL);
  162. ZEND_ASSERT(IS_OBJ_VALID(EG(objects_store).object_buckets[object->handle]));
  163. EG(objects_store).object_buckets[handle] = SET_OBJ_INVALID(object);
  164. if (!(OBJ_FLAGS(object) & IS_OBJ_FREE_CALLED)) {
  165. GC_ADD_FLAGS(object, IS_OBJ_FREE_CALLED);
  166. if (object->handlers->free_obj) {
  167. GC_ADDREF(object);
  168. object->handlers->free_obj(object);
  169. GC_DELREF(object);
  170. }
  171. }
  172. ptr = ((char*)object) - object->handlers->offset;
  173. GC_REMOVE_FROM_BUFFER(object);
  174. efree(ptr);
  175. ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle);
  176. }
  177. }
  178. /* }}} */
  179. /*
  180. * Local variables:
  181. * tab-width: 4
  182. * c-basic-offset: 4
  183. * indent-tabs-mode: t
  184. * End:
  185. * vim600: sw=4 ts=4 fdm=marker
  186. * vim<600: sw=4 ts=4
  187. */