DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Untracked file

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 * 
 * The Original Code is the Netscape Portable Runtime (NSPR).
 * 
 * The Initial Developer of the Original Code is Netscape
 * Communications Corporation.  Portions created by Netscape are 
 * Copyright (C) 1998-2000 Netscape Communications Corporation.  All
 * Rights Reserved.
 * 
 * Contributor(s):
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL"), in which case the provisions of the GPL are applicable 
 * instead of those above.  If you wish to allow use of your 
 * version of this file only under the terms of the GPL and not to
 * allow others to use your version of this file under the MPL,
 * indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by
 * the GPL.  If you do not delete the provisions above, a recipient
 * may use your version of this file under either the MPL or the
 * GPL.
 */

/***********************************************************************
**
** Name: dbmalloc.c
**
** Description: Testing malloc (OBSOLETE)
**
** Modification History:
** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
**	         The debug mode will print all of the printfs associated with this test.
**			 The regress mode will be the default mode. Since the regress tool limits
**           the output to a one line status:PASS or FAIL,all of the printf statements
**			 have been handled with an if (debug_mode) statement. 
***********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "nspr.h"

void
usage
(
    void
)
{
    fprintf(stderr, "Usage: dbmalloc ('-m'|'-s') '-f' num_fails ('-d'|'-n') filename [...]\n");
    exit(0);
}

typedef struct node_struct
{
    struct node_struct *next, *prev;
    int line;
    char value[4];
}
    node_t,
   *node_pt;

node_pt get_node(const char *line)
{
    node_pt rv;
    int l = strlen(line);
    rv = (node_pt)PR_MALLOC(sizeof(node_t) + l + 1 - 4);
    if( (node_pt)0 == rv ) return (node_pt)0;
    memcpy(&rv->value[0], line, l+1);
    rv->next = rv->prev = (node_pt)0;
    return rv;
}

void
dump
(
    const char *name,
    node_pt     node,
    int         mf,
    int         debug
)
{
    if( (node_pt)0 != node->prev ) dump(name, node->prev, mf, debug);
    if( 0 != debug ) printf("[%s]: %6d: %s", name, node->line, node->value);
    if( node->line == mf ) fprintf(stderr, "[%s]: Line %d was allocated!\n", name, node->line);
    if( (node_pt)0 != node->next ) dump(name, node->next, mf, debug);
    return;
}

void
release
(
    node_pt node
)
{
    if( (node_pt)0 != node->prev ) release(node->prev);
    if( (node_pt)0 != node->next ) release(node->next);
    PR_DELETE(node);
}

int
t2
(
    const char *name,
    int         mf,
    int         debug
)
{
    int rv;
    FILE *fp;
    int l = 0;
    node_pt head = (node_pt)0;
    char buffer[ BUFSIZ ];

    fp = fopen(name, "r");
    if( (FILE *)0 == fp )
    {
        fprintf(stderr, "[%s]: Cannot open \"%s.\"\n", name, name);
        return -1;
    }

    /* fgets mallocs a buffer, first time through. */
    if( (char *)0 == fgets(buffer, BUFSIZ, fp) )
    {
        fprintf(stderr, "[%s]: \"%s\" is empty.\n", name, name);
        (void)fclose(fp);
        return -1;
    }

    rewind(fp);

    if( PR_SUCCESS != PR_ClearMallocCount() )
    {
        fprintf(stderr, "[%s]: Cannot clear malloc count.\n", name);
        (void)fclose(fp);
        return -1;
    }

    if( PR_SUCCESS != PR_SetMallocCountdown(mf) )
    {
        fprintf(stderr, "[%s]: Cannot set malloc countdown to %d\n", name, mf);
        (void)fclose(fp);
        return -1;
    }

    while( fgets(buffer, BUFSIZ, fp) )
    {
        node_pt n;
        node_pt *w = &head;

        if( (strlen(buffer) == (BUFSIZ-1)) && (buffer[BUFSIZ-2] != '\n') )
            buffer[BUFSIZ-2] == '\n';

        l++;

        n = get_node(buffer);
        if( (node_pt)0 == n ) 
        {
            printf("[%s]: Line %d: malloc failure!\n", name, l);
            continue;
        }

        n->line = l;

        while( 1 )
        {
            int comp;

            if( (node_pt)0 == *w )
            {
                *w = n;
                break;
            }

            comp = strcmp((*w)->value, n->value);
            if( comp < 0 ) w = &(*w)->next;
            else w = &(*w)->prev;
        }
    }

    (void)fclose(fp);

    dump(name, head, mf, debug);

    rv = PR_GetMallocCount();
    PR_ClearMallocCountdown();

    release(head);

    return rv;
}

int nf = 0;
int debug = 0;

void
test
(
    const char *name
)
{
    int n, i;

    extern int nf, debug;

    printf("[%s]: starting test 0\n", name);
    n = t2(name, 0, debug);
    if( -1 == n ) return;
    printf("[%s]: test 0 had %ld allocations.\n", name, n);

    if( 0 >= n ) return;

    for( i = 0; i < nf; i++ )
    {
        int which = rand() % n;
        if( 0 == which ) printf("[%s]: starting test %d -- no allocation should fail\n", name, i+1);
        else printf("[%s]: starting test %d -- allocation %d should fail\n", name, i+1, which);
        (void)t2(name, which, debug);
        printf("[%s]: test %d done.\n", name, i+1);
    }

    return;
}

int
main
(
    int     argc,
    char   *argv[]
)
{
    int okay = 0;
    int multithread = 0;

    struct threadlist
    {
        struct threadlist *next;
        PRThread *thread;
    }
        *threadhead = (struct threadlist *)0;

    extern int nf, debug;

    srand(time(0));

    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
    PR_STDIO_INIT();

    printf("[main]: We %s using the debugging malloc.\n",
           PR_IsDebuggingMalloc() ? "ARE" : "ARE NOT");

    while( argv++, --argc )
    {
        if( '-' == argv[0][0] )
        {
            switch( argv[0][1] )
            {
                case 'f':
                    nf = atoi(argv[0][2] ? &argv[0][2] :
                              --argc ? *++argv : "0");
                    break;
                case 'd':
                    debug = 1;
                    break;
                case 'n':
                    debug = 0;
                    break;
                case 'm':
                    multithread = 1;
                    break;
                case 's':
                    multithread = 0;
                    break;
                default:
                    usage();
                    break;
            }
        }
        else
        {
            FILE *fp = fopen(*argv, "r");
            if( (FILE *)0 == fp )
            {
                fprintf(stderr, "Cannot open \"%s.\"\n", *argv);
                continue;
            }

            okay++;
            (void)fclose(fp);
            if( multithread )
            {
                struct threadlist *n;

                n = (struct threadlist *)malloc(sizeof(struct threadlist));
                if( (struct threadlist *)0 == n ) 
                {
                    fprintf(stderr, "This is getting tedious. \"%s\"\n", *argv);
                    continue;
                }

                n->next = threadhead;
                n->thread = PR_CreateThread(PR_USER_THREAD, (void (*)(void *))test, 
                                            *argv, PR_PRIORITY_NORMAL, 
                                            PR_LOCAL_THREAD, PR_JOINABLE_THREAD,
                                            0);
                if( (PRThread *)0 == n->thread )
                {
                    fprintf(stderr, "Can't create thread for \"%s.\"\n", *argv);
                    continue;
                }
                else
                {
                    threadhead = n;
                }
            }
            else
            {
                test(*argv);
            }
        }
    }

    if( okay == 0 ) usage();
    else while( (struct threadlist *)0 != threadhead )
    {
        struct threadlist *x = threadhead->next;
        (void)PR_JoinThread(threadhead->thread);
        PR_DELETE(threadhead);
        threadhead = x;
    }

    return 0;
}