Bug 462294 - Add "View Video" to context menu for <video> elements. r=gavin, ui...
[wine-gecko.git] / nsprpub / pr / tests / dbmalloc.c
blob4295d0c0e4a48d4b6e1ca2793b223f96ae5c57ed
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
13 * License.
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.
22 * Contributor(s):
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 /***********************************************************************
40 ** Name: dbmalloc.c
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 ***********************************************************************/
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <time.h>
54 #include <string.h>
55 #include "nspr.h"
57 void
58 usage
60 void
63 fprintf(stderr, "Usage: dbmalloc ('-m'|'-s') '-f' num_fails ('-d'|'-n') filename [...]\n");
64 exit(0);
67 typedef struct node_struct
69 struct node_struct *next, *prev;
70 int line;
71 char value[4];
73 node_t,
74 *node_pt;
76 node_pt get_node(const char *line)
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;
87 void
88 dump
90 const char *name,
91 node_pt node,
92 int mf,
93 int debug
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;
103 void
104 release
106 node_pt node
109 if( (node_pt)0 != node->prev ) release(node->prev);
110 if( (node_pt)0 != node->next ) release(node->next);
111 PR_DELETE(node);
117 const char *name,
118 int mf,
119 int debug
122 int rv;
123 FILE *fp;
124 int l = 0;
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);
132 return -1;
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);
139 (void)fclose(fp);
140 return -1;
143 rewind(fp);
145 if( PR_SUCCESS != PR_ClearMallocCount() )
147 fprintf(stderr, "[%s]: Cannot clear malloc count.\n", name);
148 (void)fclose(fp);
149 return -1;
152 if( PR_SUCCESS != PR_SetMallocCountdown(mf) )
154 fprintf(stderr, "[%s]: Cannot set malloc countdown to %d\n", name, mf);
155 (void)fclose(fp);
156 return -1;
159 while( fgets(buffer, BUFSIZ, fp) )
161 node_pt n;
162 node_pt *w = &head;
164 if( (strlen(buffer) == (BUFSIZ-1)) && (buffer[BUFSIZ-2] != '\n') )
165 buffer[BUFSIZ-2] == '\n';
167 l++;
169 n = get_node(buffer);
170 if( (node_pt)0 == n )
172 printf("[%s]: Line %d: malloc failure!\n", name, l);
173 continue;
176 n->line = l;
178 while( 1 )
180 int comp;
182 if( (node_pt)0 == *w )
184 *w = n;
185 break;
188 comp = strcmp((*w)->value, n->value);
189 if( comp < 0 ) w = &(*w)->next;
190 else w = &(*w)->prev;
194 (void)fclose(fp);
196 dump(name, head, mf, debug);
198 rv = PR_GetMallocCount();
199 PR_ClearMallocCountdown();
201 release(head);
203 return rv;
206 int nf = 0;
207 int debug = 0;
209 void
210 test
212 const char *name
215 int n, i;
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);
224 if( 0 >= n ) return;
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);
235 return;
239 main
241 int argc,
242 char *argv[]
245 int okay = 0;
246 int multithread = 0;
248 struct threadlist
250 struct threadlist *next;
251 PRThread *thread;
253 *threadhead = (struct threadlist *)0;
255 extern int nf, debug;
257 srand(time(0));
259 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
260 PR_STDIO_INIT();
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] )
269 switch( argv[0][1] )
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;
292 else
294 FILE *fp = fopen(*argv, "r");
295 if( (FILE *)0 == fp )
297 fprintf(stderr, "Cannot open \"%s.\"\n", *argv);
298 continue;
301 okay++;
302 (void)fclose(fp);
303 if( multithread )
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);
311 continue;
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);
322 continue;
324 else
326 threadhead = n;
329 else
331 test(*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);
342 threadhead = x;
345 return 0;