small sisez
[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 #define SZ      32
50
51 extern char *optarg;
52 extern int opterr;
53
54 int
55 main(int argn, char *argv[]) {
56         MemoryContext   *base, *child;
57         int i;
58         int count=0, iscntx=0;
59
60         opentlog(TL_OPEN_STDERR,TL_DEBUG, NULL);
61         opterr=0;
62
63         while((i=getopt(argn,argv,"c:hm")) != EOF) {
64                 switch(i) {
65                         case 'c':
66                                 count=atoi(optarg);
67                                 break;
68                         case 'm':
69                                 iscntx=1;
70                                 break;
71                         case 'h':
72                         default:
73                                 usage();
74                                 break;
75                 }
76         }
77                                 
78         if ( count == 0 ) {
79                 char *ptr, *ptr1;
80
81                 /* test correctness */
82                 base = allocMemoryContext(NULL, MC_DEBUG);
83                 child = allocMemoryContext(base, MC_DEBUG);
84
85                 ptr = mcalloc(base, 30);
86                 for(i=0;i<26;i++)
87                         ptr[i] = 97+i;
88                 ptr[i]='\0';
89                 
90                 ptr1 = mcstrdup(base, ptr);
91                 strupper(ptr1); 
92                 printf("lc:%s uc:%s free:%d\n", ptr, ptr1, base->chunk->freesize);
93
94                 mcfree(ptr1);
95                 printf("lc:%s free:%d\n", ptr, base->chunk->freesize);
96
97                 ptr1 = mcstrdup(base, ptr);
98                 mcfree(ptr);
99                 strupper(ptr1); 
100                 printf("uc:%s free:%d\n", ptr1, base->chunk->freesize);
101
102                 ptr= mcstrdup(base, ptr1);
103                 strlower(ptr);
104                 ptr1 =mcrealloc(ptr1, 60);
105                 strcat(ptr1, ptr);  
106                 printf("mixed:%s free:%d\n", ptr1, base->chunk->freesize);
107
108                 ptr = mcrealloc(ptr1, 59);
109                 tassert( ptr==ptr1 );
110                 printf("mixed:%s free:%d\n", ptr, base->chunk->freesize);
111
112                 ptr = mcrealloc(ptr1, 120); 
113                 tassert( ptr==ptr1 );
114                 printf("mixed:%s free:%d\n", ptr, base->chunk->freesize);
115         
116                 ptr = mcalloc(base, CNTXCHUNK);
117                 strcpy(ptr, ptr1);
118                 printf("mixed:%s free:%d freenew:%d\n", ptr1, base->chunk->freesize, base->chunk->next->freesize);
119
120                 ptr= mcstrdup(child, ptr1);
121                 printf("mixed:%s free:%d freechild:%d\n", ptr1, base->chunk->freesize, child->chunk->freesize);
122
123                 freeMemoryContext(child);
124                 printf("Child: %d\n", (int)(base->child));
125
126                 child = allocMemoryContext(base, MC_DEBUG);
127                 freeMemoryContext(base);
128         } else {
129                 struct timeval begin;   
130                 char *ptr, *ptr1;
131         
132                 srandom(1);
133                 if ( iscntx ) {
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");
141                                 *ptr = i%256;
142                                 *ptr1 = i%256;
143                                 mcfree(ptr1);
144
145                                 ptr = mcrealloc(ptr, 1+random()%SZ );
146                                 if ( !ptr )
147                                         tlog(TL_CRIT|TL_EXIT,"No memory");
148                                 *ptr = (i+1)%256;
149
150                                 ptr1=mcalloc(base, 1+random()%SZ );
151                                 *ptr1 = (i+2)%256;
152
153                                 ptr = mcrealloc(ptr, 1+random()%SZ );
154                                 if ( !ptr )
155                                         tlog(TL_CRIT|TL_EXIT,"No memory");
156                                 *ptr = (i+3)%256;
157         
158                         }
159                         printf("MC elapsed: %f sec\n", elapsedtime(&begin));
160
161                         freeMemoryContext(base);
162                 } else {
163                         gettimeofday(&begin, NULL);
164                         for(i=0;i<count;i++) {
165                                 ptr = malloc( 1+random()%SZ );
166                                 ptr1 = malloc( 1+random()%SZ );
167                                 if ( !(ptr && ptr1) )
168                                         tlog(TL_CRIT|TL_EXIT,"No memory");
169                                 *ptr = i%256;
170                                 *ptr1 = i%256;
171                                 free(ptr1);
172
173                                 ptr = realloc(ptr, 1+random()%SZ );
174                                 if ( !ptr )
175                                         tlog(TL_CRIT|TL_EXIT,"No memory");
176                                 *ptr = (i+1)%256;
177
178                                 ptr1=malloc( 1+random()%SZ );
179                                 *ptr1 = (i+2)%256;
180
181                                 ptr = realloc(ptr, 1+random()%SZ );
182                                 if ( !ptr )
183                                         tlog(TL_CRIT|TL_EXIT,"No memory");
184                                 *ptr = (i+3)%256;
185         
186                         }
187                         printf("Malloc elapsed: %f sec\n", elapsedtime(&begin));
188                 }       
189
190         }
191         return 0;
192 }
193
194         
195