diff options
author | Peter D'Hoye <peter.dhoye@gmail.com> | 2009-07-23 21:37:35 +0000 |
---|---|---|
committer | Peter D'Hoye <peter.dhoye@gmail.com> | 2009-07-23 21:37:35 +0000 |
commit | 840cd1069292e3f29d18e57f2274ec1e979f858b (patch) | |
tree | 0ee1d43fa3863de53c99432dc32001e625703d1c /apps/plugins/pdbox/dbestfit-3.3/bysize.c | |
parent | 0d9b7ec73e71188632a4fd584dfd745aeb7571b3 (diff) | |
download | rockbox-840cd1069292e3f29d18e57f2274ec1e979f858b.tar.gz rockbox-840cd1069292e3f29d18e57f2274ec1e979f858b.zip |
Another pdbox patch by Wincent Balin (FS #10416): switch to using TLSF as memory allocator. Probably the last patch I commit for him, next changes are for him :)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@22017 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/pdbox/dbestfit-3.3/bysize.c')
-rw-r--r-- | apps/plugins/pdbox/dbestfit-3.3/bysize.c | 428 |
1 files changed, 0 insertions, 428 deletions
diff --git a/apps/plugins/pdbox/dbestfit-3.3/bysize.c b/apps/plugins/pdbox/dbestfit-3.3/bysize.c deleted file mode 100644 index 8728e247b9..0000000000 --- a/apps/plugins/pdbox/dbestfit-3.3/bysize.c +++ /dev/null | |||
@@ -1,428 +0,0 @@ | |||
1 | /***************************************************************************** | ||
2 | * | ||
3 | * Size-sorted list/tree functions. | ||
4 | * | ||
5 | * Author: Daniel Stenberg | ||
6 | * Date: March 7, 1997 | ||
7 | * Version: 2.0 | ||
8 | * Email: Daniel.Stenberg@sth.frontec.se | ||
9 | * | ||
10 | * | ||
11 | * v2.0 | ||
12 | * - Added SPLAY TREE functionality. | ||
13 | * | ||
14 | * Adds and removes CHUNKS from a list or tree. | ||
15 | * | ||
16 | ****************************************************************************/ | ||
17 | |||
18 | #include <stdio.h> | ||
19 | #include <stdlib.h> | ||
20 | |||
21 | #define SPLAY /* we use the splay version as that is much faster */ | ||
22 | |||
23 | #ifndef TRUE | ||
24 | #define TRUE 1 | ||
25 | #endif | ||
26 | #ifndef FALSE | ||
27 | #define FALSE 0 | ||
28 | #endif | ||
29 | |||
30 | #ifndef SPLAY /* these routines are for the non-splay version */ | ||
31 | |||
32 | struct ChunkInfo { | ||
33 | struct ChunkInfo *larger; | ||
34 | struct ChunkInfo *smaller; | ||
35 | size_t size; | ||
36 | }; | ||
37 | |||
38 | /* the CHUNK list anchor */ | ||
39 | struct ChunkInfo *chunkHead=NULL; | ||
40 | |||
41 | /*********************************************************************** | ||
42 | |||
43 | findchunkbysize() | ||
44 | |||
45 | Find the chunk that is smaller than the input size. Returns | ||
46 | NULL if none is. | ||
47 | |||
48 | **********************************************************************/ | ||
49 | |||
50 | static struct ChunkInfo *findchunkbysize(size_t size) | ||
51 | { | ||
52 | struct ChunkInfo *test = chunkHead; | ||
53 | struct ChunkInfo *smaller = NULL; | ||
54 | while(test && (test->size < size)) { | ||
55 | smaller = test; | ||
56 | test = test->larger; | ||
57 | } | ||
58 | return smaller; | ||
59 | } | ||
60 | |||
61 | /*********************************************************************** | ||
62 | |||
63 | remove_chunksize() | ||
64 | |||
65 | Remove the chunk from the size-sorted list. | ||
66 | ***********************************************************************/ | ||
67 | |||
68 | void remove_chunksize(void *data) | ||
69 | { | ||
70 | struct ChunkInfo *chunk = (struct ChunkInfo *)data; | ||
71 | if(chunk->smaller) | ||
72 | chunk->smaller->larger = chunk->larger; | ||
73 | else { | ||
74 | /* if this has no smaller, this is the head */ | ||
75 | chunkHead = chunk->larger; /* new head */ | ||
76 | } | ||
77 | if(chunk->larger) | ||
78 | chunk->larger->smaller = chunk->smaller; | ||
79 | } | ||
80 | |||
81 | void insert_bysize(char *data, size_t size) | ||
82 | { | ||
83 | struct ChunkInfo *newchunk = (struct ChunkInfo *)data; | ||
84 | struct ChunkInfo *chunk = findchunkbysize ( size ); | ||
85 | |||
86 | newchunk->size = size; | ||
87 | |||
88 | if(chunk) { | ||
89 | /* 'chunk' is smaller than size, append the new chunk ahead of this */ | ||
90 | newchunk->smaller = chunk; | ||
91 | newchunk->larger = chunk->larger; | ||
92 | if(chunk->larger) | ||
93 | chunk->larger->smaller = newchunk; | ||
94 | chunk->larger = newchunk; | ||
95 | } | ||
96 | else { | ||
97 | /* smallest CHUNK around, append first in the list */ | ||
98 | newchunk->larger = chunkHead; | ||
99 | newchunk->smaller = NULL; | ||
100 | |||
101 | if(chunkHead) | ||
102 | chunkHead->smaller = newchunk; | ||
103 | chunkHead = newchunk; | ||
104 | } | ||
105 | } | ||
106 | |||
107 | char *obtainbysize( size_t size) | ||
108 | { | ||
109 | struct ChunkInfo *chunk = findchunkbysize( size ); | ||
110 | |||
111 | if(!chunk) { | ||
112 | if(size <= (chunkHead->size)) | ||
113 | /* there is no smaller CHUNK, use the first one (if we fit within that) | ||
114 | */ | ||
115 | chunk = chunkHead; | ||
116 | } | ||
117 | else | ||
118 | /* we're on the last CHUNK that is smaller than requested, step onto | ||
119 | the bigger one */ | ||
120 | chunk = chunk->larger; | ||
121 | |||
122 | if(chunk) { | ||
123 | remove_chunksize( chunk ); /* unlink size-wise */ | ||
124 | return (char *)chunk; | ||
125 | } | ||
126 | else | ||
127 | return NULL; | ||
128 | } | ||
129 | |||
130 | void print_sizes(void) | ||
131 | { | ||
132 | struct ChunkInfo *chunk = chunkHead; | ||
133 | printf("List of CHUNKS (in size order):\n"); | ||
134 | #if 1 | ||
135 | while(chunk) { | ||
136 | printf(" START %p END %p SIZE %d\n", | ||
137 | chunk, (char *)chunk+chunk->size, chunk->size); | ||
138 | chunk = chunk->larger; | ||
139 | } | ||
140 | #endif | ||
141 | printf("End of CHUNKS:\n"); | ||
142 | } | ||
143 | |||
144 | #else /* Here follows all routines dealing with the SPLAY TREES */ | ||
145 | |||
146 | typedef struct tree_node Tree; | ||
147 | struct tree_node { | ||
148 | Tree *smaller; /* smaller node */ | ||
149 | Tree *larger; /* larger node */ | ||
150 | Tree *same; /* points to a node with identical key */ | ||
151 | int key; /* the "sort" key */ | ||
152 | }; | ||
153 | |||
154 | Tree *chunkHead = NULL; /* the root */ | ||
155 | |||
156 | #define compare(i,j) ((i)-(j)) | ||
157 | |||
158 | /* Set this to a key value that will *NEVER* appear otherwise */ | ||
159 | #define KEY_NOTUSED -1 | ||
160 | |||
161 | /* | ||
162 | * Splay using the key i (which may or may not be in the tree.) The starting | ||
163 | * root is t. Weight fields are maintained. | ||
164 | */ | ||
165 | Tree * splay (int i, Tree *t) | ||
166 | { | ||
167 | Tree N, *l, *r, *y; | ||
168 | int comp; | ||
169 | |||
170 | if (t == NULL) | ||
171 | return t; | ||
172 | N.smaller = N.larger = NULL; | ||
173 | l = r = &N; | ||
174 | |||
175 | for (;;) { | ||
176 | comp = compare(i, t->key); | ||
177 | if (comp < 0) { | ||
178 | if (t->smaller == NULL) | ||
179 | break; | ||
180 | if (compare(i, t->smaller->key) < 0) { | ||
181 | y = t->smaller; /* rotate smaller */ | ||
182 | t->smaller = y->larger; | ||
183 | y->larger = t; | ||
184 | |||
185 | t = y; | ||
186 | if (t->smaller == NULL) | ||
187 | break; | ||
188 | } | ||
189 | r->smaller = t; /* link smaller */ | ||
190 | r = t; | ||
191 | t = t->smaller; | ||
192 | } | ||
193 | else if (comp > 0) { | ||
194 | if (t->larger == NULL) | ||
195 | break; | ||
196 | if (compare(i, t->larger->key) > 0) { | ||
197 | y = t->larger; /* rotate larger */ | ||
198 | t->larger = y->smaller; | ||
199 | y->smaller = t; | ||
200 | t = y; | ||
201 | if (t->larger == NULL) | ||
202 | break; | ||
203 | } | ||
204 | l->larger = t; /* link larger */ | ||
205 | l = t; | ||
206 | t = t->larger; | ||
207 | } | ||
208 | else { | ||
209 | break; | ||
210 | } | ||
211 | } | ||
212 | |||
213 | l->larger = r->smaller = NULL; | ||
214 | |||
215 | l->larger = t->smaller; /* assemble */ | ||
216 | r->smaller = t->larger; | ||
217 | t->smaller = N.larger; | ||
218 | t->larger = N.smaller; | ||
219 | |||
220 | return t; | ||
221 | } | ||
222 | |||
223 | /* Insert key i into the tree t. Return a pointer to the resulting tree or | ||
224 | NULL if something went wrong. */ | ||
225 | Tree *insert(int i, Tree *t, Tree *new) | ||
226 | { | ||
227 | if (new == NULL) { | ||
228 | return t; | ||
229 | } | ||
230 | |||
231 | if (t != NULL) { | ||
232 | t = splay(i,t); | ||
233 | if (compare(i, t->key)==0) { | ||
234 | /* it already exists one of this size */ | ||
235 | |||
236 | new->same = t; | ||
237 | new->key = i; | ||
238 | new->smaller = t->smaller; | ||
239 | new->larger = t->larger; | ||
240 | |||
241 | t->smaller = new; | ||
242 | t->key = KEY_NOTUSED; | ||
243 | |||
244 | return new; /* new root node */ | ||
245 | } | ||
246 | } | ||
247 | |||
248 | if (t == NULL) { | ||
249 | new->smaller = new->larger = NULL; | ||
250 | } | ||
251 | else if (compare(i, t->key) < 0) { | ||
252 | new->smaller = t->smaller; | ||
253 | new->larger = t; | ||
254 | t->smaller = NULL; | ||
255 | } | ||
256 | else { | ||
257 | new->larger = t->larger; | ||
258 | new->smaller = t; | ||
259 | t->larger = NULL; | ||
260 | } | ||
261 | new->key = i; | ||
262 | |||
263 | new->same = NULL; /* no identical node (yet) */ | ||
264 | |||
265 | return new; | ||
266 | } | ||
267 | |||
268 | /* Finds and deletes the best-fit node from the tree. Return a pointer to the | ||
269 | resulting tree. best-fit means the smallest node that fits the requested | ||
270 | size. */ | ||
271 | Tree *removebestfit(int i, Tree *t, Tree **removed) | ||
272 | { | ||
273 | Tree *x; | ||
274 | |||
275 | if (t==NULL) | ||
276 | return NULL; | ||
277 | t = splay(i,t); | ||
278 | if(compare(i, t->key) > 0) { | ||
279 | /* too small node, try the larger chain */ | ||
280 | if(t->larger) | ||
281 | t=splay(t->larger->key, t); | ||
282 | else { | ||
283 | /* fail */ | ||
284 | *removed = NULL; | ||
285 | return t; | ||
286 | } | ||
287 | } | ||
288 | |||
289 | if (compare(i, t->key) <= 0) { /* found it */ | ||
290 | |||
291 | /* FIRST! Check if there is a list with identical sizes */ | ||
292 | x = t->same; | ||
293 | if(x) { | ||
294 | /* there is, pick one from the list */ | ||
295 | |||
296 | /* 'x' is the new root node */ | ||
297 | |||
298 | x->key = t->key; | ||
299 | x->larger = t->larger; | ||
300 | x->smaller = t->smaller; | ||
301 | *removed = t; | ||
302 | return x; /* new root */ | ||
303 | } | ||
304 | |||
305 | if (t->smaller == NULL) { | ||
306 | x = t->larger; | ||
307 | } | ||
308 | else { | ||
309 | x = splay(i, t->smaller); | ||
310 | x->larger = t->larger; | ||
311 | } | ||
312 | *removed = t; | ||
313 | |||
314 | return x; | ||
315 | } | ||
316 | else { | ||
317 | *removed = NULL; /* no match */ | ||
318 | return t; /* It wasn't there */ | ||
319 | } | ||
320 | } | ||
321 | |||
322 | |||
323 | /* Deletes the node we point out from the tree if it's there. Return a pointer | ||
324 | to the resulting tree. */ | ||
325 | Tree *removebyaddr(Tree *t, Tree *remove) | ||
326 | { | ||
327 | Tree *x; | ||
328 | |||
329 | if (!t || !remove) | ||
330 | return NULL; | ||
331 | |||
332 | if(KEY_NOTUSED == remove->key) { | ||
333 | /* just unlink ourselves nice and quickly: */ | ||
334 | remove->smaller->same = remove->same; | ||
335 | if(remove->same) | ||
336 | remove->same->smaller = remove->smaller; | ||
337 | /* voila, we're done! */ | ||
338 | return t; | ||
339 | } | ||
340 | |||
341 | t = splay(remove->key,t); | ||
342 | |||
343 | /* Check if there is a list with identical sizes */ | ||
344 | |||
345 | x = t->same; | ||
346 | if(x) { | ||
347 | /* 'x' is the new root node */ | ||
348 | |||
349 | x->key = t->key; | ||
350 | x->larger = t->larger; | ||
351 | x->smaller = t->smaller; | ||
352 | |||
353 | return x; /* new root */ | ||
354 | } | ||
355 | |||
356 | /* Remove the actualy root node: */ | ||
357 | |||
358 | if (t->smaller == NULL) { | ||
359 | x = t->larger; | ||
360 | } | ||
361 | else { | ||
362 | x = splay(remove->key, t->smaller); | ||
363 | x->larger = t->larger; | ||
364 | } | ||
365 | |||
366 | return x; | ||
367 | } | ||
368 | |||
369 | #ifdef DEBUG | ||
370 | int printtree(Tree * t, int d, char output) | ||
371 | { | ||
372 | int distance=0; | ||
373 | Tree *node; | ||
374 | int i; | ||
375 | if (t == NULL) | ||
376 | return 0; | ||
377 | distance += printtree(t->larger, d+1, output); | ||
378 | for (i=0; i<d; i++) | ||
379 | if(output) | ||
380 | printf(" "); | ||
381 | |||
382 | if(output) { | ||
383 | printf("%d[%d]", t->key, i); | ||
384 | } | ||
385 | |||
386 | for(node = t->same; node; node = node->same) { | ||
387 | distance += i; /* this has the same "virtual" distance */ | ||
388 | |||
389 | if(output) | ||
390 | printf(" [+]"); | ||
391 | } | ||
392 | if(output) | ||
393 | puts(""); | ||
394 | |||
395 | distance += i; | ||
396 | distance += printtree(t->smaller, d+1, output); | ||
397 | return distance; | ||
398 | } | ||
399 | #endif /* DEBUG */ | ||
400 | |||
401 | /* Here follow the look-alike interface so that the tree-function names are | ||
402 | the same as the list-ones to enable easy interchange */ | ||
403 | |||
404 | void remove_chunksize(void *data) | ||
405 | { | ||
406 | chunkHead = removebyaddr(chunkHead, data); | ||
407 | } | ||
408 | |||
409 | void insert_bysize(char *data, size_t size) | ||
410 | { | ||
411 | chunkHead = insert(size, chunkHead, (Tree *)data); | ||
412 | } | ||
413 | |||
414 | char *obtainbysize( size_t size) | ||
415 | { | ||
416 | Tree *receive; | ||
417 | chunkHead = removebestfit(size, chunkHead, &receive); | ||
418 | return (char *)receive; | ||
419 | } | ||
420 | |||
421 | #ifdef DEBUG | ||
422 | void print_sizes(void) | ||
423 | { | ||
424 | printtree(chunkHead, 0, 1); | ||
425 | } | ||
426 | #endif /* DEBUG */ | ||
427 | |||
428 | #endif | ||