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
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/
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
15 * The Original Code is the Netscape Portable Runtime (NSPR).
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.
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.
36 * ***** END LICENSE BLOCK ***** */
38 /***********************************************************************
42 ** Description: Testing malloc (OBSOLETE)
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 ***********************************************************************/
63 fprintf(stderr
, "Usage: dbmalloc ('-m'|'-s') '-f' num_fails ('-d'|'-n') filename [...]\n");
67 typedef struct node_struct
69 struct node_struct
*next
, *prev
;
76 node_pt
get_node(const char *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;
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
);
109 if( (node_pt
)0 != node
->prev
) release(node
->prev
);
110 if( (node_pt
)0 != node
->next
) release(node
->next
);
125 node_pt head
= (node_pt
)0;
126 char buffer
[ BUFSIZ
];
128 fp
= fopen(name
, "r");
129 if( (FILE *)0 == fp
)
131 fprintf(stderr
, "[%s]: Cannot open \"%s.\"\n", name
, name
);
135 /* fgets mallocs a buffer, first time through. */
136 if( (char *)0 == fgets(buffer
, BUFSIZ
, fp
) )
138 fprintf(stderr
, "[%s]: \"%s\" is empty.\n", name
, name
);
145 if( PR_SUCCESS
!= PR_ClearMallocCount() )
147 fprintf(stderr
, "[%s]: Cannot clear malloc count.\n", name
);
152 if( PR_SUCCESS
!= PR_SetMallocCountdown(mf
) )
154 fprintf(stderr
, "[%s]: Cannot set malloc countdown to %d\n", name
, mf
);
159 while( fgets(buffer
, BUFSIZ
, fp
) )
164 if( (strlen(buffer
) == (BUFSIZ
-1)) && (buffer
[BUFSIZ
-2] != '\n') )
165 buffer
[BUFSIZ
-2] == '\n';
169 n
= get_node(buffer
);
170 if( (node_pt
)0 == n
)
172 printf("[%s]: Line %d: malloc failure!\n", name
, l
);
182 if( (node_pt
)0 == *w
)
188 comp
= strcmp((*w
)->value
, n
->value
);
189 if( comp
< 0 ) w
= &(*w
)->next
;
190 else w
= &(*w
)->prev
;
196 dump(name
, head
, mf
, debug
);
198 rv
= PR_GetMallocCount();
199 PR_ClearMallocCountdown();
217 extern int nf
, debug
;
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
);
226 for( i
= 0; i
< nf
; i
++ )
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);
250 struct threadlist
*next
;
253 *threadhead
= (struct threadlist
*)0;
255 extern int nf
, debug
;
259 PR_Init(PR_USER_THREAD
, PR_PRIORITY_NORMAL
, 0);
262 printf("[main]: We %s using the debugging malloc.\n",
263 PR_IsDebuggingMalloc() ? "ARE" : "ARE NOT");
265 while( argv
++, --argc
)
267 if( '-' == argv
[0][0] )
272 nf
= atoi(argv
[0][2] ? &argv
[0][2] :
273 --argc
? *++argv
: "0");
294 FILE *fp
= fopen(*argv
, "r");
295 if( (FILE *)0 == fp
)
297 fprintf(stderr
, "Cannot open \"%s.\"\n", *argv
);
305 struct threadlist
*n
;
307 n
= (struct threadlist
*)malloc(sizeof(struct threadlist
));
308 if( (struct threadlist
*)0 == n
)
310 fprintf(stderr
, "This is getting tedious. \"%s\"\n", *argv
);
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
,
319 if( (PRThread
*)0 == n
->thread
)
321 fprintf(stderr
, "Can't create thread for \"%s.\"\n", *argv
);
336 if( okay
== 0 ) usage();
337 else while( (struct threadlist
*)0 != threadhead
)
339 struct threadlist
*x
= threadhead
->next
;
340 (void)PR_JoinThread(threadhead
->thread
);
341 PR_DELETE(threadhead
);