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]]\n"
54 main(int argn, char *argv[]) {
55 MemoryContext *base, *child;
57 int count=0, iscntx=0, flags=0;
59 opentlog(TL_OPEN_STDERR,TL_DEBUG, NULL);
62 while((i=getopt(argn,argv,"s:Dc:ht")) != EOF) {
86 /* test correctness */
87 base = allocMemoryContext(NULL, flags);
88 child = allocMemoryContext(base, flags);
90 ptr = mcalloc(base, 30);
95 ptr1 = mcstrdup(base, ptr);
97 printf("lc:%s uc:%s free:%d\n", ptr, ptr1, base->chunk->freesize);
100 printf("lc:%s free:%d\n", ptr, base->chunk->freesize);
102 ptr1 = mcstrdup(base, ptr);
105 printf("uc:%s free:%d\n", ptr1, base->chunk->freesize);
107 ptr= mcstrdup(base, ptr1);
109 ptr1 =mcrealloc(ptr1, 60);
111 printf("mixed:%s free:%d\n", ptr1, base->chunk->freesize);
113 ptr = mcrealloc(ptr1, 59);
114 tassert( ptr==ptr1 );
115 printf("mixed:%s free:%d\n", ptr, base->chunk->freesize);
117 ptr = mcrealloc(ptr1, 120);
118 tassert( ptr==ptr1 );
119 printf("mixed:%s free:%d\n", ptr, base->chunk->freesize);
121 ptr = mcalloc(base, CNTXCHUNK);
123 printf("mixed:%s free:%d freenew:%d\n", ptr1, base->chunk->freesize, base->chunk->next->freesize);
125 ptr= mcstrdup(child, ptr1);
126 printf("mixed:%s free:%d freechild:%d\n", ptr1, base->chunk->freesize, child->chunk->freesize);
128 freeMemoryContext(child);
129 printf("Child: %d\n", (int)(base->child));
131 child = allocMemoryContext(base, flags);
132 freeMemoryContext(base);
134 struct timeval begin;
139 gettimeofday(&begin, NULL);
140 base = allocMemoryContext(NULL, flags);
141 for(i=0;i<count;i++) {
142 ptr = mcalloc(base, 1+random()%SZ );
143 ptr1 = mcalloc(base, 1+random()%SZ );
144 if ( !(ptr && ptr1) )
145 tlog(TL_CRIT|TL_EXIT,"No memory");
150 ptr = mcrealloc(ptr, 1+random()%SZ );
152 tlog(TL_CRIT|TL_EXIT,"No memory");
155 ptr1=mcalloc(base, 1+random()%SZ );
158 ptr = mcrealloc(ptr, 1+random()%SZ );
160 tlog(TL_CRIT|TL_EXIT,"No memory");
164 freeMemoryContext(base);
165 printf("MC elapsed: %f sec\n", elapsedtime(&begin));
168 char **all, **allptr;
169 gettimeofday(&begin, NULL);
170 allptr=all=malloc(sizeof(char*)*count*2);
172 tlog(TL_CRIT|TL_EXIT,"No memory");
173 for(i=0;i<count;i++) {
174 ptr = malloc( 1+random()%SZ );
175 ptr1 = malloc( 1+random()%SZ );
176 if ( !(ptr && ptr1) )
177 tlog(TL_CRIT|TL_EXIT,"No memory");
182 ptr = realloc(ptr, 1+random()%SZ );
184 tlog(TL_CRIT|TL_EXIT,"No memory");
187 *allptr = ptr1=malloc( 1+random()%SZ );
191 *allptr = ptr = realloc(ptr, 1+random()%SZ );
194 tlog(TL_CRIT|TL_EXIT,"No memory");
198 while( allptr-all < count ) {
203 printf("Malloc elapsed: %f sec\n", elapsedtime(&begin));