+/*
+ * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
+ * 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 <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+
+#include "tmalloc.h"
+#include "tlog.h"
+#include "tools.h"
+
+static void
+usage() {
+ puts(
+ "Usage:\n"
+ "memtest [-c COUNT [-m]]\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;
+
+ opentlog(TL_OPEN_STDERR,TL_DEBUG, NULL);
+ opterr=0;
+
+ while((i=getopt(argn,argv,"c:hm")) != EOF) {
+ switch(i) {
+ case 'c':
+ count=atoi(optarg);
+ break;
+ case 'm':
+ iscntx=1;
+ break;
+ case 'h':
+ default:
+ usage();
+ break;
+ }
+ }
+
+ if ( count == 0 ) {
+ char *ptr, *ptr1;
+
+ /* test correctness */
+ base = allocMemoryContext(NULL, MC_DEBUG);
+ child = allocMemoryContext(base, MC_DEBUG);
+
+ 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, base->chunk->freesize);
+
+ mcfree(ptr1);
+ printf("lc:%s free:%d\n", ptr, base->chunk->freesize);
+
+ ptr1 = mcstrdup(base, ptr);
+ mcfree(ptr);
+ strupper(ptr1);
+ printf("uc:%s free:%d\n", ptr1, 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);
+
+ ptr = mcrealloc(ptr1, 59);
+ tassert( ptr==ptr1 );
+ printf("mixed:%s free:%d\n", ptr, base->chunk->freesize);
+
+ ptr = mcrealloc(ptr1, 120);
+ tassert( ptr==ptr1 );
+ printf("mixed:%s free:%d\n", ptr, 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);
+
+ ptr= mcstrdup(child, ptr1);
+ printf("mixed:%s free:%d freechild:%d\n", ptr1, base->chunk->freesize, child->chunk->freesize);
+
+ freeMemoryContext(child);
+ printf("Child: %d\n", (int)(base->child));
+
+ child = allocMemoryContext(base, MC_DEBUG);
+ freeMemoryContext(base);
+ } else {
+ struct timeval begin;
+ char *ptr, *ptr1;
+
+ srandom(1);
+ if ( iscntx ) {
+ gettimeofday(&begin, NULL);
+ base = allocMemoryContext(NULL, MC_DEBUG);
+ for(i=0;i<count;i++) {
+ ptr = mcalloc(base, 1+random()%1024 );
+ ptr1 = mcalloc(base, 1+random()%1024 );
+ if ( !(ptr && ptr1) )
+ tlog(TL_CRIT|TL_EXIT,"No memory");
+ *ptr = i%256;
+ *ptr1 = i%256;
+ mcfree(ptr1);
+
+ ptr = mcrealloc(ptr, 1+random()%1024 );
+ if ( !ptr )
+ tlog(TL_CRIT|TL_EXIT,"No memory");
+ *ptr = (i+1)%256;
+
+ ptr1=mcalloc(base, 1+random()%1024 );
+ *ptr1 = (i+2)%256;
+
+ ptr = mcrealloc(ptr, 1+random()%1024 );
+ if ( !ptr )
+ tlog(TL_CRIT|TL_EXIT,"No memory");
+ *ptr = (i+3)%256;
+
+ }
+ printf("MC elapsed: %f sec\n", elapsedtime(&begin));
+
+ freeMemoryContext(base);
+ } else {
+ gettimeofday(&begin, NULL);
+ for(i=0;i<count;i++) {
+ ptr = malloc( 1+random()%1024 );
+ ptr1 = malloc( 1+random()%1024 );
+ if ( !(ptr && ptr1) )
+ tlog(TL_CRIT|TL_EXIT,"No memory");
+ *ptr = i%256;
+ *ptr1 = i%256;
+ free(ptr1);
+
+ ptr = realloc(ptr, 1+random()%1024 );
+ if ( !ptr )
+ tlog(TL_CRIT|TL_EXIT,"No memory");
+ *ptr = (i+1)%256;
+
+ ptr1=malloc( 1+random()%1024 );
+ *ptr1 = (i+2)%256;
+
+ ptr = realloc(ptr, 1+random()%1024 );
+ if ( !ptr )
+ tlog(TL_CRIT|TL_EXIT,"No memory");
+ *ptr = (i+3)%256;
+
+ }
+ printf("Malloc elapsed: %f sec\n", elapsedtime(&begin));
+ }
+
+ }
+ return 0;
+}
+
+
+