83097e313bb11b7051ba03be41f72ed0ec764721
[tedtools.git] / memtest.c
1 /*
2  * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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.
28  */
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34
35
36 #include "tmalloc.h"
37 #include "tlog.h"
38 #include "tools.h"
39
40 static void
41 usage() {
42         puts(
43         "Usage:\n"
44         "memtest [-c COUNT [-m]]\n"
45         );
46         exit(1);
47 }
48
49 extern char *optarg;
50 extern int opterr;
51
52 int
53 main(int argn, char *argv[]) {
54         MemoryContext   *base, *child;
55         int i;
56         int count=0, iscntx=0;
57
58         opentlog(TL_OPEN_STDERR,TL_DEBUG, NULL);
59         opterr=0;
60
61         while((i=getopt(argn,argv,"c:hm")) != EOF) {
62                 switch(i) {
63                         case 'c':
64                                 count=atoi(optarg);
65                                 break;
66                         case 'm':
67                                 iscntx=1;
68                                 break;
69                         case 'h':
70                         default:
71                                 usage();
72                                 break;
73                 }
74         }
75                                 
76         if ( count == 0 ) {
77                 char *ptr, *ptr1;
78
79                 /* test correctness */
80                 base = allocMemoryContext(NULL, MC_DEBUG);
81                 child = allocMemoryContext(base, MC_DEBUG);
82
83                 ptr = mcalloc(base, 30);
84                 for(i=0;i<26;i++)
85                         ptr[i] = 97+i;
86                 ptr[i]='\0';
87                 
88                 ptr1 = mcstrdup(base, ptr);
89                 strupper(ptr1); 
90                 printf("lc:%s uc:%s free:%d\n", ptr, ptr1, base->chunk->freesize);
91
92                 mcfree(ptr1);
93                 printf("lc:%s free:%d\n", ptr, base->chunk->freesize);
94
95                 ptr1 = mcstrdup(base, ptr);
96                 mcfree(ptr);
97                 strupper(ptr1); 
98                 printf("uc:%s free:%d\n", ptr1, base->chunk->freesize);
99
100                 ptr= mcstrdup(base, ptr1);
101                 strlower(ptr);
102                 ptr1 =mcrealloc(ptr1, 60);
103                 strcat(ptr1, ptr);  
104                 printf("mixed:%s free:%d\n", ptr1, base->chunk->freesize);
105
106                 ptr = mcrealloc(ptr1, 59);
107                 tassert( ptr==ptr1 );
108                 printf("mixed:%s free:%d\n", ptr, base->chunk->freesize);
109
110                 ptr = mcrealloc(ptr1, 120); 
111                 tassert( ptr==ptr1 );
112                 printf("mixed:%s free:%d\n", ptr, base->chunk->freesize);
113         
114                 ptr = mcalloc(base, CNTXCHUNK);
115                 strcpy(ptr, ptr1);
116                 printf("mixed:%s free:%d freenew:%d\n", ptr1, base->chunk->freesize, base->chunk->next->freesize);
117
118                 ptr= mcstrdup(child, ptr1);
119                 printf("mixed:%s free:%d freechild:%d\n", ptr1, base->chunk->freesize, child->chunk->freesize);
120
121                 freeMemoryContext(child);
122                 printf("Child: %d\n", (int)(base->child));
123
124                 child = allocMemoryContext(base, MC_DEBUG);
125                 freeMemoryContext(base);
126         } else {
127                 struct timeval begin;   
128                 char *ptr, *ptr1;
129         
130                 srandom(1);
131                 if ( iscntx ) {
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");
139                                 *ptr = i%256;
140                                 *ptr1 = i%256;
141                                 mcfree(ptr1);
142
143                                 ptr = mcrealloc(ptr, 1+random()%1024 );
144                                 if ( !ptr )
145                                         tlog(TL_CRIT|TL_EXIT,"No memory");
146                                 *ptr = (i+1)%256;
147
148                                 ptr1=mcalloc(base, 1+random()%1024 );
149                                 *ptr1 = (i+2)%256;
150
151                                 ptr = mcrealloc(ptr, 1+random()%1024 );
152                                 if ( !ptr )
153                                         tlog(TL_CRIT|TL_EXIT,"No memory");
154                                 *ptr = (i+3)%256;
155         
156                         }
157                         printf("MC elapsed: %f sec\n", elapsedtime(&begin));
158
159                         freeMemoryContext(base);
160                 } else {
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");
167                                 *ptr = i%256;
168                                 *ptr1 = i%256;
169                                 free(ptr1);
170
171                                 ptr = realloc(ptr, 1+random()%1024 );
172                                 if ( !ptr )
173                                         tlog(TL_CRIT|TL_EXIT,"No memory");
174                                 *ptr = (i+1)%256;
175
176                                 ptr1=malloc( 1+random()%1024 );
177                                 *ptr1 = (i+2)%256;
178
179                                 ptr = realloc(ptr, 1+random()%1024 );
180                                 if ( !ptr )
181                                         tlog(TL_CRIT|TL_EXIT,"No memory");
182                                 *ptr = (i+3)%256;
183         
184                         }
185                         printf("Malloc elapsed: %f sec\n", elapsedtime(&begin));
186                 }       
187
188         }
189         return 0;
190 }
191
192         
193