Skip to content

zend_weakrefs: Add zend_weakrefs_hash_(clean|destroy)() #16439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Zend/zend_weakrefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ ZEND_API zend_result zend_weakrefs_hash_del(HashTable *ht, zend_object *key) {
return FAILURE;
}

ZEND_API void zend_weakrefs_hash_clean(HashTable *ht) {
zend_ulong obj_key;
ZEND_HASH_FOREACH_NUM_KEY(ht, obj_key) {
zend_weakrefs_hash_del(ht, zend_weakref_key_to_object(obj_key));
} ZEND_HASH_FOREACH_END();
}

void zend_weakrefs_init(void) {
zend_hash_init(&EG(weakrefs), 8, NULL, NULL, 0);
}
Expand Down
6 changes: 6 additions & 0 deletions Zend/zend_weakrefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ static zend_always_inline void *zend_weakrefs_hash_add_ptr(HashTable *ht, zend_o
return NULL;
}
}
ZEND_API void zend_weakrefs_hash_clean(HashTable *ht);
static zend_always_inline void zend_weakrefs_hash_destroy(HashTable *ht) {
zend_weakrefs_hash_clean(ht);
ZEND_ASSERT(zend_hash_num_elements(ht) == 0);
zend_hash_destroy(ht);
}

/* Because php uses the raw numbers as a hash function, raw pointers will lead to hash collisions.
* We have a guarantee that the lowest ZEND_MM_ALIGNED_OFFSET_LOG2 bits of a pointer are zero.
Expand Down
6 changes: 1 addition & 5 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1354,11 +1354,7 @@ PHP_RINIT_FUNCTION(zend_test)

PHP_RSHUTDOWN_FUNCTION(zend_test)
{
zend_ulong obj_key;
ZEND_HASH_FOREACH_NUM_KEY(&ZT_G(global_weakmap), obj_key) {
zend_weakrefs_hash_del(&ZT_G(global_weakmap), zend_weakref_key_to_object(obj_key));
} ZEND_HASH_FOREACH_END();
zend_hash_destroy(&ZT_G(global_weakmap));
zend_weakrefs_hash_destroy(&ZT_G(global_weakmap));

if (ZT_G(zend_test_heap)) {
free(ZT_G(zend_test_heap));
Expand Down
Loading