static hash_link *mem_entry;
struct rusage myusage;
+#ifdef WITH_LIB
+#include "Mem.h"
+#include <assert.h>
+extern void sizeToPoolInit();
+extern MemPool *sizeToPool(size_t size);
+#endif
+extern char *malloc_options;
+void my_free(char *, int, void *);
+
FILE *fp;
char *fn;
int initsiz;
int i;
int a;
+int run_stats=0;
void *my_xmalloc(size_t);
void *my_xcalloc(int, size_t);
int my_xfree(void *);
#ifdef WITH_LIB
MemPool *pool;
#endif
- int id;
int size;
} memitem;
void size2id(size_t, memitem *);
void badformat();
void init_stats(), print_stats();
-
+void my_hash_insert(hash_table *h, const char *k, memitem *item);
+static void *xmemAlloc(memitem *item);
+static void xmemFree(memitem *item);
int
ptrcmp(const void *a,const void *b)
{
- return (a==b);
+ return (strcmp(a,b));
}
main(int argc,char **argv)
{
char c;
extern char *optarg;
+ malloc_options = "A";
a=0;
- while ((c = getopt(argc, argv, "f:i:M:m:")) != -1) {
+ while ((c = getopt(argc, argv, "f:i:M:l:m:r:N")) != -1) {
switch (c) {
+ case 'N':
+ mem_pools_on=0;
+ break;
+ case 'r':
+ run_stats=atoi(optarg);
+ break;
case 'f':
fn=strdup(optarg);
fp=fopen(fn,"r");
case 'i':
initsiz=atoi(optarg);
break;
+ case 'l':
+ mem_max_size = atoi(optarg)*1024*1024;
+ break;
case 'M':
maxsiz=atoi(optarg);
break;
argv[0], getenv("USER"), argv[0]);
exit(1);
}
+#ifdef WITH_LIB
+ sizeToPoolInit();
+#endif
mem_table = hash_create(ptrcmp, 229, hash4); /* small hash table */
init_stats();
-
while (fgets(mbuf, 256, fp)!=NULL) {
-#if RUNTIME_STATSA
- a++;
- if (a%20000==0) print_stats();
-#endif
+ if (run_stats>0 && (++a)%run_stats==0) print_stats();
p=NULL;
switch(mbuf[0]) {
case 'm': /* malloc */
strcpy(mi->orig_ptr,p);
mi->size=size;
size2id(size,mi);
- mi->my_ptr=(void *)xmalloc(size);
- hash_insert(mem_table, mi->orig_ptr, mi);
+ mi->my_ptr = xmemAlloc(mi); /* (void *)xmalloc(size); */
+ assert(mi->my_ptr);
+ my_hash_insert(mem_table, mi->orig_ptr, mi);
mstat.mallocs++;
break;
case 'c': /* calloc */
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);
+ mi->my_ptr= xmemAlloc(mi); /*(void *)xmalloc(amt*size);*/
+ assert(mi->my_ptr);
+ my_hash_insert(mem_table, mi->orig_ptr, mi);
mstat.callocs++;
break;
case 'r':
fprintf(stderr,"invalid realloc (%s)!\n",p);
break;
}
- mi=(memitem *)mem_entry;
- xfree(mi->my_ptr);
+ mi=(memitem *)(mem_entry->item);
+ assert(mi->pool);
+ assert(mi->my_ptr);
+ xmemFree(mi); /* xfree(mi->my_ptr); */
+ size2id(atoi(p),mi); /* we don't need it here I guess? */
strcpy(mi->orig_ptr,abuf);
p=strtok(NULL,"\n");
if (!p) badformat();
- mi->my_ptr=(char *)xmalloc(atoi(p));
+ mi->my_ptr= xmemAlloc(mi); /* (char *)xmalloc(atoi(p)); */
+ assert(mi->my_ptr);
mstat.reallocs++;
break;
case 'f':
p=strtok(&mbuf[2],"\n");
mem_entry=hash_lookup(mem_table, p);
if (mem_entry==NULL) {
- fprintf(stderr,"invalid free (%s)!\n",p);
+ if (p[0]!='0')
+ fprintf(stderr,"invalid free (%s) at line %d!\n",p,a);
break;
}
- mi=(memitem *)mem_entry;
- xfree(mi->my_ptr);
+ mi=(memitem *)(mem_entry->item);
+ assert(mi->pool);
+ assert(mi->my_ptr);
+ xmemFree(mi); /* xfree(mi->my_ptr); */
hash_unlink(mem_table, mem_entry, 1);
mstat.frees++;
break;
void
print_stats()
{
+#ifdef WITH_LIB
+ memReport(stdout);
+#endif
getrusage(RUSAGE_SELF, &myusage);
printf("m/c/f/r=%d/%d/%d/%d\n",mstat.mallocs,mstat.callocs,
mstat.frees, mstat.reallocs);
+#if 0
printf("types : %d\n",size2id_len);
+#endif
printf("user time used : %d.%d\n", (int)myusage.ru_utime.tv_sec,
(int)myusage.ru_utime.tv_usec);
printf("system time used : %d.%d\n", (int)myusage.ru_stime.tv_sec,
void
size2id(size_t sz,memitem *mi)
{
- int j;
- for(j=0;j<size2id_len;j++)
- if (size2id_array[0][j]==sz) {
- size2id_array[1][j]++;
- mi->id=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);
+ mi->pool = sizeToPool(sz);
+ assert(mi->pool);
#endif
- size2id_len++;
- if (size2id_len==1) {
- size2id_alloc=100;
- size2id_array[0]=malloc(size2id_alloc*sizeof(int));
- size2id_array[1]=malloc(size2id_alloc*sizeof(int));
- } else {
- if (size2id_len > size2id_alloc )
- size2id_alloc+=100;
- 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]++;
+ return;
}
void
sprintf((char *)buf, "pl:%d/%d", id, size);
return buf;
}
+
+void
+my_hash_insert(hash_table *h, const char *k, memitem *item)
+{
+ memitem *l;
+ assert( item->pool);
+ assert( item->my_ptr);
+ hash_insert(h,k,item);
+}
+
+static void *
+xmemAlloc(memitem *item)
+{
+ extern MemPool *StringPool;
+ assert(item && item->pool);
+ if (StringPool == item->pool)
+ return memStringAlloc(item->pool, item->size);
+ else
+ return memAlloc(item->pool);
+}
+
+static void
+xmemFree(memitem *item)
+{
+ extern MemPool *StringPool;
+ assert(item && item->pool);
+ if (StringPool == item->pool)
+ return memStringFree(item->pool, item->my_ptr, item->size);
+ else
+ return memFree(item->pool, item->my_ptr);
+}
+
+void my_free(char *file, int line, void *ptr)
+{
+#if 0
+fprintf(stderr,"{%s:%d:%p",file,line,ptr);
+#endif
+free(ptr);
+#if 0
+fprintf(stderr,"}\n");
+#endif
+}