Skip to content

Commit 02db708

Browse files
committed
Remove generic dtor+ctor from SPL dllist
This is only ever used with zvals. It was particularly confusing because a lot of code mixed the generic code with zval specific code.
1 parent 6cd0b48 commit 02db708

File tree

1 file changed

+17
-77
lines changed

1 file changed

+17
-77
lines changed

ext/spl/spl_dllist.c

Lines changed: 17 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,9 @@ typedef struct _spl_ptr_llist_element {
6666
zval data;
6767
} spl_ptr_llist_element;
6868

69-
typedef void (*spl_ptr_llist_dtor_func)(spl_ptr_llist_element *);
70-
typedef void (*spl_ptr_llist_ctor_func)(spl_ptr_llist_element *);
71-
7269
typedef struct _spl_ptr_llist {
73-
spl_ptr_llist_element *head;
74-
spl_ptr_llist_element *tail;
75-
spl_ptr_llist_dtor_func dtor;
76-
spl_ptr_llist_ctor_func ctor;
70+
spl_ptr_llist_element *head;
71+
spl_ptr_llist_element *tail;
7772
int count;
7873
} spl_ptr_llist;
7974

@@ -109,31 +104,13 @@ static inline spl_dllist_object *spl_dllist_from_obj(zend_object *obj) /* {{{ */
109104

110105
#define Z_SPLDLLIST_P(zv) spl_dllist_from_obj(Z_OBJ_P((zv)))
111106

112-
/* {{{ spl_ptr_llist */
113-
static void spl_ptr_llist_zval_dtor(spl_ptr_llist_element *elem) { /* {{{ */
114-
if (!Z_ISUNDEF(elem->data)) {
115-
zval_ptr_dtor(&elem->data);
116-
ZVAL_UNDEF(&elem->data);
117-
}
118-
}
119-
/* }}} */
120-
121-
static void spl_ptr_llist_zval_ctor(spl_ptr_llist_element *elem) { /* {{{ */
122-
if (Z_REFCOUNTED(elem->data)) {
123-
Z_ADDREF(elem->data);
124-
}
125-
}
126-
/* }}} */
127-
128-
static spl_ptr_llist *spl_ptr_llist_init(spl_ptr_llist_ctor_func ctor, spl_ptr_llist_dtor_func dtor) /* {{{ */
107+
static spl_ptr_llist *spl_ptr_llist_init() /* {{{ */
129108
{
130109
spl_ptr_llist *llist = emalloc(sizeof(spl_ptr_llist));
131110

132111
llist->head = NULL;
133112
llist->tail = NULL;
134113
llist->count = 0;
135-
llist->dtor = dtor;
136-
llist->ctor = ctor;
137114

138115
return llist;
139116
}
@@ -147,14 +124,11 @@ static zend_long spl_ptr_llist_count(spl_ptr_llist *llist) /* {{{ */
147124

148125
static void spl_ptr_llist_destroy(spl_ptr_llist *llist) /* {{{ */
149126
{
150-
spl_ptr_llist_element *current = llist->head, *next;
151-
spl_ptr_llist_dtor_func dtor = llist->dtor;
127+
spl_ptr_llist_element *current = llist->head, *next;
152128

153129
while (current) {
154130
next = current->next;
155-
if (dtor) {
156-
dtor(current);
157-
}
131+
zval_ptr_dtor(&current->data);
158132
SPL_LLIST_DELREF(current);
159133
current = next;
160134
}
@@ -206,9 +180,7 @@ static void spl_ptr_llist_unshift(spl_ptr_llist *llist, zval *data) /* {{{ */
206180
llist->head = elem;
207181
llist->count++;
208182

209-
if (llist->ctor) {
210-
llist->ctor(elem);
211-
}
183+
Z_TRY_ADDREF(elem->data);
212184
}
213185
/* }}} */
214186

@@ -230,9 +202,7 @@ static void spl_ptr_llist_push(spl_ptr_llist *llist, zval *data) /* {{{ */
230202
llist->tail = elem;
231203
llist->count++;
232204

233-
if (llist->ctor) {
234-
llist->ctor(elem);
235-
}
205+
Z_TRY_ADDREF(elem->data);
236206
}
237207
/* }}} */
238208

@@ -253,14 +223,10 @@ static void spl_ptr_llist_pop(spl_ptr_llist *llist, zval *ret) /* {{{ */
253223

254224
llist->tail = tail->prev;
255225
llist->count--;
256-
ZVAL_COPY(ret, &tail->data);
226+
ZVAL_COPY_VALUE(ret, &tail->data);
227+
ZVAL_UNDEF(&tail->data);
257228

258229
tail->prev = NULL;
259-
if (llist->dtor) {
260-
llist->dtor(tail);
261-
}
262-
263-
ZVAL_UNDEF(&tail->data);
264230

265231
SPL_LLIST_DELREF(tail);
266232
}
@@ -307,13 +273,10 @@ static void spl_ptr_llist_shift(spl_ptr_llist *llist, zval *ret) /* {{{ */
307273

308274
llist->head = head->next;
309275
llist->count--;
310-
ZVAL_COPY(ret, &head->data);
276+
ZVAL_COPY_VALUE(ret, &head->data);
277+
ZVAL_UNDEF(&head->data);
311278

312279
head->next = NULL;
313-
if (llist->dtor) {
314-
llist->dtor(head);
315-
}
316-
ZVAL_UNDEF(&head->data);
317280

318281
SPL_LLIST_DELREF(head);
319282
}
@@ -322,17 +285,9 @@ static void spl_ptr_llist_shift(spl_ptr_llist *llist, zval *ret) /* {{{ */
322285
static void spl_ptr_llist_copy(spl_ptr_llist *from, spl_ptr_llist *to) /* {{{ */
323286
{
324287
spl_ptr_llist_element *current = from->head, *next;
325-
//??? spl_ptr_llist_ctor_func ctor = from->ctor;
326288

327289
while (current) {
328290
next = current->next;
329-
330-
/*??? FIXME
331-
if (ctor) {
332-
ctor(current);
333-
}
334-
*/
335-
336291
spl_ptr_llist_push(to, &current->data);
337292
current = next;
338293
}
@@ -378,7 +333,7 @@ static zend_object *spl_dllist_object_new_ex(zend_class_entry *class_type, zend_
378333
intern->ce_get_iterator = other->ce_get_iterator;
379334

380335
if (clone_orig) {
381-
intern->llist = (spl_ptr_llist *)spl_ptr_llist_init(other->llist->ctor, other->llist->dtor);
336+
intern->llist = spl_ptr_llist_init();
382337
spl_ptr_llist_copy(other->llist, intern->llist);
383338
intern->traverse_pointer = intern->llist->head;
384339
SPL_LLIST_CHECK_ADDREF(intern->traverse_pointer);
@@ -390,7 +345,7 @@ static zend_object *spl_dllist_object_new_ex(zend_class_entry *class_type, zend_
390345

391346
intern->flags = other->flags;
392347
} else {
393-
intern->llist = (spl_ptr_llist *)spl_ptr_llist_init(spl_ptr_llist_zval_ctor, spl_ptr_llist_zval_dtor);
348+
intern->llist = spl_ptr_llist_init();
394349
intern->traverse_pointer = intern->llist->head;
395350
SPL_LLIST_CHECK_ADDREF(intern->traverse_pointer);
396351
}
@@ -789,20 +744,10 @@ PHP_METHOD(SplDoublyLinkedList, offsetSet)
789744
element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
790745

791746
if (element != NULL) {
792-
/* call dtor on the old element as in spl_ptr_llist_pop */
793-
if (intern->llist->dtor) {
794-
intern->llist->dtor(element);
795-
}
796-
797747
/* the element is replaced, delref the old one as in
798748
* SplDoublyLinkedList::pop() */
799749
zval_ptr_dtor(&element->data);
800-
ZVAL_COPY_VALUE(&element->data, value);
801-
802-
/* new element, call ctor as in spl_ptr_llist_push */
803-
if (intern->llist->ctor) {
804-
intern->llist->ctor(element);
805-
}
750+
ZVAL_COPY(&element->data, value);
806751
} else {
807752
zval_ptr_dtor(value);
808753
zend_argument_error(spl_ce_OutOfRangeException, 1, "is an invalid offset");
@@ -855,16 +800,13 @@ PHP_METHOD(SplDoublyLinkedList, offsetUnset)
855800
/* finally, delete the element */
856801
llist->count--;
857802

858-
if(llist->dtor) {
859-
llist->dtor(element);
860-
}
803+
zval_ptr_dtor(&element->data);
804+
ZVAL_UNDEF(&element->data);
861805

862806
if (intern->traverse_pointer == element) {
863807
SPL_LLIST_DELREF(element);
864808
intern->traverse_pointer = NULL;
865809
}
866-
zval_ptr_dtor(&element->data);
867-
ZVAL_UNDEF(&element->data);
868810

869811
SPL_LLIST_DELREF(element);
870812
} else {
@@ -1274,9 +1216,7 @@ PHP_METHOD(SplDoublyLinkedList, add)
12741216

12751217
intern->llist->count++;
12761218

1277-
if (intern->llist->ctor) {
1278-
intern->llist->ctor(elem);
1279-
}
1219+
Z_TRY_ADDREF(elem->data);
12801220
}
12811221
} /* }}} */
12821222

0 commit comments

Comments
 (0)