1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2015 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@zend.com>                                |
16   |          Zeev Suraski <zeev@zend.com>                                |
17   +----------------------------------------------------------------------+
18*/
19
20/* $Id$ */
21
22#include "zend.h"
23#include "zend_globals.h"
24#include "zend_variables.h"
25#include "zend_API.h"
26#include "zend_objects_API.h"
27
28ZEND_API void zend_objects_store_init(zend_objects_store *objects, uint32_t init_size)
29{
30    objects->object_buckets = (zend_object **) emalloc(init_size * sizeof(zend_object*));
31    objects->top = 1; /* Skip 0 so that handles are true */
32    objects->size = init_size;
33    objects->free_list_head = -1;
34    memset(&objects->object_buckets[0], 0, sizeof(zend_object*));
35}
36
37ZEND_API void zend_objects_store_destroy(zend_objects_store *objects)
38{
39    efree(objects->object_buckets);
40    objects->object_buckets = NULL;
41}
42
43ZEND_API void zend_objects_store_call_destructors(zend_objects_store *objects)
44{
45    uint32_t i;
46
47    for (i = 1; i < objects->top ; i++) {
48        zend_object *obj = objects->object_buckets[i];
49
50        if (IS_OBJ_VALID(obj)) {
51            if (!(GC_FLAGS(obj) & IS_OBJ_DESTRUCTOR_CALLED)) {
52                GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
53                GC_REFCOUNT(obj)++;
54                obj->handlers->dtor_obj(obj);
55                GC_REFCOUNT(obj)--;
56            }
57        }
58    }
59}
60
61ZEND_API void zend_objects_store_mark_destructed(zend_objects_store *objects)
62{
63    uint32_t i;
64
65    if (!objects->object_buckets) {
66        return;
67    }
68    for (i = 1; i < objects->top ; i++) {
69        zend_object *obj = objects->object_buckets[i];
70
71        if (IS_OBJ_VALID(obj)) {
72            GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
73        }
74    }
75}
76
77ZEND_API void zend_objects_store_free_object_storage(zend_objects_store *objects)
78{
79    uint32_t i;
80
81    /* Free object contents, but don't free objects themselves */
82    for (i = objects->top - 1; i > 0 ; i--) {
83        zend_object *obj = objects->object_buckets[i];
84
85        if (IS_OBJ_VALID(obj)) {
86            if (!(GC_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
87                GC_FLAGS(obj) |= IS_OBJ_FREE_CALLED;
88                if (obj->handlers->free_obj) {
89                    GC_REFCOUNT(obj)++;
90                    obj->handlers->free_obj(obj);
91                    GC_REFCOUNT(obj)--;
92                }
93            }
94        }
95    }
96
97    /* Free objects themselves if they now have a refcount of 0, which means that
98     * they were previously part of a cycle. Everything else will report as a leak.
99     * Cycles are allowed because not all internal objects currently support GC. */
100    for (i = 1; i < objects->top ; i++) {
101        zend_object *obj = objects->object_buckets[i];
102
103        if (IS_OBJ_VALID(obj) && GC_REFCOUNT(obj) == 0) {
104            /* Not adding to free list as we are shutting down anyway */
105            void *ptr = ((char*)obj) - obj->handlers->offset;
106            GC_REMOVE_FROM_BUFFER(obj);
107            efree(ptr);
108        }
109    }
110}
111
112
113/* Store objects API */
114
115ZEND_API void zend_objects_store_put(zend_object *object)
116{
117    int handle;
118
119    if (EG(objects_store).free_list_head != -1) {
120        handle = EG(objects_store).free_list_head;
121        EG(objects_store).free_list_head = GET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[handle]);
122    } else {
123        if (EG(objects_store).top == EG(objects_store).size) {
124            EG(objects_store).size <<= 1;
125            EG(objects_store).object_buckets = (zend_object **) erealloc(EG(objects_store).object_buckets, EG(objects_store).size * sizeof(zend_object*));
126        }
127        handle = EG(objects_store).top++;
128    }
129    object->handle = handle;
130    EG(objects_store).object_buckets[handle] = object;
131}
132
133#define ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle)                                                         \
134            SET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[handle], EG(objects_store).free_list_head);  \
135            EG(objects_store).free_list_head = handle;
136
137ZEND_API void zend_objects_store_free(zend_object *object) /* {{{ */
138{
139    uint32_t handle = object->handle;
140    void *ptr = ((char*)object) - object->handlers->offset;
141
142    GC_REMOVE_FROM_BUFFER(object);
143    efree(ptr);
144    ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle);
145}
146/* }}} */
147
148ZEND_API void zend_objects_store_del(zend_object *object) /* {{{ */
149{
150    /*  Make sure we hold a reference count during the destructor call
151        otherwise, when the destructor ends the storage might be freed
152        when the refcount reaches 0 a second time
153     */
154    if (EG(objects_store).object_buckets &&
155        IS_OBJ_VALID(EG(objects_store).object_buckets[object->handle])) {
156        if (GC_REFCOUNT(object) == 0) {
157            int failure = 0;
158
159            if (!(GC_FLAGS(object) & IS_OBJ_DESTRUCTOR_CALLED)) {
160                GC_FLAGS(object) |= IS_OBJ_DESTRUCTOR_CALLED;
161
162                if (object->handlers->dtor_obj) {
163                    GC_REFCOUNT(object)++;
164                    zend_try {
165                        object->handlers->dtor_obj(object);
166                    } zend_catch {
167                        failure = 1;
168                    } zend_end_try();
169                    GC_REFCOUNT(object)--;
170                }
171            }
172
173            if (GC_REFCOUNT(object) == 0) {
174                uint32_t handle = object->handle;
175                void *ptr;
176
177                EG(objects_store).object_buckets[handle] = SET_OBJ_INVALID(object);
178                if (!(GC_FLAGS(object) & IS_OBJ_FREE_CALLED)) {
179                    GC_FLAGS(object) |= IS_OBJ_FREE_CALLED;
180                    if (object->handlers->free_obj) {
181                        zend_try {
182                            GC_REFCOUNT(object)++;
183                            object->handlers->free_obj(object);
184                            GC_REFCOUNT(object)--;
185                        } zend_catch {
186                            failure = 1;
187                        } zend_end_try();
188                    }
189                }
190                ptr = ((char*)object) - object->handlers->offset;
191                GC_REMOVE_FROM_BUFFER(object);
192                efree(ptr);
193                ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle);
194            }
195
196            if (failure) {
197                zend_bailout();
198            }
199        } else {
200            GC_REFCOUNT(object)--;
201        }
202    }
203}
204/* }}} */
205
206/* zend_object_store_set_object:
207 * It is ONLY valid to call this function from within the constructor of an
208 * overloaded object.  Its purpose is to set the object pointer for the object
209 * when you can't possibly know its value until you have parsed the arguments
210 * from the constructor function.  You MUST NOT use this function for any other
211 * weird games, or call it at any other time after the object is constructed.
212 * */
213ZEND_API void zend_object_store_set_object(zval *zobject, zend_object *object)
214{
215    EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(zobject)] = object;
216}
217
218/* Called when the ctor was terminated by an exception */
219ZEND_API void zend_object_store_ctor_failed(zend_object *obj)
220{
221    GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
222}
223
224ZEND_API zend_object_handlers *zend_get_std_object_handlers(void)
225{
226    return &std_object_handlers;
227}
228
229/*
230 * Local variables:
231 * tab-width: 4
232 * c-basic-offset: 4
233 * indent-tabs-mode: t
234 * End:
235 */
236