Back to index

lightning-sunbird  0.9+nobinonly
dbmalloc.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /***********************************************************************
00039 **
00040 ** Name: dbmalloc.c
00041 **
00042 ** Description: Testing malloc (OBSOLETE)
00043 **
00044 ** Modification History:
00045 ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
00046 **              The debug mode will print all of the printfs associated with this test.
00047 **                    The regress mode will be the default mode. Since the regress tool limits
00048 **           the output to a one line status:PASS or FAIL,all of the printf statements
00049 **                    have been handled with an if (debug_mode) statement. 
00050 ***********************************************************************/
00051 #include <stdio.h>
00052 #include <stdlib.h>
00053 #include <time.h>
00054 #include <string.h>
00055 #include "nspr.h"
00056 
00057 void
00058 usage
00059 (
00060     void
00061 )
00062 {
00063     fprintf(stderr, "Usage: dbmalloc ('-m'|'-s') '-f' num_fails ('-d'|'-n') filename [...]\n");
00064     exit(0);
00065 }
00066 
00067 typedef struct node_struct
00068 {
00069     struct node_struct *next, *prev;
00070     int line;
00071     char value[4];
00072 }
00073     node_t,
00074    *node_pt;
00075 
00076 node_pt get_node(const char *line)
00077 {
00078     node_pt rv;
00079     int l = strlen(line);
00080     rv = (node_pt)PR_MALLOC(sizeof(node_t) + l + 1 - 4);
00081     if( (node_pt)0 == rv ) return (node_pt)0;
00082     memcpy(&rv->value[0], line, l+1);
00083     rv->next = rv->prev = (node_pt)0;
00084     return rv;
00085 }
00086 
00087 void
00088 dump
00089 (
00090     const char *name,
00091     node_pt     node,
00092     int         mf,
00093     int         debug
00094 )
00095 {
00096     if( (node_pt)0 != node->prev ) dump(name, node->prev, mf, debug);
00097     if( 0 != debug ) printf("[%s]: %6d: %s", name, node->line, node->value);
00098     if( node->line == mf ) fprintf(stderr, "[%s]: Line %d was allocated!\n", name, node->line);
00099     if( (node_pt)0 != node->next ) dump(name, node->next, mf, debug);
00100     return;
00101 }
00102 
00103 void
00104 release
00105 (
00106     node_pt node
00107 )
00108 {
00109     if( (node_pt)0 != node->prev ) release(node->prev);
00110     if( (node_pt)0 != node->next ) release(node->next);
00111     PR_DELETE(node);
00112 }
00113 
00114 int
00115 t2
00116 (
00117     const char *name,
00118     int         mf,
00119     int         debug
00120 )
00121 {
00122     int rv;
00123     FILE *fp;
00124     int l = 0;
00125     node_pt head = (node_pt)0;
00126     char buffer[ BUFSIZ ];
00127 
00128     fp = fopen(name, "r");
00129     if( (FILE *)0 == fp )
00130     {
00131         fprintf(stderr, "[%s]: Cannot open \"%s.\"\n", name, name);
00132         return -1;
00133     }
00134 
00135     /* fgets mallocs a buffer, first time through. */
00136     if( (char *)0 == fgets(buffer, BUFSIZ, fp) )
00137     {
00138         fprintf(stderr, "[%s]: \"%s\" is empty.\n", name, name);
00139         (void)fclose(fp);
00140         return -1;
00141     }
00142 
00143     rewind(fp);
00144 
00145     if( PR_SUCCESS != PR_ClearMallocCount() )
00146     {
00147         fprintf(stderr, "[%s]: Cannot clear malloc count.\n", name);
00148         (void)fclose(fp);
00149         return -1;
00150     }
00151 
00152     if( PR_SUCCESS != PR_SetMallocCountdown(mf) )
00153     {
00154         fprintf(stderr, "[%s]: Cannot set malloc countdown to %d\n", name, mf);
00155         (void)fclose(fp);
00156         return -1;
00157     }
00158 
00159     while( fgets(buffer, BUFSIZ, fp) )
00160     {
00161         node_pt n;
00162         node_pt *w = &head;
00163 
00164         if( (strlen(buffer) == (BUFSIZ-1)) && (buffer[BUFSIZ-2] != '\n') )
00165             buffer[BUFSIZ-2] == '\n';
00166 
00167         l++;
00168 
00169         n = get_node(buffer);
00170         if( (node_pt)0 == n ) 
00171         {
00172             printf("[%s]: Line %d: malloc failure!\n", name, l);
00173             continue;
00174         }
00175 
00176         n->line = l;
00177 
00178         while( 1 )
00179         {
00180             int comp;
00181 
00182             if( (node_pt)0 == *w )
00183             {
00184                 *w = n;
00185                 break;
00186             }
00187 
00188             comp = strcmp((*w)->value, n->value);
00189             if( comp < 0 ) w = &(*w)->next;
00190             else w = &(*w)->prev;
00191         }
00192     }
00193 
00194     (void)fclose(fp);
00195 
00196     dump(name, head, mf, debug);
00197 
00198     rv = PR_GetMallocCount();
00199     PR_ClearMallocCountdown();
00200 
00201     release(head);
00202 
00203     return rv;
00204 }
00205 
00206 int nf = 0;
00207 int debug = 0;
00208 
00209 void
00210 test
00211 (
00212     const char *name
00213 )
00214 {
00215     int n, i;
00216 
00217     extern int nf, debug;
00218 
00219     printf("[%s]: starting test 0\n", name);
00220     n = t2(name, 0, debug);
00221     if( -1 == n ) return;
00222     printf("[%s]: test 0 had %ld allocations.\n", name, n);
00223 
00224     if( 0 >= n ) return;
00225 
00226     for( i = 0; i < nf; i++ )
00227     {
00228         int which = rand() % n;
00229         if( 0 == which ) printf("[%s]: starting test %d -- no allocation should fail\n", name, i+1);
00230         else printf("[%s]: starting test %d -- allocation %d should fail\n", name, i+1, which);
00231         (void)t2(name, which, debug);
00232         printf("[%s]: test %d done.\n", name, i+1);
00233     }
00234 
00235     return;
00236 }
00237 
00238 int
00239 main
00240 (
00241     int     argc,
00242     char   *argv[]
00243 )
00244 {
00245     int okay = 0;
00246     int multithread = 0;
00247 
00248     struct threadlist
00249     {
00250         struct threadlist *next;
00251         PRThread *thread;
00252     }
00253         *threadhead = (struct threadlist *)0;
00254 
00255     extern int nf, debug;
00256 
00257     srand(time(0));
00258 
00259     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
00260     PR_STDIO_INIT();
00261 
00262     printf("[main]: We %s using the debugging malloc.\n",
00263            PR_IsDebuggingMalloc() ? "ARE" : "ARE NOT");
00264 
00265     while( argv++, --argc )
00266     {
00267         if( '-' == argv[0][0] )
00268         {
00269             switch( argv[0][1] )
00270             {
00271                 case 'f':
00272                     nf = atoi(argv[0][2] ? &argv[0][2] :
00273                               --argc ? *++argv : "0");
00274                     break;
00275                 case 'd':
00276                     debug = 1;
00277                     break;
00278                 case 'n':
00279                     debug = 0;
00280                     break;
00281                 case 'm':
00282                     multithread = 1;
00283                     break;
00284                 case 's':
00285                     multithread = 0;
00286                     break;
00287                 default:
00288                     usage();
00289                     break;
00290             }
00291         }
00292         else
00293         {
00294             FILE *fp = fopen(*argv, "r");
00295             if( (FILE *)0 == fp )
00296             {
00297                 fprintf(stderr, "Cannot open \"%s.\"\n", *argv);
00298                 continue;
00299             }
00300 
00301             okay++;
00302             (void)fclose(fp);
00303             if( multithread )
00304             {
00305                 struct threadlist *n;
00306 
00307                 n = (struct threadlist *)malloc(sizeof(struct threadlist));
00308                 if( (struct threadlist *)0 == n ) 
00309                 {
00310                     fprintf(stderr, "This is getting tedious. \"%s\"\n", *argv);
00311                     continue;
00312                 }
00313 
00314                 n->next = threadhead;
00315                 n->thread = PR_CreateThread(PR_USER_THREAD, (void (*)(void *))test, 
00316                                             *argv, PR_PRIORITY_NORMAL, 
00317                                             PR_LOCAL_THREAD, PR_JOINABLE_THREAD,
00318                                             0);
00319                 if( (PRThread *)0 == n->thread )
00320                 {
00321                     fprintf(stderr, "Can't create thread for \"%s.\"\n", *argv);
00322                     continue;
00323                 }
00324                 else
00325                 {
00326                     threadhead = n;
00327                 }
00328             }
00329             else
00330             {
00331                 test(*argv);
00332             }
00333         }
00334     }
00335 
00336     if( okay == 0 ) usage();
00337     else while( (struct threadlist *)0 != threadhead )
00338     {
00339         struct threadlist *x = threadhead->next;
00340         (void)PR_JoinThread(threadhead->thread);
00341         PR_DELETE(threadhead);
00342         threadhead = x;
00343     }
00344 
00345     return 0;
00346 }
00347