forked from colder/php-weakref
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwr_store.c
More file actions
181 lines (147 loc) · 5.32 KB
/
wr_store.c
File metadata and controls
181 lines (147 loc) · 5.32 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
+----------------------------------------------------------------------+
| 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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Etienne Kneuss <colder@php.net> |
| Fix for PHP7+Apache2: CommerceByte Consulting <jim@commercebyte.com> |
+----------------------------------------------------------------------+
*/
/* $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;
} /* }}} */
/* See comment in wr_store_destroy */
int wr_store_dtor_restore(zval *val,int arg_num, va_list arg_list, zend_hash_key *key)
{
zend_object_dtor_obj_t *dtor = (zend_object_dtor_obj_t*) val;
((zend_object_handlers *) key->h)->dtor_obj = *dtor;
return ZEND_HASH_APPLY_REMOVE;
}
void wr_store_destroy() /* {{{ */
{
wr_store *store = WR_G(store);
/*
jim@commercebyte.com -
Explicitly restore the dtor handlers
Not doing so causes a disaster in PHP7 + Apache2 lib environment
*/
zend_hash_apply_with_arguments(&store->old_dtors, wr_store_dtor_restore, 0);
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);
if (!store) {
/* Sanity check, should never get here as long as wr_store_dtor_restore() is engaged */
return;
}
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
*/