From: Peep P. <so...@us...> - 2004-03-20 20:36:36
|
Update of /cvsroot/agd/server/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23277 Modified Files: array.c array.h compile.c compile_options.h Log Message: Removed preallocation of arrays. Index: compile_options.h =================================================================== RCS file: /cvsroot/agd/server/src/compile_options.h,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- compile_options.h 16 Mar 2004 20:12:59 -0000 1.10 +++ compile_options.h 20 Mar 2004 20:26:36 -0000 1.11 @@ -8,16 +8,6 @@ (DGD has 64k by default) */ #define MAX_STRING_LENGTH 1024 -/* The default length an array has. If an item - is added to the array and it doesn't have enough - memory, memory is realloc()'d. - Bigger number uses more memory, smaller number - calls realloc() more often if arrays are generally - long (and that can cause lots of memory fragmentation - which would eventually crash the driver with an "out of memory" - error. */ -#define LIST_DEFAULT_LENGTH 25 - /* The size of the interpreter's value stack. This determines how many local variables, function arguments and return values we can have at one time. Index: compile.c =================================================================== RCS file: /cvsroot/agd/server/src/compile.c,v retrieving revision 1.15 retrieving revision 1.16 diff -u -d -r1.15 -r1.16 --- compile.c 20 Mar 2004 19:16:57 -0000 1.15 +++ compile.c 20 Mar 2004 20:26:36 -0000 1.16 @@ -438,7 +438,10 @@ void end_block_level(void) { pop_scope(); - curr_block = array_pop(&block_history); + block_history.length--; + curr_block = block_history.data[block_history.length]; + /*block_history.data = realloc(block_history.data, + block_history.length);*/ /* if(locals_level > 0) locals_level--; if(!locals_level) { Index: array.c =================================================================== RCS file: /cvsroot/agd/server/src/array.c,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- array.c 20 Mar 2004 19:16:28 -0000 1.10 +++ array.c 20 Mar 2004 20:26:36 -0000 1.11 @@ -14,30 +14,9 @@ void init_array(array_t *a) { a->length = 0; - a->allocated = 0; a->data = NULL; } -/* Doubles the length of the allocated array until all of the array fits in. - User needs to set the length before using (array must also be inited). */ -void alloc_array(array_t *a) -{ - int needs_it; - - needs_it = 0; - while(a->length >= a->allocated) { - if(!a->allocated) - a->allocated = LIST_DEFAULT_LENGTH; - else - a->allocated *= 2; /* This could take a while if length&allocated diff is large */ - needs_it = 1; - } - - if(needs_it) { - a->data = realloc(a->data, sizeof(void *) * a->allocated); - } -} - void free_array(array_t *a, void (*freefun)(void *)) { int i; @@ -56,8 +35,7 @@ return; a->length++; - alloc_array(a); - + a->data = realloc(a->data, sizeof(void *) * a->length); a->data[a->length-1] = data; } @@ -65,33 +43,37 @@ { int i; +#ifdef DEBUG if(index > a->length) { printf("array_insert(): illegal index %d!\n", index); return; } +#endif a->length++; - alloc_array(a); + a->data = realloc(a->data, sizeof(void *) * a->length); for(i=a->length-1;i>index;i--) { a->data[i] = a->data[i-1]; } a->data[index] = data; } +#if 0 void *array_pop(array_t *a) { void *p; a->length--; - /* No reducing is done (length/allocated only goes bigger). */ p = a->data[a->length]; return p; } +#endif void array_remove(array_t *a, int index) { int i; if(index == a->length - 1) { a->length--; + a->data = realloc(a->data, sizeof(void *) * a->length); return; } @@ -99,7 +81,8 @@ a->data[i] = a->data[i+1]; } a->length--; - a->data[++i] = NULL; + a->data = realloc(a->data, sizeof(void *) * a->length); +/* a->data[++i] = NULL;*/ } void array_remove_by_data(array_t *a, void *data) @@ -120,9 +103,7 @@ p = type_xmalloc(array_t); p->length = a->length; - p->allocated = 0; - p->data = NULL; - alloc_array(p); + p->data = malloc(sizeof(void *) * p->length); for(i = 0; i < p->length; i++) if(copyfun) @@ -139,7 +120,7 @@ oldlen = a1->length; a1->length += a2->length; - alloc_array(a1); + a1->data = realloc(a1->data, sizeof(void *) * a1->length); for(i = oldlen; i < a1->length; i++) a1->data[i] = a2->data[i - oldlen]; Index: array.h =================================================================== RCS file: /cvsroot/agd/server/src/array.h,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- array.h 12 Mar 2004 08:40:10 -0000 1.5 +++ array.h 20 Mar 2004 20:26:36 -0000 1.6 @@ -3,7 +3,6 @@ typedef struct array_t { int length; - int allocated; void **data; } array_t; |