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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
|
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.00 of the Zend 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.zend.com/license/2_00.txt. |
| If you did not receive a copy of the Zend license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@zend.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: krakjoe@php.net |
+----------------------------------------------------------------------+
*/
#include "zend.h"
#include "zend_interfaces.h"
#include "zend_objects_API.h"
#include "zend_types.h"
#include "zend_weakrefs.h"
#include "zend_weakrefs_arginfo.h"
typedef struct _zend_weakref {
zend_object *referent;
zend_object std;
} zend_weakref;
typedef struct _zend_weakmap {
HashTable ht;
zend_object std;
} zend_weakmap;
typedef struct _zend_weakmap_iterator {
zend_object_iterator it;
uint32_t ht_iter;
} zend_weakmap_iterator;
/* 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.
*
* 1. For a single WeakReference,
* 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()).
* 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 *).
* 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
* tagged pointers of at most 1 WeakReference and 1 or more WeakMaps to the same tagged pointer.
*
* 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),
* i.e. the least two significant bits of the pointer can be used as a tag (ZEND_WEAKREF_TAG_*). */
#define ZEND_WEAKREF_TAG_REF 0
#define ZEND_WEAKREF_TAG_MAP 1
#define ZEND_WEAKREF_TAG_HT 2
#define ZEND_WEAKREF_GET_TAG(p) (((uintptr_t) (p)) & 3)
#define ZEND_WEAKREF_GET_PTR(p) ((void *) (((uintptr_t) (p)) & ~3))
#define ZEND_WEAKREF_ENCODE(p, t) ((void *) (((uintptr_t) (p)) | (t)))
zend_class_entry *zend_ce_weakref;
static zend_class_entry *zend_ce_weakmap;
static zend_object_handlers zend_weakref_handlers;
static zend_object_handlers zend_weakmap_handlers;
#define zend_weakref_from(o) ((zend_weakref*)(((char*) o) - XtOffsetOf(zend_weakref, std)))
#define zend_weakref_fetch(z) zend_weakref_from(Z_OBJ_P(z))
#define zend_weakmap_from(o) ((zend_weakmap*)(((char*) o) - XtOffsetOf(zend_weakmap, std)))
#define zend_weakmap_fetch(z) zend_weakmap_from(Z_OBJ_P(z))
static inline void zend_weakref_unref_single(
void *ptr, uintptr_t tag, zend_object *object)
{
if (tag == ZEND_WEAKREF_TAG_REF) {
/* Unreferencing WeakReference (at ptr) singleton that pointed to object. */
zend_weakref *wr = ptr;
wr->referent = NULL;
} else {
/* unreferencing WeakMap entry (at ptr) with a key of object. */
ZEND_ASSERT(tag == ZEND_WEAKREF_TAG_MAP);
zend_hash_index_del((HashTable *) ptr, zend_object_to_weakref_key(object));
}
}
static void zend_weakref_unref(zend_object *object, void *tagged_ptr) {
void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
if (tag == ZEND_WEAKREF_TAG_HT) {
HashTable *ht = ptr;
ZEND_HASH_MAP_FOREACH_PTR(ht, tagged_ptr) {
zend_weakref_unref_single(
ZEND_WEAKREF_GET_PTR(tagged_ptr), ZEND_WEAKREF_GET_TAG(tagged_ptr), object);
} ZEND_HASH_FOREACH_END();
zend_hash_destroy(ht);
FREE_HASHTABLE(ht);
} else {
zend_weakref_unref_single(ptr, tag, object);
}
}
static void zend_weakref_register(zend_object *object, void *payload) {
GC_ADD_FLAGS(object, IS_OBJ_WEAKLY_REFERENCED);
zend_ulong obj_key = zend_object_to_weakref_key(object);
zval *zv = zend_hash_index_lookup(&EG(weakrefs), obj_key);
if (Z_TYPE_P(zv) == IS_NULL) {
ZVAL_PTR(zv, payload);
return;
}
void *tagged_ptr = Z_PTR_P(zv);
if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_HT) {
HashTable *ht = ZEND_WEAKREF_GET_PTR(tagged_ptr);
zend_hash_index_add_new_ptr(ht, (zend_ulong) payload, payload);
return;
}
/* Convert simple pointer to hashtable. */
HashTable *ht = emalloc(sizeof(HashTable));
zend_hash_init(ht, 0, NULL, NULL, 0);
zend_hash_index_add_new_ptr(ht, (zend_ulong) tagged_ptr, tagged_ptr);
zend_hash_index_add_new_ptr(ht, (zend_ulong) payload, payload);
/* Replace the single WeakMap or WeakReference entry in EG(weakrefs) with a HashTable with 2 entries in place. */
ZVAL_PTR(zv, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_HT));
}
static void zend_weakref_unregister(zend_object *object, void *payload, bool weakref_free) {
zend_ulong obj_key = zend_object_to_weakref_key(object);
void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), obj_key);
ZEND_ASSERT(tagged_ptr && "Weakref not registered?");
void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
if (tag != ZEND_WEAKREF_TAG_HT) {
ZEND_ASSERT(tagged_ptr == payload);
zend_hash_index_del(&EG(weakrefs), obj_key);
GC_DEL_FLAGS(object, IS_OBJ_WEAKLY_REFERENCED);
/* Do this last, as it may destroy the object. */
if (weakref_free) {
zend_weakref_unref_single(ptr, tag, object);
} else {
/* The optimization of skipping unref is only used in the destructor of WeakMap */
ZEND_ASSERT(ZEND_WEAKREF_GET_TAG(payload) == ZEND_WEAKREF_TAG_MAP);
}
return;
}
HashTable *ht = ptr;
#if ZEND_DEBUG
void *old_payload = zend_hash_index_find_ptr(ht, (zend_ulong) payload);
ZEND_ASSERT(old_payload && "Weakref not registered?");
ZEND_ASSERT(old_payload == payload);
#endif
zend_hash_index_del(ht, (zend_ulong) payload);
if (zend_hash_num_elements(ht) == 0) {
GC_DEL_FLAGS(object, IS_OBJ_WEAKLY_REFERENCED);
zend_hash_destroy(ht);
FREE_HASHTABLE(ht);
zend_hash_index_del(&EG(weakrefs), obj_key);
}
/* Do this last, as it may destroy the object. */
if (weakref_free) {
zend_weakref_unref_single(
ZEND_WEAKREF_GET_PTR(payload), ZEND_WEAKREF_GET_TAG(payload), object);
} else {
/* The optimization of skipping unref is only used in the destructor of WeakMap */
ZEND_ASSERT(ZEND_WEAKREF_GET_TAG(payload) == ZEND_WEAKREF_TAG_MAP);
}
}
ZEND_API zval *zend_weakrefs_hash_add(HashTable *ht, zend_object *key, zval *pData) {
zval *zv = zend_hash_index_add(ht, zend_object_to_weakref_key(key), pData);
if (zv) {
zend_weakref_register(key, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_MAP));
}
return zv;
}
ZEND_API zend_result zend_weakrefs_hash_del(HashTable *ht, zend_object *key) {
zval *zv = zend_hash_index_find(ht, zend_object_to_weakref_key(key));
if (zv) {
zend_weakref_unregister(key, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_MAP), 1);
return SUCCESS;
}
return FAILURE;
}
void zend_weakrefs_init(void) {
zend_hash_init(&EG(weakrefs), 8, NULL, NULL, 0);
}
/* This is called when the object is garbage collected
* to remove all WeakReference and WeakMap entries weakly referencing that object. */
void zend_weakrefs_notify(zend_object *object) {
/* Annoyingly we can't use the HT destructor here, because we need access to the key (which
* is the object address), which is not provided to the dtor. */
const zend_ulong obj_key = zend_object_to_weakref_key(object);
void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), obj_key);
#if ZEND_DEBUG
ZEND_ASSERT(tagged_ptr && "Tracking of the IS_OBJ_WEAKLY_REFERENCE flag should be precise");
#endif
if (tagged_ptr) {
zend_weakref_unref(object, tagged_ptr);
zend_hash_index_del(&EG(weakrefs), obj_key);
}
}
void zend_weakrefs_shutdown(void) {
zend_hash_destroy(&EG(weakrefs));
}
static zend_object* zend_weakref_new(zend_class_entry *ce) {
zend_weakref *wr = zend_object_alloc(sizeof(zend_weakref), zend_ce_weakref);
zend_object_std_init(&wr->std, zend_ce_weakref);
return &wr->std;
}
static zend_always_inline bool zend_weakref_find(zend_object *referent, zval *return_value) {
void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), zend_object_to_weakref_key(referent));
if (!tagged_ptr) {
return 0;
}
void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
if (tag == ZEND_WEAKREF_TAG_REF) {
zend_weakref *wr;
found_weakref:
wr = ptr;
RETVAL_OBJ_COPY(&wr->std);
return 1;
}
if (tag == ZEND_WEAKREF_TAG_HT) {
ZEND_HASH_MAP_FOREACH_PTR(ptr, tagged_ptr) {
if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_REF) {
ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
goto found_weakref;
}
} ZEND_HASH_FOREACH_END();
}
return 0;
}
static zend_always_inline void zend_weakref_create(zend_object *referent, zval *return_value) {
zend_weakref *wr;
object_init_ex(return_value, zend_ce_weakref);
wr = zend_weakref_fetch(return_value);
wr->referent = referent;
zend_weakref_register(wr->referent, ZEND_WEAKREF_ENCODE(wr, ZEND_WEAKREF_TAG_REF));
}
static zend_always_inline void zend_weakref_get(zval *weakref, zval *return_value) {
zend_weakref *wr = zend_weakref_fetch(weakref);
if (wr->referent) {
RETVAL_OBJ_COPY(wr->referent);
}
}
static void zend_weakref_free(zend_object *zo) {
zend_weakref *wr = zend_weakref_from(zo);
if (wr->referent) {
zend_weakref_unregister(wr->referent, ZEND_WEAKREF_ENCODE(wr, ZEND_WEAKREF_TAG_REF), 1);
}
zend_object_std_dtor(&wr->std);
}
static HashTable *zend_weakref_get_debug_info(zend_object *object, int *is_temp)
{
*is_temp = 1;
HashTable *ht = zend_new_array(1);
zend_object *referent = zend_weakref_from(object)->referent;
zval value;
if (referent) {
ZVAL_OBJ_COPY(&value, referent);
} else {
ZVAL_NULL(&value);
}
zend_hash_update(ht, ZSTR_KNOWN(ZEND_STR_OBJECT), &value);
return ht;
}
ZEND_COLD ZEND_METHOD(WeakReference, __construct)
{
zend_throw_error(NULL, "Direct instantiation of WeakReference is not allowed, use WeakReference::create instead");
}
ZEND_METHOD(WeakReference, create)
{
zend_object *referent;
ZEND_PARSE_PARAMETERS_START(1,1)
Z_PARAM_OBJ(referent)
ZEND_PARSE_PARAMETERS_END();
if (zend_weakref_find(referent, return_value)) {
return;
}
zend_weakref_create(referent, return_value);
}
ZEND_METHOD(WeakReference, get)
{
ZEND_PARSE_PARAMETERS_NONE();
zend_weakref_get(ZEND_THIS, return_value);
}
static zend_object *zend_weakmap_create_object(zend_class_entry *ce)
{
zend_weakmap *wm = zend_object_alloc(sizeof(zend_weakmap), ce);
zend_object_std_init(&wm->std, ce);
zend_hash_init(&wm->ht, 0, NULL, ZVAL_PTR_DTOR, 0);
return &wm->std;
}
static void zend_weakmap_free_obj(zend_object *object)
{
zend_weakmap *wm = zend_weakmap_from(object);
zend_ulong obj_key;
ZEND_HASH_MAP_FOREACH_NUM_KEY(&wm->ht, obj_key) {
/* 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).
* Let freeing the corresponding values for WeakMap entries be done in zend_hash_destroy, freeing objects sequentially.
* The performance difference is notable for larger WeakMaps with worse cache locality. */
zend_weakref_unregister(
zend_weakref_key_to_object(obj_key), ZEND_WEAKREF_ENCODE(&wm->ht, ZEND_WEAKREF_TAG_MAP), 0);
} ZEND_HASH_FOREACH_END();
zend_hash_destroy(&wm->ht);
zend_object_std_dtor(&wm->std);
}
static zval *zend_weakmap_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
{
if (offset == NULL) {
zend_throw_error(NULL, "Cannot append to WeakMap");
return NULL;
}
ZVAL_DEREF(offset);
if (Z_TYPE_P(offset) != IS_OBJECT) {
zend_type_error("WeakMap key must be an object");
return NULL;
}
zend_weakmap *wm = zend_weakmap_from(object);
zend_object *obj_addr = Z_OBJ_P(offset);
zval *zv = zend_hash_index_find(&wm->ht, zend_object_to_weakref_key(obj_addr));
if (zv == NULL) {
if (type != BP_VAR_IS) {
zend_throw_error(NULL,
"Object %s#%d not contained in WeakMap", ZSTR_VAL(obj_addr->ce->name), obj_addr->handle);
return NULL;
}
return NULL;
}
if (type == BP_VAR_W || type == BP_VAR_RW) {
ZVAL_MAKE_REF(zv);
}
return zv;
}
static void zend_weakmap_write_dimension(zend_object *object, zval *offset, zval *value)
{
if (offset == NULL) {
zend_throw_error(NULL, "Cannot append to WeakMap");
return;
}
ZVAL_DEREF(offset);
if (Z_TYPE_P(offset) != IS_OBJECT) {
zend_type_error("WeakMap key must be an object");
return;
}
zend_weakmap *wm = zend_weakmap_from(object);
zend_object *obj_addr = Z_OBJ_P(offset);
zend_ulong obj_key = zend_object_to_weakref_key(obj_addr);
Z_TRY_ADDREF_P(value);
zval *zv = zend_hash_index_find(&wm->ht, obj_key);
if (zv) {
/* Because the destructors can have side effects such as resizing or rehashing the WeakMap storage,
* free the zval only after overwriting the original value. */
zval zv_orig;
ZVAL_COPY_VALUE(&zv_orig, zv);
ZVAL_COPY_VALUE(zv, value);
zval_ptr_dtor(&zv_orig);
return;
}
zend_weakref_register(obj_addr, ZEND_WEAKREF_ENCODE(&wm->ht, ZEND_WEAKREF_TAG_MAP));
zend_hash_index_add_new(&wm->ht, obj_key, value);
}
// todo: make zend_weakmap_has_dimension return bool as well
/* int return and check_empty due to Object Handler API */
static int zend_weakmap_has_dimension(zend_object *object, zval *offset, int check_empty)
{
ZVAL_DEREF(offset);
if (Z_TYPE_P(offset) != IS_OBJECT) {
zend_type_error("WeakMap key must be an object");
return 0;
}
zend_weakmap *wm = zend_weakmap_from(object);
zval *zv = zend_hash_index_find(&wm->ht, zend_object_to_weakref_key(Z_OBJ_P(offset)));
if (!zv) {
return 0;
}
if (check_empty) {
return i_zend_is_true(zv);
}
return Z_TYPE_P(zv) != IS_NULL;
}
static void zend_weakmap_unset_dimension(zend_object *object, zval *offset)
{
ZVAL_DEREF(offset);
if (Z_TYPE_P(offset) != IS_OBJECT) {
zend_type_error("WeakMap key must be an object");
return;
}
zend_weakmap *wm = zend_weakmap_from(object);
zend_object *obj_addr = Z_OBJ_P(offset);
if (!zend_hash_index_exists(&wm->ht, zend_object_to_weakref_key(obj_addr))) {
/* Object not in WeakMap, do nothing. */
return;
}
zend_weakref_unregister(obj_addr, ZEND_WEAKREF_ENCODE(&wm->ht, ZEND_WEAKREF_TAG_MAP), 1);
}
static zend_result zend_weakmap_count_elements(zend_object *object, zend_long *count)
{
zend_weakmap *wm = zend_weakmap_from(object);
*count = zend_hash_num_elements(&wm->ht);
return SUCCESS;
}
static HashTable *zend_weakmap_get_properties_for(zend_object *object, zend_prop_purpose purpose)
{
if (purpose != ZEND_PROP_PURPOSE_DEBUG) {
return NULL;
}
zend_weakmap *wm = zend_weakmap_from(object);
HashTable *ht;
ALLOC_HASHTABLE(ht);
zend_hash_init(ht, zend_hash_num_elements(&wm->ht), NULL, ZVAL_PTR_DTOR, 0);
zend_ulong obj_key;
zval *val;
ZEND_HASH_MAP_FOREACH_NUM_KEY_VAL(&wm->ht, obj_key, val) {
zend_object *obj = zend_weakref_key_to_object(obj_key);
zval pair;
array_init(&pair);
GC_ADDREF(obj);
add_assoc_object(&pair, "key", obj);
Z_TRY_ADDREF_P(val);
add_assoc_zval(&pair, "value", val);
zend_hash_next_index_insert_new(ht, &pair);
} ZEND_HASH_FOREACH_END();
return ht;
}
HashTable *zend_weakmap_get_gc(zend_object *object, zval **table, int *n)
{
zend_weakmap *wm = zend_weakmap_from(object);
zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
zval *val;
ZEND_HASH_MAP_FOREACH_VAL(&wm->ht, val) {
zend_get_gc_buffer_add_zval(gc_buffer, val);
} ZEND_HASH_FOREACH_END();
zend_get_gc_buffer_use(gc_buffer, table, n);
return NULL;
}
HashTable *zend_weakmap_get_key_entry_gc(zend_object *object, zval **table, int *n)
{
zend_weakmap *wm = zend_weakmap_from(object);
zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
zend_ulong h;
zval *val;
ZEND_HASH_MAP_FOREACH_NUM_KEY_VAL(&wm->ht, h, val) {
zend_object *key = zend_weakref_key_to_object(h);
zend_get_gc_buffer_add_obj(gc_buffer, key);
zend_get_gc_buffer_add_ptr(gc_buffer, val);
} ZEND_HASH_FOREACH_END();
zend_get_gc_buffer_use(gc_buffer, table, n);
return NULL;
}
HashTable *zend_weakmap_get_entry_gc(zend_object *object, zval **table, int *n)
{
zend_weakmap *wm = zend_weakmap_from(object);
zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
zval *val;
ZEND_HASH_MAP_FOREACH_VAL(&wm->ht, val) {
zend_get_gc_buffer_add_ptr(gc_buffer, val);
} ZEND_HASH_FOREACH_END();
zend_get_gc_buffer_use(gc_buffer, table, n);
return NULL;
}
HashTable *zend_weakmap_get_object_key_entry_gc(zend_object *object, zval **table, int *n)
{
zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
const zend_ulong obj_key = zend_object_to_weakref_key(object);
void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), obj_key);
#if ZEND_DEBUG
ZEND_ASSERT(tagged_ptr && "Tracking of the IS_OBJ_WEAKLY_REFERENCE flag should be precise");
#endif
void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
if (tag == ZEND_WEAKREF_TAG_HT) {
HashTable *ht = ptr;
ZEND_HASH_MAP_FOREACH_PTR(ht, tagged_ptr) {
if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_MAP) {
zend_weakmap *wm = (zend_weakmap*) ZEND_WEAKREF_GET_PTR(tagged_ptr);
zval *zv = zend_hash_index_find(&wm->ht, obj_key);
ZEND_ASSERT(zv);
zend_get_gc_buffer_add_ptr(gc_buffer, zv);
zend_get_gc_buffer_add_obj(gc_buffer, &wm->std);
}
} ZEND_HASH_FOREACH_END();
} else if (tag == ZEND_WEAKREF_TAG_MAP) {
zend_weakmap *wm = (zend_weakmap*) ptr;
zval *zv = zend_hash_index_find(&wm->ht, obj_key);
ZEND_ASSERT(zv);
zend_get_gc_buffer_add_ptr(gc_buffer, zv);
zend_get_gc_buffer_add_obj(gc_buffer, &wm->std);
}
zend_get_gc_buffer_use(gc_buffer, table, n);
return NULL;
}
HashTable *zend_weakmap_get_object_entry_gc(zend_object *object, zval **table, int *n)
{
zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
const zend_ulong obj_key = zend_object_to_weakref_key(object);
void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), obj_key);
#if ZEND_DEBUG
ZEND_ASSERT(tagged_ptr && "Tracking of the IS_OBJ_WEAKLY_REFERENCE flag should be precise");
#endif
void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
if (tag == ZEND_WEAKREF_TAG_HT) {
HashTable *ht = ptr;
ZEND_HASH_MAP_FOREACH_PTR(ht, tagged_ptr) {
if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_MAP) {
zend_weakmap *wm = (zend_weakmap*) ZEND_WEAKREF_GET_PTR(tagged_ptr);
zval *zv = zend_hash_index_find(&wm->ht, obj_key);
ZEND_ASSERT(zv);
zend_get_gc_buffer_add_ptr(gc_buffer, zv);
}
} ZEND_HASH_FOREACH_END();
} else if (tag == ZEND_WEAKREF_TAG_MAP) {
zend_weakmap *wm = (zend_weakmap*) ptr;
zval *zv = zend_hash_index_find(&wm->ht, obj_key);
ZEND_ASSERT(zv);
zend_get_gc_buffer_add_ptr(gc_buffer, zv);
}
zend_get_gc_buffer_use(gc_buffer, table, n);
return NULL;
}
static zend_object *zend_weakmap_clone_obj(zend_object *old_object)
{
zend_object *new_object = zend_weakmap_create_object(zend_ce_weakmap);
zend_weakmap *old_wm = zend_weakmap_from(old_object);
zend_weakmap *new_wm = zend_weakmap_from(new_object);
zend_hash_copy(&new_wm->ht, &old_wm->ht, NULL);
zend_ulong obj_key;
zval *val;
ZEND_HASH_MAP_FOREACH_NUM_KEY_VAL(&new_wm->ht, obj_key, val) {
zend_weakref_register(
zend_weakref_key_to_object(obj_key), ZEND_WEAKREF_ENCODE(new_wm, ZEND_WEAKREF_TAG_MAP));
zval_add_ref(val);
} ZEND_HASH_FOREACH_END();
return new_object;
}
static HashPosition *zend_weakmap_iterator_get_pos_ptr(zend_weakmap_iterator *iter) {
ZEND_ASSERT(iter->ht_iter != (uint32_t) -1);
return &EG(ht_iterators)[iter->ht_iter].pos;
}
static void zend_weakmap_iterator_dtor(zend_object_iterator *obj_iter)
{
zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
zend_hash_iterator_del(iter->ht_iter);
zval_ptr_dtor(&iter->it.data);
}
static zend_result zend_weakmap_iterator_valid(zend_object_iterator *obj_iter)
{
zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
return zend_hash_has_more_elements_ex(&wm->ht, pos);
}
static zval *zend_weakmap_iterator_get_current_data(zend_object_iterator *obj_iter)
{
zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
return zend_hash_get_current_data_ex(&wm->ht, pos);
}
static void zend_weakmap_iterator_get_current_key(zend_object_iterator *obj_iter, zval *key)
{
zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
zend_string *string_key;
zend_ulong num_key;
int key_type = zend_hash_get_current_key_ex(&wm->ht, &string_key, &num_key, pos);
if (key_type == HASH_KEY_NON_EXISTENT) {
ZVAL_NULL(key);
return;
}
if (key_type != HASH_KEY_IS_LONG) {
ZEND_ASSERT(0 && "Must have integer key");
}
ZVAL_OBJ_COPY(key, zend_weakref_key_to_object(num_key));
}
static void zend_weakmap_iterator_move_forward(zend_object_iterator *obj_iter)
{
zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
zend_hash_move_forward_ex(&wm->ht, pos);
}
static void zend_weakmap_iterator_rewind(zend_object_iterator *obj_iter)
{
zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
zend_hash_internal_pointer_reset_ex(&wm->ht, pos);
}
static const zend_object_iterator_funcs zend_weakmap_iterator_funcs = {
zend_weakmap_iterator_dtor,
zend_weakmap_iterator_valid,
zend_weakmap_iterator_get_current_data,
zend_weakmap_iterator_get_current_key,
zend_weakmap_iterator_move_forward,
zend_weakmap_iterator_rewind,
NULL,
NULL, /* get_gc */
};
/* by_ref is int due to Iterator API */
static zend_object_iterator *zend_weakmap_get_iterator(
zend_class_entry *ce, zval *object, int by_ref)
{
zend_weakmap *wm = zend_weakmap_fetch(object);
zend_weakmap_iterator *iter = emalloc(sizeof(zend_weakmap_iterator));
zend_iterator_init(&iter->it);
iter->it.funcs = &zend_weakmap_iterator_funcs;
ZVAL_COPY(&iter->it.data, object);
iter->ht_iter = zend_hash_iterator_add(&wm->ht, 0);
return &iter->it;
}
ZEND_METHOD(WeakMap, offsetGet)
{
zval *key;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &key) == FAILURE) {
RETURN_THROWS();
}
zval *zv = zend_weakmap_read_dimension(Z_OBJ_P(ZEND_THIS), key, BP_VAR_R, NULL);
if (!zv) {
RETURN_THROWS();
}
ZVAL_COPY(return_value, zv);
}
ZEND_METHOD(WeakMap, offsetSet)
{
zval *key, *value;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &key, &value) == FAILURE) {
RETURN_THROWS();
}
zend_weakmap_write_dimension(Z_OBJ_P(ZEND_THIS), key, value);
}
ZEND_METHOD(WeakMap, offsetExists)
{
zval *key;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &key) == FAILURE) {
RETURN_THROWS();
}
RETURN_BOOL(zend_weakmap_has_dimension(Z_OBJ_P(ZEND_THIS), key, /* check_empty */ 0));
}
ZEND_METHOD(WeakMap, offsetUnset)
{
zval *key;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &key) == FAILURE) {
RETURN_THROWS();
}
zend_weakmap_unset_dimension(Z_OBJ_P(ZEND_THIS), key);
}
ZEND_METHOD(WeakMap, count)
{
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
zend_long count;
zend_weakmap_count_elements(Z_OBJ_P(ZEND_THIS), &count);
RETURN_LONG(count);
}
ZEND_METHOD(WeakMap, getIterator)
{
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
zend_create_internal_iterator_zval(return_value, ZEND_THIS);
}
void zend_register_weakref_ce(void) /* {{{ */
{
zend_ce_weakref = register_class_WeakReference();
zend_ce_weakref->create_object = zend_weakref_new;
zend_ce_weakref->default_object_handlers = &zend_weakref_handlers;
memcpy(&zend_weakref_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
zend_weakref_handlers.offset = XtOffsetOf(zend_weakref, std);
zend_weakref_handlers.free_obj = zend_weakref_free;
zend_weakref_handlers.get_debug_info = zend_weakref_get_debug_info;
zend_weakref_handlers.clone_obj = NULL;
zend_ce_weakmap = register_class_WeakMap(zend_ce_arrayaccess, zend_ce_countable, zend_ce_aggregate);
zend_ce_weakmap->create_object = zend_weakmap_create_object;
zend_ce_weakmap->get_iterator = zend_weakmap_get_iterator;
zend_ce_weakmap->default_object_handlers = &zend_weakmap_handlers;
memcpy(&zend_weakmap_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
zend_weakmap_handlers.offset = XtOffsetOf(zend_weakmap, std);
zend_weakmap_handlers.free_obj = zend_weakmap_free_obj;
zend_weakmap_handlers.read_dimension = zend_weakmap_read_dimension;
zend_weakmap_handlers.write_dimension = zend_weakmap_write_dimension;
zend_weakmap_handlers.has_dimension = zend_weakmap_has_dimension;
zend_weakmap_handlers.unset_dimension = zend_weakmap_unset_dimension;
zend_weakmap_handlers.count_elements = zend_weakmap_count_elements;
zend_weakmap_handlers.get_properties_for = zend_weakmap_get_properties_for;
zend_weakmap_handlers.get_gc = zend_weakmap_get_gc;
zend_weakmap_handlers.clone_obj = zend_weakmap_clone_obj;
}
/* }}} */
|