/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 | 123 | #define PTR_HEAP_BLOCK_SIZE 64 |
31 | | |
32 | 6 | #define SPL_HEAP_CORRUPTED 0x00000001 |
33 | 252 | #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 | 143 | static inline spl_heap_object *spl_heap_from_obj(zend_object *obj) /* {{{ */ { |
76 | 143 | return (spl_heap_object*)((char*)(obj) - XtOffsetOf(spl_heap_object, std)); |
77 | 143 | } |
78 | | /* }}} */ |
79 | | |
80 | 8 | #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 | 123 | { |
254 | 123 | spl_ptr_heap *heap = emalloc(sizeof(spl_ptr_heap)); |
255 | | |
256 | 123 | heap->dtor = dtor; |
257 | 123 | heap->ctor = ctor; |
258 | 123 | heap->cmp = cmp; |
259 | 123 | heap->elements = ecalloc(PTR_HEAP_BLOCK_SIZE, elem_size); |
260 | 123 | heap->max_size = PTR_HEAP_BLOCK_SIZE; |
261 | 123 | heap->count = 0; |
262 | 123 | heap->flags = 0; |
263 | 123 | heap->elem_size = elem_size; |
264 | | |
265 | 123 | return heap; |
266 | 123 | } |
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 | 123 | static void spl_ptr_heap_destroy(spl_ptr_heap *heap) { /* {{{ */ |
379 | | /* Heap might be null if we OOMed during object initialization. */ |
380 | 123 | if (!heap) { |
381 | 0 | return; |
382 | 0 | } |
383 | | |
384 | 123 | heap->flags |= SPL_HEAP_WRITE_LOCKED; |
385 | | |
386 | 123 | for (size_t i = 0; i < heap->count; ++i) { |
387 | 0 | heap->dtor(spl_heap_elem(heap, i)); |
388 | 0 | } |
389 | | |
390 | 123 | heap->flags &= ~SPL_HEAP_WRITE_LOCKED; |
391 | | |
392 | 123 | efree(heap->elements); |
393 | 123 | efree(heap); |
394 | 123 | } |
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 | 123 | { |
404 | 123 | spl_heap_object *intern = spl_heap_from_obj(object); |
405 | | |
406 | 123 | zend_object_std_dtor(&intern->std); |
407 | | |
408 | 123 | spl_ptr_heap_destroy(intern->heap); |
409 | 123 | } |
410 | | /* }}} */ |
411 | | |
412 | | static zend_object *spl_heap_object_new_ex(zend_class_entry *class_type, zend_object *orig, int clone_orig) /* {{{ */ |
413 | 123 | { |
414 | 123 | spl_heap_object *intern; |
415 | 123 | zend_class_entry *parent = class_type; |
416 | 123 | int inherited = 0; |
417 | | |
418 | 123 | intern = zend_object_alloc(sizeof(spl_heap_object), parent); |
419 | | |
420 | 123 | zend_object_std_init(&intern->std, class_type); |
421 | 123 | object_properties_init(&intern->std, class_type); |
422 | | |
423 | 123 | 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 | 123 | while (parent) { |
440 | 123 | if (parent == spl_ce_SplPriorityQueue) { |
441 | 39 | 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 | 39 | intern->flags = SPL_PQUEUE_EXTR_DATA; |
443 | 39 | break; |
444 | 39 | } |
445 | | |
446 | 84 | if (parent == spl_ce_SplMinHeap || parent == spl_ce_SplMaxHeap |
447 | 84 | || parent == spl_ce_SplHeap) { |
448 | 84 | intern->heap = spl_ptr_heap_init( |
449 | 84 | parent == spl_ce_SplMinHeap ? spl_ptr_heap_zval_min_cmp : spl_ptr_heap_zval_max_cmp, |
450 | 84 | spl_ptr_heap_zval_ctor, spl_ptr_heap_zval_dtor, sizeof(zval)); |
451 | 84 | break; |
452 | 84 | } |
453 | | |
454 | 0 | parent = parent->parent; |
455 | 0 | inherited = 1; |
456 | 0 | } |
457 | | |
458 | 123 | ZEND_ASSERT(parent); |
459 | | |
460 | 123 | 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 | 123 | return &intern->std; |
473 | 123 | } |
474 | | /* }}} */ |
475 | | |
476 | | static zend_object *spl_heap_object_new(zend_class_entry *class_type) /* {{{ */ |
477 | 123 | { |
478 | 123 | return spl_heap_object_new_ex(class_type, NULL, 0); |
479 | 123 | } |
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 | 9 | { |
553 | 9 | spl_heap_object *intern = spl_heap_from_obj(obj); |
554 | 9 | *gc_data = (zval *) intern->heap->elements; |
555 | 9 | *gc_data_count = intern->heap->count; |
556 | | |
557 | 9 | return zend_std_get_properties(obj); |
558 | 9 | } |
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 | ZEND_PARSE_PARAMETERS_NONE(); |
579 | | |
580 | 0 | count = spl_ptr_heap_count(intern->heap); |
581 | 0 | RETURN_LONG(count); |
582 | 0 | } |
583 | | /* }}} */ |
584 | | |
585 | | /* {{{ Return true if the heap is empty. */ |
586 | | PHP_METHOD(SplHeap, isEmpty) |
587 | 0 | { |
588 | 0 | spl_heap_object *intern = Z_SPLHEAP_P(ZEND_THIS); |
589 | |
|
590 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
591 | | |
592 | 0 | RETURN_BOOL(spl_ptr_heap_count(intern->heap) == 0); |
593 | 0 | } |
594 | | /* }}} */ |
595 | | |
596 | | static zend_result spl_heap_consistency_validations(const spl_heap_object *intern, bool write) |
597 | 6 | { |
598 | 6 | if (intern->heap->flags & SPL_HEAP_CORRUPTED) { |
599 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0); |
600 | 0 | return FAILURE; |
601 | 0 | } |
602 | | |
603 | 6 | if (write && (intern->heap->flags & SPL_HEAP_WRITE_LOCKED)) { |
604 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Heap cannot be changed when it is already being modified.", 0); |
605 | 0 | return FAILURE; |
606 | 0 | } |
607 | | |
608 | 6 | return SUCCESS; |
609 | 6 | } |
610 | | |
611 | | /* {{{ Push $value on the heap */ |
612 | | PHP_METHOD(SplHeap, insert) |
613 | 0 | { |
614 | 0 | zval *value; |
615 | 0 | spl_heap_object *intern; |
616 | |
|
617 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
618 | 0 | Z_PARAM_ZVAL(value); |
619 | 0 | ZEND_PARSE_PARAMETERS_END(); |
620 | | |
621 | 0 | intern = Z_SPLHEAP_P(ZEND_THIS); |
622 | |
|
623 | 0 | if (UNEXPECTED(spl_heap_consistency_validations(intern, true) != SUCCESS)) { |
624 | 0 | RETURN_THROWS(); |
625 | 0 | } |
626 | | |
627 | 0 | Z_TRY_ADDREF_P(value); |
628 | 0 | spl_ptr_heap_insert(intern->heap, value, ZEND_THIS); |
629 | |
|
630 | 0 | RETURN_TRUE; |
631 | 0 | } |
632 | | /* }}} */ |
633 | | |
634 | | /* {{{ extract the element out of the top of the heap */ |
635 | | PHP_METHOD(SplHeap, extract) |
636 | 0 | { |
637 | 0 | spl_heap_object *intern; |
638 | |
|
639 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
640 | | |
641 | 0 | intern = Z_SPLHEAP_P(ZEND_THIS); |
642 | |
|
643 | 0 | if (UNEXPECTED(spl_heap_consistency_validations(intern, true) != SUCCESS)) { |
644 | 0 | RETURN_THROWS(); |
645 | 0 | } |
646 | | |
647 | 0 | if (spl_ptr_heap_delete_top(intern->heap, return_value, ZEND_THIS) == FAILURE) { |
648 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Can't extract from an empty heap", 0); |
649 | 0 | RETURN_THROWS(); |
650 | 0 | } |
651 | 0 | } |
652 | | /* }}} */ |
653 | | |
654 | | /* {{{ Push $value with the priority $priodiry on the priorityqueue */ |
655 | | PHP_METHOD(SplPriorityQueue, insert) |
656 | 0 | { |
657 | 0 | zval *data, *priority; |
658 | 0 | spl_heap_object *intern; |
659 | 0 | spl_pqueue_elem elem; |
660 | |
|
661 | 0 | ZEND_PARSE_PARAMETERS_START(2, 2) |
662 | 0 | Z_PARAM_ZVAL(data); |
663 | 0 | Z_PARAM_ZVAL(priority); |
664 | 0 | ZEND_PARSE_PARAMETERS_END(); |
665 | | |
666 | 0 | intern = Z_SPLHEAP_P(ZEND_THIS); |
667 | |
|
668 | 0 | if (UNEXPECTED(spl_heap_consistency_validations(intern, true) != SUCCESS)) { |
669 | 0 | RETURN_THROWS(); |
670 | 0 | } |
671 | | |
672 | 0 | ZVAL_COPY(&elem.data, data); |
673 | 0 | ZVAL_COPY(&elem.priority, priority); |
674 | | |
675 | | /* If we know this call came from non inherited SplPriorityQueue it's |
676 | | * possible to do specialization on the type of the priority parameter. */ |
677 | 0 | if (!intern->fptr_cmp) { |
678 | 0 | int type = Z_TYPE(elem.priority); |
679 | 0 | spl_ptr_heap_cmp_func new_cmp = |
680 | 0 | (type == IS_LONG) ? spl_ptr_pqueue_elem_cmp_long : |
681 | 0 | ((type == IS_DOUBLE) ? spl_ptr_pqueue_elem_cmp_double : spl_ptr_pqueue_elem_cmp); |
682 | |
|
683 | 0 | if (intern->heap->count == 0) { /* Specialize empty queue */ |
684 | 0 | intern->heap->cmp = new_cmp; |
685 | 0 | } else if (new_cmp != intern->heap->cmp) { /* Despecialize on type conflict. */ |
686 | 0 | intern->heap->cmp = spl_ptr_pqueue_elem_cmp; |
687 | 0 | } |
688 | 0 | } |
689 | |
|
690 | 0 | spl_ptr_heap_insert(intern->heap, &elem, ZEND_THIS); |
691 | |
|
692 | 0 | RETURN_TRUE; |
693 | 0 | } |
694 | | /* }}} */ |
695 | | |
696 | | /* {{{ extract the element out of the top of the priority queue */ |
697 | | PHP_METHOD(SplPriorityQueue, extract) |
698 | 0 | { |
699 | 0 | spl_pqueue_elem elem; |
700 | 0 | spl_heap_object *intern; |
701 | |
|
702 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
703 | | |
704 | 0 | intern = Z_SPLHEAP_P(ZEND_THIS); |
705 | |
|
706 | 0 | if (UNEXPECTED(spl_heap_consistency_validations(intern, true) != SUCCESS)) { |
707 | 0 | RETURN_THROWS(); |
708 | 0 | } |
709 | | |
710 | 0 | if (spl_ptr_heap_delete_top(intern->heap, &elem, ZEND_THIS) == FAILURE) { |
711 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Can't extract from an empty heap", 0); |
712 | 0 | RETURN_THROWS(); |
713 | 0 | } |
714 | | |
715 | 0 | spl_pqueue_extract_helper(return_value, &elem, intern->flags); |
716 | 0 | spl_ptr_heap_pqueue_elem_dtor(&elem); |
717 | 0 | } |
718 | | /* }}} */ |
719 | | |
720 | | /* {{{ Peek at the top element of the priority queue */ |
721 | | PHP_METHOD(SplPriorityQueue, top) |
722 | 0 | { |
723 | 0 | spl_heap_object *intern; |
724 | 0 | spl_pqueue_elem *elem; |
725 | |
|
726 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
727 | | |
728 | 0 | intern = Z_SPLHEAP_P(ZEND_THIS); |
729 | |
|
730 | 0 | if (UNEXPECTED(spl_heap_consistency_validations(intern, false) != SUCCESS)) { |
731 | 0 | RETURN_THROWS(); |
732 | 0 | } |
733 | | |
734 | 0 | elem = spl_ptr_heap_top(intern->heap); |
735 | |
|
736 | 0 | if (!elem) { |
737 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Can't peek at an empty heap", 0); |
738 | 0 | RETURN_THROWS(); |
739 | 0 | } |
740 | | |
741 | 0 | spl_pqueue_extract_helper(return_value, elem, intern->flags); |
742 | 0 | } |
743 | | /* }}} */ |
744 | | |
745 | | |
746 | | /* {{{ Set the flags of extraction*/ |
747 | | PHP_METHOD(SplPriorityQueue, setExtractFlags) |
748 | 0 | { |
749 | 0 | zend_long value; |
750 | 0 | spl_heap_object *intern; |
751 | |
|
752 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &value) == FAILURE) { |
753 | 0 | RETURN_THROWS(); |
754 | 0 | } |
755 | | |
756 | 0 | value &= SPL_PQUEUE_EXTR_MASK; |
757 | 0 | if (!value) { |
758 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Must specify at least one extract flag", 0); |
759 | 0 | RETURN_THROWS(); |
760 | 0 | } |
761 | | |
762 | 0 | intern = Z_SPLHEAP_P(ZEND_THIS); |
763 | 0 | intern->flags = value; |
764 | 0 | RETURN_LONG(intern->flags); |
765 | 0 | } |
766 | | /* }}} */ |
767 | | |
768 | | /* {{{ Get the flags of extraction*/ |
769 | | PHP_METHOD(SplPriorityQueue, getExtractFlags) |
770 | 0 | { |
771 | 0 | spl_heap_object *intern; |
772 | |
|
773 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
774 | | |
775 | 0 | intern = Z_SPLHEAP_P(ZEND_THIS); |
776 | |
|
777 | 0 | RETURN_LONG(intern->flags); |
778 | 0 | } |
779 | | /* }}} */ |
780 | | |
781 | | /* {{{ Recover from a corrupted state*/ |
782 | | PHP_METHOD(SplHeap, recoverFromCorruption) |
783 | 0 | { |
784 | 0 | spl_heap_object *intern; |
785 | |
|
786 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
787 | | |
788 | 0 | intern = Z_SPLHEAP_P(ZEND_THIS); |
789 | |
|
790 | 0 | intern->heap->flags = intern->heap->flags & ~SPL_HEAP_CORRUPTED; |
791 | |
|
792 | 0 | RETURN_TRUE; |
793 | 0 | } |
794 | | /* }}} */ |
795 | | |
796 | | /* {{{ Tells if the heap is in a corrupted state*/ |
797 | | PHP_METHOD(SplHeap, isCorrupted) |
798 | 0 | { |
799 | 0 | spl_heap_object *intern; |
800 | |
|
801 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
802 | | |
803 | 0 | intern = Z_SPLHEAP_P(ZEND_THIS); |
804 | |
|
805 | 0 | RETURN_BOOL(intern->heap->flags & SPL_HEAP_CORRUPTED); |
806 | 0 | } |
807 | | /* }}} */ |
808 | | |
809 | | /* {{{ compare the priorities */ |
810 | | PHP_METHOD(SplPriorityQueue, compare) |
811 | 0 | { |
812 | 0 | zval *a, *b; |
813 | |
|
814 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a, &b) == FAILURE) { |
815 | 0 | RETURN_THROWS(); |
816 | 0 | } |
817 | | |
818 | 0 | RETURN_LONG(spl_ptr_heap_zval_max_cmp(a, b, NULL)); |
819 | 0 | } |
820 | | /* }}} */ |
821 | | |
822 | | /* {{{ Peek at the top element of the heap */ |
823 | | PHP_METHOD(SplHeap, top) |
824 | 0 | { |
825 | 0 | zval *value; |
826 | 0 | spl_heap_object *intern; |
827 | |
|
828 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
829 | | |
830 | 0 | intern = Z_SPLHEAP_P(ZEND_THIS); |
831 | |
|
832 | 0 | if (UNEXPECTED(spl_heap_consistency_validations(intern, false) != SUCCESS)) { |
833 | 0 | RETURN_THROWS(); |
834 | 0 | } |
835 | | |
836 | 0 | value = spl_ptr_heap_top(intern->heap); |
837 | |
|
838 | 0 | if (!value) { |
839 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Can't peek at an empty heap", 0); |
840 | 0 | RETURN_THROWS(); |
841 | 0 | } |
842 | | |
843 | 0 | RETURN_COPY_DEREF(value); |
844 | 0 | } |
845 | | /* }}} */ |
846 | | |
847 | | /* {{{ compare the values */ |
848 | | PHP_METHOD(SplMinHeap, compare) |
849 | 0 | { |
850 | 0 | zval *a, *b; |
851 | |
|
852 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a, &b) == FAILURE) { |
853 | 0 | RETURN_THROWS(); |
854 | 0 | } |
855 | | |
856 | 0 | RETURN_LONG(spl_ptr_heap_zval_min_cmp(a, b, NULL)); |
857 | 0 | } |
858 | | /* }}} */ |
859 | | |
860 | | /* {{{ compare the values */ |
861 | | PHP_METHOD(SplMaxHeap, compare) |
862 | 0 | { |
863 | 0 | zval *a, *b; |
864 | |
|
865 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a, &b) == FAILURE) { |
866 | 0 | RETURN_THROWS(); |
867 | 0 | } |
868 | | |
869 | 0 | RETURN_LONG(spl_ptr_heap_zval_max_cmp(a, b, NULL)); |
870 | 0 | } |
871 | | /* }}} */ |
872 | | |
873 | | static void spl_heap_it_dtor(zend_object_iterator *iter) /* {{{ */ |
874 | 0 | { |
875 | 0 | zend_user_it_invalidate_current(iter); |
876 | 0 | zval_ptr_dtor(&iter->data); |
877 | 0 | } |
878 | | /* }}} */ |
879 | | |
880 | | static void spl_heap_it_rewind(zend_object_iterator *iter) /* {{{ */ |
881 | 0 | { |
882 | | /* do nothing, the iterator always points to the top element */ |
883 | 0 | } |
884 | | /* }}} */ |
885 | | |
886 | | static zend_result spl_heap_it_valid(zend_object_iterator *iter) /* {{{ */ |
887 | 0 | { |
888 | 0 | return ((Z_SPLHEAP_P(&iter->data))->heap->count != 0 ? SUCCESS : FAILURE); |
889 | 0 | } |
890 | | /* }}} */ |
891 | | |
892 | | static zval *spl_heap_it_get_current_data(zend_object_iterator *iter) /* {{{ */ |
893 | 0 | { |
894 | 0 | spl_heap_object *object = Z_SPLHEAP_P(&iter->data); |
895 | |
|
896 | 0 | if (UNEXPECTED(spl_heap_consistency_validations(object, false) != SUCCESS)) { |
897 | 0 | return NULL; |
898 | 0 | } |
899 | | |
900 | 0 | if (object->heap->count == 0) { |
901 | 0 | return NULL; |
902 | 0 | } else { |
903 | 0 | return spl_heap_elem(object->heap, 0); |
904 | 0 | } |
905 | 0 | } |
906 | | /* }}} */ |
907 | | |
908 | | static zval *spl_pqueue_it_get_current_data(zend_object_iterator *iter) /* {{{ */ |
909 | 0 | { |
910 | 0 | zend_user_iterator *user_it = (zend_user_iterator *) iter; |
911 | 0 | spl_heap_object *object = Z_SPLHEAP_P(&iter->data); |
912 | |
|
913 | 0 | if (UNEXPECTED(spl_heap_consistency_validations(object, false) != SUCCESS)) { |
914 | 0 | return NULL; |
915 | 0 | } |
916 | | |
917 | 0 | if (object->heap->count == 0) { |
918 | 0 | return NULL; |
919 | 0 | } |
920 | | |
921 | 0 | if (Z_ISUNDEF(user_it->value)) { |
922 | 0 | spl_pqueue_elem *elem = spl_heap_elem(object->heap, 0); |
923 | 0 | spl_pqueue_extract_helper(&user_it->value, elem, object->flags); |
924 | 0 | } |
925 | 0 | return &user_it->value; |
926 | 0 | } |
927 | | /* }}} */ |
928 | | |
929 | | static void spl_heap_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */ |
930 | 0 | { |
931 | 0 | spl_heap_object *object = Z_SPLHEAP_P(&iter->data); |
932 | |
|
933 | 0 | ZVAL_LONG(key, object->heap->count - 1); |
934 | 0 | } |
935 | | /* }}} */ |
936 | | |
937 | | static void spl_heap_it_move_forward(zend_object_iterator *iter) /* {{{ */ |
938 | 0 | { |
939 | 0 | spl_heap_object *object = Z_SPLHEAP_P(&iter->data); |
940 | |
|
941 | 0 | if (UNEXPECTED(spl_heap_consistency_validations(object, false) != SUCCESS)) { |
942 | 0 | return; |
943 | 0 | } |
944 | | |
945 | 0 | spl_ptr_heap_delete_top(object->heap, NULL, &iter->data); |
946 | 0 | zend_user_it_invalidate_current(iter); |
947 | 0 | } |
948 | | /* }}} */ |
949 | | |
950 | | /* {{{ Return current array key */ |
951 | | PHP_METHOD(SplHeap, key) |
952 | 0 | { |
953 | 0 | spl_heap_object *intern = Z_SPLHEAP_P(ZEND_THIS); |
954 | |
|
955 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
956 | | |
957 | 0 | RETURN_LONG(intern->heap->count - 1); |
958 | 0 | } |
959 | | /* }}} */ |
960 | | |
961 | | /* {{{ Move to next entry */ |
962 | | PHP_METHOD(SplHeap, next) |
963 | 0 | { |
964 | 0 | spl_heap_object *intern = Z_SPLHEAP_P(ZEND_THIS); |
965 | |
|
966 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
967 | | |
968 | 0 | spl_ptr_heap_delete_top(intern->heap, NULL, ZEND_THIS); |
969 | 0 | } |
970 | | /* }}} */ |
971 | | |
972 | | /* {{{ Check whether the datastructure contains more entries */ |
973 | | PHP_METHOD(SplHeap, valid) |
974 | 0 | { |
975 | 0 | spl_heap_object *intern = Z_SPLHEAP_P(ZEND_THIS); |
976 | |
|
977 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
978 | | |
979 | 0 | RETURN_BOOL(intern->heap->count != 0); |
980 | 0 | } |
981 | | /* }}} */ |
982 | | |
983 | | /* {{{ Rewind the datastructure back to the start */ |
984 | | PHP_METHOD(SplHeap, rewind) |
985 | 0 | { |
986 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
987 | | /* do nothing, the iterator always points to the top element */ |
988 | 0 | } |
989 | | /* }}} */ |
990 | | |
991 | | /* {{{ Return current datastructure entry */ |
992 | | PHP_METHOD(SplHeap, current) |
993 | 0 | { |
994 | 0 | spl_heap_object *intern = Z_SPLHEAP_P(ZEND_THIS); |
995 | |
|
996 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
997 | | |
998 | 0 | if (!intern->heap->count) { |
999 | 0 | RETURN_NULL(); |
1000 | 0 | } else { |
1001 | 0 | zval *element = spl_heap_elem(intern->heap, 0); |
1002 | 0 | RETURN_COPY_DEREF(element); |
1003 | 0 | } |
1004 | 0 | } |
1005 | | /* }}} */ |
1006 | | |
1007 | | /* {{{ Return current datastructure entry */ |
1008 | | PHP_METHOD(SplPriorityQueue, current) |
1009 | 0 | { |
1010 | 0 | spl_heap_object *intern = Z_SPLHEAP_P(ZEND_THIS); |
1011 | |
|
1012 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
1013 | | |
1014 | 0 | if (!intern->heap->count) { |
1015 | 0 | RETURN_NULL(); |
1016 | 0 | } else { |
1017 | 0 | spl_pqueue_elem *elem = spl_heap_elem(intern->heap, 0); |
1018 | 0 | spl_pqueue_extract_helper(return_value, elem, intern->flags); |
1019 | 0 | } |
1020 | 0 | } |
1021 | | /* }}} */ |
1022 | | |
1023 | | /* {{{ */ |
1024 | | PHP_METHOD(SplHeap, __debugInfo) |
1025 | 0 | { |
1026 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
1027 | | |
1028 | 0 | RETURN_ARR(spl_heap_object_get_debug_info(spl_ce_SplHeap, Z_OBJ_P(ZEND_THIS))); |
1029 | 0 | } /* }}} */ |
1030 | | |
1031 | | /* {{{ */ |
1032 | | PHP_METHOD(SplPriorityQueue, __debugInfo) |
1033 | 0 | { |
1034 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
1035 | | |
1036 | 0 | RETURN_ARR(spl_heap_object_get_debug_info(spl_ce_SplPriorityQueue, Z_OBJ_P(ZEND_THIS))); |
1037 | 0 | } /* }}} */ |
1038 | | |
1039 | | static void spl_heap_serialize_internal_state(zval *return_value, spl_heap_object *intern, bool is_pqueue) |
1040 | 0 | { |
1041 | 0 | zval heap_elements; |
1042 | 0 | int heap_count = intern->heap->count; |
1043 | |
|
1044 | 0 | array_init(return_value); |
1045 | 0 | add_assoc_long(return_value, "flags", intern->flags); |
1046 | |
|
1047 | 0 | array_init_size(&heap_elements, heap_count); |
1048 | |
|
1049 | 0 | if (heap_count == 0) { |
1050 | 0 | add_assoc_zval(return_value, "heap_elements", &heap_elements); |
1051 | 0 | return; |
1052 | 0 | } |
1053 | | |
1054 | 0 | for (int heap_idx = 0; heap_idx < heap_count; ++heap_idx) { |
1055 | 0 | if (is_pqueue) { |
1056 | 0 | spl_pqueue_elem *elem = spl_heap_elem(intern->heap, heap_idx); |
1057 | 0 | zval entry; |
1058 | 0 | array_init(&entry); |
1059 | 0 | add_assoc_zval_ex(&entry, "data", strlen("data"), &elem->data); |
1060 | 0 | Z_TRY_ADDREF(elem->data); |
1061 | 0 | add_assoc_zval_ex(&entry, "priority", strlen("priority"), &elem->priority); |
1062 | 0 | Z_TRY_ADDREF(elem->priority); |
1063 | 0 | zend_hash_next_index_insert(Z_ARRVAL(heap_elements), &entry); |
1064 | 0 | } else { |
1065 | 0 | zval *elem = spl_heap_elem(intern->heap, heap_idx); |
1066 | 0 | zend_hash_next_index_insert(Z_ARRVAL(heap_elements), elem); |
1067 | 0 | Z_TRY_ADDREF_P(elem); |
1068 | 0 | } |
1069 | 0 | } |
1070 | |
|
1071 | 0 | add_assoc_zval(return_value, "heap_elements", &heap_elements); |
1072 | 0 | } |
1073 | | |
1074 | | static zend_result spl_heap_unserialize_internal_state(HashTable *state_ht, spl_heap_object *intern, zval *this_ptr, bool is_pqueue) |
1075 | 0 | { |
1076 | 0 | zval *flags_val = zend_hash_str_find(state_ht, "flags", strlen("flags")); |
1077 | 0 | if (!flags_val || Z_TYPE_P(flags_val) != IS_LONG) { |
1078 | 0 | return FAILURE; |
1079 | 0 | } |
1080 | | |
1081 | 0 | zend_long flags_value = Z_LVAL_P(flags_val); |
1082 | |
|
1083 | 0 | if (is_pqueue) { |
1084 | 0 | flags_value &= SPL_PQUEUE_EXTR_MASK; |
1085 | 0 | if (!flags_value) { |
1086 | 0 | return FAILURE; |
1087 | 0 | } |
1088 | 0 | } else if (flags_value != 0) { /* Regular heaps should not have user-visible flags */ |
1089 | 0 | return FAILURE; |
1090 | 0 | } |
1091 | | |
1092 | 0 | intern->flags = (int) flags_value; |
1093 | |
|
1094 | 0 | zval *heap_elements = zend_hash_str_find(state_ht, "heap_elements", strlen("heap_elements")); |
1095 | 0 | if (!heap_elements) { |
1096 | 0 | return FAILURE; |
1097 | 0 | } |
1098 | | |
1099 | 0 | if (Z_TYPE_P(heap_elements) != IS_ARRAY) { |
1100 | 0 | return FAILURE; |
1101 | 0 | } |
1102 | | |
1103 | 0 | ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(heap_elements), zval *val) { |
1104 | 0 | if (is_pqueue) { |
1105 | | /* PriorityQueue elements are serialized as arrays with 'data' and 'priority' keys */ |
1106 | 0 | if (Z_TYPE_P(val) != IS_ARRAY || zend_hash_num_elements(Z_ARRVAL_P(val)) != 2) { |
1107 | 0 | return FAILURE; |
1108 | 0 | } |
1109 | | |
1110 | 0 | zval *data_val = zend_hash_str_find(Z_ARRVAL_P(val), "data", strlen("data") ); |
1111 | 0 | zval *priority_val = zend_hash_str_find(Z_ARRVAL_P(val), "priority", strlen("priority")); |
1112 | |
|
1113 | 0 | if (!data_val || !priority_val) { |
1114 | 0 | return FAILURE; |
1115 | 0 | } |
1116 | | |
1117 | 0 | spl_pqueue_elem elem; |
1118 | 0 | ZVAL_COPY(&elem.data, data_val); |
1119 | 0 | ZVAL_COPY(&elem.priority, priority_val); |
1120 | 0 | spl_ptr_heap_insert(intern->heap, &elem, this_ptr); |
1121 | 0 | if (EG(exception)) { |
1122 | 0 | return FAILURE; |
1123 | 0 | } |
1124 | 0 | } else { |
1125 | 0 | Z_TRY_ADDREF_P(val); |
1126 | 0 | spl_ptr_heap_insert(intern->heap, val, this_ptr); |
1127 | 0 | if (EG(exception)) { |
1128 | 0 | return FAILURE; |
1129 | 0 | } |
1130 | 0 | } |
1131 | 0 | } ZEND_HASH_FOREACH_END(); |
1132 | | |
1133 | 0 | return SUCCESS; |
1134 | 0 | } |
1135 | | |
1136 | | static void spl_heap_serialize_internal(INTERNAL_FUNCTION_PARAMETERS, bool is_pqueue) |
1137 | 0 | { |
1138 | 0 | spl_heap_object *intern = Z_SPLHEAP_P(ZEND_THIS); |
1139 | 0 | zval props, state; |
1140 | |
|
1141 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
1142 | | |
1143 | 0 | if (UNEXPECTED(spl_heap_consistency_validations(intern, false) != SUCCESS)) { |
1144 | 0 | RETURN_THROWS(); |
1145 | 0 | } |
1146 | | |
1147 | 0 | if (intern->heap->flags & SPL_HEAP_WRITE_LOCKED) { |
1148 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Cannot serialize heap while it is being modified.", 0); |
1149 | 0 | RETURN_THROWS(); |
1150 | 0 | } |
1151 | | |
1152 | 0 | array_init(return_value); |
1153 | |
|
1154 | 0 | ZVAL_ARR(&props, zend_array_dup(zend_std_get_properties(&intern->std))); |
1155 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &props); |
1156 | |
|
1157 | 0 | spl_heap_serialize_internal_state(&state, intern, is_pqueue); |
1158 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &state); |
1159 | 0 | } |
1160 | | |
1161 | | PHP_METHOD(SplPriorityQueue, __serialize) |
1162 | 0 | { |
1163 | 0 | spl_heap_serialize_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU, true); |
1164 | 0 | } |
1165 | | |
1166 | | PHP_METHOD(SplPriorityQueue, __unserialize) |
1167 | 2 | { |
1168 | 2 | HashTable *data; |
1169 | 2 | spl_heap_object *intern = Z_SPLHEAP_P(ZEND_THIS); |
1170 | | |
1171 | 6 | ZEND_PARSE_PARAMETERS_START(1, 1) |
1172 | 8 | Z_PARAM_ARRAY_HT(data) |
1173 | 2 | ZEND_PARSE_PARAMETERS_END(); |
1174 | | |
1175 | 2 | if (zend_hash_num_elements(data) != 2) { |
1176 | 2 | zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name)); |
1177 | 2 | RETURN_THROWS(); |
1178 | 2 | } |
1179 | | |
1180 | 0 | zval *props = zend_hash_index_find(data, 0); |
1181 | 0 | if (!props || Z_TYPE_P(props) != IS_ARRAY) { |
1182 | 0 | zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name)); |
1183 | 0 | RETURN_THROWS(); |
1184 | 0 | } |
1185 | | |
1186 | 0 | object_properties_load(&intern->std, Z_ARRVAL_P(props)); |
1187 | 0 | if (EG(exception)) { |
1188 | 0 | zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name)); |
1189 | 0 | RETURN_THROWS(); |
1190 | 0 | } |
1191 | | |
1192 | 0 | zval *state = zend_hash_index_find(data, 1); |
1193 | 0 | if (!state || Z_TYPE_P(state) != IS_ARRAY) { |
1194 | 0 | zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name)); |
1195 | 0 | RETURN_THROWS(); |
1196 | 0 | } |
1197 | | |
1198 | 0 | if (spl_heap_unserialize_internal_state(Z_ARRVAL_P(state), intern, ZEND_THIS, true) != SUCCESS) { |
1199 | 0 | zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name)); |
1200 | 0 | RETURN_THROWS(); |
1201 | 0 | } |
1202 | | |
1203 | 0 | if (EG(exception)) { |
1204 | 0 | RETURN_THROWS(); |
1205 | 0 | } |
1206 | | |
1207 | 0 | if (UNEXPECTED(spl_heap_consistency_validations(intern, false) != SUCCESS)) { |
1208 | 0 | RETURN_THROWS(); |
1209 | 0 | } |
1210 | 0 | } |
1211 | | |
1212 | | PHP_METHOD(SplHeap, __serialize) |
1213 | 0 | { |
1214 | 0 | spl_heap_serialize_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU, false); |
1215 | 0 | } |
1216 | | |
1217 | | PHP_METHOD(SplHeap, __unserialize) |
1218 | 6 | { |
1219 | 6 | HashTable *data; |
1220 | 6 | spl_heap_object *intern = Z_SPLHEAP_P(ZEND_THIS); |
1221 | | |
1222 | 18 | ZEND_PARSE_PARAMETERS_START(1, 1) |
1223 | 24 | Z_PARAM_ARRAY_HT(data) |
1224 | 6 | ZEND_PARSE_PARAMETERS_END(); |
1225 | | |
1226 | 6 | if (UNEXPECTED(spl_heap_consistency_validations(intern, true) != SUCCESS)) { |
1227 | 0 | RETURN_THROWS(); |
1228 | 0 | } |
1229 | | |
1230 | 6 | if (zend_hash_num_elements(data) != 2) { |
1231 | 5 | zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name)); |
1232 | 5 | RETURN_THROWS(); |
1233 | 5 | } |
1234 | | |
1235 | 1 | zval *props = zend_hash_index_find(data, 0); |
1236 | 1 | if (!props || Z_TYPE_P(props) != IS_ARRAY) { |
1237 | 1 | zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name)); |
1238 | 1 | RETURN_THROWS(); |
1239 | 1 | } |
1240 | | |
1241 | 0 | object_properties_load(&intern->std, Z_ARRVAL_P(props)); |
1242 | 0 | if (EG(exception)) { |
1243 | 0 | zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name)); |
1244 | 0 | RETURN_THROWS(); |
1245 | 0 | } |
1246 | | |
1247 | 0 | zval *state = zend_hash_index_find(data, 1); |
1248 | 0 | if (!state || Z_TYPE_P(state) != IS_ARRAY) { |
1249 | 0 | zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name)); |
1250 | 0 | RETURN_THROWS(); |
1251 | 0 | } |
1252 | | |
1253 | 0 | if (spl_heap_unserialize_internal_state(Z_ARRVAL_P(state), intern, ZEND_THIS, false) != SUCCESS) { |
1254 | 0 | zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(intern->std.ce->name)); |
1255 | 0 | RETURN_THROWS(); |
1256 | 0 | } |
1257 | | |
1258 | 0 | if (UNEXPECTED(spl_heap_consistency_validations(intern, false) != SUCCESS)) { |
1259 | 0 | RETURN_THROWS(); |
1260 | 0 | } |
1261 | 0 | } |
1262 | | |
1263 | | /* iterator handler table */ |
1264 | | static const zend_object_iterator_funcs spl_heap_it_funcs = { |
1265 | | spl_heap_it_dtor, |
1266 | | spl_heap_it_valid, |
1267 | | spl_heap_it_get_current_data, |
1268 | | spl_heap_it_get_current_key, |
1269 | | spl_heap_it_move_forward, |
1270 | | spl_heap_it_rewind, |
1271 | | NULL, |
1272 | | NULL, /* get_gc */ |
1273 | | }; |
1274 | | |
1275 | | static const zend_object_iterator_funcs spl_pqueue_it_funcs = { |
1276 | | spl_heap_it_dtor, |
1277 | | spl_heap_it_valid, |
1278 | | spl_pqueue_it_get_current_data, |
1279 | | spl_heap_it_get_current_key, |
1280 | | spl_heap_it_move_forward, |
1281 | | spl_heap_it_rewind, |
1282 | | NULL, |
1283 | | NULL, /* get_gc */ |
1284 | | }; |
1285 | | |
1286 | | static zend_object_iterator *spl_heap_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */ |
1287 | 0 | { |
1288 | 0 | if (by_ref) { |
1289 | 0 | zend_throw_error(NULL, "An iterator cannot be used with foreach by reference"); |
1290 | 0 | return NULL; |
1291 | 0 | } |
1292 | | |
1293 | 0 | zend_user_iterator *iterator = emalloc(sizeof(zend_user_iterator)); |
1294 | 0 | zend_iterator_init(&iterator->it); |
1295 | |
|
1296 | 0 | ZVAL_OBJ_COPY(&iterator->it.data, Z_OBJ_P(object)); |
1297 | 0 | iterator->it.funcs = &spl_heap_it_funcs; |
1298 | 0 | iterator->ce = ce; |
1299 | 0 | ZVAL_UNDEF(&iterator->value); |
1300 | |
|
1301 | 0 | return &iterator->it; |
1302 | 0 | } |
1303 | | /* }}} */ |
1304 | | |
1305 | | static zend_object_iterator *spl_pqueue_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */ |
1306 | 0 | { |
1307 | 0 | if (by_ref) { |
1308 | 0 | zend_throw_error(NULL, "An iterator cannot be used with foreach by reference"); |
1309 | 0 | return NULL; |
1310 | 0 | } |
1311 | | |
1312 | 0 | zend_user_iterator *iterator = emalloc(sizeof(zend_user_iterator)); |
1313 | 0 | zend_iterator_init(&iterator->it); |
1314 | |
|
1315 | 0 | ZVAL_OBJ_COPY(&iterator->it.data, Z_OBJ_P(object)); |
1316 | 0 | iterator->it.funcs = &spl_pqueue_it_funcs; |
1317 | 0 | iterator->ce = ce; |
1318 | 0 | ZVAL_UNDEF(&iterator->value); |
1319 | |
|
1320 | 0 | return &iterator->it; |
1321 | 0 | } |
1322 | | /* }}} */ |
1323 | | |
1324 | | PHP_MINIT_FUNCTION(spl_heap) /* {{{ */ |
1325 | 16 | { |
1326 | 16 | spl_ce_SplHeap = register_class_SplHeap(zend_ce_iterator, zend_ce_countable); |
1327 | 16 | spl_ce_SplHeap->create_object = spl_heap_object_new; |
1328 | 16 | spl_ce_SplHeap->default_object_handlers = &spl_handler_SplHeap; |
1329 | 16 | spl_ce_SplHeap->get_iterator = spl_heap_get_iterator; |
1330 | | |
1331 | 16 | memcpy(&spl_handler_SplHeap, &std_object_handlers, sizeof(zend_object_handlers)); |
1332 | | |
1333 | 16 | spl_handler_SplHeap.offset = XtOffsetOf(spl_heap_object, std); |
1334 | 16 | spl_handler_SplHeap.clone_obj = spl_heap_object_clone; |
1335 | 16 | spl_handler_SplHeap.count_elements = spl_heap_object_count_elements; |
1336 | 16 | spl_handler_SplHeap.get_gc = spl_heap_object_get_gc; |
1337 | 16 | spl_handler_SplHeap.free_obj = spl_heap_object_free_storage; |
1338 | | |
1339 | 16 | spl_ce_SplMinHeap = register_class_SplMinHeap(spl_ce_SplHeap); |
1340 | 16 | spl_ce_SplMinHeap->create_object = spl_heap_object_new; |
1341 | 16 | spl_ce_SplMinHeap->get_iterator = spl_heap_get_iterator; |
1342 | | |
1343 | 16 | spl_ce_SplMaxHeap = register_class_SplMaxHeap(spl_ce_SplHeap); |
1344 | 16 | spl_ce_SplMaxHeap->create_object = spl_heap_object_new; |
1345 | 16 | spl_ce_SplMaxHeap->get_iterator = spl_heap_get_iterator; |
1346 | | |
1347 | 16 | spl_ce_SplPriorityQueue = register_class_SplPriorityQueue(zend_ce_iterator, zend_ce_countable); |
1348 | 16 | spl_ce_SplPriorityQueue->create_object = spl_heap_object_new; |
1349 | 16 | spl_ce_SplPriorityQueue->default_object_handlers = &spl_handler_SplPriorityQueue; |
1350 | 16 | spl_ce_SplPriorityQueue->get_iterator = spl_pqueue_get_iterator; |
1351 | | |
1352 | 16 | memcpy(&spl_handler_SplPriorityQueue, &std_object_handlers, sizeof(zend_object_handlers)); |
1353 | | |
1354 | 16 | spl_handler_SplPriorityQueue.offset = XtOffsetOf(spl_heap_object, std); |
1355 | 16 | spl_handler_SplPriorityQueue.clone_obj = spl_heap_object_clone; |
1356 | 16 | spl_handler_SplPriorityQueue.count_elements = spl_heap_object_count_elements; |
1357 | 16 | spl_handler_SplPriorityQueue.get_gc = spl_pqueue_object_get_gc; |
1358 | 16 | spl_handler_SplPriorityQueue.free_obj = spl_heap_object_free_storage; |
1359 | | |
1360 | 16 | return SUCCESS; |
1361 | 16 | } |
1362 | | /* }}} */ |