Skip to content

Commit ad64fc7

Browse files
committed
Differenciate WeakMaps from bare HashTables used as weak maps in GC
1 parent 63acc4b commit ad64fc7

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

Zend/zend_weakrefs.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,21 @@ typedef struct _zend_weakmap_iterator {
3636
uint32_t ht_iter;
3737
} zend_weakmap_iterator;
3838

39-
/* 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+
/* EG(weakrefs) is a map from a key corresponding to a zend_object pointer to all the WeakReference, WeakMap, and/or bare HashTable entries relating to that pointer.
4040
*
4141
* 1. For a single WeakReference,
4242
* 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()).
4343
* 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 *).
44-
* 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
45-
* tagged pointers of at most 1 WeakReference and 1 or more WeakMaps to the same tagged pointer.
44+
* 3. For a single bare HashTable, the HashTable's corresponding value's tag is a ZEND_WEAKREF_TAG_BARE_HT and the pointer is a HashTable*.
45+
* 4. 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
46+
* tagged pointers of at most 1 WeakReference and 1 or more WeakMap or bare HashTable to the same tagged pointer.
4647
*
4748
* 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),
4849
* i.e. the least two significant bits of the pointer can be used as a tag (ZEND_WEAKREF_TAG_*). */
49-
#define ZEND_WEAKREF_TAG_REF 0
50-
#define ZEND_WEAKREF_TAG_MAP 1
51-
#define ZEND_WEAKREF_TAG_HT 2
50+
#define ZEND_WEAKREF_TAG_REF 0
51+
#define ZEND_WEAKREF_TAG_MAP 1
52+
#define ZEND_WEAKREF_TAG_HT 2
53+
#define ZEND_WEAKREF_TAG_BARE_HT 3
5254
#define ZEND_WEAKREF_GET_TAG(p) (((uintptr_t) (p)) & 3)
5355
#define ZEND_WEAKREF_GET_PTR(p) ((void *) (((uintptr_t) (p)) & ~3))
5456
#define ZEND_WEAKREF_ENCODE(p, t) ((void *) (((uintptr_t) (p)) | (t)))
@@ -72,8 +74,8 @@ static inline void zend_weakref_unref_single(
7274
zend_weakref *wr = ptr;
7375
wr->referent = NULL;
7476
} else {
75-
/* unreferencing WeakMap entry (at ptr) with a key of object. */
76-
ZEND_ASSERT(tag == ZEND_WEAKREF_TAG_MAP);
77+
/* unreferencing WeakMap or bare HashTable entry (at ptr) with a key of object. */
78+
ZEND_ASSERT(tag == ZEND_WEAKREF_TAG_MAP || tag == ZEND_WEAKREF_TAG_BARE_HT);
7779
zend_hash_index_del((HashTable *) ptr, zend_object_to_weakref_key(object));
7880
}
7981
}
@@ -166,18 +168,20 @@ static void zend_weakref_unregister(zend_object *object, void *payload, bool wea
166168
}
167169
}
168170

171+
/* Insert 'pData' into bare HashTable 'ht', with the given 'key'. 'key' is
172+
* weakly referenced. 'ht' is considered to be a bare HashTable, not a WeakMap. */
169173
ZEND_API zval *zend_weakrefs_hash_add(HashTable *ht, zend_object *key, zval *pData) {
170174
zval *zv = zend_hash_index_add(ht, zend_object_to_weakref_key(key), pData);
171175
if (zv) {
172-
zend_weakref_register(key, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_MAP));
176+
zend_weakref_register(key, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_BARE_HT));
173177
}
174178
return zv;
175179
}
176180

177181
ZEND_API zend_result zend_weakrefs_hash_del(HashTable *ht, zend_object *key) {
178182
zval *zv = zend_hash_index_find(ht, zend_object_to_weakref_key(key));
179183
if (zv) {
180-
zend_weakref_unregister(key, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_MAP), 1);
184+
zend_weakref_unregister(key, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_BARE_HT), 1);
181185
return SUCCESS;
182186
}
183187
return FAILURE;

0 commit comments

Comments
 (0)