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