e5bf0884e41dc6477bfe02f698621af529ddbef4
[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 [-t [-D]] [-s MAXSIZE] [-C COUNTCOUNT]]\n"
45         );
46         exit(1);
47 }
48
49
50 extern char *optarg;
51 extern int opterr;
52
53 int
54 main(int argn, char *argv[]) {
55         MemoryContext   *base, *child;
56         int i, SZ=32;
57         int count=0, iscntx=0, flags=0,  COUNT=1;
58
59         opentlog(TL_OPEN_STDERR,TL_DEBUG, NULL);
60         opterr=0;
61
62         while((i=getopt(argn,argv,"s:Dc:htC:")) != EOF) {
63                 switch(i) {
64                         case 's':
65                                 SZ=atoi(optarg);
66                                 break;
67                         case 'C':
68                                 COUNT=atoi(optarg);
69                                 break;
70                         case 'c':
71                                 count=atoi(optarg);
72                                 break;
73                         case 'D':
74                                 flags=MC_DEBUG;
75                                 break;
76                         case 't':
77                                 iscntx=1;
78                                 break;
79                         case 'h':
80                         default:
81                                 usage();
82                                 break;
83                 }
84         }
85                                 
86         if ( count == 0 ) {
87                 char *ptr, *ptr1;
88
89                 /* test correctness */
90                 base = allocMemoryContext(NULL, flags);
91                 child = allocMemoryContext(base, flags);
92
93                 ptr = mcalloc(base, 30);
94                 for(i=0;i<26;i++)
95                         ptr[i] = 97+i;
96                 ptr[i]='\0';
97                 
98                 ptr1 = mcstrdup(base, ptr);
99                 strupper(ptr1); 
100                 printf("lc:%s uc:%s free:%d\n", ptr, ptr1, (int)base->chunk->freesize);
101
102                 mcfree(ptr1);
103                 printf("lc:%s free:%d\n", ptr, (int)base->chunk->freesize);
104
105                 ptr1 = mcstrdup(base, ptr);
106                 mcfree(ptr);
107                 strupper(ptr1); 
108                 printf("uc:%s free:%d\n", ptr1, (int)base->chunk->freesize);
109
110                 ptr= mcstrdup(base, ptr1);
111                 strlower(ptr);
112                 ptr1 =mcrealloc(ptr1, 60);
113                 strcat(ptr1, ptr);  
114                 printf("mixed:%s free:%d\n", ptr1, (int)base->chunk->freesize);
115
116                 ptr = mcrealloc(ptr1, 59);
117                 tassert( ptr==ptr1 );
118                 printf("mixed:%s free:%d\n", ptr, (int)base->chunk->freesize);
119
120                 ptr = mcrealloc(ptr1, 120); 
121                 tassert( ptr==ptr1 );
122                 printf("mixed:%s free:%d\n", ptr, (int)base->chunk->freesize);
123         
124                 ptr1 = mcalloc(base, CNTXCHUNK);
125                 strcpy(ptr1, ptr);
126                 printf("mixed:%s free:%d freenew:%d\n", ptr1, (int)base->chunk->freesize, (int)base->chunk->next->freesize);
127
128                 ptr1= mcstrdup(child, ptr);
129                 printf("mixed:%s free:%d freechild:%d\n", ptr1, (int)base->chunk->freesize, (int)child->chunk->freesize);
130
131                 freeMemoryContext(child);
132                 printf("Child: IS %sNULL\n", (base->child == NULL) ? "" : "NOT ");
133
134                 child = allocMemoryContext(base, flags);
135                 freeMemoryContext(base);
136         } else {
137                 struct timeval begin;   
138                 char *ptr, *ptr1;
139         
140                 srandom(1);
141                 if ( iscntx ) {
142                         gettimeofday(&begin, NULL);
143                         while(COUNT-- > 0) {
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");
150                                 *ptr = i%256;
151                                 *ptr1 = i%256;
152                                 mcfree(ptr1);
153
154                                 ptr = mcrealloc(ptr, 1+random()%SZ );
155                                 if ( !ptr )
156                                         tlog(TL_CRIT|TL_EXIT,"No memory");
157                                 *ptr = (i+1)%256;
158
159                                 ptr1=mcalloc(base, 1+random()%SZ );
160                                 *ptr1 = (i+2)%256;
161
162                                 ptr = mcrealloc(ptr, 1+random()%SZ );
163                                 if ( !ptr )
164                                         tlog(TL_CRIT|TL_EXIT,"No memory");
165                                 *ptr = (i+3)%256;
166         
167                         }
168                         freeMemoryContext(base);
169                         }
170                         printf("MC elapsed: %f sec\n", elapsedtime(&begin));
171
172                 } else {
173                         char **all, **allptr;
174                         gettimeofday(&begin, NULL);
175                         while(COUNT-- > 0) {
176                         allptr=all=malloc(sizeof(char*)*count*2);
177                         if ( !all )
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");
184                                 *ptr = i%256;
185                                 *ptr1 = i%256;
186                                 free(ptr1);
187
188                                 ptr = realloc(ptr, 1+random()%SZ );
189                                 if ( !ptr )
190                                         tlog(TL_CRIT|TL_EXIT,"No memory");
191                                 *ptr = (i+1)%256;
192
193                                 *allptr = ptr1=malloc( 1+random()%SZ );
194                                 allptr++;
195                                 *ptr1 = (i+2)%256;
196
197                                 *allptr = ptr = realloc(ptr, 1+random()%SZ );
198                                 allptr++;
199                                 if ( !ptr )
200                                         tlog(TL_CRIT|TL_EXIT,"No memory");
201                                 *ptr = (i+3)%256;
202                         }
203                         allptr=all;
204                         while( allptr-all < count ) {
205                                 free(*allptr);
206                                 allptr++;
207                         }
208                         free(all);
209                         }
210                         printf("Malloc elapsed: %f sec\n", elapsedtime(&begin));
211                 }       
212
213         }
214         return 0;
215 }
216
217         
218