From: kostas <> Date: Sat, 28 Feb 1998 02:04:15 +0000 (+0000) Subject: realloc + bugfix etc X-Git-Tag: SQUID_3_0_PRE1~3953 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=da537f52b6ea9f15f68d4f6337c433fc6f0f025c;p=thirdparty%2Fsquid.git realloc + bugfix etc --- diff --git a/test-suite/Makefile b/test-suite/Makefile index 5a3739c2dc..9e8856b8c8 100644 --- a/test-suite/Makefile +++ b/test-suite/Makefile @@ -1,5 +1,5 @@ CC = gcc -CFLAGS = -g +CFLAGS = -O2 OBJS = membanger.o hash.o LIB = -lmylib diff --git a/test-suite/hash.c b/test-suite/hash.c index 164d2f16c2..0a61749ddd 100644 --- a/test-suite/hash.c +++ b/test-suite/hash.c @@ -1,6 +1,6 @@ /* - * $Id: hash.c,v 1.1 1998/02/27 07:17:22 kostas Exp $ + * $Id: hash.c,v 1.2 1998/02/27 19:04:15 kostas Exp $ * * DEBUG: section 0 Hash Tables * AUTHOR: Harvest Derived @@ -112,8 +112,8 @@ #include #include #include "hash.h" -static int hash_unlink(hash_table *, hash_link *, int); +extern void print_stats(); /* * hash_url() - Returns a well-distributed hash function for URLs. * The best way is to sum up the last half of the string. @@ -245,6 +245,11 @@ hash_insert(hash_table * hid, const char *k, void *item) assert(k != NULL); /* Add to the given hash table 'hid' */ new = calloc(1, sizeof(hash_link)); + if (!new) { + fprintf(stderr,"calloc failed!\n"); + print_stats(); + exit(1); + } new->item = item; new->key = (char *) k; i = hid->hash(k, hid->size); @@ -342,7 +347,7 @@ hash_delete(hash_table * hid, const char *key) * On success, it returns 0 and deletes the link; otherwise, * returns non-zero on error. */ -static int +int hash_unlink(hash_table * hid, hash_link * hl, int FreeLink) { hash_link *walker, *prev; diff --git a/test-suite/hash.h b/test-suite/hash.h index ab8c1daf28..4519945b92 100644 --- a/test-suite/hash.h +++ b/test-suite/hash.h @@ -27,6 +27,7 @@ extern hash_table *hash_create(HASHCMP *, int, HASHHASH *); extern void hash_insert(hash_table *, const char *, void *); extern int hash_delete(hash_table *, const char *); int hash_delete_link(hash_table *, hash_link *); +int hash_unlink(hash_table *,hash_link *, int); void hash_join(hash_table *, hash_link *); int hash_remove_link(hash_table *, hash_link *); hash_link *hash_lookup(hash_table *, const void *); diff --git a/test-suite/membanger.c b/test-suite/membanger.c index 144292d293..4db2154b12 100644 --- a/test-suite/membanger.c +++ b/test-suite/membanger.c @@ -22,13 +22,14 @@ int minchunk; HASHCMP ptrcmp; char mbuf[256]; char abuf[32]; -int *p; +char *p; int size; void *addr; int amt; int i; +int a; void *my_xmalloc(size_t); void *my_xcalloc(int, size_t); int my_xfree(void *); @@ -36,26 +37,30 @@ int my_xfree(void *); #define xmalloc my_xmalloc #define xcalloc my_xcalloc #define xfree my_xfree + int *size2id_array[2]; int size2id_len=0; int size2id_alloc=0; -int size2id(size_t); - -void init_stats(), print_stats(); - typedef struct { - char *orig_ptr; + char orig_ptr[32]; void *my_ptr; +#ifdef WITH_LIB + MemPool *pool; +#endif int id; int size; } memitem; struct { - int mallocs,frees,callocs; + int mallocs,frees,callocs,reallocs; } mstat; memitem *mi; +void size2id(size_t, memitem *); +void badformat(); +void init_stats(), print_stats(); + int ptrcmp(const void *a,const void *b) @@ -67,6 +72,7 @@ main(int argc,char **argv) { char c; extern char *optarg; + a=0; while ((c = getopt(argc, argv, "f:i:M:m:")) != -1) { switch (c) { case 'f': @@ -99,35 +105,72 @@ main(int argc,char **argv) init_stats(); while (fgets(mbuf, 256, fp)!=NULL) { +#if RUNTIME_STATSA + a++; + if (a%20000==0) print_stats(); +#endif + p=NULL; switch(mbuf[0]) { case 'm': /* malloc */ - sscanf(&mbuf[2],"%d:%s", &size, abuf); + p=strtok(&mbuf[2],":"); + if (!p) badformat(); + size=atoi(p); + p=strtok(NULL,"\n"); + if (!p) badformat(); mi=malloc(sizeof(memitem)); - mi->orig_ptr=(char *)strdup(abuf); + strcpy(mi->orig_ptr,p); mi->size=size; - mi->id=size2id(size); + size2id(size,mi); mi->my_ptr=(void *)xmalloc(size); hash_insert(mem_table, mi->orig_ptr, mi); mstat.mallocs++; break; case 'c': /* calloc */ - sscanf(&mbuf[2],"%d:%d:%s",&amt ,&size, abuf); + p=strtok(&mbuf[2],":"); + if (!p) badformat(); + amt=atoi(p); + p=strtok(NULL,":"); + if (!p) badformat(); + size=atoi(p); + p=strtok(NULL,"\n"); + if (!p) badformat(); mi=malloc(sizeof(memitem)); - mi->orig_ptr=(char *)strdup(abuf); - mi->id=size2id(size); + strcpy(mi->orig_ptr,p); + size2id(size,mi); mi->size=amt*size; mi->my_ptr=(void *)xmalloc(amt*size); hash_insert(mem_table, mi->orig_ptr, mi); mstat.callocs++; break; + case 'r': + p=strtok(&mbuf[2],":"); + if (!p) badformat(); + strcpy(abuf,p); + p=strtok(NULL,":"); + if (!p) badformat(); + mem_entry=hash_lookup(mem_table, p); + if (mem_entry==NULL) { + fprintf(stderr,"invalid realloc (%s)!\n",p); + break; + } + mi=(memitem *)mem_entry; + xfree(mi->my_ptr); + strcpy(mi->orig_ptr,abuf); + p=strtok(NULL,"\n"); + if (!p) badformat(); + mi->my_ptr=(char *)xmalloc(atoi(p)); + mstat.reallocs++; + break; case 'f': - sscanf(&mbuf[2],"%s", abuf); - mem_entry=hash_lookup(mem_table, abuf); + p=strtok(&mbuf[2],"\n"); + mem_entry=hash_lookup(mem_table, p); if (mem_entry==NULL) { - fprintf(stderr,"invalid free!\n"); + fprintf(stderr,"invalid free (%s)!\n",p); + break; } mi=(memitem *)mem_entry; xfree(mi->my_ptr); + hash_unlink(mem_table, mem_entry, 1); mstat.frees++; break; default: @@ -167,7 +210,8 @@ void print_stats() { getrusage(RUSAGE_SELF, &myusage); - printf("m/c/f=%d/%d/%d\n",mstat.mallocs,mstat.callocs,mstat.frees); + printf("m/c/f/r=%d/%d/%d/%d\n",mstat.mallocs,mstat.callocs, + mstat.frees, mstat.reallocs); printf("types : %d\n",size2id_len); printf("user time used : %d.%d\n", (int)myusage.ru_utime.tv_sec, (int)myusage.ru_utime.tv_usec); @@ -177,15 +221,23 @@ print_stats() printf("page faults : %d\n", (int)myusage.ru_majflt); } -int -size2id(size_t sz) +void +size2id(size_t sz,memitem *mi) { int j; for(j=0;jid=j; + return; } + + /* we have a different size, so we need a new pool */ + + mi->id=size2id_len; +#ifdef WITH_LIB + mi->pool = memPoolCreate(size2id_len, sz); +#endif size2id_len++; if (size2id_len==1) { size2id_alloc=100; @@ -197,8 +249,23 @@ size2id(size_t sz) size2id_array[0]=realloc(size2id_array[0],size2id_alloc*sizeof(int)); size2id_array[1]=realloc(size2id_array[1],size2id_alloc*sizeof(int)); } + size2id_array[0][size2id_len-1]=sz; - size2id_array[1][size2id_len-1]=0; - return size2id_len-1; + size2id_array[1][size2id_len-1]++; +} + +void +badformat() +{ + fprintf(stderr,"pummel.bad.format\n"); + exit(1); } +/* unused code, saved for parts */ +const char * +make_nam(int id, int size) +{ + const char *buf = malloc(30); /* argh */ + sprintf((char *)buf, "pl:%d/%d", id, size); + return buf; +}