1.1 --- a/sourcepawn/compiler/sctracker.c Fri Mar 28 06:21:34 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,429 +0,0 @@
1.4 -#include <malloc.h>
1.5 -#include <string.h>
1.6 -#include <assert.h>
1.7 -#include "sc.h"
1.8 -#include "sctracker.h"
1.9 -
1.10 -memuse_list_t *heapusage = NULL;
1.11 -memuse_list_t *stackusage = NULL;
1.12 -funcenum_t *firstenum = NULL;
1.13 -funcenum_t *lastenum = NULL;
1.14 -pstruct_t *firststruct = NULL;
1.15 -pstruct_t *laststruct = NULL;
1.16 -
1.17 -structarg_t *pstructs_getarg(pstruct_t *pstruct, const char *member)
1.18 -{
1.19 - int i;
1.20 -
1.21 - for (i=0; i<pstruct->argcount; i++)
1.22 - {
1.23 - if (strcmp(pstruct->args[i]->name, member) == 0)
1.24 - {
1.25 - return pstruct->args[i];
1.26 - }
1.27 - }
1.28 -
1.29 - return NULL;
1.30 -}
1.31 -
1.32 -pstruct_t *pstructs_add(const char *name)
1.33 -{
1.34 - pstruct_t *p = (pstruct_t *)malloc(sizeof(pstruct_t));
1.35 -
1.36 - memset(p, 0, sizeof(pstruct_t));
1.37 - strcpy(p->name, name);
1.38 -
1.39 - if (!firststruct)
1.40 - {
1.41 - firststruct = p;
1.42 - laststruct = p;
1.43 - } else {
1.44 - laststruct->next = p;
1.45 - laststruct = p;
1.46 - }
1.47 -
1.48 - return p;
1.49 -}
1.50 -
1.51 -void pstructs_free()
1.52 -{
1.53 - pstruct_t *p, *next;
1.54 -
1.55 - p = firststruct;
1.56 - while (p)
1.57 - {
1.58 - while (p->argcount--)
1.59 - {
1.60 - free(p->args[p->argcount]);
1.61 - }
1.62 - free(p->args);
1.63 - next = p->next;
1.64 - free(p);
1.65 - p = next;
1.66 - }
1.67 - firststruct = NULL;
1.68 - laststruct = NULL;
1.69 -}
1.70 -
1.71 -pstruct_t *pstructs_find(const char *name)
1.72 -{
1.73 - pstruct_t *p = firststruct;
1.74 -
1.75 - while (p)
1.76 - {
1.77 - if (strcmp(p->name, name) == 0)
1.78 - {
1.79 - return p;
1.80 - }
1.81 - p = p->next;
1.82 - }
1.83 -
1.84 - return NULL;
1.85 -}
1.86 -
1.87 -structarg_t *pstructs_addarg(pstruct_t *pstruct, const structarg_t *arg)
1.88 -{
1.89 - structarg_t *newarg;
1.90 - int i;
1.91 -
1.92 - for (i=0; i<pstruct->argcount; i++)
1.93 - {
1.94 - if (strcmp(pstruct->args[i]->name, arg->name) == 0)
1.95 - {
1.96 - /* Don't allow dup names */
1.97 - return NULL;
1.98 - }
1.99 - }
1.100 -
1.101 - newarg = (structarg_t *)malloc(sizeof(structarg_t));
1.102 -
1.103 - memcpy(newarg, arg, sizeof(structarg_t));
1.104 -
1.105 - if (pstruct->argcount == 0)
1.106 - {
1.107 - pstruct->args = (structarg_t **)malloc(sizeof(structarg_t *) * 1);
1.108 - } else {
1.109 - pstruct->args = (structarg_t **)realloc(
1.110 - pstruct->args,
1.111 - sizeof(structarg_t *) * (pstruct->argcount + 1));
1.112 - }
1.113 -
1.114 - newarg->offs = pstruct->argcount * sizeof(cell);
1.115 - newarg->index = pstruct->argcount;
1.116 - pstruct->args[pstruct->argcount++] = newarg;
1.117 -
1.118 - return newarg;
1.119 -}
1.120 -
1.121 -void funcenums_free()
1.122 -{
1.123 - funcenum_t *e, *next;
1.124 -
1.125 - e = firstenum;
1.126 - while (e)
1.127 - {
1.128 - functag_t *tag, *nexttag;
1.129 - tag = e->first;
1.130 - while (tag)
1.131 - {
1.132 - nexttag = tag->next;
1.133 - free(tag);
1.134 - tag = nexttag;
1.135 - }
1.136 - next = e->next;
1.137 - free(e);
1.138 - e = next;
1.139 - }
1.140 -
1.141 - firstenum = NULL;
1.142 - lastenum = NULL;
1.143 -}
1.144 -
1.145 -funcenum_t *funcenums_find_byval(int value)
1.146 -{
1.147 - funcenum_t *e = firstenum;
1.148 -
1.149 - while (e)
1.150 - {
1.151 - if (e->value == value)
1.152 - {
1.153 - return e;
1.154 - }
1.155 - e = e->next;
1.156 - }
1.157 -
1.158 - return NULL;
1.159 -}
1.160 -
1.161 -funcenum_t *funcenums_add(const char *name)
1.162 -{
1.163 - funcenum_t *e = (funcenum_t *)malloc(sizeof(funcenum_t));
1.164 -
1.165 - memset(e, 0, sizeof(funcenum_t));
1.166 -
1.167 - if (firstenum == NULL)
1.168 - {
1.169 - firstenum = e;
1.170 - lastenum = e;
1.171 - } else {
1.172 - lastenum->next = e;
1.173 - lastenum = e;
1.174 - }
1.175 -
1.176 - strcpy(e->name, name);
1.177 - e->value = pc_addfunctag((char *)name);
1.178 -
1.179 - return e;
1.180 -}
1.181 -
1.182 -functag_t *functags_add(funcenum_t *en, functag_t *src)
1.183 -{
1.184 - functag_t *t = (functag_t *)malloc(sizeof(functag_t));
1.185 -
1.186 - memcpy(t, src, sizeof(functag_t));
1.187 -
1.188 - t->next = NULL;
1.189 -
1.190 - if (en->first == NULL)
1.191 - {
1.192 - en->first = t;
1.193 - en->last = t;
1.194 - } else {
1.195 - en->last->next = t;
1.196 - en->last = t;
1.197 - }
1.198 -
1.199 - return t;
1.200 -}
1.201 -
1.202 -/**
1.203 - * Creates a new mem usage tracker entry
1.204 - */
1.205 -void _push_memlist(memuse_list_t **head)
1.206 -{
1.207 - memuse_list_t *newlist = (memuse_list_t *)malloc(sizeof(memuse_list_t));
1.208 - if (*head != NULL)
1.209 - {
1.210 - newlist->list_id = (*head)->list_id + 1;
1.211 - } else {
1.212 - newlist->list_id = 0;
1.213 - }
1.214 - newlist->prev = *head;
1.215 - newlist->head = NULL;
1.216 - *head = newlist;
1.217 -}
1.218 -
1.219 -/**
1.220 - * Pops a heap list but does not free it.
1.221 - */
1.222 -memuse_list_t *_pop_save_memlist(memuse_list_t **head)
1.223 -{
1.224 - memuse_list_t *oldlist = *head;
1.225 - *head = (*head)->prev;
1.226 - return oldlist;
1.227 -}
1.228 -
1.229 -/**
1.230 - * Marks a memory usage on a memory list
1.231 - */
1.232 -int _mark_memlist(memuse_list_t *head, int type, int size)
1.233 -{
1.234 - memuse_t *use;
1.235 - if (type==MEMUSE_STATIC && size==0)
1.236 - {
1.237 - return 0;
1.238 - }
1.239 - use=head->head;
1.240 - if (use && (type==MEMUSE_STATIC)
1.241 - && (use->type == type))
1.242 - {
1.243 - use->size += size;
1.244 - } else {
1.245 - use=(memuse_t *)malloc(sizeof(memuse_t));
1.246 - use->type=type;
1.247 - use->size=size;
1.248 - use->prev=head->head;
1.249 - head->head=use;
1.250 - }
1.251 - return size;
1.252 -}
1.253 -
1.254 -void _reset_memlist(memuse_list_t **head)
1.255 -{
1.256 - memuse_list_t *curlist = *head;
1.257 - memuse_list_t *tmplist;
1.258 - while (curlist) {
1.259 - memuse_t *curuse = curlist->head;
1.260 - memuse_t *tmpuse;
1.261 - while (curuse) {
1.262 - tmpuse = curuse->prev;
1.263 - free(curuse);
1.264 - curuse = tmpuse;
1.265 - }
1.266 - tmplist = curlist->prev;
1.267 - free(curlist);
1.268 - curlist = tmplist;
1.269 - }
1.270 - *head = NULL;
1.271 -}
1.272 -
1.273 -
1.274 -/**
1.275 - * Wrapper for pushing the heap list
1.276 - */
1.277 -void pushheaplist()
1.278 -{
1.279 - _push_memlist(&heapusage);
1.280 -}
1.281 -
1.282 -/**
1.283 - * Wrapper for popping and saving the heap list
1.284 - */
1.285 -memuse_list_t *popsaveheaplist()
1.286 -{
1.287 - return _pop_save_memlist(&heapusage);
1.288 -}
1.289 -
1.290 -/**
1.291 - * Wrapper for marking the heap
1.292 - */
1.293 -int markheap(int type, int size)
1.294 -{
1.295 - return _mark_memlist(heapusage, type, size);
1.296 -}
1.297 -
1.298 -/**
1.299 - * Wrapper for pushing the stack list
1.300 - */
1.301 -void pushstacklist()
1.302 -{
1.303 - _push_memlist(&stackusage);
1.304 -}
1.305 -
1.306 -/**
1.307 - * Wrapper for marking the stack
1.308 - */
1.309 -int markstack(int type, int size)
1.310 -{
1.311 - return _mark_memlist(stackusage, type, size);
1.312 -}
1.313 -
1.314 -/**
1.315 - * Generates code to free all heap allocations on a tracker
1.316 - */
1.317 -void _heap_freeusage(memuse_list_t *heap, int dofree)
1.318 -{
1.319 - memuse_t *cur=heap->head;
1.320 - memuse_t *tmp;
1.321 - while (cur)
1.322 - {
1.323 - if (cur->type == MEMUSE_STATIC)
1.324 - {
1.325 - modheap((-1)*cur->size*sizeof(cell));
1.326 - } else {
1.327 - modheap_i();
1.328 - }
1.329 - if (dofree)
1.330 - {
1.331 - tmp=cur->prev;
1.332 - free(cur);
1.333 - cur=tmp;
1.334 - } else {
1.335 - cur=cur->prev;
1.336 - }
1.337 - }
1.338 - if (dofree)
1.339 - {
1.340 - heap->head=NULL;
1.341 - }
1.342 -}
1.343 -
1.344 -void _stack_genusage(memuse_list_t *stack, int dofree)
1.345 -{
1.346 - memuse_t *cur = stack->head;
1.347 - memuse_t *tmp;
1.348 - while (cur)
1.349 - {
1.350 - if (cur->type == MEMUSE_DYNAMIC)
1.351 - {
1.352 - /* no idea yet */
1.353 - assert(0);
1.354 - } else {
1.355 - modstk(cur->size * sizeof(cell));
1.356 - }
1.357 - if (dofree)
1.358 - {
1.359 - tmp = cur->prev;
1.360 - free(cur);
1.361 - cur = tmp;
1.362 - } else {
1.363 - cur = cur->prev;
1.364 - }
1.365 - }
1.366 - if (dofree)
1.367 - {
1.368 - stack->head = NULL;
1.369 - }
1.370 -}
1.371 -
1.372 -/**
1.373 - * Pops a heap list and frees it.
1.374 - */
1.375 -void popheaplist()
1.376 -{
1.377 - memuse_list_t *oldlist;
1.378 - assert(heapusage!=NULL);
1.379 -
1.380 - _heap_freeusage(heapusage, 1);
1.381 - assert(heapusage->head==NULL);
1.382 -
1.383 - oldlist=heapusage->prev;
1.384 - free(heapusage);
1.385 - heapusage=oldlist;
1.386 -}
1.387 -
1.388 -void genstackfree(int stop_id)
1.389 -{
1.390 - memuse_list_t *curlist = stackusage;
1.391 - while (curlist && curlist->list_id > stop_id)
1.392 - {
1.393 - _stack_genusage(curlist, 0);
1.394 - curlist = curlist->prev;
1.395 - }
1.396 -}
1.397 -
1.398 -void genheapfree(int stop_id)
1.399 -{
1.400 - memuse_list_t *curlist = heapusage;
1.401 - while (curlist && curlist->list_id > stop_id)
1.402 - {
1.403 - _heap_freeusage(curlist, 0);
1.404 - curlist = curlist->prev;
1.405 - }
1.406 -}
1.407 -
1.408 -void popstacklist(int codegen)
1.409 -{
1.410 - memuse_list_t *oldlist;
1.411 - assert(stackusage != NULL);
1.412 -
1.413 - if (codegen)
1.414 - {
1.415 - _stack_genusage(stackusage, 1);
1.416 - assert(stackusage->head==NULL);
1.417 - }
1.418 -
1.419 - oldlist = stackusage->prev;
1.420 - free(stackusage);
1.421 - stackusage = oldlist;
1.422 -}
1.423 -
1.424 -void resetstacklist()
1.425 -{
1.426 - _reset_memlist(&stackusage);
1.427 -}
1.428 -
1.429 -void resetheaplist()
1.430 -{
1.431 - _reset_memlist(&heapusage);
1.432 -}