]> git.sesse.net Git - ffmpeg/blob - libavutil/tree.c
tls_gnutls: fix hang on disconnection
[ffmpeg] / libavutil / tree.c
1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "error.h"
22 #include "log.h"
23 #include "mem.h"
24 #include "tree.h"
25
26 typedef struct AVTreeNode {
27     struct AVTreeNode *child[2];
28     void *elem;
29     int state;
30 } AVTreeNode;
31
32 #if FF_API_CONTEXT_SIZE
33 const int av_tree_node_size = sizeof(AVTreeNode);
34 #endif
35
36 struct AVTreeNode *av_tree_node_alloc(void)
37 {
38     return av_mallocz(sizeof(struct AVTreeNode));
39 }
40
41 void *av_tree_find(const AVTreeNode *t, void *key,
42                    int (*cmp)(void *key, const void *b), void *next[2])
43 {
44     if (t) {
45         unsigned int v = cmp(key, t->elem);
46         if (v) {
47             if (next)
48                 next[v >> 31] = t->elem;
49             return av_tree_find(t->child[(v >> 31) ^ 1], key, cmp, next);
50         } else {
51             if (next) {
52                 av_tree_find(t->child[0], key, cmp, next);
53                 av_tree_find(t->child[1], key, cmp, next);
54             }
55             return t->elem;
56         }
57     }
58     return NULL;
59 }
60
61 void *av_tree_insert(AVTreeNode **tp, void *key,
62                      int (*cmp)(void *key, const void *b), AVTreeNode **next)
63 {
64     AVTreeNode *t = *tp;
65     if (t) {
66         unsigned int v = cmp(t->elem, key);
67         void *ret;
68         if (!v) {
69             if (*next)
70                 return t->elem;
71             else if (t->child[0] || t->child[1]) {
72                 int i = !t->child[0];
73                 void *next_elem[2];
74                 av_tree_find(t->child[i], key, cmp, next_elem);
75                 key = t->elem = next_elem[i];
76                 v   = -i;
77             } else {
78                 *next = t;
79                 *tp   = NULL;
80                 return NULL;
81             }
82         }
83         ret = av_tree_insert(&t->child[v >> 31], key, cmp, next);
84         if (!ret) {
85             int i              = (v >> 31) ^ !!*next;
86             AVTreeNode **child = &t->child[i];
87             t->state += 2 * i - 1;
88
89             if (!(t->state & 1)) {
90                 if (t->state) {
91                     /* The following code is equivalent to
92                      * if ((*child)->state * 2 == -t->state)
93                      *     rotate(child, i ^ 1);
94                      * rotate(tp, i);
95                      *
96                      * with rotate():
97                      * static void rotate(AVTreeNode **tp, int i)
98                      * {
99                      *     AVTreeNode *t= *tp;
100                      *
101                      *     *tp = t->child[i];
102                      *     t->child[i] = t->child[i]->child[i ^ 1];
103                      *     (*tp)->child[i ^ 1] = t;
104                      *     i = 4 * t->state + 2 * (*tp)->state + 12;
105                      *     t->state     = ((0x614586 >> i) & 3) - 1;
106                      *     (*tp)->state = ((0x400EEA >> i) & 3) - 1 +
107                      *                    ((*tp)->state >> 1);
108                      * }
109                      * but such a rotate function is both bigger and slower
110                      */
111                     if ((*child)->state * 2 == -t->state) {
112                         *tp                    = (*child)->child[i ^ 1];
113                         (*child)->child[i ^ 1] = (*tp)->child[i];
114                         (*tp)->child[i]        = *child;
115                         *child                 = (*tp)->child[i ^ 1];
116                         (*tp)->child[i ^ 1]    = t;
117
118                         (*tp)->child[0]->state = -((*tp)->state > 0);
119                         (*tp)->child[1]->state = (*tp)->state < 0;
120                         (*tp)->state           = 0;
121                     } else {
122                         *tp                 = *child;
123                         *child              = (*child)->child[i ^ 1];
124                         (*tp)->child[i ^ 1] = t;
125                         if ((*tp)->state)
126                             t->state = 0;
127                         else
128                             t->state >>= 1;
129                         (*tp)->state = -t->state;
130                     }
131                 }
132             }
133             if (!(*tp)->state ^ !!*next)
134                 return key;
135         }
136         return ret;
137     } else {
138         *tp   = *next;
139         *next = NULL;
140         if (*tp) {
141             (*tp)->elem = key;
142             return NULL;
143         } else
144             return key;
145     }
146 }
147
148 void av_tree_destroy(AVTreeNode *t)
149 {
150     if (t) {
151         av_tree_destroy(t->child[0]);
152         av_tree_destroy(t->child[1]);
153         av_free(t);
154     }
155 }
156
157 void av_tree_enumerate(AVTreeNode *t, void *opaque,
158                        int (*cmp)(void *opaque, void *elem),
159                        int (*enu)(void *opaque, void *elem))
160 {
161     if (t) {
162         int v = cmp ? cmp(opaque, t->elem) : 0;
163         if (v >= 0)
164             av_tree_enumerate(t->child[0], opaque, cmp, enu);
165         if (v == 0)
166             enu(opaque, t->elem);
167         if (v <= 0)
168             av_tree_enumerate(t->child[1], opaque, cmp, enu);
169     }
170 }
171
172 #ifdef TEST
173
174 #include "common.h"
175 #include "lfg.h"
176
177 static int check(AVTreeNode *t)
178 {
179     if (t) {
180         int left  = check(t->child[0]);
181         int right = check(t->child[1]);
182
183         if (left > 999 || right > 999)
184             return 1000;
185         if (right - left != t->state)
186             return 1000;
187         if (t->state > 1 || t->state < -1)
188             return 1000;
189         return FFMAX(left, right) + 1;
190     }
191     return 0;
192 }
193
194 static void print(AVTreeNode *t, int depth)
195 {
196     int i;
197     for (i = 0; i < depth * 4; i++)
198         av_log(NULL, AV_LOG_ERROR, " ");
199     if (t) {
200         av_log(NULL, AV_LOG_ERROR, "Node %p %2d %p\n", t, t->state, t->elem);
201         print(t->child[0], depth + 1);
202         print(t->child[1], depth + 1);
203     } else
204         av_log(NULL, AV_LOG_ERROR, "NULL\n");
205 }
206
207 static int cmp(void *a, const void *b)
208 {
209     return (uint8_t *) a - (const uint8_t *) b;
210 }
211
212 int main(void)
213 {
214     int i;
215     AVTreeNode *root = NULL, *node = NULL;
216     AVLFG prng;
217
218     av_lfg_init(&prng, 1);
219
220     for (i = 0; i < 10000; i++) {
221         AVTreeNode *node2 = NULL;
222         intptr_t j = av_lfg_get(&prng) % 86294;
223         void *ret, *jj = (void *)(j + 1);
224
225         while (ret = av_tree_find(root, jj, cmp, NULL)) {
226             j  = av_lfg_get(&prng) % 86294;
227             jj = (void *)(j + 1);
228         }
229
230         if (check(root) > 999) {
231             av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i);
232             print(root, 0);
233             return 1;
234         }
235
236         if (!node)
237             node = av_tree_node_alloc();
238         if (!node) {
239             av_log(NULL, AV_LOG_ERROR, "Memory allocation failure.\n");
240             return 1;
241         }
242         av_tree_insert(&root, jj, cmp, &node);
243
244         while (ret = av_tree_find(root, jj, cmp, NULL)) {
245             j  = av_lfg_get(&prng) % 86294;
246             jj = (void *)(j + 1);
247         }
248
249         ret = av_tree_insert(&root, jj, cmp, &node2);
250         if (ret != jj)
251             av_tree_destroy(node2);
252         ret = av_tree_find(root, jj, cmp, NULL);
253         if (ret)
254             av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i);
255     }
256
257     av_tree_destroy(root);
258
259     return 0;
260 }
261 #endif