/* * Copyright (c) 2004 Teodor Sigaev * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include "tmalloc.h" #include "tlog.h" #include "tools.h" static void usage() { puts( "Usage:\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, 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,"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 'D': flags=MC_DEBUG; break; case 't': iscntx=1; break; case 'h': default: usage(); break; } } if ( count == 0 ) { char *ptr, *ptr1; /* test correctness */ base = allocMemoryContext(NULL, flags); child = allocMemoryContext(base, flags); ptr = mcalloc(base, 30); for(i=0;i<26;i++) ptr[i] = 97+i; ptr[i]='\0'; ptr1 = mcstrdup(base, ptr); strupper(ptr1); printf("lc:%s uc:%s free:%d\n", ptr, ptr1, (int)base->chunk->freesize); mcfree(ptr1); 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, (int)base->chunk->freesize); ptr= mcstrdup(base, ptr1); strlower(ptr); ptr1 =mcrealloc(ptr1, 60); strcat(ptr1, ptr); 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, (int)base->chunk->freesize); ptr = mcrealloc(ptr1, 120); tassert( ptr==ptr1 ); printf("mixed:%s free:%d\n", ptr, (int)base->chunk->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); 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: IS %sNULL\n", (base->child == NULL) ? "" : "NOT "); child = allocMemoryContext(base, flags); freeMemoryContext(base); } else { struct timeval begin; char *ptr, *ptr1; srandom(1); if ( iscntx ) { gettimeofday(&begin, NULL); while(COUNT-- > 0) { base = allocMemoryContext(NULL, flags); for(i=0;i 0) { allptr=all=malloc(sizeof(char*)*count*2); if ( !all ) tlog(TL_CRIT|TL_EXIT,"No memory"); for(i=0;i