Skip to content

Commit 8094104

Browse files
committed
ext/spl: Refactor debug handlers
Mainly to use zend_mangle_property_name() directly instead of spl_gen_private_prop_name()
1 parent 54047c1 commit 8094104

File tree

7 files changed

+70
-91
lines changed

7 files changed

+70
-91
lines changed

ext/spl/spl_array.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
#include "zend_interfaces.h"
2525
#include "zend_exceptions.h"
2626

27-
#include "spl_functions.h"
2827
#include "spl_iterators.h"
2928
#include "spl_array.h"
3029
#include "spl_array_arginfo.h"
3130
#include "spl_exceptions.h"
31+
#include "spl_functions.h" /* For spl_set_private_debug_info_property() */
3232

3333
/* Defined later in the file */
3434
static zend_object_handlers spl_handler_ArrayIterator;
@@ -763,9 +763,6 @@ static HashTable *spl_array_get_properties_for(zend_object *object, zend_prop_pu
763763

764764
static inline HashTable* spl_array_get_debug_info(zend_object *obj) /* {{{ */
765765
{
766-
zval *storage;
767-
zend_string *zname;
768-
zend_class_entry *base;
769766
spl_array_object *intern = spl_array_from_obj(obj);
770767

771768
if (!intern->std.properties) {
@@ -780,14 +777,13 @@ static inline HashTable* spl_array_get_debug_info(zend_object *obj) /* {{{ */
780777
debug_info = zend_new_array(zend_hash_num_elements(intern->std.properties) + 1);
781778
zend_hash_copy(debug_info, intern->std.properties, (copy_ctor_func_t) zval_add_ref);
782779

783-
storage = &intern->array;
780+
zval *storage = &intern->array;
784781
Z_TRY_ADDREF_P(storage);
785782

786-
base = obj->handlers == &spl_handler_ArrayIterator
783+
const zend_class_entry *base_class_ce = instanceof_function(obj->ce, spl_ce_ArrayIterator)
787784
? spl_ce_ArrayIterator : spl_ce_ArrayObject;
788-
zname = spl_gen_private_prop_name(base, "storage", sizeof("storage")-1);
789-
zend_symtable_update(debug_info, zname, storage);
790-
zend_string_release_ex(zname, 0);
785+
786+
spl_set_private_debug_info_property(base_class_ce, "storage", strlen("storage"), debug_info, storage);
791787

792788
return debug_info;
793789
}

ext/spl/spl_directory.c

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
#include "zend_exceptions.h"
2828
#include "zend_interfaces.h"
2929

30-
#include "spl_functions.h" /* For spl_gen_private_prop_name() */
3130
#include "spl_iterators.h"
3231
#include "spl_directory.h"
3332
#include "spl_directory_arginfo.h"
3433
#include "spl_exceptions.h"
34+
#include "spl_functions.h" /* For spl_set_private_debug_info_property() */
3535

3636
#define SPL_HAS_FLAG(flags, test_flag) ((flags & test_flag) ? 1 : 0)
3737

@@ -600,84 +600,69 @@ static inline HashTable *spl_filesystem_object_get_debug_info(zend_object *objec
600600
{
601601
spl_filesystem_object *intern = spl_filesystem_from_obj(object);
602602
zval tmp;
603-
HashTable *rv;
604-
zend_string *pnstr;
605-
zend_string *path;
606-
char stmp[2];
603+
HashTable *debug_info;
604+
zend_string *path_name;
607605

608606
if (!intern->std.properties) {
609607
rebuild_object_properties(&intern->std);
610608
}
611609

612-
rv = zend_array_dup(intern->std.properties);
610+
// TODO Do zend_new_array() + zend_hash_copy() trick?
611+
debug_info = zend_array_dup(intern->std.properties);
613612

614-
pnstr = spl_gen_private_prop_name(spl_ce_SplFileInfo, "pathName", sizeof("pathName")-1);
615-
path = spl_filesystem_object_get_pathname(intern);
616-
if (path) {
617-
ZVAL_STR_COPY(&tmp, path);
613+
path_name = spl_filesystem_object_get_pathname(intern);
614+
if (path_name) {
615+
ZVAL_STR_COPY(&tmp, path_name);
618616
} else {
619617
ZVAL_EMPTY_STRING(&tmp);
620618
}
621-
zend_symtable_update(rv, pnstr, &tmp);
622-
zend_string_release_ex(pnstr, 0);
619+
/* IMPORTANT: Do not free path_name as spl_filesystem_object_get_pathname()
620+
* updates/sets the intern->file_name and returns the pointer to
621+
* intern->file_name which must remain allocated. */
622+
spl_set_private_debug_info_property(spl_ce_SplFileInfo, "pathName", strlen("pathName"), debug_info, &tmp);
623623

624624
if (intern->file_name) {
625-
zend_string *path;
626-
627-
pnstr = spl_gen_private_prop_name(spl_ce_SplFileInfo, "fileName", sizeof("fileName")-1);
628-
path = spl_filesystem_object_get_path(intern);
629-
625+
zend_string *path = spl_filesystem_object_get_path(intern);
630626
if (path && ZSTR_LEN(path) && ZSTR_LEN(path) < ZSTR_LEN(intern->file_name)) {
631627
/* +1 to skip the trailing / of the path in the file name */
632628
ZVAL_STRINGL(&tmp, ZSTR_VAL(intern->file_name) + ZSTR_LEN(path) + 1, ZSTR_LEN(intern->file_name) - (ZSTR_LEN(path) + 1));
633629
} else {
634630
ZVAL_STR_COPY(&tmp, intern->file_name);
635631
}
636-
zend_symtable_update(rv, pnstr, &tmp);
637-
zend_string_release_ex(pnstr, /* persistent */ false);
638632
if (path) {
639633
zend_string_release_ex(path, /* persistent */ false);
640634
}
635+
636+
spl_set_private_debug_info_property(spl_ce_SplFileInfo, "fileName", strlen("fileName"), debug_info, &tmp);
641637
}
642638
if (intern->type == SPL_FS_DIR) {
643639
#ifdef HAVE_GLOB
644-
pnstr = spl_gen_private_prop_name(spl_ce_DirectoryIterator, "glob", sizeof("glob")-1);
645-
if (php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
640+
if (php_stream_is(intern->u.dir.dirp, &php_glob_stream_ops)) {
646641
ZVAL_STR_COPY(&tmp, intern->path);
647642
} else {
648643
ZVAL_FALSE(&tmp);
649644
}
650-
zend_symtable_update(rv, pnstr, &tmp);
651-
zend_string_release_ex(pnstr, 0);
645+
spl_set_private_debug_info_property(spl_ce_DirectoryIterator, "glob", strlen("glob"), debug_info, &tmp);
652646
#endif
653-
pnstr = spl_gen_private_prop_name(spl_ce_RecursiveDirectoryIterator, "subPathName", sizeof("subPathName")-1);
654647
if (intern->u.dir.sub_path) {
655648
ZVAL_STR_COPY(&tmp, intern->u.dir.sub_path);
656649
} else {
657650
ZVAL_EMPTY_STRING(&tmp);
658651
}
659-
zend_symtable_update(rv, pnstr, &tmp);
660-
zend_string_release_ex(pnstr, 0);
652+
spl_set_private_debug_info_property(spl_ce_RecursiveDirectoryIterator, "subPathName", strlen("subPathName"), debug_info, &tmp);
661653
}
662654
if (intern->type == SPL_FS_FILE) {
663-
pnstr = spl_gen_private_prop_name(spl_ce_SplFileObject, "openMode", sizeof("openMode")-1);
664655
ZVAL_STR_COPY(&tmp, intern->u.file.open_mode);
665-
zend_symtable_update(rv, pnstr, &tmp);
666-
zend_string_release_ex(pnstr, 0);
667-
stmp[1] = '\0';
668-
stmp[0] = intern->u.file.delimiter;
669-
pnstr = spl_gen_private_prop_name(spl_ce_SplFileObject, "delimiter", sizeof("delimiter")-1);
670-
ZVAL_STRINGL(&tmp, stmp, 1);
671-
zend_symtable_update(rv, pnstr, &tmp);
672-
zend_string_release_ex(pnstr, 0);
673-
stmp[0] = intern->u.file.enclosure;
674-
pnstr = spl_gen_private_prop_name(spl_ce_SplFileObject, "enclosure", sizeof("enclosure")-1);
675-
ZVAL_STRINGL(&tmp, stmp, 1);
676-
zend_symtable_update(rv, pnstr, &tmp);
677-
zend_string_release_ex(pnstr, 0);
678-
}
679-
680-
return rv;
656+
spl_set_private_debug_info_property(spl_ce_SplFileObject, "openMode", strlen("openMode"), debug_info, &tmp);
657+
658+
ZVAL_STR(&tmp, ZSTR_CHAR((zend_uchar)intern->u.file.delimiter));
659+
spl_set_private_debug_info_property(spl_ce_SplFileObject, "delimiter", strlen("delimiter"), debug_info, &tmp);
660+
661+
ZVAL_STR(&tmp, ZSTR_CHAR((zend_uchar)intern->u.file.enclosure));
662+
spl_set_private_debug_info_property(spl_ce_SplFileObject, "enclosure", strlen("enclosure"), debug_info, &tmp);
663+
}
664+
665+
return debug_info;
681666
}
682667
/* }}} */
683668

ext/spl/spl_dllist.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525

2626
#include "ext/standard/php_var.h"
2727
#include "zend_smart_str.h"
28-
#include "spl_functions.h" /* For spl_gen_private_prop_name() */
2928
#include "spl_dllist.h"
3029
#include "spl_dllist_arginfo.h"
3130
#include "spl_exceptions.h"
31+
#include "spl_functions.h" /* For spl_set_private_debug_info_property() */
3232

3333
static zend_object_handlers spl_handler_SplDoublyLinkedList;
3434
PHPAPI zend_class_entry *spl_ce_SplDoublyLinkedList;
@@ -424,41 +424,37 @@ static zend_result spl_dllist_object_count_elements(zend_object *object, zend_lo
424424
static inline HashTable* spl_dllist_object_get_debug_info(zend_object *obj) /* {{{{ */
425425
{
426426
spl_dllist_object *intern = spl_dllist_from_obj(obj);
427-
spl_ptr_llist_element *current = intern->llist->head, *next;
427+
spl_ptr_llist_element *current = intern->llist->head;
428428
zval tmp, dllist_array;
429-
zend_string *pnstr;
430-
int i = 0;
431429
HashTable *debug_info;
432430

433431
if (!intern->std.properties) {
434432
rebuild_object_properties(&intern->std);
435433
}
436434

437-
debug_info = zend_new_array(1);
435+
/* +2 As we are adding 2 additional key-entries */
436+
debug_info = zend_new_array(zend_hash_num_elements(intern->std.properties) + 2);
438437
zend_hash_copy(debug_info, intern->std.properties, (copy_ctor_func_t) zval_add_ref);
439438

440-
pnstr = spl_gen_private_prop_name(spl_ce_SplDoublyLinkedList, "flags", sizeof("flags")-1);
441439
ZVAL_LONG(&tmp, intern->flags);
442-
zend_hash_add(debug_info, pnstr, &tmp);
443-
zend_string_release_ex(pnstr, 0);
440+
spl_set_private_debug_info_property(spl_ce_SplDoublyLinkedList, "flags", strlen("flags"), debug_info, &tmp);
444441

445442
array_init(&dllist_array);
446443

444+
zend_ulong index = 0;
447445
while (current) {
448-
next = current->next;
446+
spl_ptr_llist_element *next = current->next;
449447

450-
add_index_zval(&dllist_array, i, &current->data);
448+
add_index_zval(&dllist_array, index, &current->data);
451449
if (Z_REFCOUNTED(current->data)) {
452450
Z_ADDREF(current->data);
453451
}
454-
i++;
452+
index++;
455453

456454
current = next;
457455
}
458456

459-
pnstr = spl_gen_private_prop_name(spl_ce_SplDoublyLinkedList, "dllist", sizeof("dllist")-1);
460-
zend_hash_add(debug_info, pnstr, &dllist_array);
461-
zend_string_release_ex(pnstr, 0);
457+
spl_set_private_debug_info_property(spl_ce_SplDoublyLinkedList, "dllist", strlen("dllist"), debug_info, &dllist_array);
462458

463459
return debug_info;
464460
}

ext/spl/spl_functions.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,21 @@ void spl_add_classes(zend_class_entry *pce, zval *list, bool sub, int allow, int
7777
}
7878
/* }}} */
7979

80-
zend_string * spl_gen_private_prop_name(zend_class_entry *ce, char *prop_name, size_t prop_len) /* {{{ */
80+
void spl_set_private_debug_info_property(
81+
const zend_class_entry *ce,
82+
const char *property,
83+
size_t property_len,
84+
HashTable *debug_info,
85+
zval *value
86+
)
8187
{
82-
return zend_mangle_property_name(ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), prop_name, prop_len, 0);
88+
zend_string *mangled_named = zend_mangle_property_name(
89+
ZSTR_VAL(ce->name),
90+
ZSTR_LEN(ce->name),
91+
property,
92+
property_len,
93+
/* persistent */ false
94+
);
95+
zend_hash_update(debug_info, mangled_named, value);
96+
zend_string_release_ex(mangled_named, /* persistent */ false);
8397
}
84-
/* }}} */

ext/spl/spl_functions.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ void spl_add_interfaces(zval * list, zend_class_entry * pce, int allow, int ce_f
3131
void spl_add_traits(zval * list, zend_class_entry * pce, int allow, int ce_flags);
3232
void spl_add_classes(zend_class_entry *pce, zval *list, bool sub, int allow, int ce_flags);
3333

34-
/* caller must efree(return) */
35-
zend_string *spl_gen_private_prop_name(zend_class_entry *ce, char *prop_name, size_t prop_len);
34+
void spl_set_private_debug_info_property(const zend_class_entry *ce, const char *property, size_t property_len, HashTable *debug_info, zval *value);
3635

3736
#endif /* PHP_FUNCTIONS_H */

ext/spl/spl_heap.c

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
#include "zend_interfaces.h"
2323
#include "zend_exceptions.h"
2424

25-
#include "spl_functions.h"
2625
#include "spl_heap.h"
2726
#include "spl_heap_arginfo.h"
2827
#include "spl_exceptions.h"
28+
#include "spl_functions.h" /* For spl_set_private_debug_info_property() */
2929

3030
#define PTR_HEAP_BLOCK_SIZE 64
3131

@@ -503,33 +503,28 @@ static zend_result spl_heap_object_count_elements(zend_object *object, zend_long
503503
}
504504
/* }}} */
505505

506-
static inline HashTable* spl_heap_object_get_debug_info(zend_class_entry *ce, zend_object *obj) { /* {{{ */
506+
static inline HashTable* spl_heap_object_get_debug_info(const zend_class_entry *ce, zend_object *obj) { /* {{{ */
507507
spl_heap_object *intern = spl_heap_from_obj(obj);
508508
zval tmp, heap_array;
509-
zend_string *pnstr;
510509
HashTable *debug_info;
511-
int i;
512510

513511
if (!intern->std.properties) {
514512
rebuild_object_properties(&intern->std);
515513
}
516514

517-
debug_info = zend_new_array(zend_hash_num_elements(intern->std.properties) + 1);
515+
/* +3 As we are adding 3 additional key-entries */
516+
debug_info = zend_new_array(zend_hash_num_elements(intern->std.properties) + 3);
518517
zend_hash_copy(debug_info, intern->std.properties, (copy_ctor_func_t) zval_add_ref);
519518

520-
pnstr = spl_gen_private_prop_name(ce, "flags", sizeof("flags")-1);
521519
ZVAL_LONG(&tmp, intern->flags);
522-
zend_hash_update(debug_info, pnstr, &tmp);
523-
zend_string_release_ex(pnstr, 0);
520+
spl_set_private_debug_info_property(ce, "flags", strlen("flags"), debug_info, &tmp);
524521

525-
pnstr = spl_gen_private_prop_name(ce, "isCorrupted", sizeof("isCorrupted")-1);
526522
ZVAL_BOOL(&tmp, intern->heap->flags&SPL_HEAP_CORRUPTED);
527-
zend_hash_update(debug_info, pnstr, &tmp);
528-
zend_string_release_ex(pnstr, 0);
523+
spl_set_private_debug_info_property(ce, "isCorrupted", strlen("isCorrupted"), debug_info, &tmp);
529524

530525
array_init(&heap_array);
531526

532-
for (i = 0; i < intern->heap->count; ++i) {
527+
for (zend_ulong i = 0; i < intern->heap->count; ++i) {
533528
if (ce == spl_ce_SplPriorityQueue) {
534529
spl_pqueue_elem *pq_elem = spl_heap_elem(intern->heap, i);
535530
zval elem;
@@ -542,9 +537,7 @@ static inline HashTable* spl_heap_object_get_debug_info(zend_class_entry *ce, ze
542537
}
543538
}
544539

545-
pnstr = spl_gen_private_prop_name(ce, "heap", sizeof("heap")-1);
546-
zend_hash_update(debug_info, pnstr, &heap_array);
547-
zend_string_release_ex(pnstr, 0);
540+
spl_set_private_debug_info_property(ce, "heap", strlen("heap"), debug_info, &heap_array);
548541

549542
return debug_info;
550543
}

ext/spl/spl_observer.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
#include "zend_exceptions.h"
2828

2929
#include "php_spl.h" /* For php_spl_object_hash() */
30-
#include "spl_functions.h" /* For spl_gen_private_prop_name() */
3130
#include "spl_observer.h"
3231
#include "spl_observer_arginfo.h"
3332
#include "spl_iterators.h"
3433
#include "spl_exceptions.h"
34+
#include "spl_functions.h" /* For spl_set_private_debug_info_property() */
3535

3636
PHPAPI zend_class_entry *spl_ce_SplObserver;
3737
PHPAPI zend_class_entry *spl_ce_SplSubject;
@@ -321,7 +321,6 @@ static inline HashTable* spl_object_storage_debug_info(zend_object *obj) /* {{{
321321
spl_SplObjectStorageElement *element;
322322
HashTable *props;
323323
zval tmp, storage;
324-
zend_string *zname;
325324
HashTable *debug_info;
326325

327326
props = obj->handlers->get_properties(obj);
@@ -343,9 +342,7 @@ static inline HashTable* spl_object_storage_debug_info(zend_object *obj) /* {{{
343342
zend_hash_next_index_insert(Z_ARRVAL(storage), &tmp);
344343
} ZEND_HASH_FOREACH_END();
345344

346-
zname = spl_gen_private_prop_name(spl_ce_SplObjectStorage, "storage", sizeof("storage")-1);
347-
zend_symtable_update(debug_info, zname, &storage);
348-
zend_string_release_ex(zname, 0);
345+
spl_set_private_debug_info_property(spl_ce_SplObjectStorage, "storage", strlen("storage"), debug_info, &storage);
349346

350347
return debug_info;
351348
}

0 commit comments

Comments
 (0)