2 * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 "memtest [-c COUNT [-m]]\n"
53 main(int argn, char *argv[]) {
54 MemoryContext *base, *child;
56 int count=0, iscntx=0;
58 opentlog(TL_OPEN_STDERR,TL_DEBUG, NULL);
61 while((i=getopt(argn,argv,"c:hm")) != EOF) {
79 /* test correctness */
80 base = allocMemoryContext(NULL, MC_DEBUG);
81 child = allocMemoryContext(base, MC_DEBUG);
83 ptr = mcalloc(base, 30);
88 ptr1 = mcstrdup(base, ptr);
90 printf("lc:%s uc:%s free:%d\n", ptr, ptr1, base->chunk->freesize);
93 printf("lc:%s free:%d\n", ptr, base->chunk->freesize);
95 ptr1 = mcstrdup(base, ptr);
98 printf("uc:%s free:%d\n", ptr1, base->chunk->freesize);
100 ptr= mcstrdup(base, ptr1);
102 ptr1 =mcrealloc(ptr1, 60);
104 printf("mixed:%s free:%d\n", ptr1, base->chunk->freesize);
106 ptr = mcrealloc(ptr1, 59);
107 tassert( ptr==ptr1 );
108 printf("mixed:%s free:%d\n", ptr, base->chunk->freesize);
110 ptr = mcrealloc(ptr1, 120);
111 tassert( ptr==ptr1 );
112 printf("mixed:%s free:%d\n", ptr, base->chunk->freesize);
114 ptr = mcalloc(base, CNTXCHUNK);
116 printf("mixed:%s free:%d freenew:%d\n", ptr1, base->chunk->freesize, base->chunk->next->freesize);
118 ptr= mcstrdup(child, ptr1);
119 printf("mixed:%s free:%d freechild:%d\n", ptr1, base->chunk->freesize, child->chunk->freesize);
121 freeMemoryContext(child);
122 printf("Child: %d\n", (int)(base->child));
124 child = allocMemoryContext(base, MC_DEBUG);
125 freeMemoryContext(base);
127 struct timeval begin;
132 gettimeofday(&begin, NULL);
133 base = allocMemoryContext(NULL, MC_DEBUG);
134 for(i=0;i<count;i++) {
135 ptr = mcalloc(base, 1+random()%1024 );
136 ptr1 = mcalloc(base, 1+random()%1024 );
137 if ( !(ptr && ptr1) )
138 tlog(TL_CRIT|TL_EXIT,"No memory");
143 ptr = mcrealloc(ptr, 1+random()%1024 );
145 tlog(TL_CRIT|TL_EXIT,"No memory");
148 ptr1=mcalloc(base, 1+random()%1024 );
151 ptr = mcrealloc(ptr, 1+random()%1024 );
153 tlog(TL_CRIT|TL_EXIT,"No memory");
157 printf("MC elapsed: %f sec\n", elapsedtime(&begin));
159 freeMemoryContext(base);
161 gettimeofday(&begin, NULL);
162 for(i=0;i<count;i++) {
163 ptr = malloc( 1+random()%1024 );
164 ptr1 = malloc( 1+random()%1024 );
165 if ( !(ptr && ptr1) )
166 tlog(TL_CRIT|TL_EXIT,"No memory");
171 ptr = realloc(ptr, 1+random()%1024 );
173 tlog(TL_CRIT|TL_EXIT,"No memory");
176 ptr1=malloc( 1+random()%1024 );
179 ptr = realloc(ptr, 1+random()%1024 );
181 tlog(TL_CRIT|TL_EXIT,"No memory");
185 printf("Malloc elapsed: %f sec\n", elapsedtime(&begin));