1 #include "php_common_handlers.h"
2 #include "php_deque_handlers.h"
3 
4 #include "../objects/php_priority_queue.h"
5 #include "../../ds/ds_priority_queue.h"
6 
7 zend_object_handlers php_priority_queue_handlers;
8 
php_ds_priority_queue_free_object(zend_object * object)9 static void php_ds_priority_queue_free_object(zend_object *object)
10 {
11     php_ds_priority_queue_t *queue = php_ds_priority_queue_fetch_object(object);
12     ds_priority_queue_free(queue->queue);
13 
14     if (queue->gc_data != NULL) {
15         efree(queue->gc_data);
16     }
17     zend_object_std_dtor(&queue->std);
18 }
19 
php_ds_priority_queue_count_elements(zend_object * obj,zend_long * count)20 static int php_ds_priority_queue_count_elements
21 #if PHP_VERSION_ID >= 80000
22 (zend_object *obj, zend_long *count) {
23     ds_priority_queue_t *pq = php_ds_priority_queue_fetch_object(obj)->queue;
24 #else
25 (zval *obj, zend_long *count) {
26     ds_priority_queue_t *pq = Z_DS_PRIORITY_QUEUE_P(obj);
27 #endif
28     *count = DS_PRIORITY_QUEUE_SIZE(pq);
29     return SUCCESS;
30 }
31 
32 static zend_object *php_ds_priority_queue_clone_obj
33 #if PHP_VERSION_ID >= 80000
34 (zend_object *obj) {
35     ds_priority_queue_t *pq = php_ds_priority_queue_fetch_object(obj)->queue;
36 #else
37 (zval *obj) {
38     ds_priority_queue_t *pq = Z_DS_PRIORITY_QUEUE_P(obj);
39 #endif
40     return php_ds_priority_queue_create_clone(pq);
41 }
42 
43 static HashTable *php_ds_priority_queue_get_debug_info
44 #if PHP_VERSION_ID >= 80000
45 (zend_object *obj, int *is_temp) {
46     ds_priority_queue_t *pq = php_ds_priority_queue_fetch_object(obj)->queue;
47 #else
48 (zval *obj, int *is_temp) {
49     ds_priority_queue_t *pq = Z_DS_PRIORITY_QUEUE_P(obj);
50 #endif
51     zval arr;
52     *is_temp = 1;
53     ds_priority_queue_to_array(pq, &arr);
54     return Z_ARRVAL(arr);
55 }
56 
57 static HashTable *php_ds_priority_queue_get_gc
58 #if PHP_VERSION_ID >= 80000
59 (zend_object *obj, zval **gc_data, int *gc_size) {
60     ds_priority_queue_t *pq = php_ds_priority_queue_fetch_object(obj)->queue;
61 #else
62 (zval *obj, zval **gc_data, int *gc_size) {
63     ds_priority_queue_t *pq = Z_DS_PRIORITY_QUEUE_P(obj);
64 #endif
65     if (DS_PRIORITY_QUEUE_IS_EMPTY(pq)) {
66         *gc_data = NULL;
67         *gc_size = 0;
68     } else {
69         *gc_data = (zval*) pq->nodes;
70         *gc_size = pq->size * 2;
71     }
72     return NULL;
73 }
74 
75 void php_ds_register_priority_queue_handlers()
76 {
77     memcpy(&php_priority_queue_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
78 
79     php_priority_queue_handlers.offset = XtOffsetOf(php_ds_priority_queue_t, std);
80 
81     php_priority_queue_handlers.get_gc            = php_ds_priority_queue_get_gc;
82     php_priority_queue_handlers.free_obj          = php_ds_priority_queue_free_object;
83     php_priority_queue_handlers.clone_obj         = php_ds_priority_queue_clone_obj;
84     php_priority_queue_handlers.cast_object       = php_ds_default_cast_object;
85     php_priority_queue_handlers.get_debug_info    = php_ds_priority_queue_get_debug_info;
86     php_priority_queue_handlers.count_elements    = php_ds_priority_queue_count_elements;
87 }
88