Coverage Report

Created: 2025-09-27 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/spl/spl_heap.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright (c) The PHP Group                                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to version 3.01 of the PHP license,      |
6
   | that is bundled with this package in the file LICENSE, and is        |
7
   | available through the world-wide-web at the following url:           |
8
   | https://www.php.net/license/3_01.txt                                 |
9
   | If you did not receive a copy of the PHP license and are unable to   |
10
   | obtain it through the world-wide-web, please send a note to          |
11
   | license@php.net so we can mail you a copy immediately.               |
12
   +----------------------------------------------------------------------+
13
   | Authors: Etienne Kneuss <colder@php.net>                             |
14
   +----------------------------------------------------------------------+
15
 */
16
17
#ifdef HAVE_CONFIG_H
18
# include "config.h"
19
#endif
20
21
#include "php.h"
22
#include "zend_interfaces.h"
23
#include "zend_exceptions.h"
24
25
#include "spl_heap.h"
26
#include "spl_heap_arginfo.h"
27
#include "spl_exceptions.h"
28
#include "spl_functions.h" /* For spl_set_private_debug_info_property() */
29
30
658
#define PTR_HEAP_BLOCK_SIZE 64
31
32
0
#define SPL_HEAP_CORRUPTED       0x00000001
33
1.31k
#define SPL_HEAP_WRITE_LOCKED    0x00000002
34
35
static zend_object_handlers spl_handler_SplHeap;
36
static zend_object_handlers spl_handler_SplPriorityQueue;
37
38
PHPAPI zend_class_entry  *spl_ce_SplHeap;
39
PHPAPI zend_class_entry  *spl_ce_SplMaxHeap;
40
PHPAPI zend_class_entry  *spl_ce_SplMinHeap;
41
PHPAPI zend_class_entry  *spl_ce_SplPriorityQueue;
42
43
44
typedef void (*spl_ptr_heap_dtor_func)(void *);
45
typedef void (*spl_ptr_heap_ctor_func)(void *);
46
typedef int  (*spl_ptr_heap_cmp_func)(void *, void *, zval *);
47
48
typedef struct _spl_ptr_heap {
49
  void                   *elements;
50
  spl_ptr_heap_ctor_func  ctor;
51
  spl_ptr_heap_dtor_func  dtor;
52
  spl_ptr_heap_cmp_func   cmp;
53
  size_t                  count;
54
  int                     flags;
55
  size_t                  max_size;
56
  size_t                  elem_size;
57
} spl_ptr_heap;
58
59
typedef struct _spl_heap_object spl_heap_object;
60
typedef struct _spl_heap_it spl_heap_it;
61
62
struct _spl_heap_object {
63
  spl_ptr_heap       *heap;
64
  int                 flags;
65
  zend_function      *fptr_cmp;
66
  zend_function      *fptr_count;
67
  zend_object         std;
68
};
69
70
typedef struct _spl_pqueue_elem {
71
  zval data;
72
  zval priority;
73
} spl_pqueue_elem;
74
75
669
static inline spl_heap_object *spl_heap_from_obj(zend_object *obj) /* {{{ */ {
76
669
  return (spl_heap_object*)((char*)(obj) - XtOffsetOf(spl_heap_object, std));
77
669
}
78
/* }}} */
79
80
5
#define Z_SPLHEAP_P(zv)  spl_heap_from_obj(Z_OBJ_P((zv)))
81
82
0
static zend_always_inline void *spl_heap_elem(spl_ptr_heap *heap, size_t i) {
83
0
  return (void *) ((char *) heap->elements + heap->elem_size * i);
84
0
}
85
86
0
static zend_always_inline void spl_heap_elem_copy(spl_ptr_heap *heap, void *to, void *from) {
87
0
  assert(to != from);
88
89
  /* Specialized for cases of heap and priority queue. With the size being
90
   * constant known at compile time the compiler can fully inline calls to memcpy. */
91
0
  if (heap->elem_size == sizeof(spl_pqueue_elem)) {
92
0
    memcpy(to, from, sizeof(spl_pqueue_elem));
93
0
  } else {
94
0
    ZEND_ASSERT(heap->elem_size == sizeof(zval));
95
0
    memcpy(to, from, sizeof(zval));
96
0
  }
97
0
}
98
99
0
static void spl_ptr_heap_zval_dtor(void *elem) { /* {{{ */
100
0
  zval_ptr_dtor((zval *) elem);
101
0
}
102
/* }}} */
103
104
0
static void spl_ptr_heap_zval_ctor(void *elem) { /* {{{ */
105
0
  Z_TRY_ADDREF_P((zval *) elem);
106
0
}
107
/* }}} */
108
109
0
static void spl_ptr_heap_pqueue_elem_dtor(void *elem) { /* {{{ */
110
0
  spl_pqueue_elem *pq_elem = elem;
111
0
  zval_ptr_dtor(&pq_elem->data);
112
0
  zval_ptr_dtor(&pq_elem->priority);
113
0
}
114
/* }}} */
115
116
0
static void spl_ptr_heap_pqueue_elem_ctor(void *elem) { /* {{{ */
117
0
  spl_pqueue_elem *pq_elem = elem;
118
0
  Z_TRY_ADDREF_P(&pq_elem->data);
119
0
  Z_TRY_ADDREF_P(&pq_elem->priority);
120
0
}
121
/* }}} */
122
123
0
static zend_result spl_ptr_heap_cmp_cb_helper(zval *object, spl_heap_object *heap_object, zval *a, zval *b, zend_long *result) { /* {{{ */
124
0
  zval zresult;
125
126
0
  zend_call_method_with_2_params(Z_OBJ_P(object), heap_object->std.ce, &heap_object->fptr_cmp, "compare", &zresult, a, b);
127
128
0
  if (EG(exception)) {
129
0
    return FAILURE;
130
0
  }
131
132
0
  *result = zval_get_long(&zresult);
133
0
  zval_ptr_dtor(&zresult);
134
135
0
  return SUCCESS;
136
0
}
137
/* }}} */
138
139
static void spl_pqueue_extract_helper(zval *result, spl_pqueue_elem *elem, int flags) /* {{{ */
140
0
{
141
0
  if ((flags & SPL_PQUEUE_EXTR_BOTH) == SPL_PQUEUE_EXTR_BOTH) {
142
0
    array_init(result);
143
0
    Z_TRY_ADDREF(elem->data);
144
0
    add_assoc_zval_ex(result, "data", sizeof("data") - 1, &elem->data);
145
0
    Z_TRY_ADDREF(elem->priority);
146
0
    add_assoc_zval_ex(result, "priority", sizeof("priority") - 1, &elem->priority);
147
0
    return;
148
0
  }
149
150
0
  if (flags & SPL_PQUEUE_EXTR_DATA) {
151
0
    ZVAL_COPY(result, &elem->data);
152
0
    return;
153
0
  }
154
155
0
  if (flags & SPL_PQUEUE_EXTR_PRIORITY) {
156
0
    ZVAL_COPY(result, &elem->priority);
157
0
    return;
158
0
  }
159
160
0
  ZEND_UNREACHABLE();
161
0
}
162
/* }}} */
163
164
0
static int spl_ptr_heap_zval_max_cmp(void *x, void *y, zval *object) { /* {{{ */
165
0
  zval *a = x, *b = y;
166
167
0
  if (EG(exception)) {
168
0
    return 0;
169
0
  }
170
171
0
  if (object) {
172
0
    spl_heap_object *heap_object = Z_SPLHEAP_P(object);
173
0
    if (heap_object->fptr_cmp) {
174
0
      zend_long lval = 0;
175
0
      if (spl_ptr_heap_cmp_cb_helper(object, heap_object, a, b, &lval) == FAILURE) {
176
        /* exception or call failure */
177
0
        return 0;
178
0
      }
179
0
      return ZEND_NORMALIZE_BOOL(lval);
180
0
    }
181
0
  }
182
183
0
  return zend_compare(a, b);
184
0
}
185
/* }}} */
186
187
0
static int spl_ptr_heap_zval_min_cmp(void *x, void *y, zval *object) { /* {{{ */
188
0
  zval *a = x, *b = y;
189
190
0
  if (EG(exception)) {
191
0
    return 0;
192
0
  }
193
194
0
  if (object) {
195
0
    spl_heap_object *heap_object = Z_SPLHEAP_P(object);
196
0
    if (heap_object->fptr_cmp) {
197
0
      zend_long lval = 0;
198
0
      if (spl_ptr_heap_cmp_cb_helper(object, heap_object, a, b, &lval) == FAILURE) {
199
        /* exception or call failure */
200
0
        return 0;
201
0
      }
202
0
      return ZEND_NORMALIZE_BOOL(lval);
203
0
    }
204
0
  }
205
206
0
  return zend_compare(b, a);
207
0
}
208
/* }}} */
209
210
0
static int spl_ptr_pqueue_elem_cmp(void *x, void *y, zval *object) { /* {{{ */
211
0
  spl_pqueue_elem *a = x;
212
0
  spl_pqueue_elem *b = y;
213
0
  zval *a_priority_p = &a->priority;
214
0
  zval *b_priority_p = &b->priority;
215
216
0
  if (EG(exception)) {
217
0
    return 0;
218
0
  }
219
220
0
  if (object) {
221
0
    spl_heap_object *heap_object = Z_SPLHEAP_P(object);
222
0
    if (heap_object->fptr_cmp) {
223
0
      zend_long lval = 0;
224
0
      if (spl_ptr_heap_cmp_cb_helper(object, heap_object, a_priority_p, b_priority_p, &lval) == FAILURE) {
225
        /* exception or call failure */
226
0
        return 0;
227
0
      }
228
0
      return ZEND_NORMALIZE_BOOL(lval);
229
0
    }
230
0
  }
231
232
0
  return zend_compare(a_priority_p, b_priority_p);
233
0
}
234
/* }}} */
235
236
/* Specialized comparator used when we are absolutely sure an instance of the
237
 * not inherited SplPriorityQueue class contains only priorities as longs. This
238
 * fact is tracked during insertion into the queue. */
239
0
static int spl_ptr_pqueue_elem_cmp_long(void *x, void *y, zval *object) {
240
0
  zend_long a = Z_LVAL(((spl_pqueue_elem*) x)->priority);
241
0
  zend_long b = Z_LVAL(((spl_pqueue_elem*) y)->priority);
242
0
  return a>b ? 1 : (a<b ? -1 : 0);
243
0
}
244
245
/* same as spl_ptr_pqueue_elem_cmp_long */
246
0
static int spl_ptr_pqueue_elem_cmp_double(void *x, void *y, zval *object) {
247
0
  double a = Z_DVAL(((spl_pqueue_elem*) x)->priority);
248
0
  double b = Z_DVAL(((spl_pqueue_elem*) y)->priority);
249
0
  return ZEND_THREEWAY_COMPARE(a, b);
250
0
}
251
252
static spl_ptr_heap *spl_ptr_heap_init(spl_ptr_heap_cmp_func cmp, spl_ptr_heap_ctor_func ctor, spl_ptr_heap_dtor_func dtor, size_t elem_size) /* {{{ */
253
658
{
254
658
  spl_ptr_heap *heap = emalloc(sizeof(spl_ptr_heap));
255
256
658
  heap->dtor     = dtor;
257
658
  heap->ctor     = ctor;
258
658
  heap->cmp      = cmp;
259
658
  heap->elements = ecalloc(PTR_HEAP_BLOCK_SIZE, elem_size);
260
658
  heap->max_size = PTR_HEAP_BLOCK_SIZE;
261
658
  heap->count    = 0;
262
658
  heap->flags    = 0;
263
658
  heap->elem_size = elem_size;
264
265
658
  return heap;
266
658
}
267
/* }}} */
268
269
0
static void spl_ptr_heap_insert(spl_ptr_heap *heap, void *elem, void *cmp_userdata) { /* {{{ */
270
0
  if (heap->count+1 > heap->max_size) {
271
0
    size_t alloc_size = heap->max_size * heap->elem_size;
272
    /* we need to allocate more memory */
273
0
    heap->elements  = safe_erealloc(heap->elements, 2, alloc_size, 0);
274
0
    memset((char *) heap->elements + alloc_size, 0, alloc_size);
275
0
    heap->max_size *= 2;
276
0
  }
277
278
0
  heap->flags |= SPL_HEAP_WRITE_LOCKED;
279
280
  /* sifting up */
281
0
  size_t pos;
282
0
  for (pos = heap->count; pos > 0 && heap->cmp(spl_heap_elem(heap, (pos-1)/2), elem, cmp_userdata) < 0; pos = (pos-1)/2) {
283
0
    spl_heap_elem_copy(heap, spl_heap_elem(heap, pos), spl_heap_elem(heap, (pos-1)/2));
284
0
  }
285
0
  heap->count++;
286
287
0
  heap->flags &= ~SPL_HEAP_WRITE_LOCKED;
288
289
0
  if (EG(exception)) {
290
    /* exception thrown during comparison */
291
0
    heap->flags |= SPL_HEAP_CORRUPTED;
292
0
  }
293
294
0
  spl_heap_elem_copy(heap, spl_heap_elem(heap, pos), elem);
295
0
}
296
/* }}} */
297
298
0
static void *spl_ptr_heap_top(spl_ptr_heap *heap) { /* {{{ */
299
0
  if (heap->count == 0) {
300
0
    return NULL;
301
0
  }
302
303
0
  return heap->elements;
304
0
}
305
/* }}} */
306
307
0
static zend_result spl_ptr_heap_delete_top(spl_ptr_heap *heap, void *elem, void *cmp_userdata) { /* {{{ */
308
0
  const size_t limit = (heap->count-1)/2;
309
0
  void *bottom;
310
311
0
  if (heap->count == 0) {
312
0
    return FAILURE;
313
0
  }
314
315
0
  heap->flags |= SPL_HEAP_WRITE_LOCKED;
316
317
0
  if (elem) {
318
0
    spl_heap_elem_copy(heap, elem, spl_heap_elem(heap, 0));
319
0
  } else {
320
0
    heap->dtor(spl_heap_elem(heap, 0));
321
0
  }
322
323
0
  bottom = spl_heap_elem(heap, --heap->count);
324
325
0
  size_t parent_idx, child_idx;
326
0
  for (parent_idx = 0; parent_idx < limit; parent_idx = child_idx) {
327
    /* Find smaller child */
328
0
    child_idx = parent_idx * 2 + 1;
329
0
    if (child_idx != heap->count && heap->cmp(spl_heap_elem(heap, child_idx+1), spl_heap_elem(heap, child_idx), cmp_userdata) > 0) {
330
0
      child_idx++; /* next child is bigger */
331
0
    }
332
333
    /* swap elements between two levels */
334
0
    if(heap->cmp(bottom, spl_heap_elem(heap, child_idx), cmp_userdata) < 0) {
335
0
      spl_heap_elem_copy(heap, spl_heap_elem(heap, parent_idx), spl_heap_elem(heap, child_idx));
336
0
    } else {
337
0
      break;
338
0
    }
339
0
  }
340
341
0
  heap->flags &= ~SPL_HEAP_WRITE_LOCKED;
342
343
0
  if (EG(exception)) {
344
    /* exception thrown during comparison */
345
0
    heap->flags |= SPL_HEAP_CORRUPTED;
346
0
  }
347
348
0
  void *to = spl_heap_elem(heap, parent_idx);
349
0
  if (to != bottom) {
350
0
    spl_heap_elem_copy(heap, to, bottom);
351
0
  }
352
0
  return SUCCESS;
353
0
}
354
/* }}} */
355
356
0
static spl_ptr_heap *spl_ptr_heap_clone(spl_ptr_heap *from) { /* {{{ */
357
0
  spl_ptr_heap *heap = emalloc(sizeof(spl_ptr_heap));
358
359
0
  heap->dtor     = from->dtor;
360
0
  heap->ctor     = from->ctor;
361
0
  heap->cmp      = from->cmp;
362
0
  heap->max_size = from->max_size;
363
0
  heap->count    = from->count;
364
0
  heap->flags    = from->flags;
365
0
  heap->elem_size = from->elem_size;
366
367
0
  heap->elements = safe_emalloc(from->elem_size, from->max_size, 0);
368
0
  memcpy(heap->elements, from->elements, from->elem_size * from->max_size);
369
370
0
  for (size_t i = 0; i < heap->count; ++i) {
371
0
    heap->ctor(spl_heap_elem(heap, i));
372
0
  }
373
374
0
  return heap;
375
0
}
376
/* }}} */
377
378
658
static void spl_ptr_heap_destroy(spl_ptr_heap *heap) { /* {{{ */
379
  /* Heap might be null if we OOMed during object initialization. */
380
658
  if (!heap) {
381
0
    return;
382
0
  }
383
384
658
  heap->flags |= SPL_HEAP_WRITE_LOCKED;
385
386
658
  for (size_t i = 0; i < heap->count; ++i) {
387
0
    heap->dtor(spl_heap_elem(heap, i));
388
0
  }
389
390
658
  heap->flags &= ~SPL_HEAP_WRITE_LOCKED;
391
392
658
  efree(heap->elements);
393
658
  efree(heap);
394
658
}
395
/* }}} */
396
397
0
static size_t spl_ptr_heap_count(spl_ptr_heap *heap) { /* {{{ */
398
0
  return heap->count;
399
0
}
400
/* }}} */
401
402
static void spl_heap_object_free_storage(zend_object *object) /* {{{ */
403
658
{
404
658
  spl_heap_object *intern = spl_heap_from_obj(object);
405
406
658
  zend_object_std_dtor(&intern->std);
407
408
658
  spl_ptr_heap_destroy(intern->heap);
409
658
}
410
/* }}} */
411
412
static zend_object *spl_heap_object_new_ex(zend_class_entry *class_type, zend_object *orig, int clone_orig) /* {{{ */
413
658
{
414
658
  spl_heap_object   *intern;
415
658
  zend_class_entry  *parent = class_type;
416
658
  int                inherited = 0;
417
418
658
  intern = zend_object_alloc(sizeof(spl_heap_object), parent);
419
420
658
  zend_object_std_init(&intern->std, class_type);
421
658
  object_properties_init(&intern->std, class_type);
422
423
658
  if (orig) {
424
0
    spl_heap_object *other = spl_heap_from_obj(orig);
425
0
    intern->std.handlers = other->std.handlers;
426
427
0
    if (clone_orig) {
428
0
      intern->heap = spl_ptr_heap_clone(other->heap);
429
0
    } else {
430
0
      intern->heap = other->heap;
431
0
    }
432
433
0
    intern->flags = other->flags;
434
0
    intern->fptr_cmp = other->fptr_cmp;
435
0
    intern->fptr_count = other->fptr_count;
436
0
    return &intern->std;
437
0
  }
438
439
658
  while (parent) {
440
658
    if (parent == spl_ce_SplPriorityQueue) {
441
38
      intern->heap = spl_ptr_heap_init(spl_ptr_pqueue_elem_cmp, spl_ptr_heap_pqueue_elem_ctor, spl_ptr_heap_pqueue_elem_dtor, sizeof(spl_pqueue_elem));
442
38
      intern->flags = SPL_PQUEUE_EXTR_DATA;
443
38
      break;
444
38
    }
445
446
620
    if (parent == spl_ce_SplMinHeap || parent == spl_ce_SplMaxHeap
447
620
        || parent == spl_ce_SplHeap) {
448
620
      intern->heap = spl_ptr_heap_init(
449
620
        parent == spl_ce_SplMinHeap ? spl_ptr_heap_zval_min_cmp : spl_ptr_heap_zval_max_cmp,
450
620
        spl_ptr_heap_zval_ctor, spl_ptr_heap_zval_dtor, sizeof(zval));
451
620
      break;
452
620
    }
453
454
0
    parent = parent->parent;
455
0
    inherited = 1;
456
0
  }
457
458
658
  ZEND_ASSERT(parent);
459
460
658
  if (inherited) {
461
0
    intern->fptr_cmp = zend_hash_str_find_ptr(&class_type->function_table, "compare", sizeof("compare") - 1);
462
0
    if (intern->fptr_cmp->common.scope == parent) {
463
0
      intern->fptr_cmp = NULL;
464
0
    }
465
    /* Find count() method */
466
0
    intern->fptr_count = zend_hash_find_ptr(&class_type->function_table, ZSTR_KNOWN(ZEND_STR_COUNT));
467
0
    if (intern->fptr_count->common.scope == parent) {
468
0
      intern->fptr_count = NULL;
469
0
    }
470
0
  }
471
472
658
  return &intern->std;
473
658
}
474
/* }}} */
475
476
static zend_object *spl_heap_object_new(zend_class_entry *class_type) /* {{{ */
477
658
{
478
658
  return spl_heap_object_new_ex(class_type, NULL, 0);
479
658
}
480
/* }}} */
481
482
static zend_object *spl_heap_object_clone(zend_object *old_object) /* {{{ */
483
0
{
484
0
  zend_object *new_object = spl_heap_object_new_ex(old_object->ce, old_object, 1);
485
486
0
  zend_objects_clone_members(new_object, old_object);
487
488
0
  return new_object;
489
0
}
490
/* }}} */
491
492
static zend_result spl_heap_object_count_elements(zend_object *object, zend_long *count) /* {{{ */
493
0
{
494
0
  spl_heap_object *intern = spl_heap_from_obj(object);
495
496
0
  if (intern->fptr_count) {
497
0
    zval rv;
498
0
    zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv);
499
0
    if (!Z_ISUNDEF(rv)) {
500
0
      *count = zval_get_long(&rv);
501
0
      zval_ptr_dtor(&rv);
502
0
      return SUCCESS;
503
0
    }
504
0
    *count = 0;
505
0
    return FAILURE;
506
0
  }
507
508
0
  *count = spl_ptr_heap_count(intern->heap);
509
510
0
  return SUCCESS;
511
0
}
512
/* }}} */
513
514
0
static HashTable* spl_heap_object_get_debug_info(const zend_class_entry *ce, zend_object *obj) { /* {{{ */
515
0
  spl_heap_object *intern = spl_heap_from_obj(obj);
516
0
  zval tmp, heap_array;
517
0
  HashTable *debug_info;
518
0
  HashTable *properties = zend_std_get_properties_ex(&intern->std);
519
520
  /* +3 As we are adding 3 additional key-entries */
521
0
  debug_info = zend_new_array(zend_hash_num_elements(properties) + 3);
522
0
  zend_hash_copy(debug_info, properties, (copy_ctor_func_t) zval_add_ref);
523
524
0
  ZVAL_LONG(&tmp, intern->flags);
525
0
  spl_set_private_debug_info_property(ce, "flags", strlen("flags"), debug_info, &tmp);
526
527
0
  ZVAL_BOOL(&tmp, intern->heap->flags&SPL_HEAP_CORRUPTED);
528
0
  spl_set_private_debug_info_property(ce, "isCorrupted", strlen("isCorrupted"), debug_info, &tmp);
529
530
0
  array_init(&heap_array);
531
532
0
  for (size_t i = 0; i < intern->heap->count; ++i) {
533
0
    if (ce == spl_ce_SplPriorityQueue) {
534
0
      spl_pqueue_elem *pq_elem = spl_heap_elem(intern->heap, i);
535
0
      zval elem;
536
0
      spl_pqueue_extract_helper(&elem, pq_elem, SPL_PQUEUE_EXTR_BOTH);
537
0
      add_index_zval(&heap_array, i, &elem);
538
0
    } else {
539
0
      zval *elem = spl_heap_elem(intern->heap, i);
540
0
      add_index_zval(&heap_array, i, elem);
541
0
      Z_TRY_ADDREF_P(elem);
542
0
    }
543
0
  }
544
545
0
  spl_set_private_debug_info_property(ce, "heap", strlen("heap"), debug_info, &heap_array);
546
547
0
  return debug_info;
548
0
}
549
/* }}} */
550
551
static HashTable *spl_heap_object_get_gc(zend_object *obj, zval **gc_data, int *gc_data_count) /* {{{ */
552
3
{
553
3
  spl_heap_object *intern = spl_heap_from_obj(obj);
554
3
  *gc_data = (zval *) intern->heap->elements;
555
3
  *gc_data_count = intern->heap->count;
556
557
3
  return zend_std_get_properties(obj);
558
3
}
559
/* }}} */
560
561
static HashTable *spl_pqueue_object_get_gc(zend_object *obj, zval **gc_data, int *gc_data_count) /* {{{ */
562
3
{
563
3
  spl_heap_object *intern = spl_heap_from_obj(obj);
564
3
  *gc_data = (zval *) intern->heap->elements;
565
  /* Two zvals (value and priority) per pqueue entry */
566
3
  *gc_data_count = 2 * intern->heap->count;
567
568
3
  return zend_std_get_properties(obj);
569
3
}
570
/* }}} */
571
572
/* {{{ Return the number of elements in the heap. */
573
PHP_METHOD(SplHeap, count)
574
0
{
575
0
  zend_long count;
576
0
  spl_heap_object *intern = Z_SPLHEAP_P(ZEND_THIS);
577
578
0
  if (zend_parse_parameters_none() == FAILURE) {
579
0
    RETURN_THROWS();
580
0
  }
581
582
0
  count = spl_ptr_heap_count(intern->heap);
583
0
  RETURN_LONG(count);
584
0
}
585
/* }}} */
586
587
/* {{{ Return true if the heap is empty. */
588
PHP_METHOD(SplHeap, isEmpty)
589
0
{
590
0
  spl_heap_object *intern = Z_SPLHEAP_P(ZEND_THIS);
591
592
0
  if (zend_parse_parameters_none() == FAILURE) {
593
0
    RETURN_THROWS();
594
0
  }
595
596
0
  RETURN_BOOL(spl_ptr_heap_count(intern->heap) == 0);
597
0
}
598
/* }}} */
599
600
static zend_result spl_heap_consistency_validations(const spl_heap_object *intern, bool write)
601
0
{
602
0
  if (intern->heap->flags & SPL_HEAP_CORRUPTED) {
603
0
    zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0);
604
0
    return FAILURE;
605
0
  }
606
607
0
  if (write && (intern->heap->flags & SPL_HEAP_WRITE_LOCKED)) {
608
0
    zend_throw_exception(spl_ce_RuntimeException, "Heap cannot be changed when it is already being modified.", 0);
609
0
    return FAILURE;
610
0
  }
611
612
0
  return SUCCESS;
613
0
}
614
615
/* {{{ Push $value on the heap */
616
PHP_METHOD(SplHeap, insert)
617
0
{
618
0
  zval *value;
619
0
  spl_heap_object *intern;
620
621
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
622
0
    Z_PARAM_ZVAL(value);
623
0
  ZEND_PARSE_PARAMETERS_END();
624
625
0
  intern = Z_SPLHEAP_P(ZEND_THIS);
626
627
0
  if (UNEXPECTED(spl_heap_consistency_validations(intern, true) != SUCCESS)) {
628
0
    RETURN_THROWS();
629
0
  }
630
631
0
  Z_TRY_ADDREF_P(value);
632
0
  spl_ptr_heap_insert(intern->heap, value, ZEND_THIS);
633
634
0
  RETURN_TRUE;
635
0
}
636
/* }}} */
637
638
/* {{{ extract the element out of the top of the heap */
639
PHP_METHOD(SplHeap, extract)
640
0
{
641
0
  spl_heap_object *intern;
642
643
0
  if (zend_parse_parameters_none() == FAILURE) {
644
0
    RETURN_THROWS();
645
0
  }
646
647
0
  intern = Z_SPLHEAP_P(ZEND_THIS);
648
649
0
  if (UNEXPECTED(spl_heap_consistency_validations(intern, true) != SUCCESS)) {
650
0
    RETURN_THROWS();
651
0
  }
652
653
0
  if (spl_ptr_heap_delete_top(intern->heap, return_value, ZEND_THIS) == FAILURE) {
654
0
    zend_throw_exception(spl_ce_RuntimeException, "Can't extract from an empty heap", 0);
655
0
    RETURN_THROWS();
656
0
  }
657
0
}
658
/* }}} */
659
660
/* {{{ Push $value with the priority $priodiry on the priorityqueue */
661
PHP_METHOD(SplPriorityQueue, insert)
662
0
{
663
0
  zval *data, *priority;
664
0
  spl_heap_object *intern;
665
0
  spl_pqueue_elem elem;
666
667
0
  ZEND_PARSE_PARAMETERS_START(2, 2)
668
0
    Z_PARAM_ZVAL(data);
669
0
    Z_PARAM_ZVAL(priority);
670
0
  ZEND_PARSE_PARAMETERS_END();
671
672
0
  intern = Z_SPLHEAP_P(ZEND_THIS);
673
674
0
  if (UNEXPECTED(spl_heap_consistency_validations(intern, true) != SUCCESS)) {
675
0
    RETURN_THROWS();
676
0
  }
677
678
0
  ZVAL_COPY(&elem.data, data);
679
0
  ZVAL_COPY(&elem.priority, priority);
680
681
  /* If we know this call came from non inherited SplPriorityQueue it's
682
   * possible to do specialization on the type of the priority parameter. */
683
0
  if (!intern->fptr_cmp) {
684
0
    int type = Z_TYPE(elem.priority);
685
0
    spl_ptr_heap_cmp_func new_cmp =
686
0
      (type == IS_LONG) ? spl_ptr_pqueue_elem_cmp_long :
687
0
      ((type == IS_DOUBLE) ? spl_ptr_pqueue_elem_cmp_double : spl_ptr_pqueue_elem_cmp);
688
689
0
    if (intern->heap->count == 0) { /* Specialize empty queue */
690
0
      intern->heap->cmp = new_cmp;
691
0
    } else if (new_cmp != intern->heap->cmp) { /* Despecialize on type conflict. */
692
0
      intern->heap->cmp = spl_ptr_pqueue_elem_cmp;
693
0
    }
694
0
  }
695
696
0
  spl_ptr_heap_insert(intern->heap, &elem, ZEND_THIS);
697
698
0
  RETURN_TRUE;
699
0
}
700
/* }}} */
701
702
/* {{{ extract the element out of the top of the priority queue */
703
PHP_METHOD(SplPriorityQueue, extract)
704
0
{
705
0
  spl_pqueue_elem elem;
706
0
  spl_heap_object *intern;
707
708
0
  if (zend_parse_parameters_none() == FAILURE) {
709
0
    RETURN_THROWS();
710
0
  }
711
712
0
  intern = Z_SPLHEAP_P(ZEND_THIS);
713
714
0
  if (UNEXPECTED(spl_heap_consistency_validations(intern, true) != SUCCESS)) {
715
0
    RETURN_THROWS();
716
0
  }
717
718
0
  if (spl_ptr_heap_delete_top(intern->heap, &elem, ZEND_THIS) == FAILURE) {
719
0
    zend_throw_exception(spl_ce_RuntimeException, "Can't extract from an empty heap", 0);
720
0
    RETURN_THROWS();
721
0
  }
722
723
0
  spl_pqueue_extract_helper(return_value, &elem, intern->flags);
724
0
  spl_ptr_heap_pqueue_elem_dtor(&elem);
725
0
}
726
/* }}} */
727
728
/* {{{ Peek at the top element of the priority queue */
729
PHP_METHOD(SplPriorityQueue, top)
730
0
{
731
0
  spl_heap_object *intern;
732
0
  spl_pqueue_elem *elem;
733
734
0
  if (zend_parse_parameters_none() == FAILURE) {
735
0
    RETURN_THROWS();
736
0
  }
737
738
0
  intern = Z_SPLHEAP_P(ZEND_THIS);
739
740
0
  if (UNEXPECTED(spl_heap_consistency_validations(intern, false) != SUCCESS)) {
741
0
    RETURN_THROWS();
742
0
  }
743
744
0
  elem = spl_ptr_heap_top(intern->heap);
745
746
0
  if (!elem) {
747
0
    zend_throw_exception(spl_ce_RuntimeException, "Can't peek at an empty heap", 0);
748
0
    RETURN_THROWS();
749
0
  }
750
751
0
  spl_pqueue_extract_helper(return_value, elem, intern->flags);
752
0
}
753
/* }}} */
754
755
756
/* {{{ Set the flags of extraction*/
757
PHP_METHOD(SplPriorityQueue, setExtractFlags)
758
0
{
759
0
  zend_long value;
760
0
  spl_heap_object *intern;
761
762
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &value) == FAILURE) {
763
0
    RETURN_THROWS();
764
0
  }
765
766
0
  value &= SPL_PQUEUE_EXTR_MASK;
767
0
  if (!value) {
768
0
    zend_throw_exception(spl_ce_RuntimeException, "Must specify at least one extract flag", 0);
769
0
    RETURN_THROWS();
770
0
  }
771
772
0
  intern = Z_SPLHEAP_P(ZEND_THIS);
773
0
  intern->flags = value;
774
0
  RETURN_LONG(intern->flags);
775
0
}
776
/* }}} */
777
778
/* {{{ Get the flags of extraction*/
779
PHP_METHOD(SplPriorityQueue, getExtractFlags)
780
0
{
781
0
  spl_heap_object *intern;
782
783
0
  if (zend_parse_parameters_none() == FAILURE) {
784
0
    RETURN_THROWS();
785
0
  }
786
787
0
  intern = Z_SPLHEAP_P(ZEND_THIS);
788
789
0
  RETURN_LONG(intern->flags);
790
0
}
791
/* }}} */
792
793
/* {{{ Recover from a corrupted state*/
794
PHP_METHOD(SplHeap, recoverFromCorruption)
795
0
{
796
0
  spl_heap_object *intern;
797
798
0
  if (zend_parse_parameters_none() == FAILURE) {
799
0
    RETURN_THROWS();
800
0
  }
801
802
0
  intern = Z_SPLHEAP_P(ZEND_THIS);
803
804
0
  intern->heap->flags = intern->heap->flags & ~SPL_HEAP_CORRUPTED;
805
806
0
  RETURN_TRUE;
807
0
}
808
/* }}} */
809
810
/* {{{ Tells if the heap is in a corrupted state*/
811
PHP_METHOD(SplHeap, isCorrupted)
812
0
{
813
0
  spl_heap_object *intern;
814
815
0
  if (zend_parse_parameters_none() == FAILURE) {
816
0
    RETURN_THROWS();
817
0
  }
818
819
0
  intern = Z_SPLHEAP_P(ZEND_THIS);
820
821
0
  RETURN_BOOL(intern->heap->flags & SPL_HEAP_CORRUPTED);
822
0
}
823
/* }}} */
824
825
/* {{{ compare the priorities */
826
PHP_METHOD(SplPriorityQueue, compare)
827
0
{
828
0
  zval *a, *b;
829
830
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a, &b) == FAILURE) {
831
0
    RETURN_THROWS();
832
0
  }
833
834
0
  RETURN_LONG(spl_ptr_heap_zval_max_cmp(a, b, NULL));
835
0
}
836
/* }}} */
837
838
/* {{{ Peek at the top element of the heap */
839
PHP_METHOD(SplHeap, top)
840
0
{
841
0
  zval *value;
842
0
  spl_heap_object *intern;
843
844
0
  if (zend_parse_parameters_none() == FAILURE) {
845
0
    RETURN_THROWS();
846
0
  }
847
848
0
  intern = Z_SPLHEAP_P(ZEND_THIS);
849
850
0
  if (UNEXPECTED(spl_heap_consistency_validations(intern, false) != SUCCESS)) {
851
0
    RETURN_THROWS();
852
0
  }
853
854
0
  value = spl_ptr_heap_top(intern->heap);
855
856
0
  if (!value) {
857
0
    zend_throw_exception(spl_ce_RuntimeException, "Can't peek at an empty heap", 0);
858
0
    RETURN_THROWS();
859
0
  }
860
861
0
  RETURN_COPY_DEREF(value);
862
0
}
863
/* }}} */
864
865
/* {{{ compare the values */
866
PHP_METHOD(SplMinHeap, compare)
867
0
{
868
0
  zval *a, *b;
869
870
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a, &b) == FAILURE) {
871
0
    RETURN_THROWS();
872
0
  }
873
874
0
  RETURN_LONG(spl_ptr_heap_zval_min_cmp(a, b, NULL));
875
0
}
876
/* }}} */
877
878
/* {{{ compare the values */
879
PHP_METHOD(SplMaxHeap, compare)
880
0
{
881
0
  zval *a, *b;
882
883
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a, &b) == FAILURE) {
884
0
    RETURN_THROWS();
885
0
  }
886
887
0
  RETURN_LONG(spl_ptr_heap_zval_max_cmp(a, b, NULL));
888
0
}
889
/* }}} */
890
891
static void spl_heap_it_dtor(zend_object_iterator *iter) /* {{{ */
892
0
{
893
0
  zend_user_it_invalidate_current(iter);
894
0
  zval_ptr_dtor(&iter->data);
895
0
}
896
/* }}} */
897
898
static void spl_heap_it_rewind(zend_object_iterator *iter) /* {{{ */
899
0
{
900
  /* do nothing, the iterator always points to the top element */
901
0
}
902
/* }}} */
903
904
static zend_result spl_heap_it_valid(zend_object_iterator *iter) /* {{{ */
905
0
{
906
0
  return ((Z_SPLHEAP_P(&iter->data))->heap->count != 0 ? SUCCESS : FAILURE);
907
0
}
908
/* }}} */
909
910
static zval *spl_heap_it_get_current_data(zend_object_iterator *iter) /* {{{ */
911
0
{
912
0
  spl_heap_object *object = Z_SPLHEAP_P(&iter->data);
913
914
0
  if (UNEXPECTED(spl_heap_consistency_validations(object, false) != SUCCESS)) {
915
0
    return NULL;
916
0
  }
917
918
0
  if (object->heap->count == 0) {
919
0
    return NULL;
920
0
  } else {
921
0
    return spl_heap_elem(object->heap, 0);
922
0
  }
923
0
}
924
/* }}} */
925
926
static zval *spl_pqueue_it_get_current_data(zend_object_iterator *iter) /* {{{ */
927
0
{
928
0
  zend_user_iterator *user_it = (zend_user_iterator *) iter;
929
0
  spl_heap_object *object = Z_SPLHEAP_P(&iter->data);
930
931
0
  if (UNEXPECTED(spl_heap_consistency_validations(object, false) != SUCCESS)) {
932
0
    return NULL;
933
0
  }
934
935
0
  if (object->heap->count == 0) {
936
0
    return NULL;
937
0
  }
938
939
0
  if (Z_ISUNDEF(user_it->value)) {
940
0
    spl_pqueue_elem *elem = spl_heap_elem(object->heap, 0);
941
0
    spl_pqueue_extract_helper(&user_it->value, elem, object->flags);
942
0
  }
943
0
  return &user_it->value;
944
0
}
945
/* }}} */
946
947
static void spl_heap_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
948
0
{
949
0
  spl_heap_object *object = Z_SPLHEAP_P(&iter->data);
950
951
0
  ZVAL_LONG(key, object->heap->count - 1);
952
0
}
953
/* }}} */
954
955
static void spl_heap_it_move_forward(zend_object_iterator *iter) /* {{{ */
956
0
{
957
0
  spl_heap_object *object = Z_SPLHEAP_P(&iter->data);
958
959
0
  if (UNEXPECTED(spl_heap_consistency_validations(object, false) != SUCCESS)) {
960
0
    return;
961
0
  }
962
963
0
  spl_ptr_heap_delete_top(object->heap, NULL, &iter->data);
964
0
  zend_user_it_invalidate_current(iter);
965
0
}
966
/* }}} */
967
968
/* {{{ Return current array key */
969
PHP_METHOD(SplHeap, key)
970
0
{
971
0
  spl_heap_object *intern = Z_SPLHEAP_P(ZEND_THIS);
972
973
0
  if (zend_parse_parameters_none() == FAILURE) {
974
0
    RETURN_THROWS();
975
0
  }
976
977
0
  RETURN_LONG(intern->heap->count - 1);
978
0
}
979
/* }}} */
980
981
/* {{{ Move to next entry */
982
PHP_METHOD(SplHeap, next)
983
0
{
984
0
  spl_heap_object *intern = Z_SPLHEAP_P(ZEND_THIS);
985
986
0
  if (zend_parse_parameters_none() == FAILURE) {
987
0
    RETURN_THROWS();
988
0
  }
989
990
0
  spl_ptr_heap_delete_top(intern->heap, NULL, ZEND_THIS);
991
0
}
992
/* }}} */
993
994
/* {{{ Check whether the datastructure contains more entries */
995
PHP_METHOD(SplHeap, valid)
996
0
{
997
0
  spl_heap_object *intern = Z_SPLHEAP_P(ZEND_THIS);
998
999
0
  if (zend_parse_parameters_none() == FAILURE) {
1000
0
    RETURN_THROWS();
1001
0
  }
1002
1003
0
  RETURN_BOOL(intern->heap->count != 0);
1004
0
}
1005
/* }}} */
1006
1007
/* {{{ Rewind the datastructure back to the start */
1008
PHP_METHOD(SplHeap, rewind)
1009
0
{
1010
0
  if (zend_parse_parameters_none() == FAILURE) {
1011
0
    RETURN_THROWS();
1012
0
  }
1013
  /* do nothing, the iterator always points to the top element */
1014
0
}
1015
/* }}} */
1016
1017
/* {{{ Return current datastructure entry */
1018
PHP_METHOD(SplHeap, current)
1019
0
{
1020
0
  spl_heap_object *intern  = Z_SPLHEAP_P(ZEND_THIS);
1021
1022
0
  if (zend_parse_parameters_none() == FAILURE) {
1023
0
    RETURN_THROWS();
1024
0
  }
1025
1026
0
  if (!intern->heap->count) {
1027
0
    RETURN_NULL();
1028
0
  } else {
1029
0
    zval *element = spl_heap_elem(intern->heap, 0);
1030
0
    RETURN_COPY_DEREF(element);
1031
0
  }
1032
0
}
1033
/* }}} */
1034
1035
/* {{{ Return current datastructure entry */
1036
PHP_METHOD(SplPriorityQueue, current)
1037
0
{
1038
0
  spl_heap_object  *intern  = Z_SPLHEAP_P(ZEND_THIS);
1039
1040
0
  if (zend_parse_parameters_none() == FAILURE) {
1041
0
    RETURN_THROWS();
1042
0
  }
1043
1044
0
  if (!intern->heap->count) {
1045
0
    RETURN_NULL();
1046
0
  } else {
1047
0
    spl_pqueue_elem *elem = spl_heap_elem(intern->heap, 0);
1048
0
    spl_pqueue_extract_helper(return_value, elem, intern->flags);
1049
0
  }
1050
0
}
1051
/* }}} */
1052
1053
/* {{{ */
1054
PHP_METHOD(SplHeap, __debugInfo)
1055
0
{
1056
0
  if (zend_parse_parameters_none() == FAILURE) {
1057
0
    RETURN_THROWS();
1058
0
  }
1059
1060
0
  RETURN_ARR(spl_heap_object_get_debug_info(spl_ce_SplHeap, Z_OBJ_P(ZEND_THIS)));
1061
0
} /* }}} */
1062
1063
/* {{{ */
1064
PHP_METHOD(SplPriorityQueue, __debugInfo)
1065
0
{
1066
0
  if (zend_parse_parameters_none() == FAILURE) {
1067
0
    RETURN_THROWS();
1068
0
  }
1069
1070
0
  RETURN_ARR(spl_heap_object_get_debug_info(spl_ce_SplPriorityQueue, Z_OBJ_P(ZEND_THIS)));
1071
0
} /* }}} */
1072
1073
static void spl_heap_serialize_internal_state(zval *return_value, spl_heap_object *intern, bool is_pqueue)
1074
0
{
1075
0
  zval heap_elements;
1076
0
  int heap_count = intern->heap->count;
1077
1078
0
  array_init(return_value);
1079
0
  add_assoc_long(return_value, "flags", intern->flags);
1080
1081
0
  array_init_size(&heap_elements, heap_count);
1082
1083
0
  if (heap_count == 0) {
1084
0
    add_assoc_zval(return_value, "heap_elements", &heap_elements);
1085
0
    return;
1086
0
  }
1087
1088
0
  for (int heap_idx = 0; heap_idx < heap_count; ++heap_idx) {
1089
0
    if (is_pqueue) {
1090
0
      spl_pqueue_elem *elem = spl_heap_elem(intern->heap, heap_idx);
1091
0
      zval entry;
1092
0
      array_init(&entry);
1093
0
      add_assoc_zval_ex(&entry, "data", strlen("data"), &elem->data);
1094
0
      Z_TRY_ADDREF(elem->data);
1095
0
      add_assoc_zval_ex(&entry, "priority", strlen("priority"), &elem->priority);
1096
0
      Z_TRY_ADDREF(elem->priority);
1097
0
      zend_hash_next_index_insert(Z_ARRVAL(heap_elements), &entry);
1098
0
    } else {
1099
0
      zval *elem = spl_heap_elem(intern->heap, heap_idx);
1100
0
      zend_hash_next_index_insert(Z_ARRVAL(heap_elements), elem);
1101
0
      Z_TRY_ADDREF_P(elem);
1102
0
    }
1103
0
  }
1104
1105
0
  add_assoc_zval(return_value, "heap_elements", &heap_elements);
1106
0
}
1107
1108
static zend_result spl_heap_unserialize_internal_state(HashTable *state_ht, spl_heap_object *intern, zval *this_ptr, bool is_pqueue)
1109
0
{
1110
0
  zval *flags_val = zend_hash_str_find(state_ht, "flags", strlen("flags"));
1111
0
  if (!flags_val || Z_TYPE_P(flags_val) != IS_LONG) {
1112
0
    return FAILURE;
1113
0
  }
1114
1115
0
  zend_long flags_value = Z_LVAL_P(flags_val);
1116
1117
0
  if (is_pqueue) {
1118
0
    flags_value &= SPL_PQUEUE_EXTR_MASK;
1119
0
    if (!flags_value) {
1120
0
      return FAILURE;
1121
0
    }
1122
0
  } else if (flags_value != 0) { /* Regular heaps should not have user-visible flags */
1123
0
    return FAILURE;
1124
0
  }
1125
1126
0
  intern->flags = (int) flags_value;
1127
1128
0
  zval *heap_elements = zend_hash_str_find(state_ht, "heap_elements", strlen("heap_elements"));
1129
0
  if (!heap_elements) {
1130
0
    return FAILURE;
1131
0
  }
1132
1133
0
  if (Z_TYPE_P(heap_elements) != IS_ARRAY) {
1134
0
    return FAILURE;
1135
0
  }
1136
1137
0
  ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(heap_elements), zval *val) {
1138
0
    if (is_pqueue) {
1139
      /* PriorityQueue elements are serialized as arrays with 'data' and 'priority' keys */
1140
0
      if (Z_TYPE_P(val) != IS_ARRAY || zend_hash_num_elements(Z_ARRVAL_P(val)) != 2) {
1141
0
        return FAILURE;
1142
0
      }
1143
1144
0
      zval *data_val = zend_hash_str_find(Z_ARRVAL_P(val), "data", strlen("data") );
1145
0
      zval *priority_val = zend_hash_str_find(Z_ARRVAL_P(val), "priority", strlen("priority"));
1146
1147
0
      if (!data_val || !priority_val) {
1148
0
        return FAILURE;
1149
0
      }
1150
1151
0
      spl_pqueue_elem elem;
1152
0
      ZVAL_COPY(&elem.data, data_val);
1153
0
      ZVAL_COPY(&elem.priority, priority_val);
1154
0
      spl_ptr_heap_insert(intern->heap, &elem, this_ptr);
1155
0
      if (EG(exception)) {
1156
0
        return FAILURE;
1157
0
      }
1158
0
    } else {
1159
0
      Z_TRY_ADDREF_P(val);
1160
0
      spl_ptr_heap_insert(intern->heap, val, this_ptr);
1161
0
      if (EG(exception)) {
1162
0
        return FAILURE;
1163
0
      }
1164
0
    }
1165
0
  } ZEND_HASH_FOREACH_END();
1166
1167
0
  return SUCCESS;
1168
0
}
1169
1170
PHP_METHOD(SplPriorityQueue, __serialize)
1171
0
{
1172
0
  spl_heap_object *intern = Z_SPLHEAP_P(ZEND_THIS);
1173
0
  zval props, state;
1174
1175
0
  ZEND_PARSE_PARAMETERS_NONE();
1176
1177
0
  if (UNEXPECTED(spl_heap_consistency_validations(intern, false) != SUCCESS)) {
1178
0
    RETURN_THROWS();
1179
0
  }
1180
1181
0
  if (intern->heap->flags & SPL_HEAP_WRITE_LOCKED) {
1182
0
    zend_throw_exception(spl_ce_RuntimeException, "Cannot serialize heap while it is being modified.", 0);
1183
0
    RETURN_THROWS();
1184
0
  }
1185
1186
0
  array_init(return_value);
1187
1188
0
  ZVAL_ARR(&props, zend_std_get_properties(&intern->std));
1189
0
  Z_TRY_ADDREF(props);
1190
0
  zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &props);
1191
1192
0
  spl_heap_serialize_internal_state(&state, intern, true);
1193
0
  zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &state);
1194
0
}
1195
1196
PHP_METHOD(SplPriorityQueue, __unserialize)
1197
2
{
1198
2
  HashTable *data;
1199
2
  spl_heap_object *intern = Z_SPLHEAP_P(ZEND_THIS);
1200
1201
6
  ZEND_PARSE_PARAMETERS_START(1, 1)
1202
8
    Z_PARAM_ARRAY_HT(data)
1203
2
  ZEND_PARSE_PARAMETERS_END();
1204
1205
2
  if (zend_hash_num_elements(data) != 2) {
1206
2
    zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name));
1207
2
    RETURN_THROWS();
1208
2
  }
1209
1210
0
  zval *props = zend_hash_index_find(data, 0);
1211
0
  if (!props || Z_TYPE_P(props) != IS_ARRAY) {
1212
0
    zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name));
1213
0
    RETURN_THROWS();
1214
0
  }
1215
1216
0
  object_properties_load(&intern->std, Z_ARRVAL_P(props));
1217
0
  if (EG(exception)) {
1218
0
    zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name));
1219
0
    RETURN_THROWS();
1220
0
  }
1221
1222
0
  zval *state = zend_hash_index_find(data, 1);
1223
0
  if (!state || Z_TYPE_P(state) != IS_ARRAY) {
1224
0
    zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name));
1225
0
    RETURN_THROWS();
1226
0
  }
1227
1228
0
  if (spl_heap_unserialize_internal_state(Z_ARRVAL_P(state), intern, ZEND_THIS, true) != SUCCESS) {
1229
0
    zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name));
1230
0
    RETURN_THROWS();
1231
0
  }
1232
1233
0
  if (EG(exception)) {
1234
0
    RETURN_THROWS();
1235
0
  }
1236
1237
0
  if (UNEXPECTED(spl_heap_consistency_validations(intern, false) != SUCCESS)) {
1238
0
    RETURN_THROWS();
1239
0
  }
1240
0
}
1241
1242
PHP_METHOD(SplHeap, __serialize)
1243
0
{
1244
0
  spl_heap_object *intern = Z_SPLHEAP_P(ZEND_THIS);
1245
0
  zval props, state;
1246
1247
0
  ZEND_PARSE_PARAMETERS_NONE();
1248
1249
0
  if (UNEXPECTED(spl_heap_consistency_validations(intern, false) != SUCCESS)) {
1250
0
    RETURN_THROWS();
1251
0
  }
1252
1253
0
  if (intern->heap->flags & SPL_HEAP_WRITE_LOCKED) {
1254
0
    zend_throw_exception(spl_ce_RuntimeException, "Cannot serialize heap while it is being modified.", 0);
1255
0
    RETURN_THROWS();
1256
0
  }
1257
1258
0
  array_init(return_value);
1259
1260
0
  ZVAL_ARR(&props, zend_std_get_properties(&intern->std));
1261
0
  Z_TRY_ADDREF(props);
1262
0
  zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &props);
1263
1264
0
  spl_heap_serialize_internal_state(&state, intern, false);
1265
0
  zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &state);
1266
0
}
1267
1268
PHP_METHOD(SplHeap, __unserialize)
1269
3
{
1270
3
  HashTable *data;
1271
3
  spl_heap_object *intern = Z_SPLHEAP_P(ZEND_THIS);
1272
1273
9
  ZEND_PARSE_PARAMETERS_START(1, 1)
1274
12
    Z_PARAM_ARRAY_HT(data)
1275
3
  ZEND_PARSE_PARAMETERS_END();
1276
1277
3
  if (zend_hash_num_elements(data) != 2) {
1278
3
    zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name));
1279
3
    RETURN_THROWS();
1280
3
  }
1281
1282
0
  zval *props = zend_hash_index_find(data, 0);
1283
0
  if (!props || Z_TYPE_P(props) != IS_ARRAY) {
1284
0
    zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name));
1285
0
    RETURN_THROWS();
1286
0
  }
1287
1288
0
  object_properties_load(&intern->std, Z_ARRVAL_P(props));
1289
0
  if (EG(exception)) {
1290
0
    zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name));
1291
0
    RETURN_THROWS();
1292
0
  }
1293
1294
0
  zval *state = zend_hash_index_find(data, 1);
1295
0
  if (!state || Z_TYPE_P(state) != IS_ARRAY) {
1296
0
    zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name));
1297
0
    RETURN_THROWS();
1298
0
  }
1299
1300
0
  if (spl_heap_unserialize_internal_state(Z_ARRVAL_P(state), intern, ZEND_THIS, false) != SUCCESS) {
1301
0
    zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name));
1302
0
    RETURN_THROWS();
1303
0
  }
1304
1305
0
  if (EG(exception)) {
1306
0
    RETURN_THROWS();
1307
0
  }
1308
1309
0
  if (UNEXPECTED(spl_heap_consistency_validations(intern, false) != SUCCESS)) {
1310
0
    RETURN_THROWS();
1311
0
  }
1312
0
}
1313
1314
/* iterator handler table */
1315
static const zend_object_iterator_funcs spl_heap_it_funcs = {
1316
  spl_heap_it_dtor,
1317
  spl_heap_it_valid,
1318
  spl_heap_it_get_current_data,
1319
  spl_heap_it_get_current_key,
1320
  spl_heap_it_move_forward,
1321
  spl_heap_it_rewind,
1322
  NULL,
1323
  NULL, /* get_gc */
1324
};
1325
1326
static const zend_object_iterator_funcs spl_pqueue_it_funcs = {
1327
  spl_heap_it_dtor,
1328
  spl_heap_it_valid,
1329
  spl_pqueue_it_get_current_data,
1330
  spl_heap_it_get_current_key,
1331
  spl_heap_it_move_forward,
1332
  spl_heap_it_rewind,
1333
  NULL,
1334
  NULL, /* get_gc */
1335
};
1336
1337
static zend_object_iterator *spl_heap_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
1338
0
{
1339
0
  if (by_ref) {
1340
0
    zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
1341
0
    return NULL;
1342
0
  }
1343
1344
0
  zend_user_iterator *iterator = emalloc(sizeof(zend_user_iterator));
1345
0
  zend_iterator_init(&iterator->it);
1346
1347
0
  ZVAL_OBJ_COPY(&iterator->it.data, Z_OBJ_P(object));
1348
0
  iterator->it.funcs = &spl_heap_it_funcs;
1349
0
  iterator->ce       = ce;
1350
0
  ZVAL_UNDEF(&iterator->value);
1351
1352
0
  return &iterator->it;
1353
0
}
1354
/* }}} */
1355
1356
static zend_object_iterator *spl_pqueue_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
1357
0
{
1358
0
  if (by_ref) {
1359
0
    zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
1360
0
    return NULL;
1361
0
  }
1362
1363
0
  zend_user_iterator *iterator = emalloc(sizeof(zend_user_iterator));
1364
0
  zend_iterator_init(&iterator->it);
1365
1366
0
  ZVAL_OBJ_COPY(&iterator->it.data, Z_OBJ_P(object));
1367
0
  iterator->it.funcs = &spl_pqueue_it_funcs;
1368
0
  iterator->ce       = ce;
1369
0
  ZVAL_UNDEF(&iterator->value);
1370
1371
0
  return &iterator->it;
1372
0
}
1373
/* }}} */
1374
1375
PHP_MINIT_FUNCTION(spl_heap) /* {{{ */
1376
16
{
1377
16
  spl_ce_SplHeap = register_class_SplHeap(zend_ce_iterator, zend_ce_countable);
1378
16
  spl_ce_SplHeap->create_object = spl_heap_object_new;
1379
16
  spl_ce_SplHeap->default_object_handlers = &spl_handler_SplHeap;
1380
16
  spl_ce_SplHeap->get_iterator = spl_heap_get_iterator;
1381
1382
16
  memcpy(&spl_handler_SplHeap, &std_object_handlers, sizeof(zend_object_handlers));
1383
1384
16
  spl_handler_SplHeap.offset         = XtOffsetOf(spl_heap_object, std);
1385
16
  spl_handler_SplHeap.clone_obj      = spl_heap_object_clone;
1386
16
  spl_handler_SplHeap.count_elements = spl_heap_object_count_elements;
1387
16
  spl_handler_SplHeap.get_gc         = spl_heap_object_get_gc;
1388
16
  spl_handler_SplHeap.free_obj = spl_heap_object_free_storage;
1389
1390
16
  spl_ce_SplMinHeap = register_class_SplMinHeap(spl_ce_SplHeap);
1391
16
  spl_ce_SplMinHeap->create_object = spl_heap_object_new;
1392
16
  spl_ce_SplMinHeap->get_iterator = spl_heap_get_iterator;
1393
1394
16
  spl_ce_SplMaxHeap = register_class_SplMaxHeap(spl_ce_SplHeap);
1395
16
  spl_ce_SplMaxHeap->create_object = spl_heap_object_new;
1396
16
  spl_ce_SplMaxHeap->get_iterator = spl_heap_get_iterator;
1397
1398
16
  spl_ce_SplPriorityQueue = register_class_SplPriorityQueue(zend_ce_iterator, zend_ce_countable);
1399
16
  spl_ce_SplPriorityQueue->create_object = spl_heap_object_new;
1400
16
  spl_ce_SplPriorityQueue->default_object_handlers = &spl_handler_SplPriorityQueue;
1401
16
  spl_ce_SplPriorityQueue->get_iterator = spl_pqueue_get_iterator;
1402
1403
16
  memcpy(&spl_handler_SplPriorityQueue, &std_object_handlers, sizeof(zend_object_handlers));
1404
1405
16
  spl_handler_SplPriorityQueue.offset         = XtOffsetOf(spl_heap_object, std);
1406
16
  spl_handler_SplPriorityQueue.clone_obj      = spl_heap_object_clone;
1407
16
  spl_handler_SplPriorityQueue.count_elements = spl_heap_object_count_elements;
1408
16
  spl_handler_SplPriorityQueue.get_gc         = spl_pqueue_object_get_gc;
1409
16
  spl_handler_SplPriorityQueue.free_obj = spl_heap_object_free_storage;
1410
1411
16
  return SUCCESS;
1412
16
}
1413
/* }}} */