/src/openssl/ssl/pqueue.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2005-2020 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include "ssl_local.h" |
11 | | #include <openssl/bn.h> |
12 | | |
13 | | pitem *pitem_new(unsigned char *prio64be, void *data) |
14 | 0 | { |
15 | 0 | pitem *item = OPENSSL_malloc(sizeof(*item)); |
16 | |
|
17 | 0 | if (item == NULL) |
18 | 0 | return NULL; |
19 | | |
20 | 0 | memcpy(item->priority, prio64be, sizeof(item->priority)); |
21 | 0 | item->data = data; |
22 | 0 | item->next = NULL; |
23 | |
|
24 | 0 | return item; |
25 | 0 | } |
26 | | |
27 | | pitem *pitem_new_u64(uint64_t prio, void *data) |
28 | 0 | { |
29 | 0 | pitem *item = OPENSSL_malloc(sizeof(*item)); |
30 | 0 | unsigned char *p_item_prio; |
31 | |
|
32 | 0 | if (item == NULL) |
33 | 0 | return NULL; |
34 | | |
35 | 0 | p_item_prio = item->priority; |
36 | 0 | l2n8(prio, p_item_prio); |
37 | 0 | item->data = data; |
38 | 0 | item->next = NULL; |
39 | |
|
40 | 0 | return item; |
41 | 0 | } |
42 | | |
43 | | void pitem_free(pitem *item) |
44 | 0 | { |
45 | 0 | OPENSSL_free(item); |
46 | 0 | } |
47 | | |
48 | | pqueue *pqueue_new(void) |
49 | 0 | { |
50 | 0 | pqueue *pq = OPENSSL_zalloc(sizeof(*pq)); |
51 | |
|
52 | 0 | return pq; |
53 | 0 | } |
54 | | |
55 | | void pqueue_free(pqueue *pq) |
56 | 0 | { |
57 | 0 | OPENSSL_free(pq); |
58 | 0 | } |
59 | | |
60 | | pitem *pqueue_insert(pqueue *pq, pitem *item) |
61 | 0 | { |
62 | 0 | pitem *curr, *next; |
63 | |
|
64 | 0 | if (pq->items == NULL) { |
65 | 0 | pq->items = item; |
66 | 0 | return item; |
67 | 0 | } |
68 | | |
69 | 0 | for (curr = NULL, next = pq->items; |
70 | 0 | next != NULL; curr = next, next = next->next) { |
71 | | /* |
72 | | * we can compare 64-bit value in big-endian encoding with memcmp:-) |
73 | | */ |
74 | 0 | int cmp = memcmp(next->priority, item->priority, 8); |
75 | 0 | if (cmp > 0) { /* next > item */ |
76 | 0 | item->next = next; |
77 | |
|
78 | 0 | if (curr == NULL) |
79 | 0 | pq->items = item; |
80 | 0 | else |
81 | 0 | curr->next = item; |
82 | |
|
83 | 0 | return item; |
84 | 0 | } |
85 | | |
86 | 0 | else if (cmp == 0) /* duplicates not allowed */ |
87 | 0 | return NULL; |
88 | 0 | } |
89 | | |
90 | 0 | item->next = NULL; |
91 | 0 | curr->next = item; |
92 | |
|
93 | 0 | return item; |
94 | 0 | } |
95 | | |
96 | | pitem *pqueue_peek(pqueue *pq) |
97 | 0 | { |
98 | 0 | return pq->items; |
99 | 0 | } |
100 | | |
101 | | pitem *pqueue_pop(pqueue *pq) |
102 | 0 | { |
103 | 0 | pitem *item = pq->items; |
104 | |
|
105 | 0 | if (pq->items != NULL) |
106 | 0 | pq->items = pq->items->next; |
107 | |
|
108 | 0 | return item; |
109 | 0 | } |
110 | | |
111 | | pitem *pqueue_find(pqueue *pq, unsigned char *prio64be) |
112 | 0 | { |
113 | 0 | pitem *next; |
114 | 0 | pitem *found = NULL; |
115 | |
|
116 | 0 | if (pq->items == NULL) |
117 | 0 | return NULL; |
118 | | |
119 | 0 | for (next = pq->items; next->next != NULL; next = next->next) { |
120 | 0 | if (memcmp(next->priority, prio64be, 8) == 0) { |
121 | 0 | found = next; |
122 | 0 | break; |
123 | 0 | } |
124 | 0 | } |
125 | | |
126 | | /* check the one last node */ |
127 | 0 | if (memcmp(next->priority, prio64be, 8) == 0) |
128 | 0 | found = next; |
129 | |
|
130 | 0 | if (!found) |
131 | 0 | return NULL; |
132 | | |
133 | 0 | return found; |
134 | 0 | } |
135 | | |
136 | | pitem *pqueue_find_u64(pqueue *pq, uint64_t prio) |
137 | 0 | { |
138 | 0 | unsigned char prio64be[8], *p_prio64be = prio64be; |
139 | |
|
140 | 0 | l2n8(prio, p_prio64be); |
141 | |
|
142 | 0 | return pqueue_find(pq, prio64be); |
143 | 0 | } |
144 | | |
145 | | pitem *pqueue_iterator(pqueue *pq) |
146 | 0 | { |
147 | 0 | return pqueue_peek(pq); |
148 | 0 | } |
149 | | |
150 | | pitem *pqueue_next(piterator *item) |
151 | 0 | { |
152 | 0 | pitem *ret; |
153 | |
|
154 | 0 | if (item == NULL || *item == NULL) |
155 | 0 | return NULL; |
156 | | |
157 | | /* *item != NULL */ |
158 | 0 | ret = *item; |
159 | 0 | *item = (*item)->next; |
160 | |
|
161 | 0 | return ret; |
162 | 0 | } |
163 | | |
164 | | size_t pqueue_size(pqueue *pq) |
165 | 0 | { |
166 | 0 | pitem *item = pq->items; |
167 | 0 | size_t count = 0; |
168 | |
|
169 | 0 | while (item != NULL) { |
170 | 0 | count++; |
171 | 0 | item = item->next; |
172 | 0 | } |
173 | 0 | return count; |
174 | 0 | } |