VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/dbmalloc.c@ 1

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 KB
Line 
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
57void
58usage
59(
60 void
61)
62{
63 fprintf(stderr, "Usage: dbmalloc ('-m'|'-s') '-f' num_fails ('-d'|'-n') filename [...]\n");
64 exit(0);
65}
66
67typedef 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
76node_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
87void
88dump
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
103void
104release
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
114int
115t2
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
206int nf = 0;
207int debug = 0;
208
209void
210test
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
238int
239main
240(
241 int argc,
242 char *argv[]
243)
244{
245 int okay = 0;
246 int multithread = 0;
247
248 struct threadlist
249 {
250 struct threadlist *next;
251 PRThread *thread;
252 }
253 *threadhead = (struct threadlist *)0;
254
255 extern int nf, debug;
256
257 srand(time(0));
258
259 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
260 PR_STDIO_INIT();
261
262 printf("[main]: We %s using the debugging malloc.\n",
263 PR_IsDebuggingMalloc() ? "ARE" : "ARE NOT");
264
265 while( argv++, --argc )
266 {
267 if( '-' == argv[0][0] )
268 {
269 switch( argv[0][1] )
270 {
271 case 'f':
272 nf = atoi(argv[0][2] ? &argv[0][2] :
273 --argc ? *++argv : "0");
274 break;
275 case 'd':
276 debug = 1;
277 break;
278 case 'n':
279 debug = 0;
280 break;
281 case 'm':
282 multithread = 1;
283 break;
284 case 's':
285 multithread = 0;
286 break;
287 default:
288 usage();
289 break;
290 }
291 }
292 else
293 {
294 FILE *fp = fopen(*argv, "r");
295 if( (FILE *)0 == fp )
296 {
297 fprintf(stderr, "Cannot open \"%s.\"\n", *argv);
298 continue;
299 }
300
301 okay++;
302 (void)fclose(fp);
303 if( multithread )
304 {
305 struct threadlist *n;
306
307 n = (struct threadlist *)malloc(sizeof(struct threadlist));
308 if( (struct threadlist *)0 == n )
309 {
310 fprintf(stderr, "This is getting tedious. \"%s\"\n", *argv);
311 continue;
312 }
313
314 n->next = threadhead;
315 n->thread = PR_CreateThread(PR_USER_THREAD, (void (*)(void *))test,
316 *argv, PR_PRIORITY_NORMAL,
317 PR_LOCAL_THREAD, PR_JOINABLE_THREAD,
318 0);
319 if( (PRThread *)0 == n->thread )
320 {
321 fprintf(stderr, "Can't create thread for \"%s.\"\n", *argv);
322 continue;
323 }
324 else
325 {
326 threadhead = n;
327 }
328 }
329 else
330 {
331 test(*argv);
332 }
333 }
334 }
335
336 if( okay == 0 ) usage();
337 else while( (struct threadlist *)0 != threadhead )
338 {
339 struct threadlist *x = threadhead->next;
340 (void)PR_JoinThread(threadhead->thread);
341 PR_DELETE(threadhead);
342 threadhead = x;
343 }
344
345 return 0;
346}
347
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette