add .gitignore
[tedtools.git] / memtest.c
index 83097e3..e5bf088 100644 (file)
--- a/memtest.c
+++ b/memtest.c
@@ -41,29 +41,39 @@ static void
 usage() {
        puts(
        "Usage:\n"
-       "memtest [-c COUNT [-m]]\n"
+       "memtest [-c COUNT [-t [-D]] [-s MAXSIZE] [-C COUNTCOUNT]]\n"
        );
        exit(1);
 }
 
+
 extern char *optarg;
 extern int opterr;
 
 int
 main(int argn, char *argv[]) {
        MemoryContext   *base, *child;
-       int i;
-       int count=0, iscntx=0;
+       int i, SZ=32;
+       int count=0, iscntx=0, flags=0,  COUNT=1;
 
        opentlog(TL_OPEN_STDERR,TL_DEBUG, NULL);
        opterr=0;
 
-       while((i=getopt(argn,argv,"c:hm")) != EOF) {
+       while((i=getopt(argn,argv,"s:Dc:htC:")) != EOF) {
                switch(i) {
+                       case 's':
+                               SZ=atoi(optarg);
+                               break;
+                       case 'C':
+                               COUNT=atoi(optarg);
+                               break;
                        case 'c':
                                count=atoi(optarg);
                                break;
-                       case 'm':
+                       case 'D':
+                               flags=MC_DEBUG;
+                               break;
+                       case 't':
                                iscntx=1;
                                break;
                        case 'h':
@@ -77,8 +87,8 @@ main(int argn, char *argv[]) {
                char *ptr, *ptr1;
 
                /* test correctness */
-               base = allocMemoryContext(NULL, MC_DEBUG);
-               child = allocMemoryContext(base, MC_DEBUG);
+               base = allocMemoryContext(NULL, flags);
+               child = allocMemoryContext(base, flags);
 
                ptr = mcalloc(base, 30);
                for(i=0;i<26;i++)
@@ -87,41 +97,41 @@ main(int argn, char *argv[]) {
                
                ptr1 = mcstrdup(base, ptr);
                strupper(ptr1); 
-               printf("lc:%s uc:%s free:%d\n", ptr, ptr1, base->chunk->freesize);
+               printf("lc:%s uc:%s free:%d\n", ptr, ptr1, (int)base->chunk->freesize);
 
                mcfree(ptr1);
-               printf("lc:%s free:%d\n", ptr, base->chunk->freesize);
+               printf("lc:%s free:%d\n", ptr, (int)base->chunk->freesize);
 
                ptr1 = mcstrdup(base, ptr);
                mcfree(ptr);
                strupper(ptr1); 
-               printf("uc:%s free:%d\n", ptr1, base->chunk->freesize);
+               printf("uc:%s free:%d\n", ptr1, (int)base->chunk->freesize);
 
                ptr= mcstrdup(base, ptr1);
                strlower(ptr);
                ptr1 =mcrealloc(ptr1, 60);
                strcat(ptr1, ptr);  
-               printf("mixed:%s free:%d\n", ptr1, base->chunk->freesize);
+               printf("mixed:%s free:%d\n", ptr1, (int)base->chunk->freesize);
 
                ptr = mcrealloc(ptr1, 59);
                tassert( ptr==ptr1 );
-               printf("mixed:%s free:%d\n", ptr, base->chunk->freesize);
+               printf("mixed:%s free:%d\n", ptr, (int)base->chunk->freesize);
 
                ptr = mcrealloc(ptr1, 120); 
                tassert( ptr==ptr1 );
-               printf("mixed:%s free:%d\n", ptr, base->chunk->freesize);
+               printf("mixed:%s free:%d\n", ptr, (int)base->chunk->freesize);
        
-               ptr = mcalloc(base, CNTXCHUNK);
-               strcpy(ptr, ptr1);
-               printf("mixed:%s free:%d freenew:%d\n", ptr1, base->chunk->freesize, base->chunk->next->freesize);
+               ptr1 = mcalloc(base, CNTXCHUNK);
+               strcpy(ptr1, ptr);
+               printf("mixed:%s free:%d freenew:%d\n", ptr1, (int)base->chunk->freesize, (int)base->chunk->next->freesize);
 
-               ptr= mcstrdup(child, ptr1);
-               printf("mixed:%s free:%d freechild:%d\n", ptr1, base->chunk->freesize, child->chunk->freesize);
+               ptr1= mcstrdup(child, ptr);
+               printf("mixed:%s free:%d freechild:%d\n", ptr1, (int)base->chunk->freesize, (int)child->chunk->freesize);
 
                freeMemoryContext(child);
-               printf("Child: %d\n", (int)(base->child));
+               printf("Child: IS %sNULL\n", (base->child == NULL) ? "" : "NOT ");
 
-               child = allocMemoryContext(base, MC_DEBUG);
+               child = allocMemoryContext(base, flags);
                freeMemoryContext(base);
        } else {
                struct timeval begin;   
@@ -130,57 +140,72 @@ main(int argn, char *argv[]) {
                srandom(1);
                if ( iscntx ) {
                        gettimeofday(&begin, NULL);
-                       base = allocMemoryContext(NULL, MC_DEBUG);
+                       while(COUNT-- > 0) {
+                       base = allocMemoryContext(NULL, flags);
                        for(i=0;i<count;i++) {
-                               ptr = mcalloc(base, 1+random()%1024 );
-                               ptr1 = mcalloc(base, 1+random()%1024 );
+                               ptr = mcalloc(base, 1+random()%SZ );
+                               ptr1 = mcalloc(base, 1+random()%SZ );
                                if ( !(ptr && ptr1) )
                                        tlog(TL_CRIT|TL_EXIT,"No memory");
                                *ptr = i%256;
                                *ptr1 = i%256;
                                mcfree(ptr1);
 
-                               ptr = mcrealloc(ptr, 1+random()%1024 );
+                               ptr = mcrealloc(ptr, 1+random()%SZ );
                                if ( !ptr )
                                        tlog(TL_CRIT|TL_EXIT,"No memory");
                                *ptr = (i+1)%256;
 
-                               ptr1=mcalloc(base, 1+random()%1024 );
+                               ptr1=mcalloc(base, 1+random()%SZ );
                                *ptr1 = (i+2)%256;
 
-                               ptr = mcrealloc(ptr, 1+random()%1024 );
+                               ptr = mcrealloc(ptr, 1+random()%SZ );
                                if ( !ptr )
                                        tlog(TL_CRIT|TL_EXIT,"No memory");
                                *ptr = (i+3)%256;
        
+                       }
+                       freeMemoryContext(base);
                        }
                        printf("MC elapsed: %f sec\n", elapsedtime(&begin));
 
-                       freeMemoryContext(base);
                } else {
+                       char **all, **allptr;
                        gettimeofday(&begin, NULL);
+                       while(COUNT-- > 0) {
+                       allptr=all=malloc(sizeof(char*)*count*2);
+                       if ( !all )
+                               tlog(TL_CRIT|TL_EXIT,"No memory");
                        for(i=0;i<count;i++) {
-                               ptr = malloc( 1+random()%1024 );
-                               ptr1 = malloc( 1+random()%1024 );
+                               ptr = malloc( 1+random()%SZ );
+                               ptr1 = malloc( 1+random()%SZ );
                                if ( !(ptr && ptr1) )
                                        tlog(TL_CRIT|TL_EXIT,"No memory");
                                *ptr = i%256;
                                *ptr1 = i%256;
                                free(ptr1);
 
-                               ptr = realloc(ptr, 1+random()%1024 );
+                               ptr = realloc(ptr, 1+random()%SZ );
                                if ( !ptr )
                                        tlog(TL_CRIT|TL_EXIT,"No memory");
                                *ptr = (i+1)%256;
 
-                               ptr1=malloc( 1+random()%1024 );
+                               *allptr = ptr1=malloc( 1+random()%SZ );
+                               allptr++;
                                *ptr1 = (i+2)%256;
 
-                               ptr = realloc(ptr, 1+random()%1024 );
+                               *allptr = ptr = realloc(ptr, 1+random()%SZ );
+                               allptr++;
                                if ( !ptr )
                                        tlog(TL_CRIT|TL_EXIT,"No memory");
                                *ptr = (i+3)%256;
-       
+                       }
+                       allptr=all;
+                       while( allptr-all < count ) {
+                               free(*allptr);
+                               allptr++;
+                       }
+                       free(all);
                        }
                        printf("Malloc elapsed: %f sec\n", elapsedtime(&begin));
                }