1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /***********************************************************************
10 ** Description: Testing malloc (OBSOLETE)
12 ** Modification History:
13 ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
14 ** The debug mode will print all of the printfs associated with this test.
15 ** The regress mode will be the default mode. Since the regress tool limits
16 ** the output to a one line status:PASS or FAIL,all of the printf statements
17 ** have been handled with an if (debug_mode) statement.
18 ***********************************************************************/
31 fprintf(stderr
, "Usage: dbmalloc ('-m'|'-s') '-f' num_fails ('-d'|'-n') filename [...]\n");
35 typedef struct node_struct
37 struct node_struct
*next
, *prev
;
44 node_pt
get_node(const char *line
)
48 rv
= (node_pt
)PR_MALLOC(sizeof(node_t
) + l
+ 1 - 4);
49 if( (node_pt
)0 == rv
) {
52 memcpy(&rv
->value
[0], line
, l
+1);
53 rv
->next
= rv
->prev
= (node_pt
)0;
66 if( (node_pt
)0 != node
->prev
) {
67 dump(name
, node
->prev
, mf
, debug
);
70 printf("[%s]: %6d: %s", name
, node
->line
, node
->value
);
72 if( node
->line
== mf
) {
73 fprintf(stderr
, "[%s]: Line %d was allocated!\n", name
, node
->line
);
75 if( (node_pt
)0 != node
->next
) {
76 dump(name
, node
->next
, mf
, debug
);
87 if( (node_pt
)0 != node
->prev
) {
90 if( (node_pt
)0 != node
->next
) {
107 node_pt head
= (node_pt
)0;
108 char buffer
[ BUFSIZ
];
110 fp
= fopen(name
, "r");
111 if( (FILE *)0 == fp
)
113 fprintf(stderr
, "[%s]: Cannot open \"%s.\"\n", name
, name
);
117 /* fgets mallocs a buffer, first time through. */
118 if( (char *)0 == fgets(buffer
, BUFSIZ
, fp
) )
120 fprintf(stderr
, "[%s]: \"%s\" is empty.\n", name
, name
);
127 if( PR_SUCCESS
!= PR_ClearMallocCount() )
129 fprintf(stderr
, "[%s]: Cannot clear malloc count.\n", name
);
134 if( PR_SUCCESS
!= PR_SetMallocCountdown(mf
) )
136 fprintf(stderr
, "[%s]: Cannot set malloc countdown to %d\n", name
, mf
);
141 while( fgets(buffer
, BUFSIZ
, fp
) )
146 if( (strlen(buffer
) == (BUFSIZ
-1)) && (buffer
[BUFSIZ
-2] != '\n') ) {
147 buffer
[BUFSIZ
-2] == '\n';
152 n
= get_node(buffer
);
153 if( (node_pt
)0 == n
)
155 printf("[%s]: Line %d: malloc failure!\n", name
, l
);
165 if( (node_pt
)0 == *w
)
171 comp
= strcmp((*w
)->value
, n
->value
);
183 dump(name
, head
, mf
, debug
);
185 rv
= PR_GetMallocCount();
186 PR_ClearMallocCountdown();
204 extern int nf
, debug
;
206 printf("[%s]: starting test 0\n", name
);
207 n
= t2(name
, 0, debug
);
211 printf("[%s]: test 0 had %ld allocations.\n", name
, n
);
217 for( i
= 0; i
< nf
; i
++ )
219 int which
= rand() % n
;
221 printf("[%s]: starting test %d -- no allocation should fail\n", name
, i
+1);
224 printf("[%s]: starting test %d -- allocation %d should fail\n", name
, i
+1, which
);
226 (void)t2(name
, which
, debug
);
227 printf("[%s]: test %d done.\n", name
, i
+1);
233 int main(int argc
, char **argv
)
240 struct threadlist
*next
;
243 *threadhead
= (struct threadlist
*)0;
245 extern int nf
, debug
;
249 PR_Init(PR_USER_THREAD
, PR_PRIORITY_NORMAL
, 0);
252 printf("[main]: We %s using the debugging malloc.\n",
253 PR_IsDebuggingMalloc() ? "ARE" : "ARE NOT");
255 while( argv
++, --argc
)
257 if( '-' == argv
[0][0] )
262 nf
= atoi(argv
[0][2] ? &argv
[0][2] :
263 --argc
? *++argv
: "0");
284 FILE *fp
= fopen(*argv
, "r");
285 if( (FILE *)0 == fp
)
287 fprintf(stderr
, "Cannot open \"%s.\"\n", *argv
);
295 struct threadlist
*n
;
297 n
= (struct threadlist
*)malloc(sizeof(struct threadlist
));
298 if( (struct threadlist
*)0 == n
)
300 fprintf(stderr
, "This is getting tedious. \"%s\"\n", *argv
);
304 n
->next
= threadhead
;
305 n
->thread
= PR_CreateThread(PR_USER_THREAD
, (void (*)(void *))test
,
306 *argv
, PR_PRIORITY_NORMAL
,
307 PR_LOCAL_THREAD
, PR_JOINABLE_THREAD
,
309 if( (PRThread
*)0 == n
->thread
)
311 fprintf(stderr
, "Can't create thread for \"%s.\"\n", *argv
);
329 else while( (struct threadlist
*)0 != threadhead
)
331 struct threadlist
*x
= threadhead
->next
;
332 (void)PR_JoinThread(threadhead
->thread
);
333 PR_DELETE(threadhead
);