Upload Tizen:Base source
[toolchains/nspr.git] / mozilla / nsprpub / pr / tests / dbmalloc.c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is the Netscape Portable Runtime (NSPR).
16  *
17  * The Initial Developer of the Original Code is
18  * Netscape Communications Corporation.
19  * Portions created by the Initial Developer are Copyright (C) 1998-2000
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 /***********************************************************************
39 **
40 ** Name: dbmalloc.c
41 **
42 ** Description: Testing malloc (OBSOLETE)
43 **
44 ** Modification History:
45 ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
46 **               The debug mode will print all of the printfs associated with this test.
47 **                       The regress mode will be the default mode. Since the regress tool limits
48 **           the output to a one line status:PASS or FAIL,all of the printf statements
49 **                       have been handled with an if (debug_mode) statement. 
50 ***********************************************************************/
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <time.h>
54 #include <string.h>
55 #include "nspr.h"
56
57 void
58 usage
59 (
60     void
61 )
62 {
63     fprintf(stderr, "Usage: dbmalloc ('-m'|'-s') '-f' num_fails ('-d'|'-n') filename [...]\n");
64     exit(0);
65 }
66
67 typedef struct node_struct
68 {
69     struct node_struct *next, *prev;
70     int line;
71     char value[4];
72 }
73     node_t,
74    *node_pt;
75
76 node_pt get_node(const char *line)
77 {
78     node_pt rv;
79     int l = strlen(line);
80     rv = (node_pt)PR_MALLOC(sizeof(node_t) + l + 1 - 4);
81     if( (node_pt)0 == rv ) return (node_pt)0;
82     memcpy(&rv->value[0], line, l+1);
83     rv->next = rv->prev = (node_pt)0;
84     return rv;
85 }
86
87 void
88 dump
89 (
90     const char *name,
91     node_pt     node,
92     int         mf,
93     int         debug
94 )
95 {
96     if( (node_pt)0 != node->prev ) dump(name, node->prev, mf, debug);
97     if( 0 != debug ) printf("[%s]: %6d: %s", name, node->line, node->value);
98     if( node->line == mf ) fprintf(stderr, "[%s]: Line %d was allocated!\n", name, node->line);
99     if( (node_pt)0 != node->next ) dump(name, node->next, mf, debug);
100     return;
101 }
102
103 void
104 release
105 (
106     node_pt node
107 )
108 {
109     if( (node_pt)0 != node->prev ) release(node->prev);
110     if( (node_pt)0 != node->next ) release(node->next);
111     PR_DELETE(node);
112 }
113
114 int
115 t2
116 (
117     const char *name,
118     int         mf,
119     int         debug
120 )
121 {
122     int rv;
123     FILE *fp;
124     int l = 0;
125     node_pt head = (node_pt)0;
126     char buffer[ BUFSIZ ];
127
128     fp = fopen(name, "r");
129     if( (FILE *)0 == fp )
130     {
131         fprintf(stderr, "[%s]: Cannot open \"%s.\"\n", name, name);
132         return -1;
133     }
134
135     /* fgets mallocs a buffer, first time through. */
136     if( (char *)0 == fgets(buffer, BUFSIZ, fp) )
137     {
138         fprintf(stderr, "[%s]: \"%s\" is empty.\n", name, name);
139         (void)fclose(fp);
140         return -1;
141     }
142
143     rewind(fp);
144
145     if( PR_SUCCESS != PR_ClearMallocCount() )
146     {
147         fprintf(stderr, "[%s]: Cannot clear malloc count.\n", name);
148         (void)fclose(fp);
149         return -1;
150     }
151
152     if( PR_SUCCESS != PR_SetMallocCountdown(mf) )
153     {
154         fprintf(stderr, "[%s]: Cannot set malloc countdown to %d\n", name, mf);
155         (void)fclose(fp);
156         return -1;
157     }
158
159     while( fgets(buffer, BUFSIZ, fp) )
160     {
161         node_pt n;
162         node_pt *w = &head;
163
164         if( (strlen(buffer) == (BUFSIZ-1)) && (buffer[BUFSIZ-2] != '\n') )
165             buffer[BUFSIZ-2] == '\n';
166
167         l++;
168
169         n = get_node(buffer);
170         if( (node_pt)0 == n ) 
171         {
172             printf("[%s]: Line %d: malloc failure!\n", name, l);
173             continue;
174         }
175
176         n->line = l;
177
178         while( 1 )
179         {
180             int comp;
181
182             if( (node_pt)0 == *w )
183             {
184                 *w = n;
185                 break;
186             }
187
188             comp = strcmp((*w)->value, n->value);
189             if( comp < 0 ) w = &(*w)->next;
190             else w = &(*w)->prev;
191         }
192     }
193
194     (void)fclose(fp);
195
196     dump(name, head, mf, debug);
197
198     rv = PR_GetMallocCount();
199     PR_ClearMallocCountdown();
200
201     release(head);
202
203     return rv;
204 }
205
206 int nf = 0;
207 int debug = 0;
208
209 void
210 test
211 (
212     const char *name
213 )
214 {
215     int n, i;
216
217     extern int nf, debug;
218
219     printf("[%s]: starting test 0\n", name);
220     n = t2(name, 0, debug);
221     if( -1 == n ) return;
222     printf("[%s]: test 0 had %ld allocations.\n", name, n);
223
224     if( 0 >= n ) return;
225
226     for( i = 0; i < nf; i++ )
227     {
228         int which = rand() % n;
229         if( 0 == which ) printf("[%s]: starting test %d -- no allocation should fail\n", name, i+1);
230         else printf("[%s]: starting test %d -- allocation %d should fail\n", name, i+1, which);
231         (void)t2(name, which, debug);
232         printf("[%s]: test %d done.\n", name, i+1);
233     }
234
235     return;
236 }
237
238 int main(int argc, char **argv)
239 {
240     int okay = 0;
241     int multithread = 0;
242
243     struct threadlist
244     {
245         struct threadlist *next;
246         PRThread *thread;
247     }
248         *threadhead = (struct threadlist *)0;
249
250     extern int nf, debug;
251
252     srand(time(0));
253
254     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
255     PR_STDIO_INIT();
256
257     printf("[main]: We %s using the debugging malloc.\n",
258            PR_IsDebuggingMalloc() ? "ARE" : "ARE NOT");
259
260     while( argv++, --argc )
261     {
262         if( '-' == argv[0][0] )
263         {
264             switch( argv[0][1] )
265             {
266                 case 'f':
267                     nf = atoi(argv[0][2] ? &argv[0][2] :
268                               --argc ? *++argv : "0");
269                     break;
270                 case 'd':
271                     debug = 1;
272                     break;
273                 case 'n':
274                     debug = 0;
275                     break;
276                 case 'm':
277                     multithread = 1;
278                     break;
279                 case 's':
280                     multithread = 0;
281                     break;
282                 default:
283                     usage();
284                     break;
285             }
286         }
287         else
288         {
289             FILE *fp = fopen(*argv, "r");
290             if( (FILE *)0 == fp )
291             {
292                 fprintf(stderr, "Cannot open \"%s.\"\n", *argv);
293                 continue;
294             }
295
296             okay++;
297             (void)fclose(fp);
298             if( multithread )
299             {
300                 struct threadlist *n;
301
302                 n = (struct threadlist *)malloc(sizeof(struct threadlist));
303                 if( (struct threadlist *)0 == n ) 
304                 {
305                     fprintf(stderr, "This is getting tedious. \"%s\"\n", *argv);
306                     continue;
307                 }
308
309                 n->next = threadhead;
310                 n->thread = PR_CreateThread(PR_USER_THREAD, (void (*)(void *))test, 
311                                             *argv, PR_PRIORITY_NORMAL, 
312                                             PR_LOCAL_THREAD, PR_JOINABLE_THREAD,
313                                             0);
314                 if( (PRThread *)0 == n->thread )
315                 {
316                     fprintf(stderr, "Can't create thread for \"%s.\"\n", *argv);
317                     continue;
318                 }
319                 else
320                 {
321                     threadhead = n;
322                 }
323             }
324             else
325             {
326                 test(*argv);
327             }
328         }
329     }
330
331     if( okay == 0 ) usage();
332     else while( (struct threadlist *)0 != threadhead )
333     {
334         struct threadlist *x = threadhead->next;
335         (void)PR_JoinThread(threadhead->thread);
336         PR_DELETE(threadhead);
337         threadhead = x;
338     }
339
340     return 0;
341 }
342