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 [-t [-D]] [-s MAXSIZE] [-C COUNTCOUNT]]\n"
54 main(int argn, char *argv[]) {
55 MemoryContext *base, *child;
57 int count=0, iscntx=0, flags=0, COUNT=1;
59 opentlog(TL_OPEN_STDERR,TL_DEBUG, NULL);
62 while((i=getopt(argn,argv,"s:Dc:htC:")) != EOF) {
89 /* test correctness */
90 base = allocMemoryContext(NULL, flags);
91 child = allocMemoryContext(base, flags);
93 ptr = mcalloc(base, 30);
98 ptr1 = mcstrdup(base, ptr);
100 printf("lc:%s uc:%s free:%d\n", ptr, ptr1, base->chunk->freesize);
103 printf("lc:%s free:%d\n", ptr, base->chunk->freesize);
105 ptr1 = mcstrdup(base, ptr);
108 printf("uc:%s free:%d\n", ptr1, base->chunk->freesize);
110 ptr= mcstrdup(base, ptr1);
112 ptr1 =mcrealloc(ptr1, 60);
114 printf("mixed:%s free:%d\n", ptr1, base->chunk->freesize);
116 ptr = mcrealloc(ptr1, 59);
117 tassert( ptr==ptr1 );
118 printf("mixed:%s free:%d\n", ptr, base->chunk->freesize);
120 ptr = mcrealloc(ptr1, 120);
121 tassert( ptr==ptr1 );
122 printf("mixed:%s free:%d\n", ptr, base->chunk->freesize);
124 ptr1 = mcalloc(base, CNTXCHUNK);
126 printf("mixed:%s free:%d freenew:%d\n", ptr1, base->chunk->freesize, base->chunk->next->freesize);
128 ptr1= mcstrdup(child, ptr);
129 printf("mixed:%s free:%d freechild:%d\n", ptr1, base->chunk->freesize, child->chunk->freesize);
131 freeMemoryContext(child);
132 printf("Child: %d\n", (int)(base->child));
134 child = allocMemoryContext(base, flags);
135 freeMemoryContext(base);
137 struct timeval begin;
142 gettimeofday(&begin, NULL);
144 base = allocMemoryContext(NULL, flags);
145 for(i=0;i<count;i++) {
146 ptr = mcalloc(base, 1+random()%SZ );
147 ptr1 = mcalloc(base, 1+random()%SZ );
148 if ( !(ptr && ptr1) )
149 tlog(TL_CRIT|TL_EXIT,"No memory");
154 ptr = mcrealloc(ptr, 1+random()%SZ );
156 tlog(TL_CRIT|TL_EXIT,"No memory");
159 ptr1=mcalloc(base, 1+random()%SZ );
162 ptr = mcrealloc(ptr, 1+random()%SZ );
164 tlog(TL_CRIT|TL_EXIT,"No memory");
168 freeMemoryContext(base);
170 printf("MC elapsed: %f sec\n", elapsedtime(&begin));
173 char **all, **allptr;
174 gettimeofday(&begin, NULL);
176 allptr=all=malloc(sizeof(char*)*count*2);
178 tlog(TL_CRIT|TL_EXIT,"No memory");
179 for(i=0;i<count;i++) {
180 ptr = malloc( 1+random()%SZ );
181 ptr1 = malloc( 1+random()%SZ );
182 if ( !(ptr && ptr1) )
183 tlog(TL_CRIT|TL_EXIT,"No memory");
188 ptr = realloc(ptr, 1+random()%SZ );
190 tlog(TL_CRIT|TL_EXIT,"No memory");
193 *allptr = ptr1=malloc( 1+random()%SZ );
197 *allptr = ptr = realloc(ptr, 1+random()%SZ );
200 tlog(TL_CRIT|TL_EXIT,"No memory");
204 while( allptr-all < count ) {
210 printf("Malloc elapsed: %f sec\n", elapsedtime(&begin));