1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
#include "zend.h"
#include "zend_globals.h"
#include "zend_variables.h"
#define ZEND_DEBUG_OBJECTS 0
static zend_object_handlers zoh = {
zend_objects_get_address,
NULL,
zend_objects_add_ref,
zend_objects_del_ref,
zend_objects_delete_obj,
zend_objects_clone_obj
};
void zend_objects_init(zend_objects *objects, zend_uint init_size)
{
objects->object_buckets = (zend_object_bucket *) emalloc(init_size * sizeof(zend_object_bucket));
objects->top = 1; /* Skip 0 so that handles are true */
objects->size = init_size;
objects->free_list_head = -1;
}
void zend_objects_destroy(zend_objects *objects)
{
efree(objects->object_buckets);
}
zend_object_value zend_objects_new(zend_object **object, zend_class_entry *class_type)
{
zend_object_handle handle;
zend_object_value retval;
TSRMLS_FETCH();
if (EG(objects).free_list_head != -1) {
handle = EG(objects).free_list_head;
EG(objects).free_list_head = EG(objects).object_buckets[handle].bucket.free_list.next;
} else {
if (EG(objects).top == EG(objects).size) {
EG(objects).size <<= 1;
EG(objects).object_buckets = (zend_object_bucket *) erealloc(EG(objects).object_buckets, EG(objects).size * sizeof(zend_object_bucket));
}
handle = EG(objects).top++;
}
EG(objects).object_buckets[handle].valid = 1;
EG(objects).object_buckets[handle].bucket.obj.refcount = 1;
*object = &EG(objects).object_buckets[handle].bucket.obj.object;
(*object)->ce = class_type;
retval.handle = handle;
retval.handlers = &zoh;
#if ZEND_DEBUG_OBJECTS
fprintf(stderr, "Allocated object id #%d\n", handle);
#endif
return retval;
}
zend_object *zend_objects_get_address(zend_object_handle handle)
{
TSRMLS_FETCH();
if (!EG(objects).object_buckets[handle].valid) {
zend_error(E_ERROR, "Trying to access invalid object");
}
return &EG(objects).object_buckets[handle].bucket.obj.object;
}
void zend_objects_add_ref(zend_object_handle handle)
{
TSRMLS_FETCH();
if (!EG(objects).object_buckets[handle].valid) {
zend_error(E_ERROR, "Trying to add reference to invalid object");
}
EG(objects).object_buckets[handle].bucket.obj.refcount++;
#if ZEND_DEBUG_OBJECTS
fprintf(stderr, "Increased refcount of object id #%d\n", handle);
#endif
}
void zend_objects_delete_obj(zend_object_handle handle)
{
zend_object *object;
TSRMLS_FETCH();
if (!EG(objects).object_buckets[handle].valid) {
zend_error(E_ERROR, "Trying to delete invalid object");
}
object = &EG(objects).object_buckets[handle].bucket.obj.object;
zend_hash_destroy(object->properties);
efree(object->properties);
EG(objects).object_buckets[handle].valid = 0;
#if ZEND_DEBUG_OBJECTS
fprintf(stderr, "Deleted object id #%d\n", handle);
#endif
}
void zend_objects_del_ref(zend_object_handle handle)
{
TSRMLS_FETCH();
if (--EG(objects).object_buckets[handle].bucket.obj.refcount == 0) {
zend_object *object;
if (EG(objects).object_buckets[handle].valid) {
object = &EG(objects).object_buckets[handle].bucket.obj.object;
zend_hash_destroy(object->properties);
efree(object->properties);
}
EG(objects).object_buckets[handle].bucket.free_list.next = EG(objects).free_list_head;
EG(objects).free_list_head = handle;
EG(objects).object_buckets[handle].valid = 0;
#if ZEND_DEBUG_OBJECTS
fprintf(stderr, "Deallocated object id #%d\n", handle);
#endif
}
#if ZEND_DEBUG_OBJECTS
else {
fprintf(stderr, "Decreased refcount of object id #%d\n", handle);
}
#endif
}
zend_object_value zend_objects_clone_obj(zend_object_handle handle)
{
zend_object_value retval;
zend_object *old_object;
zend_object *new_object;
TSRMLS_FETCH();
if (!EG(objects).object_buckets[handle].valid) {
zend_error(E_ERROR, "Trying to clone invalid object");
}
old_object = &EG(objects).object_buckets[handle].bucket.obj.object;
retval = zend_objects_new(&new_object, old_object->ce);
ALLOC_HASHTABLE(new_object->properties);
zend_hash_init(new_object->properties, 0, NULL, ZVAL_PTR_DTOR, 0);
zend_hash_copy(new_object->properties, old_object->properties, (copy_ctor_func_t) zval_add_ref, (void *) NULL /* Not used anymore */, sizeof(zval *));
return retval;
}
|