]> jfr.im git - irc/quakenet/newserv.git/blob - patricia/patricia_alloc.c
33d4ac506bfd5837a1d84ab4a5d3b73f8280f4fb
[irc/quakenet/newserv.git] / patricia / patricia_alloc.c
1
2 #include <stdlib.h>
3 #include "patricia.h"
4 #include <assert.h>
5 #include "../core/nsmalloc.h"
6
7 #define ALLOCUNIT 100
8
9 patricia_node_t *node_freelist;
10 union prefixes *prefix_freelist;
11
12 prefix_t *newprefix() {
13 union prefixes *prefixes = prefix_freelist;
14 int i;
15
16 if (prefixes==NULL) {
17 prefixes=(union prefixes *)nsmalloc(POOL_PATRICIA,ALLOCUNIT*sizeof(prefix_t));
18
19 for (i=0;i<(ALLOCUNIT-1);i++) {
20 prefixes[i].next=&(prefixes[i+1]);
21 }
22 prefixes[ALLOCUNIT-1].next=NULL;
23 }
24
25 prefix_freelist = prefixes->next;
26 return &(prefixes->prefix);
27
28 }
29
30 void freeprefix (prefix_t *prefix) {
31 union prefixes *ups = (union prefixes *)prefix;
32 ups->next = prefix_freelist;
33 prefix_freelist = ups;
34 }
35
36
37 patricia_node_t *newnode() {
38 int i;
39 patricia_node_t *node;
40
41 if( node_freelist==NULL ) {
42 node_freelist=(patricia_node_t *)nsmalloc(POOL_PATRICIA,ALLOCUNIT*sizeof(patricia_node_t));
43
44 for (i=0;i<(ALLOCUNIT-1);i++) {
45 node_freelist[i].parent=(patricia_node_t *)&(node_freelist[i+1]);
46 }
47 node_freelist[ALLOCUNIT-1].parent=NULL;
48 }
49
50 node=node_freelist;
51 node_freelist=(patricia_node_t *)node->parent;
52
53 return node;
54 }
55
56 void freenode (patricia_node_t *node) {
57 node->parent=(patricia_node_t *)node_freelist;
58 node_freelist=node;
59 }
60
61