zend_objects_API.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. +----------------------------------------------------------------------+
  18. */
  19. #ifndef ZEND_OBJECTS_API_H
  20. #define ZEND_OBJECTS_API_H
  21. #include "zend.h"
  22. #include "zend_compile.h"
  23. #define OBJ_BUCKET_INVALID (1<<0)
  24. #define IS_OBJ_VALID(o) (!(((zend_uintptr_t)(o)) & OBJ_BUCKET_INVALID))
  25. #define SET_OBJ_INVALID(o) ((zend_object*)((((zend_uintptr_t)(o)) | OBJ_BUCKET_INVALID)))
  26. #define GET_OBJ_BUCKET_NUMBER(o) (((zend_intptr_t)(o)) >> 1)
  27. #define SET_OBJ_BUCKET_NUMBER(o, n) do { \
  28. (o) = (zend_object*)((((zend_uintptr_t)(n)) << 1) | OBJ_BUCKET_INVALID); \
  29. } while (0)
  30. #define ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(h) do { \
  31. SET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[(h)], EG(objects_store).free_list_head); \
  32. EG(objects_store).free_list_head = (h); \
  33. } while (0)
  34. #define OBJ_RELEASE(obj) zend_object_release(obj)
  35. typedef struct _zend_objects_store {
  36. zend_object **object_buckets;
  37. uint32_t top;
  38. uint32_t size;
  39. int free_list_head;
  40. } zend_objects_store;
  41. /* Global store handling functions */
  42. BEGIN_EXTERN_C()
  43. ZEND_API void ZEND_FASTCALL zend_objects_store_init(zend_objects_store *objects, uint32_t init_size);
  44. ZEND_API void ZEND_FASTCALL zend_objects_store_call_destructors(zend_objects_store *objects);
  45. ZEND_API void ZEND_FASTCALL zend_objects_store_mark_destructed(zend_objects_store *objects);
  46. ZEND_API void ZEND_FASTCALL zend_objects_store_free_object_storage(zend_objects_store *objects, bool fast_shutdown);
  47. ZEND_API void ZEND_FASTCALL zend_objects_store_destroy(zend_objects_store *objects);
  48. /* Store API functions */
  49. ZEND_API void ZEND_FASTCALL zend_objects_store_put(zend_object *object);
  50. ZEND_API void ZEND_FASTCALL zend_objects_store_del(zend_object *object);
  51. /* Called when the ctor was terminated by an exception */
  52. static zend_always_inline void zend_object_store_ctor_failed(zend_object *obj)
  53. {
  54. GC_ADD_FLAGS(obj, IS_OBJ_DESTRUCTOR_CALLED);
  55. }
  56. END_EXTERN_C()
  57. static zend_always_inline void zend_object_release(zend_object *obj)
  58. {
  59. if (GC_DELREF(obj) == 0) {
  60. zend_objects_store_del(obj);
  61. } else if (UNEXPECTED(GC_MAY_LEAK((zend_refcounted*)obj))) {
  62. gc_possible_root((zend_refcounted*)obj);
  63. }
  64. }
  65. static zend_always_inline size_t zend_object_properties_size(zend_class_entry *ce)
  66. {
  67. return sizeof(zval) *
  68. (ce->default_properties_count -
  69. ((ce->ce_flags & ZEND_ACC_USE_GUARDS) ? 0 : 1));
  70. }
  71. /* Allocates object type and zeros it, but not the standard zend_object and properties.
  72. * Standard object MUST be initialized using zend_object_std_init().
  73. * Properties MUST be initialized using object_properties_init(). */
  74. static zend_always_inline void *zend_object_alloc(size_t obj_size, zend_class_entry *ce) {
  75. void *obj = emalloc(obj_size + zend_object_properties_size(ce));
  76. memset(obj, 0, obj_size - sizeof(zend_object));
  77. return obj;
  78. }
  79. static inline zend_property_info *zend_get_property_info_for_slot(zend_object *obj, zval *slot)
  80. {
  81. zend_property_info **table = obj->ce->properties_info_table;
  82. intptr_t prop_num = slot - obj->properties_table;
  83. ZEND_ASSERT(prop_num >= 0 && prop_num < obj->ce->default_properties_count);
  84. return table[prop_num];
  85. }
  86. /* Helper for cases where we're only interested in property info of typed properties. */
  87. static inline zend_property_info *zend_get_typed_property_info_for_slot(zend_object *obj, zval *slot)
  88. {
  89. zend_property_info *prop_info = zend_get_property_info_for_slot(obj, slot);
  90. if (prop_info && ZEND_TYPE_IS_SET(prop_info->type)) {
  91. return prop_info;
  92. }
  93. return NULL;
  94. }
  95. #endif /* ZEND_OBJECTS_H */