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]]\n"
55 main(int argn, char *argv[]) {
56 MemoryContext *base, *child;
58 int count=0, iscntx=0;
60 opentlog(TL_OPEN_STDERR,TL_DEBUG, NULL);
63 while((i=getopt(argn,argv,"c:ht")) != EOF) {
81 /* test correctness */
82 base = allocMemoryContext(NULL, MC_DEBUG);
83 child = allocMemoryContext(base, MC_DEBUG);
85 ptr = mcalloc(base, 30);
90 ptr1 = mcstrdup(base, ptr);
92 printf("lc:%s uc:%s free:%d\n", ptr, ptr1, base->chunk->freesize);
95 printf("lc:%s free:%d\n", ptr, base->chunk->freesize);
97 ptr1 = mcstrdup(base, ptr);
100 printf("uc:%s free:%d\n", ptr1, base->chunk->freesize);
102 ptr= mcstrdup(base, ptr1);
104 ptr1 =mcrealloc(ptr1, 60);
106 printf("mixed:%s free:%d\n", ptr1, base->chunk->freesize);
108 ptr = mcrealloc(ptr1, 59);
109 tassert( ptr==ptr1 );
110 printf("mixed:%s free:%d\n", ptr, base->chunk->freesize);
112 ptr = mcrealloc(ptr1, 120);
113 tassert( ptr==ptr1 );
114 printf("mixed:%s free:%d\n", ptr, base->chunk->freesize);
116 ptr = mcalloc(base, CNTXCHUNK);
118 printf("mixed:%s free:%d freenew:%d\n", ptr1, base->chunk->freesize, base->chunk->next->freesize);
120 ptr= mcstrdup(child, ptr1);
121 printf("mixed:%s free:%d freechild:%d\n", ptr1, base->chunk->freesize, child->chunk->freesize);
123 freeMemoryContext(child);
124 printf("Child: %d\n", (int)(base->child));
126 child = allocMemoryContext(base, MC_DEBUG);
127 freeMemoryContext(base);
129 struct timeval begin;
134 gettimeofday(&begin, NULL);
135 base = allocMemoryContext(NULL, MC_DEBUG);
136 for(i=0;i<count;i++) {
137 ptr = mcalloc(base, 1+random()%SZ );
138 ptr1 = mcalloc(base, 1+random()%SZ );
139 if ( !(ptr && ptr1) )
140 tlog(TL_CRIT|TL_EXIT,"No memory");
145 ptr = mcrealloc(ptr, 1+random()%SZ );
147 tlog(TL_CRIT|TL_EXIT,"No memory");
150 ptr1=mcalloc(base, 1+random()%SZ );
153 ptr = mcrealloc(ptr, 1+random()%SZ );
155 tlog(TL_CRIT|TL_EXIT,"No memory");
159 freeMemoryContext(base);
160 printf("MC elapsed: %f sec\n", elapsedtime(&begin));
163 char **all, **allptr;
164 gettimeofday(&begin, NULL);
165 allptr=all=malloc(sizeof(char*)*count*2);
167 tlog(TL_CRIT|TL_EXIT,"No memory");
168 for(i=0;i<count;i++) {
169 ptr = malloc( 1+random()%SZ );
170 ptr1 = malloc( 1+random()%SZ );
171 if ( !(ptr && ptr1) )
172 tlog(TL_CRIT|TL_EXIT,"No memory");
177 ptr = realloc(ptr, 1+random()%SZ );
179 tlog(TL_CRIT|TL_EXIT,"No memory");
182 *allptr = ptr1=malloc( 1+random()%SZ );
186 *allptr = ptr = realloc(ptr, 1+random()%SZ );
189 tlog(TL_CRIT|TL_EXIT,"No memory");
193 while( allptr-all < count ) {
198 printf("Malloc elapsed: %f sec\n", elapsedtime(&begin));