-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwr_store.c
162 lines (134 loc) · 4.83 KB
/
wr_store.c
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
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
+----------------------------------------------------------------------+
| Weakref |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2011 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Etienne Kneuss <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "zend_exceptions.h"
#include "ext/standard/info.h"
#include "wr_weakref.h"
#include "php_weakref.h"
void wr_store_init() /* {{{ */
{
wr_store *store = emalloc(sizeof(wr_store));
zend_hash_init(&store->old_dtors, 0, NULL, NULL, 0);
zend_hash_init(&store->objs, 0, NULL, NULL, 0);
WR_G(store) = store;
} /* }}} */
void wr_store_destroy() /* {{{ */
{
wr_store *store = WR_G(store);
zend_object_dtor_obj_t orig_dtor;
ulong key;
ZEND_HASH_FOREACH_NUM_KEY_PTR(&store->old_dtors, key, orig_dtor) {
((zend_object_handlers *)key)->dtor_obj = orig_dtor;
} ZEND_HASH_FOREACH_END();
zend_hash_destroy(&store->old_dtors);
zend_hash_destroy(&store->objs);
efree(store);
WR_G(store) = NULL;
} /* }}} */
/* This function is invoked whenever an object tracked by a weak ref/map is
* destroyed */
void wr_store_tracked_object_dtor(zend_object *ref_obj) /* {{{ */
{
wr_store *store = WR_G(store);
zend_object_dtor_obj_t orig_dtor = zend_hash_index_find_ptr(&store->old_dtors, (ulong)ref_obj->handlers);
ulong handle_key = ref_obj->handle;
wr_ref_list *list_entry;
/* Original dtor has been called, we invalidate the necessary weakrefs: */
orig_dtor(ref_obj);
if ((list_entry = zend_hash_index_find_ptr(&store->objs, handle_key)) != NULL) {
/* Invalidate wrefs_head while dtoring, to prevent detach on same wr */
zend_hash_index_del(&store->objs, handle_key);
do {
wr_ref_list *next = list_entry->next;
list_entry->dtor(list_entry->wref_obj, ref_obj);
efree(list_entry);
list_entry = next;
} while (list_entry);
}
}
/* }}} */
/**
* This function is called when a given weak-ref/map starts tracking the 'ref'
* object.
*/
void wr_store_track(zend_object *wref_obj, wr_ref_dtor dtor, zend_object *ref_obj) /* {{{ */
{
wr_store *store = WR_G(store);
ulong handlers_key = (ulong)ref_obj->handlers;
ulong handle_key = ref_obj->handle;
if (zend_hash_index_find_ptr(&store->old_dtors, handlers_key) == NULL) {
zend_hash_index_update_ptr(&store->old_dtors, handlers_key, ref_obj->handlers->dtor_obj);
((zend_object_handlers *)ref_obj->handlers)->dtor_obj = wr_store_tracked_object_dtor;
}
wr_ref_list *tail = zend_hash_index_find_ptr(&store->objs, handle_key);
/* We now put the weakref 'wref' in the list of weak references that need
* to be invalidated when 'ref' is destroyed */
wr_ref_list *head = emalloc(sizeof(wr_ref_list));
head->wref_obj = wref_obj;
head->dtor = dtor;
head->next = tail;
zend_hash_index_update_ptr(&store->objs, handle_key, head);
}
/* }}} */
/**
* This function is called when a given weak-ref/map stops tracking the 'ref'
* object.
*/
void wr_store_untrack(zend_object *wref_obj, zend_object *ref_obj) /* {{{ */
{
wr_store *store = WR_G(store);
if (!store) {
// detach() can be called after the store has already been cleaned up,
// depending on the shutdown sequence (i.e. in case of a fatal).
// See tests/weakref_007.phpt
return;
} else {
wr_ref_list *cur = zend_hash_index_find_ptr(&store->objs, ref_obj->handle);
wr_ref_list *prev = NULL;
if (!cur) {
// We are detaching from a wr that is being dtored, skip
return;
}
while (cur && cur->wref_obj != wref_obj) {
prev = cur;
cur = cur->next;
}
assert(cur != NULL);
if (prev) {
prev->next = cur->next;
} else if (cur->next) {
zend_hash_index_update_ptr(&store->objs, ref_obj->handle, cur->next);
} else {
zend_hash_index_del(&store->objs, ref_obj->handle);
}
efree(cur);
}
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: fdm=marker
* vim: noet sw=4 ts=4
*/