[17] | 1 | /* -*- mode: C; tab-width:8; c-basic-offset:8 -*- |
---|
| 2 | * vi:set ts=8: |
---|
| 3 | * |
---|
| 4 | * al_rctree.h |
---|
| 5 | * |
---|
| 6 | * Stuff related to the rctree data structure. |
---|
| 7 | */ |
---|
| 8 | #include "al_debug.h" |
---|
| 9 | #include "al_config.h" |
---|
| 10 | #include "al_rctree.h" |
---|
| 11 | #include "al_siteconfig.h" |
---|
| 12 | |
---|
| 13 | #include <stdio.h> |
---|
| 14 | #include <stdlib.h> |
---|
| 15 | #include <string.h> |
---|
| 16 | |
---|
| 17 | /* |
---|
| 18 | * AL_RcTreeNodes are used to help manage AL_rctrees. |
---|
| 19 | */ |
---|
| 20 | typedef struct _AL_RcTreeNode { |
---|
| 21 | ALuint size; |
---|
| 22 | ALuint freeslots; |
---|
| 23 | AL_rctree **data; |
---|
| 24 | } AL_RcTreeNode; |
---|
| 25 | |
---|
| 26 | /* |
---|
| 27 | * rlist is the data structure we use to manage AL_rcTreeNodes. |
---|
| 28 | */ |
---|
| 29 | static AL_RcTreeNode rlist = { 0, 0, NULL }; |
---|
| 30 | |
---|
| 31 | /* |
---|
| 32 | * rlist_add_rctree( AL_rctree *node ) |
---|
| 33 | * |
---|
| 34 | * Adds a reference to node to the rlist. |
---|
| 35 | */ |
---|
| 36 | static void rlist_add_rctree(AL_rctree *node); |
---|
| 37 | |
---|
| 38 | /* |
---|
| 39 | * rlist_delete_rctree( AL_rctree *node ) |
---|
| 40 | * |
---|
| 41 | * Removes a reference to node from the rlist. |
---|
| 42 | */ |
---|
| 43 | static void rlist_delete_rctree( AL_rctree *node ); |
---|
| 44 | |
---|
| 45 | /* |
---|
| 46 | * rlist_realloc( void ) |
---|
| 47 | * |
---|
| 48 | * Increases the size of rlist. |
---|
| 49 | */ |
---|
| 50 | static void rlist_realloc( void ); |
---|
| 51 | |
---|
| 52 | /* |
---|
| 53 | * _alRcTreeAlloc( void ) |
---|
| 54 | * |
---|
| 55 | * Creates, initializes and returns an AL_rctree. |
---|
| 56 | */ |
---|
| 57 | AL_rctree *_alRcTreeAlloc( void ) { |
---|
| 58 | AL_rctree *retval; |
---|
| 59 | |
---|
| 60 | retval = malloc( sizeof *retval ); |
---|
| 61 | if(retval == NULL) { |
---|
| 62 | /* _alDCSetError(AL_NO_MEMORY); */ |
---|
| 63 | return NULL; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | memset(retval, 0, sizeof *retval); |
---|
| 67 | |
---|
| 68 | rlist_add_rctree( retval ); |
---|
| 69 | |
---|
| 70 | retval->type = ALRC_INVALID; |
---|
| 71 | |
---|
| 72 | retval->data.proc = 0; |
---|
| 73 | retval->data.ccell.car = NULL; |
---|
| 74 | retval->data.ccell.cdr = NULL; |
---|
| 75 | |
---|
| 76 | return retval; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | /* |
---|
| 80 | * _alRcTreeFree( AL_rctree *node ) |
---|
| 81 | * |
---|
| 82 | * Finalizes and deallocates an AL_rctree node, removing the rlist reference |
---|
| 83 | * at the same time. |
---|
| 84 | */ |
---|
| 85 | void _alRcTreeFree( AL_rctree *node ) { |
---|
| 86 | if(node == NULL) { |
---|
| 87 | return; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | if( node->type == ALRC_CONSCELL ) { |
---|
| 91 | _alRcTreeFree( node->data.ccell.car ); |
---|
| 92 | _alRcTreeFree( node->data.ccell.cdr ); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | /* remove reference from our list */ |
---|
| 96 | rlist_delete_rctree( node ); |
---|
| 97 | |
---|
| 98 | free( node ); |
---|
| 99 | |
---|
| 100 | return; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | /* |
---|
| 104 | * _alRcTreeDestroyAll( void ) |
---|
| 105 | * |
---|
| 106 | * Deallocates any and all AL_rctree objects creates thus far. |
---|
| 107 | */ |
---|
| 108 | void _alRcTreeDestroyAll( void ) { |
---|
| 109 | ALuint i; |
---|
| 110 | |
---|
| 111 | for(i = 0; i < rlist.size; i++) { |
---|
| 112 | if(rlist.data[i] == NULL) { |
---|
| 113 | continue; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | free(rlist.data[i]); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | free(rlist.data); |
---|
| 120 | |
---|
| 121 | rlist.data = NULL; |
---|
| 122 | rlist.size = 0; |
---|
| 123 | rlist.freeslots = 0; |
---|
| 124 | |
---|
| 125 | return; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /* |
---|
| 129 | * rlist_add_rctree( AL_rctree *node ) |
---|
| 130 | * |
---|
| 131 | * Adds a reference to node to the rlist. |
---|
| 132 | */ |
---|
| 133 | static void rlist_add_rctree( AL_rctree *node ) { |
---|
| 134 | ALuint i; |
---|
| 135 | |
---|
| 136 | if(rlist.freeslots <= 0) { |
---|
| 137 | rlist_realloc(); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | for(i = 0; i < rlist.size; i++) { |
---|
| 141 | if(rlist.data[i] == NULL) { |
---|
| 142 | rlist.freeslots--; |
---|
| 143 | rlist.data[i] = node; |
---|
| 144 | |
---|
| 145 | return; |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | /* weird. Do something here. */ |
---|
| 150 | assert(0); |
---|
| 151 | |
---|
| 152 | return; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | /* |
---|
| 156 | * rlist_add_rctree( AL_rctree *node ) |
---|
| 157 | * |
---|
| 158 | * Adds a reference to node to the rlist. |
---|
| 159 | */ |
---|
| 160 | static void rlist_delete_rctree( AL_rctree *node ) { |
---|
| 161 | ALuint i; |
---|
| 162 | |
---|
| 163 | for(i = 0; i < rlist.size; i++) { |
---|
| 164 | if(rlist.data[i] == node) { |
---|
| 165 | rlist.freeslots++; |
---|
| 166 | rlist.data[i] = NULL; |
---|
| 167 | |
---|
| 168 | return; |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | /* not found */ |
---|
| 173 | |
---|
| 174 | return; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | /* |
---|
| 178 | * rlist_realloc( void ) |
---|
| 179 | * |
---|
| 180 | * Increases the size of rlist. |
---|
| 181 | */ |
---|
| 182 | static void rlist_realloc( void ) { |
---|
| 183 | ALuint newsize = rlist.size * 2 + 1; |
---|
| 184 | ALuint i; |
---|
| 185 | void *temp; |
---|
| 186 | |
---|
| 187 | temp = realloc( rlist.data, newsize * sizeof *rlist.data ); |
---|
| 188 | if(temp == NULL) { |
---|
| 189 | assert(0); |
---|
| 190 | return; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | rlist.data = temp; |
---|
| 194 | |
---|
| 195 | for(i = rlist.size; i < newsize; i++) { |
---|
| 196 | rlist.data[i] = NULL; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | rlist.freeslots += newsize - rlist.size; |
---|
| 200 | |
---|
| 201 | rlist.size = newsize; |
---|
| 202 | |
---|
| 203 | return; |
---|
| 204 | } |
---|