Debian Codegraph @

pypy/module/_hpy_universal/_vendored/hpy/debug/src/dhqueue.c

raw symbols SCIP
1 #include "debug_internal.h"
2
3 // TODO: we need to make DHQueue thread-safe if we want to use the same
4 // context in multiple threads
5 void DHQueue_init(DHQueue *q) {
6 q->head = NULL;
7 q->tail = NULL;
8 q->size = 0;
9 }
10
11 void DHQueue_append(DHQueue *q, DHQueueNode *h) {
12 if (q->head == NULL) {
13 h->prev = NULL;
14 h->next = NULL;
15 q->head = h;
16 q->tail = h;
17 } else {
18 h->next = NULL;
19 h->prev = q->tail;
20 q->tail->next = h;
21 q->tail = h;
22 }
23 q->size++;
24 }
25
26 DHQueueNode *DHQueue_popfront(DHQueue *q)
27 {
28 assert(q->size > 0);
29 assert(q->head != NULL);
30 DHQueueNode *head = q->head;
31 if (q->size == 1) {
32 q->head = NULL;
33 q->tail = NULL;
34 q->size = 0;
35 }
36 else {
37 q->head = head->next;
38 q->head->prev = NULL;
39 q->size--;
40 }
41 // the following is not strictly necessary, but it makes thing much easier
42 // to debug in case of bugs
43 head->next = NULL;
44 head->prev = NULL;
45 return head;
46 }
47
48 void DHQueue_remove(DHQueue *q, DHQueueNode *h)
49 {
50 #ifndef NDEBUG
51 // if we are debugging, let's check that h is effectively in the queue
52 DHQueueNode *it = q->head;
53 bool found = false;
54 while(it != NULL) {
55 if (it == h) {
56 found = true;
57 break;
58 }
59 it = it->next;
60 }
61 assert(found);
62 #endif
63 if (q->size == 1) {
64 q->head = NULL;
65 q->tail = NULL;
66 } else if (h == q->head) {
67 assert(h->prev == NULL);
68 q->head = h->next;
69 q->head->prev = NULL;
70 } else if (h == q->tail) {
71 assert(h->next == NULL);
72 q->tail = h->prev;
73 q->tail->next = NULL;
74 }
75 else {
76 h->prev->next = h->next;
77 h->next->prev = h->prev;
78 }
79 q->size--;
80 h->next = NULL;
81 h->prev = NULL;
82 }
83
84
85 #ifndef NDEBUG
86 static void linked_item_sanity_check(DHQueueNode *h)
87 {
88 if (h == NULL)
89 return;
90 if (h->next != NULL)
91 assert(h->next->prev == h);
92 if (h->prev != NULL)
93 assert(h->prev->next == h);
94 }
95 #endif
96
97 void DHQueue_sanity_check(DHQueue *q)
98 {
99 #ifndef NDEBUG
100 if (q->head == NULL || q->tail == NULL) {
101 assert(q->head == NULL);
102 assert(q->tail == NULL);
103 assert(q->size == 0);
104 }
105 else {
106 assert(q->head->prev == NULL);
107 assert(q->tail->next == NULL);
108 assert(q->size > 0);
109 DHQueueNode *h = q->head;
110 HPy_ssize_t size = 0;
111 while(h != NULL) {
112 linked_item_sanity_check(h);
113 if (h->next == NULL)
114 assert(h == q->tail);
115 h = h->next;
116 size++;
117 }
118 assert(q->size == size);
119 }
120 #endif
121 }