xref: /PHP-8.2/Zend/zend_weakrefs.c (revision 1d94fb86)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 2.00 of the Zend license,     |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | http://www.zend.com/license/2_00.txt.                                |
9    | If you did not receive a copy of the Zend license and are unable to  |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@zend.com so we can mail you a copy immediately.              |
12    +----------------------------------------------------------------------+
13    | Authors: krakjoe@php.net                                             |
14    +----------------------------------------------------------------------+
15 */
16 
17 #include "zend.h"
18 #include "zend_interfaces.h"
19 #include "zend_objects_API.h"
20 #include "zend_weakrefs.h"
21 #include "zend_weakrefs_arginfo.h"
22 
23 typedef struct _zend_weakref {
24 	zend_object *referent;
25 	zend_object std;
26 } zend_weakref;
27 
28 typedef struct _zend_weakmap {
29 	HashTable ht;
30 	zend_object std;
31 } zend_weakmap;
32 
33 typedef struct _zend_weakmap_iterator {
34 	zend_object_iterator it;
35 	uint32_t ht_iter;
36 } zend_weakmap_iterator;
37 
38 /* EG(weakrefs) is a map from a key corresponding to a zend_object pointer to all the WeakReference and/or WeakMap entries relating to that pointer.
39  *
40  * 1. For a single WeakReference,
41  *    the HashTable's corresponding value's tag is a ZEND_WEAKREF_TAG_REF and the pointer is a singleton WeakReference instance (zend_weakref *) for that zend_object pointer (from WeakReference::create()).
42  * 2. For a single WeakMap, the HashTable's corresponding value's tag is a ZEND_WEAKREF_TAG_MAP and the pointer is a WeakMap instance (zend_weakmap *).
43  * 3. For multiple values associated with the same zend_object pointer, the HashTable entry's tag is a ZEND_WEAKREF_TAG_HT with a HashTable mapping
44  *    tagged pointers of at most 1 WeakReference and 1 or more WeakMaps to the same tagged pointer.
45  *
46  * ZEND_MM_ALIGNED_OFFSET_LOG2 is at least 2 on supported architectures (pointers to the objects in question are aligned to 4 bytes (1<<2) even on 32-bit systems),
47  * i.e. the least two significant bits of the pointer can be used as a tag (ZEND_WEAKREF_TAG_*). */
48 #define ZEND_WEAKREF_TAG_REF 0
49 #define ZEND_WEAKREF_TAG_MAP 1
50 #define ZEND_WEAKREF_TAG_HT  2
51 #define ZEND_WEAKREF_GET_TAG(p) (((uintptr_t) (p)) & 3)
52 #define ZEND_WEAKREF_GET_PTR(p) ((void *) (((uintptr_t) (p)) & ~3))
53 #define ZEND_WEAKREF_ENCODE(p, t) ((void *) (((uintptr_t) (p)) | (t)))
54 
55 zend_class_entry *zend_ce_weakref;
56 zend_class_entry *zend_ce_weakmap;
57 static zend_object_handlers zend_weakref_handlers;
58 static zend_object_handlers zend_weakmap_handlers;
59 
60 #define zend_weakref_from(o) ((zend_weakref*)(((char*) o) - XtOffsetOf(zend_weakref, std)))
61 #define zend_weakref_fetch(z) zend_weakref_from(Z_OBJ_P(z))
62 
63 #define zend_weakmap_from(o) ((zend_weakmap*)(((char*) o) - XtOffsetOf(zend_weakmap, std)))
64 #define zend_weakmap_fetch(z) zend_weakmap_from(Z_OBJ_P(z))
65 
zend_weakref_unref_single(void * ptr,uintptr_t tag,zend_object * object)66 static inline void zend_weakref_unref_single(
67 		void *ptr, uintptr_t tag, zend_object *object)
68 {
69 	if (tag == ZEND_WEAKREF_TAG_REF) {
70 		/* Unreferencing WeakReference (at ptr) singleton that pointed to object. */
71 		zend_weakref *wr = ptr;
72 		wr->referent = NULL;
73 	} else {
74 		/* unreferencing WeakMap entry (at ptr) with a key of object. */
75 		ZEND_ASSERT(tag == ZEND_WEAKREF_TAG_MAP);
76 		zend_hash_index_del((HashTable *) ptr, zend_object_to_weakref_key(object));
77 	}
78 }
79 
zend_weakref_unref(zend_object * object,void * tagged_ptr)80 static void zend_weakref_unref(zend_object *object, void *tagged_ptr) {
81 	void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
82 	uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
83 	if (tag == ZEND_WEAKREF_TAG_HT) {
84 		HashTable *ht = ptr;
85 		ZEND_HASH_MAP_FOREACH_PTR(ht, tagged_ptr) {
86 			zend_weakref_unref_single(
87 				ZEND_WEAKREF_GET_PTR(tagged_ptr), ZEND_WEAKREF_GET_TAG(tagged_ptr), object);
88 		} ZEND_HASH_FOREACH_END();
89 		zend_hash_destroy(ht);
90 		FREE_HASHTABLE(ht);
91 	} else {
92 		zend_weakref_unref_single(ptr, tag, object);
93 	}
94 }
95 
zend_weakref_register(zend_object * object,void * payload)96 static void zend_weakref_register(zend_object *object, void *payload) {
97 	GC_ADD_FLAGS(object, IS_OBJ_WEAKLY_REFERENCED);
98 
99 	zend_ulong obj_key = zend_object_to_weakref_key(object);
100 	zval *zv = zend_hash_index_lookup(&EG(weakrefs), obj_key);
101 	if (Z_TYPE_P(zv) == IS_NULL) {
102 		ZVAL_PTR(zv, payload);
103 		return;
104 	}
105 
106 	void *tagged_ptr = Z_PTR_P(zv);
107 	if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_HT) {
108 		HashTable *ht = ZEND_WEAKREF_GET_PTR(tagged_ptr);
109 		zend_hash_index_add_new_ptr(ht, (zend_ulong) payload, payload);
110 		return;
111 	}
112 
113 	/* Convert simple pointer to hashtable. */
114 	HashTable *ht = emalloc(sizeof(HashTable));
115 	zend_hash_init(ht, 0, NULL, NULL, 0);
116 	zend_hash_index_add_new_ptr(ht, (zend_ulong) tagged_ptr, tagged_ptr);
117 	zend_hash_index_add_new_ptr(ht, (zend_ulong) payload, payload);
118 	/* Replace the single WeakMap or WeakReference entry in EG(weakrefs) with a HashTable with 2 entries in place. */
119 	ZVAL_PTR(zv, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_HT));
120 }
121 
zend_weakref_unregister(zend_object * object,void * payload,bool weakref_free)122 static void zend_weakref_unregister(zend_object *object, void *payload, bool weakref_free) {
123 	zend_ulong obj_key = zend_object_to_weakref_key(object);
124 	void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), obj_key);
125 	ZEND_ASSERT(tagged_ptr && "Weakref not registered?");
126 
127 	void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
128 	uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
129 	if (tag != ZEND_WEAKREF_TAG_HT) {
130 		ZEND_ASSERT(tagged_ptr == payload);
131 		zend_hash_index_del(&EG(weakrefs), obj_key);
132 		GC_DEL_FLAGS(object, IS_OBJ_WEAKLY_REFERENCED);
133 
134 		/* Do this last, as it may destroy the object. */
135 		if (weakref_free) {
136 			zend_weakref_unref_single(ptr, tag, object);
137 		} else {
138 			/* The optimization of skipping unref is only used in the destructor of WeakMap */
139 			ZEND_ASSERT(ZEND_WEAKREF_GET_TAG(payload) == ZEND_WEAKREF_TAG_MAP);
140 		}
141 		return;
142 	}
143 
144 	HashTable *ht = ptr;
145 #if ZEND_DEBUG
146 	void *old_payload = zend_hash_index_find_ptr(ht, (zend_ulong) payload);
147 	ZEND_ASSERT(old_payload && "Weakref not registered?");
148 	ZEND_ASSERT(old_payload == payload);
149 #endif
150 	zend_hash_index_del(ht, (zend_ulong) payload);
151 	if (zend_hash_num_elements(ht) == 0) {
152 		GC_DEL_FLAGS(object, IS_OBJ_WEAKLY_REFERENCED);
153 		zend_hash_destroy(ht);
154 		FREE_HASHTABLE(ht);
155 		zend_hash_index_del(&EG(weakrefs), obj_key);
156 	}
157 
158 	/* Do this last, as it may destroy the object. */
159 	if (weakref_free)  {
160 		zend_weakref_unref_single(
161 			ZEND_WEAKREF_GET_PTR(payload), ZEND_WEAKREF_GET_TAG(payload), object);
162 	} else {
163 		/* The optimization of skipping unref is only used in the destructor of WeakMap */
164 		ZEND_ASSERT(ZEND_WEAKREF_GET_TAG(payload) == ZEND_WEAKREF_TAG_MAP);
165 	}
166 }
167 
zend_weakrefs_hash_add(HashTable * ht,zend_object * key,zval * pData)168 ZEND_API zval *zend_weakrefs_hash_add(HashTable *ht, zend_object *key, zval *pData) {
169 	zval *zv = zend_hash_index_add(ht, zend_object_to_weakref_key(key), pData);
170 	if (zv) {
171 		zend_weakref_register(key, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_MAP));
172 	}
173 	return zv;
174 }
175 
zend_weakrefs_hash_del(HashTable * ht,zend_object * key)176 ZEND_API zend_result zend_weakrefs_hash_del(HashTable *ht, zend_object *key) {
177 	zval *zv = zend_hash_index_find(ht, zend_object_to_weakref_key(key));
178 	if (zv) {
179 		zend_weakref_unregister(key, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_MAP), 1);
180 		return SUCCESS;
181 	}
182 	return FAILURE;
183 }
184 
zend_weakrefs_init(void)185 void zend_weakrefs_init(void) {
186 	zend_hash_init(&EG(weakrefs), 8, NULL, NULL, 0);
187 }
188 
189 /* This is called when the object is garbage collected
190  * to remove all WeakReference and WeakMap entries weakly referencing that object. */
zend_weakrefs_notify(zend_object * object)191 void zend_weakrefs_notify(zend_object *object) {
192 	/* Annoyingly we can't use the HT destructor here, because we need access to the key (which
193 	 * is the object address), which is not provided to the dtor. */
194 	const zend_ulong obj_key = zend_object_to_weakref_key(object);
195 	void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), obj_key);
196 #if ZEND_DEBUG
197 	ZEND_ASSERT(tagged_ptr && "Tracking of the IS_OBJ_WEAKLY_REFERENCE flag should be precise");
198 #endif
199 	if (tagged_ptr) {
200 		zend_weakref_unref(object, tagged_ptr);
201 		zend_hash_index_del(&EG(weakrefs), obj_key);
202 	}
203 }
204 
zend_weakrefs_shutdown(void)205 void zend_weakrefs_shutdown(void) {
206 	zend_hash_destroy(&EG(weakrefs));
207 }
208 
zend_weakref_new(zend_class_entry * ce)209 static zend_object* zend_weakref_new(zend_class_entry *ce) {
210 	zend_weakref *wr = zend_object_alloc(sizeof(zend_weakref), zend_ce_weakref);
211 
212 	zend_object_std_init(&wr->std, zend_ce_weakref);
213 
214 	wr->std.handlers = &zend_weakref_handlers;
215 
216 	return &wr->std;
217 }
218 
zend_weakref_find(zend_object * referent,zval * return_value)219 static zend_always_inline bool zend_weakref_find(zend_object *referent, zval *return_value) {
220 	void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), zend_object_to_weakref_key(referent));
221 	if (!tagged_ptr) {
222 		return 0;
223 	}
224 
225 	void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
226 	uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
227 	if (tag == ZEND_WEAKREF_TAG_REF) {
228 		zend_weakref *wr;
229 found_weakref:
230 		wr = ptr;
231 		RETVAL_OBJ_COPY(&wr->std);
232 		return 1;
233 	}
234 
235 	if (tag == ZEND_WEAKREF_TAG_HT) {
236 		ZEND_HASH_MAP_FOREACH_PTR(ptr, tagged_ptr) {
237 			if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_REF) {
238 				ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
239 				goto found_weakref;
240 			}
241 		} ZEND_HASH_FOREACH_END();
242 	}
243 
244 	return 0;
245 }
246 
zend_weakref_create(zend_object * referent,zval * return_value)247 static zend_always_inline void zend_weakref_create(zend_object *referent, zval *return_value) {
248 	zend_weakref *wr;
249 
250 	object_init_ex(return_value, zend_ce_weakref);
251 
252 	wr = zend_weakref_fetch(return_value);
253 	wr->referent = referent;
254 
255 	zend_weakref_register(wr->referent, ZEND_WEAKREF_ENCODE(wr, ZEND_WEAKREF_TAG_REF));
256 }
257 
zend_weakref_get(zval * weakref,zval * return_value)258 static zend_always_inline void zend_weakref_get(zval *weakref, zval *return_value) {
259 	zend_weakref *wr = zend_weakref_fetch(weakref);
260 
261 	if (wr->referent) {
262 		RETVAL_OBJ_COPY(wr->referent);
263 	}
264 }
265 
zend_weakref_free(zend_object * zo)266 static void zend_weakref_free(zend_object *zo) {
267 	zend_weakref *wr = zend_weakref_from(zo);
268 
269 	if (wr->referent) {
270 		zend_weakref_unregister(wr->referent, ZEND_WEAKREF_ENCODE(wr, ZEND_WEAKREF_TAG_REF), 1);
271 	}
272 
273 	zend_object_std_dtor(&wr->std);
274 }
275 
ZEND_METHOD(WeakReference,__construct)276 ZEND_COLD ZEND_METHOD(WeakReference, __construct)
277 {
278 	zend_throw_error(NULL, "Direct instantiation of WeakReference is not allowed, use WeakReference::create instead");
279 }
280 
ZEND_METHOD(WeakReference,create)281 ZEND_METHOD(WeakReference, create)
282 {
283 	zend_object *referent;
284 
285 	ZEND_PARSE_PARAMETERS_START(1,1)
286 		Z_PARAM_OBJ(referent)
287 	ZEND_PARSE_PARAMETERS_END();
288 
289 	if (zend_weakref_find(referent, return_value)) {
290 	    return;
291 	}
292 
293 	zend_weakref_create(referent, return_value);
294 }
295 
ZEND_METHOD(WeakReference,get)296 ZEND_METHOD(WeakReference, get)
297 {
298 	ZEND_PARSE_PARAMETERS_NONE();
299 
300 	zend_weakref_get(getThis(), return_value);
301 }
302 
zend_weakmap_create_object(zend_class_entry * ce)303 static zend_object *zend_weakmap_create_object(zend_class_entry *ce)
304 {
305 	zend_weakmap *wm = zend_object_alloc(sizeof(zend_weakmap), ce);
306 	zend_object_std_init(&wm->std, ce);
307 	wm->std.handlers = &zend_weakmap_handlers;
308 
309 	zend_hash_init(&wm->ht, 0, NULL, ZVAL_PTR_DTOR, 0);
310 	return &wm->std;
311 }
312 
zend_weakmap_free_obj(zend_object * object)313 static void zend_weakmap_free_obj(zend_object *object)
314 {
315 	zend_weakmap *wm = zend_weakmap_from(object);
316 	zend_ulong obj_key;
317 	ZEND_HASH_MAP_FOREACH_NUM_KEY(&wm->ht, obj_key) {
318 		/* Optimization: Don't call zend_weakref_unref_single to free individual entries from wm->ht when unregistering (which would do a hash table lookup, call zend_hash_index_del, and skip over any bucket collisions).
319 		 * Let freeing the corresponding values for WeakMap entries be done in zend_hash_destroy, freeing objects sequentially.
320 		 * The performance difference is notable for larger WeakMaps with worse cache locality. */
321 		zend_weakref_unregister(
322 			zend_weakref_key_to_object(obj_key), ZEND_WEAKREF_ENCODE(&wm->ht, ZEND_WEAKREF_TAG_MAP), 0);
323 	} ZEND_HASH_FOREACH_END();
324 	zend_hash_destroy(&wm->ht);
325 	zend_object_std_dtor(&wm->std);
326 }
327 
zend_weakmap_read_dimension(zend_object * object,zval * offset,int type,zval * rv)328 static zval *zend_weakmap_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
329 {
330 	if (offset == NULL) {
331 		zend_throw_error(NULL, "Cannot append to WeakMap");
332 		return NULL;
333 	}
334 
335 	ZVAL_DEREF(offset);
336 	if (Z_TYPE_P(offset) != IS_OBJECT) {
337 		zend_type_error("WeakMap key must be an object");
338 		return NULL;
339 	}
340 
341 	zend_weakmap *wm = zend_weakmap_from(object);
342 	zend_object *obj_addr = Z_OBJ_P(offset);
343 	zval *zv = zend_hash_index_find(&wm->ht, zend_object_to_weakref_key(obj_addr));
344 	if (zv == NULL) {
345 		if (type != BP_VAR_IS) {
346 			zend_throw_error(NULL,
347 				"Object %s#%d not contained in WeakMap", ZSTR_VAL(obj_addr->ce->name), obj_addr->handle);
348 			return NULL;
349 		}
350 		return NULL;
351 	}
352 
353 	if (type == BP_VAR_W || type == BP_VAR_RW) {
354 		ZVAL_MAKE_REF(zv);
355 	}
356 	return zv;
357 }
358 
zend_weakmap_write_dimension(zend_object * object,zval * offset,zval * value)359 static void zend_weakmap_write_dimension(zend_object *object, zval *offset, zval *value)
360 {
361 	if (offset == NULL) {
362 		zend_throw_error(NULL, "Cannot append to WeakMap");
363 		return;
364 	}
365 
366 	ZVAL_DEREF(offset);
367 	if (Z_TYPE_P(offset) != IS_OBJECT) {
368 		zend_type_error("WeakMap key must be an object");
369 		return;
370 	}
371 
372 	zend_weakmap *wm = zend_weakmap_from(object);
373 	zend_object *obj_addr = Z_OBJ_P(offset);
374 	zend_ulong obj_key = zend_object_to_weakref_key(obj_addr);
375 	Z_TRY_ADDREF_P(value);
376 
377 	zval *zv = zend_hash_index_find(&wm->ht, obj_key);
378 	if (zv) {
379 		/* Because the destructors can have side effects such as resizing or rehashing the WeakMap storage,
380 		 * free the zval only after overwriting the original value. */
381 		zval zv_orig;
382 		ZVAL_COPY_VALUE(&zv_orig, zv);
383 		ZVAL_COPY_VALUE(zv, value);
384 		zval_ptr_dtor(&zv_orig);
385 		return;
386 	}
387 
388 	zend_weakref_register(obj_addr, ZEND_WEAKREF_ENCODE(&wm->ht, ZEND_WEAKREF_TAG_MAP));
389 	zend_hash_index_add_new(&wm->ht, obj_key, value);
390 }
391 
392 /* int return and check_empty due to Object Handler API */
zend_weakmap_has_dimension(zend_object * object,zval * offset,int check_empty)393 static int zend_weakmap_has_dimension(zend_object *object, zval *offset, int check_empty)
394 {
395 	ZVAL_DEREF(offset);
396 	if (Z_TYPE_P(offset) != IS_OBJECT) {
397 		zend_type_error("WeakMap key must be an object");
398 		return 0;
399 	}
400 
401 	zend_weakmap *wm = zend_weakmap_from(object);
402 	zval *zv = zend_hash_index_find(&wm->ht, zend_object_to_weakref_key(Z_OBJ_P(offset)));
403 	if (!zv) {
404 		return 0;
405 	}
406 
407 	if (check_empty) {
408 		return i_zend_is_true(zv);
409 	}
410 	return Z_TYPE_P(zv) != IS_NULL;
411 }
412 
zend_weakmap_unset_dimension(zend_object * object,zval * offset)413 static void zend_weakmap_unset_dimension(zend_object *object, zval *offset)
414 {
415 	ZVAL_DEREF(offset);
416 	if (Z_TYPE_P(offset) != IS_OBJECT) {
417 		zend_type_error("WeakMap key must be an object");
418 		return;
419 	}
420 
421 	zend_weakmap *wm = zend_weakmap_from(object);
422 	zend_object *obj_addr = Z_OBJ_P(offset);
423 	if (!zend_hash_index_exists(&wm->ht, zend_object_to_weakref_key(obj_addr))) {
424 		/* Object not in WeakMap, do nothing. */
425 		return;
426 	}
427 
428 	zend_weakref_unregister(obj_addr, ZEND_WEAKREF_ENCODE(&wm->ht, ZEND_WEAKREF_TAG_MAP), 1);
429 }
430 
zend_weakmap_count_elements(zend_object * object,zend_long * count)431 static zend_result zend_weakmap_count_elements(zend_object *object, zend_long *count)
432 {
433 	zend_weakmap *wm = zend_weakmap_from(object);
434 	*count = zend_hash_num_elements(&wm->ht);
435 	return SUCCESS;
436 }
437 
zend_weakmap_get_properties_for(zend_object * object,zend_prop_purpose purpose)438 static HashTable *zend_weakmap_get_properties_for(zend_object *object, zend_prop_purpose purpose)
439 {
440 	if (purpose != ZEND_PROP_PURPOSE_DEBUG) {
441 		return NULL;
442 	}
443 
444 	zend_weakmap *wm = zend_weakmap_from(object);
445 	HashTable *ht;
446 	ALLOC_HASHTABLE(ht);
447 	zend_hash_init(ht, zend_hash_num_elements(&wm->ht), NULL, ZVAL_PTR_DTOR, 0);
448 
449 	zend_ulong obj_key;
450 	zval *val;
451 	ZEND_HASH_MAP_FOREACH_NUM_KEY_VAL(&wm->ht, obj_key, val) {
452 		zend_object *obj = zend_weakref_key_to_object(obj_key);
453 		zval pair;
454 		array_init(&pair);
455 
456 		GC_ADDREF(obj);
457 		add_assoc_object(&pair, "key", obj);
458 		Z_TRY_ADDREF_P(val);
459 		add_assoc_zval(&pair, "value", val);
460 
461 		zend_hash_next_index_insert_new(ht, &pair);
462 	} ZEND_HASH_FOREACH_END();
463 
464 	return ht;
465 }
466 
zend_weakmap_get_gc(zend_object * object,zval ** table,int * n)467 static HashTable *zend_weakmap_get_gc(zend_object *object, zval **table, int *n)
468 {
469 	zend_weakmap *wm = zend_weakmap_from(object);
470 	zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
471 	zval *val;
472 	ZEND_HASH_MAP_FOREACH_VAL(&wm->ht, val) {
473 		zend_get_gc_buffer_add_zval(gc_buffer, val);
474 	} ZEND_HASH_FOREACH_END();
475 	zend_get_gc_buffer_use(gc_buffer, table, n);
476 	return NULL;
477 }
478 
zend_weakmap_clone_obj(zend_object * old_object)479 static zend_object *zend_weakmap_clone_obj(zend_object *old_object)
480 {
481 	zend_object *new_object = zend_weakmap_create_object(zend_ce_weakmap);
482 	zend_weakmap *old_wm = zend_weakmap_from(old_object);
483 	zend_weakmap *new_wm = zend_weakmap_from(new_object);
484 	zend_hash_copy(&new_wm->ht, &old_wm->ht, NULL);
485 
486 	zend_ulong obj_key;
487 	zval *val;
488 	ZEND_HASH_MAP_FOREACH_NUM_KEY_VAL(&new_wm->ht, obj_key, val) {
489 		zend_weakref_register(
490 			zend_weakref_key_to_object(obj_key), ZEND_WEAKREF_ENCODE(new_wm, ZEND_WEAKREF_TAG_MAP));
491 		zval_add_ref(val);
492 	} ZEND_HASH_FOREACH_END();
493 	return new_object;
494 }
495 
zend_weakmap_iterator_get_pos_ptr(zend_weakmap_iterator * iter)496 static HashPosition *zend_weakmap_iterator_get_pos_ptr(zend_weakmap_iterator *iter) {
497 	ZEND_ASSERT(iter->ht_iter != (uint32_t) -1);
498 	return &EG(ht_iterators)[iter->ht_iter].pos;
499 }
500 
zend_weakmap_iterator_dtor(zend_object_iterator * obj_iter)501 static void zend_weakmap_iterator_dtor(zend_object_iterator *obj_iter)
502 {
503 	zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
504 	zend_hash_iterator_del(iter->ht_iter);
505 	zval_ptr_dtor(&iter->it.data);
506 }
507 
zend_weakmap_iterator_valid(zend_object_iterator * obj_iter)508 static int zend_weakmap_iterator_valid(zend_object_iterator *obj_iter)
509 {
510 	zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
511 	zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
512 	HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
513 	return zend_hash_has_more_elements_ex(&wm->ht, pos);
514 }
515 
zend_weakmap_iterator_get_current_data(zend_object_iterator * obj_iter)516 static zval *zend_weakmap_iterator_get_current_data(zend_object_iterator *obj_iter)
517 {
518 	zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
519 	zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
520 	HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
521 	return zend_hash_get_current_data_ex(&wm->ht, pos);
522 }
523 
zend_weakmap_iterator_get_current_key(zend_object_iterator * obj_iter,zval * key)524 static void zend_weakmap_iterator_get_current_key(zend_object_iterator *obj_iter, zval *key)
525 {
526 	zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
527 	zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
528 	HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
529 
530 	zend_string *string_key;
531 	zend_ulong num_key;
532 	int key_type = zend_hash_get_current_key_ex(&wm->ht, &string_key, &num_key, pos);
533 	if (key_type == HASH_KEY_NON_EXISTENT) {
534 		ZVAL_NULL(key);
535 		return;
536 	}
537 	if (key_type != HASH_KEY_IS_LONG) {
538 		ZEND_ASSERT(0 && "Must have integer key");
539 	}
540 
541 	ZVAL_OBJ_COPY(key, zend_weakref_key_to_object(num_key));
542 }
543 
zend_weakmap_iterator_move_forward(zend_object_iterator * obj_iter)544 static void zend_weakmap_iterator_move_forward(zend_object_iterator *obj_iter)
545 {
546 	zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
547 	zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
548 	HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
549 	zend_hash_move_forward_ex(&wm->ht, pos);
550 }
551 
zend_weakmap_iterator_rewind(zend_object_iterator * obj_iter)552 static void zend_weakmap_iterator_rewind(zend_object_iterator *obj_iter)
553 {
554 	zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
555 	zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
556 	HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
557 	zend_hash_internal_pointer_reset_ex(&wm->ht, pos);
558 }
559 
560 static const zend_object_iterator_funcs zend_weakmap_iterator_funcs = {
561 	zend_weakmap_iterator_dtor,
562 	zend_weakmap_iterator_valid,
563 	zend_weakmap_iterator_get_current_data,
564 	zend_weakmap_iterator_get_current_key,
565 	zend_weakmap_iterator_move_forward,
566 	zend_weakmap_iterator_rewind,
567 	NULL,
568 	NULL, /* get_gc */
569 };
570 
571 /* by_ref is int due to Iterator API */
zend_weakmap_get_iterator(zend_class_entry * ce,zval * object,int by_ref)572 static zend_object_iterator *zend_weakmap_get_iterator(
573 		zend_class_entry *ce, zval *object, int by_ref)
574 {
575 	zend_weakmap *wm = zend_weakmap_fetch(object);
576 	zend_weakmap_iterator *iter = emalloc(sizeof(zend_weakmap_iterator));
577 	zend_iterator_init(&iter->it);
578 	iter->it.funcs = &zend_weakmap_iterator_funcs;
579 	ZVAL_COPY(&iter->it.data, object);
580 	iter->ht_iter = zend_hash_iterator_add(&wm->ht, 0);
581 	return &iter->it;
582 }
583 
ZEND_METHOD(WeakMap,offsetGet)584 ZEND_METHOD(WeakMap, offsetGet)
585 {
586 	zval *key;
587 
588 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &key) == FAILURE) {
589 		return;
590 	}
591 
592 	zval *zv = zend_weakmap_read_dimension(Z_OBJ_P(ZEND_THIS), key, BP_VAR_R, NULL);
593 	if (!zv) {
594 		return;
595 	}
596 
597 	ZVAL_COPY(return_value, zv);
598 }
599 
ZEND_METHOD(WeakMap,offsetSet)600 ZEND_METHOD(WeakMap, offsetSet)
601 {
602 	zval *key, *value;
603 
604 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &key, &value) == FAILURE) {
605 		return;
606 	}
607 
608 	zend_weakmap_write_dimension(Z_OBJ_P(ZEND_THIS), key, value);
609 }
610 
ZEND_METHOD(WeakMap,offsetExists)611 ZEND_METHOD(WeakMap, offsetExists)
612 {
613 	zval *key;
614 
615 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &key) == FAILURE) {
616 		return;
617 	}
618 
619 	RETURN_BOOL(zend_weakmap_has_dimension(Z_OBJ_P(ZEND_THIS), key, /* check_empty */ 0));
620 }
621 
ZEND_METHOD(WeakMap,offsetUnset)622 ZEND_METHOD(WeakMap, offsetUnset)
623 {
624 	zval *key;
625 
626 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &key) == FAILURE) {
627 		return;
628 	}
629 
630 	zend_weakmap_unset_dimension(Z_OBJ_P(ZEND_THIS), key);
631 }
632 
ZEND_METHOD(WeakMap,count)633 ZEND_METHOD(WeakMap, count)
634 {
635 	if (zend_parse_parameters_none() == FAILURE) {
636 		return;
637 	}
638 
639 	zend_long count;
640 	zend_weakmap_count_elements(Z_OBJ_P(ZEND_THIS), &count);
641 	RETURN_LONG(count);
642 }
643 
ZEND_METHOD(WeakMap,getIterator)644 ZEND_METHOD(WeakMap, getIterator)
645 {
646 	if (zend_parse_parameters_none() == FAILURE) {
647 		return;
648 	}
649 
650 	zend_create_internal_iterator_zval(return_value, ZEND_THIS);
651 }
652 
zend_register_weakref_ce(void)653 void zend_register_weakref_ce(void) /* {{{ */
654 {
655 	zend_ce_weakref = register_class_WeakReference();
656 
657 	zend_ce_weakref->create_object = zend_weakref_new;
658 
659 	memcpy(&zend_weakref_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
660 	zend_weakref_handlers.offset = XtOffsetOf(zend_weakref, std);
661 
662 	zend_weakref_handlers.free_obj = zend_weakref_free;
663 	zend_weakref_handlers.clone_obj = NULL;
664 
665 	zend_ce_weakmap = register_class_WeakMap(zend_ce_arrayaccess, zend_ce_countable, zend_ce_aggregate);
666 
667 	zend_ce_weakmap->create_object = zend_weakmap_create_object;
668 	zend_ce_weakmap->get_iterator = zend_weakmap_get_iterator;
669 
670 	memcpy(&zend_weakmap_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
671 	zend_weakmap_handlers.offset = XtOffsetOf(zend_weakmap, std);
672 	zend_weakmap_handlers.free_obj = zend_weakmap_free_obj;
673 	zend_weakmap_handlers.read_dimension = zend_weakmap_read_dimension;
674 	zend_weakmap_handlers.write_dimension = zend_weakmap_write_dimension;
675 	zend_weakmap_handlers.has_dimension = zend_weakmap_has_dimension;
676 	zend_weakmap_handlers.unset_dimension = zend_weakmap_unset_dimension;
677 	zend_weakmap_handlers.count_elements = zend_weakmap_count_elements;
678 	zend_weakmap_handlers.get_properties_for = zend_weakmap_get_properties_for;
679 	zend_weakmap_handlers.get_gc = zend_weakmap_get_gc;
680 	zend_weakmap_handlers.clone_obj = zend_weakmap_clone_obj;
681 }
682 /* }}} */
683 
684