zend_objects_API.c 6.7 KB

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