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 +----------------------------------------------------------------------+
18 */
19
20 #ifndef ZEND_OBJECTS_API_H
21 #define ZEND_OBJECTS_API_H
22
23 #include "zend.h"
24 #include "zend_compile.h"
25
26 #define OBJ_BUCKET_INVALID (1<<0)
27
28 #define IS_OBJ_VALID(o) (!(((zend_uintptr_t)(o)) & OBJ_BUCKET_INVALID))
29
30 #define SET_OBJ_INVALID(o) ((zend_object*)((((zend_uintptr_t)(o)) | OBJ_BUCKET_INVALID)))
31
32 #define GET_OBJ_BUCKET_NUMBER(o) (((zend_intptr_t)(o)) >> 1)
33
34 #define SET_OBJ_BUCKET_NUMBER(o, n) do { \
35 (o) = (zend_object*)((((zend_uintptr_t)(n)) << 1) | OBJ_BUCKET_INVALID); \
36 } while (0)
37
38 #define ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(h) do { \
39 SET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[(h)], EG(objects_store).free_list_head); \
40 EG(objects_store).free_list_head = (h); \
41 } while (0)
42
43 #define OBJ_RELEASE(obj) zend_object_release(obj)
44
45 typedef struct _zend_objects_store {
46 zend_object **object_buckets;
47 uint32_t top;
48 uint32_t size;
49 int free_list_head;
50 } zend_objects_store;
51
52 /* Global store handling functions */
53 BEGIN_EXTERN_C()
54 ZEND_API void ZEND_FASTCALL zend_objects_store_init(zend_objects_store *objects, uint32_t init_size);
55 ZEND_API void ZEND_FASTCALL zend_objects_store_call_destructors(zend_objects_store *objects);
56 ZEND_API void ZEND_FASTCALL zend_objects_store_mark_destructed(zend_objects_store *objects);
57 ZEND_API void ZEND_FASTCALL zend_objects_store_free_object_storage(zend_objects_store *objects, zend_bool fast_shutdown);
58 ZEND_API void ZEND_FASTCALL zend_objects_store_destroy(zend_objects_store *objects);
59
60 /* Store API functions */
61 ZEND_API void ZEND_FASTCALL zend_objects_store_put(zend_object *object);
62 ZEND_API void ZEND_FASTCALL zend_objects_store_del(zend_object *object);
63
64 /* Called when the ctor was terminated by an exception */
zend_object_store_ctor_failed(zend_object * obj)65 static zend_always_inline void zend_object_store_ctor_failed(zend_object *obj)
66 {
67 GC_ADD_FLAGS(obj, IS_OBJ_DESTRUCTOR_CALLED);
68 }
69
70 #define ZEND_OBJECTS_STORE_HANDLERS 0, zend_object_std_dtor, zend_objects_destroy_object, zend_objects_clone_obj
71
END_EXTERN_C()72 END_EXTERN_C()
73
74 static zend_always_inline void zend_object_release(zend_object *obj)
75 {
76 if (GC_DELREF(obj) == 0) {
77 zend_objects_store_del(obj);
78 } else if (UNEXPECTED(GC_MAY_LEAK((zend_refcounted*)obj))) {
79 gc_possible_root((zend_refcounted*)obj);
80 }
81 }
82
zend_object_properties_size(zend_class_entry * ce)83 static zend_always_inline size_t zend_object_properties_size(zend_class_entry *ce)
84 {
85 return sizeof(zval) *
86 (ce->default_properties_count -
87 ((ce->ce_flags & ZEND_ACC_USE_GUARDS) ? 0 : 1));
88 }
89
90 /* Allocates object type and zeros it, but not the properties.
91 * Properties MUST be initialized using object_properties_init(). */
zend_object_alloc(size_t obj_size,zend_class_entry * ce)92 static zend_always_inline void *zend_object_alloc(size_t obj_size, zend_class_entry *ce) {
93 void *obj = emalloc(obj_size + zend_object_properties_size(ce));
94 /* Subtraction of sizeof(zval) is necessary, because zend_object_properties_size() may be
95 * -sizeof(zval), if the object has no properties. */
96 memset(obj, 0, obj_size - sizeof(zval));
97 return obj;
98 }
99
100
101 #endif /* ZEND_OBJECTS_H */
102
103 /*
104 * Local variables:
105 * tab-width: 4
106 * c-basic-offset: 4
107 * indent-tabs-mode: t
108 * End:
109 * vim600: sw=4 ts=4 fdm=marker
110 * vim<600: sw=4 ts=4
111 */
112