jdk/src/share/demo/jvmti/hprof/debug_malloc.c
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 10292 ed7db6a12c2a
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/* **************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Set of malloc/realloc/calloc/strdup/free replacement macros that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *    insert some extra words around each allocation for debugging purposes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *    and also attempt to detect invalid uses of the malloc heap through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *    various tricks like inserting clobber words at the head and tail of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *    the user's area, delayed free() calls, and setting the memory to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *    a fixed pattern on allocation and when freed.  The allocations also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *    can include warrants so that when an area is clobbered, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *    package can report where the allocation took place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *    The macros included are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *              malloc(size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *              realloc(ptr,size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *              calloc(nelem,elsize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *              strdup(s1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *              free(ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *              malloc_police()   <--- Not a system function
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *    The above macros match the standard behavior of the system functions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *    They should be used through the include file "debug_malloc.h".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *       IMPORTANT: All source files that call any of these macros
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *                  should include debug_malloc.h. This package will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *                  not work if the memory isn't allocated and freed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *                  by the macros in debug_malloc.h. The important issue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *                  is that any malloc() from debug_malloc.h must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *                  freed by the free() in debug_malloc.h.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *    The macros in debug_malloc.h will override the normal use of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *       malloc, realloc, calloc, strdup, and free with the functions below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *    These functions include:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *         void *debug_malloc(size_t, void*, int);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *         void *debug_realloc(void*, size_t, void*, int);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *         void *debug_calloc(size_t, size_t, void*, int);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *         void  debug_free(void *, void*, int);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *   In addition the function debug_malloc_police() can be called to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *      tell you what memory has not been freed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *         void debug_malloc_police(void*, int);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *      The function debug_malloc_police() is available through the macro
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *      malloc_police(). Normally you would want to call this at exit()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *      time to find out what memory is still allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *   The variable malloc_watch determines if the warrants are generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *      warrants are structures that include the filename and line number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *      of the caller who allocated the memory. This structure is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *      at the tail of the malloc space, which is allocated large enough
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *      to hold some clobber words at the head and tail, the user's request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *      and the warrant record (if malloc_watch is non-zero).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *   The macro LEFT_OVER_CHAR is what the trailing bytes of an allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *     are set to (when the allocation is not a multiple of 8) on allocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *     At free(0 time, these bytes are double checked to make sure they were
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *     not clobbered. To remove this feature #undef LEFT_OVER_CHAR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *   The memory freed will have the FREED_CHAR put into it. To remove this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *     feature #undef FREED_CHAR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *   The memory allocated (not calloc'd) will have the ALLOC_CHAR put into it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *     at the time of allocation. To remove this feature #undef ALLOC_CHAR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *   The macro MAX_FREE_DELAY_COUNT controls how many free blocks will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *     be kept around before being freed. This creates a delayed affect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *     so that free space that gets clobbered just might get detected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *     The free() call will immediately set the user space to the FREED_CHAR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *     leaving the clobber words and warrant in place (making sure they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *     haven't been clobbered). Then the free() pointer is added to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *     queue of MAX_FREE_DELAY_COUNT long, and if the queue was full, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *     oldest free()'d memory is actually freed, getting it's entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *     memory length set to the FREED_CHAR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *  WARNING: This can significantly slow down an application, depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *           on how many allocations are made. Also the additional memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *           needed for the clobber words and the warrants can be significant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *           again, depending on how many allocations are made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *           In addition, the delayed free calls can create situations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *           where you might run out of memory prematurely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * **************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
#include <ctype.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
#include <stdarg.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
#include "hprof.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
/* ***************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * Space normally looks like (clobber Word is 64 bits and aligned to 8 bytes):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *                  -----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * malloc/free get->| clobber Word  |   ---> contains -size requested by user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *                  -----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *    User gets --->| user space    |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *                  |               |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *                  |  | left_over  |  ---> left_over bytes will be <= 7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *                  -----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *                  | clobber Word  |   ---> contains -size requested by user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *                  -----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *                  |   Warrant     |   ---> Optional (malloc_watch!=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *                  |               |        Contains filename and line number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *                  |               |          where allocation happened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *                  |               |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 *                  -----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 ***************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *  Flag that tells debug_malloc/debug_free/debug_realloc to police
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *   heap space usage. (This is a dynamic flag that can be turned on/off)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
static int      malloc_watch = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
/* Character to stuff into freed space */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
#define FREED_CHAR  'F'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
/* Character to stuff into allocated space */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
#define ALLOC_CHAR  'A'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
/* Character to stuff into left over trailing bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
#define LEFT_OVER_CHAR  'Z'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
/* Number of 'free' calls that will be delayed until the end */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
#define MAX_FREE_DELAY_COUNT    1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
#undef MAX_FREE_DELAY_COUNT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
/* Maximum name of __FILE_ stored in each malloc'd area */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
#define WARRANT_NAME_MAX (32-1) /* 1 less than multiple of 8 is best */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
/* Macro to convert a user pointer to the malloc pointer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
#define user2malloc_(uptr)   (((char*)(void*)uptr)-sizeof(Word))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
/* Macro to convert a macro pointer to the user pointer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
#define malloc2user_(mptr)   (((char*)(void*)(mptr))+sizeof(Word))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
/* Size of the warrant record (this is dynamic) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
#define warrant_space  ( malloc_watch?sizeof(Warrant_Record):0 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
/* Macro to round up a number of bytes to a multiple of sizeof(Word) bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
#define round_up_(n) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        ((n)==0?0:(sizeof(Word)+(((n)-1)/sizeof(Word))*sizeof(Word)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
/* Macro to calculate the needed malloc bytes from the user's request. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
#define rbytes_(nbytes) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    (size_t)( sizeof(Word) + round_up_(nbytes) + sizeof(Word) + warrant_space )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
/* Macro to get the -size stored in space through the malloc pointer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
#define nsize1_(mptr)           (((Word*)(void*)(mptr))->nsize1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
#define nsize2_(mptr)           (((Word*)(void*)(mptr))->nsize2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
/* Macro to get the -size stored in the tail of the space through */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
/*     the malloc pointer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
#define tail_nsize1_(mptr)     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        nsize1_(((char*)(void*)(mptr))+round_up_(-nsize1_(mptr))+sizeof(Word))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
#define tail_nsize2_(mptr)     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        nsize2_(((char*)(void*)(mptr))+round_up_(-nsize1_(mptr))+sizeof(Word))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
/* Macro to get the -size stored in space through the user pointer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
#define user_nsize1_(uptr)      nsize1_(user2malloc_(uptr))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
#define user_nsize2_(uptr)      nsize2_(user2malloc_(uptr))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
/* Macro to get the -size stored in the tail of the space through */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
/*     the user pointer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
#define user_tail_nsize1_(uptr) tail_nsize1_(user2malloc_(uptr))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
#define user_tail_nsize2_(uptr) tail_nsize2_(user2malloc_(uptr))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
/* Macro to get the int* of the last 32bit word of user space */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
#define last_user_word_(mptr)   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        ((int*)(((char*)(void*)(mptr))+round_up_(-nsize1_(mptr))))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
/* Macros to get at the warrant contents from the malloc pointer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
#define warrant_(mptr) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
  (*((Warrant_Record*)(void*)(((char*)(void*)(mptr))+round_up_(-nsize1_(mptr))+sizeof(Word)*2)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
/* This struct is allocated after the tail clobber word if malloc_watch */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
/*    is true. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    void           *link;       /* Next mptr in list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    char            name[WARRANT_NAME_MAX + 1]; /* Name of allocator */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    int             line;       /* Line number where allocated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    int             id;         /* Nth allocation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
}               Warrant_Record;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
#define warrant_link_(mptr) warrant_(mptr).link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
#define warrant_name_(mptr) warrant_(mptr).name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
#define warrant_line_(mptr) warrant_(mptr).line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
#define warrant_id_(mptr)   warrant_(mptr).id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
#define MFILE(mptr) (malloc_watch?warrant_name_(mptr):"?")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
#define MLINE(mptr) (malloc_watch?warrant_line_(mptr):0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
#define MID(mptr)   (malloc_watch?warrant_id_(mptr):0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
/* This should be one machine word and is also the clobber word struct */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    int             nsize1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    int             nsize2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
}               Word;           /* Largest basic type , sizeof(double)? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
/* The first malloc pointer for the warrants */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
static void    *first_warrant_mptr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
/* Counter of allocations */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
static int id_counter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
static int largest_size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
static void * largest_addr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
static void * smallest_addr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
/* Used to isolate what the error is */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
static char *debug_check;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
static void *clobbered_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
/* Minumum macro */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
#define minimum(a,b) ((a)<(b)?(a):(b))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
/* Message routine */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
error_message(const char * format, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    FILE *error_fp = stderr; /* All debug_malloc.c messages */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    va_list ap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    va_start(ap, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    (void)fprintf(error_fp, "debug_malloc: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    (void)vfprintf(error_fp, format, ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    (void)fprintf(error_fp, "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    (void)fflush(error_fp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    va_end(ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
/* This function prints out a memory error for the memory function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 *   'name' which was called in file 'file' at line number 'line'.  The malloc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 *   pointer with the error is in 'mptr'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
memory_error(void *mptr, const char *name, int mid, const char *mfile, int mline, const char *file, int line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    char  nice_words[512];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    char  temp[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    int   len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    void *mptr_walk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    if (name == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        name = "UNKNOWN_NAME";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    if (file == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        file = "UNKNOWN_FILE";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    md_system_error(temp, (int)sizeof(temp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    (void)strcpy(nice_words, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    if ( debug_check!=NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
       (void)md_snprintf(nice_words, sizeof(nice_words),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    "%s The %s at %p appears to have been hit.",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    temp, debug_check, clobbered_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    len = -nsize1_(mptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    error_message("Error: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                   "%s The malloc space #%d is at %p [user size=%d(0x%x)],"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                   " and was allocated from file \"%s\" at line %d."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                   " [The debug function %s() detected this error "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                   "in file \"%s\" at line %d.]",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                   nice_words, mid, mptr, len, len, mfile, mline,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                   name, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /* Print out contents of this allocation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        void *uptr = malloc2user_(mptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        char *pmess;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        pmess = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        for(i=0;i<(int)sizeof(temp);i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            int ch = ((unsigned char*)uptr)[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if ( isprint(ch) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                *pmess++ = ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                *pmess++ = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                *pmess++ = 'x';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                (void)sprintf(pmess,"%02x",ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                pmess+=2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        *pmess = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        error_message("Error: %p contains user data: %s", uptr, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /* Try and print out table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    if (!malloc_watch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    mptr_walk = first_warrant_mptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    if (mptr_walk != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        error_message("Active allocations: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
           "count=%d, largest_size=%d, address range (%p,%p)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        id_counter, largest_size, smallest_addr, largest_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            int size1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            int size2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            char *mfile_walk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            if ( mptr_walk > largest_addr || mptr_walk < smallest_addr ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                error_message("Terminating list due to pointer corruption");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            size1 = -nsize1_(mptr_walk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            size2 = -nsize2_(mptr_walk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            mfile_walk = MFILE(mptr_walk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            error_message("#%d: addr=%p size1=%d size2=%d file=\"%.*s\" line=%d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                MID(mptr_walk), mptr_walk, size1, size2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                WARRANT_NAME_MAX, mfile_walk, MLINE(mptr_walk));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if ( size1 != size2 || size1 > largest_size || size1 < 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                error_message("Terminating list due to size corruption");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            mptr_walk = warrant_link_(mptr_walk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        } while (mptr_walk != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
/* This function sets the clobber word and sets up the warrant for the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
 *   malloc pointer "mptr".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
setup_space_and_issue_warrant(void *mptr, size_t size, const char *file, int line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    register int    nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    nbytes = (int)size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    if ( nbytes > largest_size || largest_addr == NULL ) largest_size = nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    if ( mptr > largest_addr ) largest_addr = mptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    if ( mptr < smallest_addr || smallest_addr == NULL ) smallest_addr = mptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /* Must be done first: */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    nsize1_(mptr) = -nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    nsize2_(mptr) = -nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    tail_nsize1_(mptr) = -nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    tail_nsize2_(mptr) = -nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
#ifdef LEFT_OVER_CHAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /* Fill in those few extra bytes just before the tail Word structure */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        register int    trailing_extra_bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        /* LINTED */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        trailing_extra_bytes = (int) (round_up_(nbytes) - nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (  trailing_extra_bytes > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            register char  *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            register int    i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            p = ((char *) mptr) + sizeof(Word) + nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            for (i = 0; i < trailing_extra_bytes; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                p[i] = LEFT_OVER_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /* Fill out warrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    if (malloc_watch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        static Warrant_Record zero_warrant;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        register void  *p1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                       *p2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        size_t len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        int start_pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        warrant_(mptr) = zero_warrant;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        p1 = warrant_name_(mptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        len = strlen(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if ( len >  WARRANT_NAME_MAX )  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            start_pos = (int)len - WARRANT_NAME_MAX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        p2 = ((char*)file) + start_pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        (void) memcpy(p1, p2, minimum(((int)len), WARRANT_NAME_MAX));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        warrant_line_(mptr) = line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        warrant_id_(mptr)   = ++id_counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        warrant_link_(mptr) = first_warrant_mptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        first_warrant_mptr = mptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
/* This function checks the clobber words at the beginning and end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
 *   allocated space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
memory_check(void *uptr, int mid, const char *mfile, int mline, const char *file, int line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    int             neg_nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    int             nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    debug_check = "pointer value itself";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    clobbered_ptr = uptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    if (uptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        memory_error((void *) NULL, "memory_check", mid, mfile, mline, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /* Check both Word structures */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    debug_check = "first beginning clobber word";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    clobbered_ptr = (char*)&user_nsize1_(uptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    neg_nbytes = user_nsize1_(uptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    if (neg_nbytes >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        memory_error(user2malloc_(uptr), "memory_check", mid, mfile, mline, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    debug_check = "second beginning clobber word";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    clobbered_ptr = (char*)&user_nsize2_(uptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    if (neg_nbytes != user_nsize2_(uptr))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        memory_error(user2malloc_(uptr), "memory_check", mid, mfile, mline, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    debug_check = "first ending clobber word";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    clobbered_ptr = (char*)&user_tail_nsize1_(uptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    if (neg_nbytes != user_tail_nsize1_(uptr))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        memory_error(user2malloc_(uptr), "memory_check", mid, mfile, mline, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    debug_check = "second ending clobber word";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    clobbered_ptr = (char*)&user_tail_nsize2_(uptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    if (neg_nbytes != user_tail_nsize2_(uptr))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        memory_error(user2malloc_(uptr), "memory_check", mid, mfile, mline, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /* Get a positive count of bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    nbytes = -neg_nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
#ifdef LEFT_OVER_CHAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        /* Check those few extra bytes just before the tail Word structure */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        register int    trailing_extra_bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        register int    i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        register char  *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        /* LINTED */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        trailing_extra_bytes = (int) (round_up_(nbytes) - nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        p = ((char *) (uptr)) + nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        debug_check = "trailing left over area";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        for (i = 0; i < trailing_extra_bytes; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            clobbered_ptr = p+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            if (p[i] != LEFT_OVER_CHAR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                memory_error(user2malloc_(uptr), "memory_check", mid, mfile, mline, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    /* Make sure debug_check is cleared */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    debug_check = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
/* This function looks for the given malloc pointer in the police line up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
 *   and removes it from the warrant list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
 *      mptr            The pointer to the malloc space being removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
remove_warrant(void *mptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    void           *mptr1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                   *last_mptr1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    /* Free it up from the list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    if (malloc_watch && mptr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        int found;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        found = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        last_mptr1 = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        mptr1 = first_warrant_mptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        while (mptr1 != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            if (mptr1 == mptr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                if (last_mptr1 == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    first_warrant_mptr = warrant_link_(mptr1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    warrant_link_(last_mptr1) = warrant_link_(mptr1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                found = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            last_mptr1 = mptr1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            mptr1 = warrant_link_(mptr1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        return found;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
actual_free(void *uptr, const char *file, int line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    void *mptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    const char *mfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    int mline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    int mid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    if ( uptr == NULL )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    mptr = user2malloc_(uptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    memory_check(uptr, (mid=MID(mptr)), (mfile=MFILE(mptr)), (mline=MLINE(mptr)), file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    if (malloc_watch && remove_warrant(mptr)==0 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        memory_check(uptr, mid, mfile, mline, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
#ifdef FREED_CHAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    if ( mptr!=NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        size_t nbytes = -nsize1_(mptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        /* LINTED */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        (void)memset(mptr, FREED_CHAR, rbytes_(nbytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    free(mptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
#ifdef MAX_FREE_DELAY_COUNT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
static void *free_delay[MAX_FREE_DELAY_COUNT];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
static int free_delay_pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
delayed_free(void *uptr, const char* file, int line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    void *mptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    void *olduptr = free_delay[free_delay_pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    size_t nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    if ( uptr==NULL )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    mptr = user2malloc_(uptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    memory_check(uptr, MID(mptr), MFILE(mptr), MLINE(mptr), file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    if ( olduptr!=NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        actual_free(olduptr, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    free_delay[free_delay_pos] = uptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    free_delay_pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    free_delay_pos = free_delay_pos % MAX_FREE_DELAY_COUNT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    nbytes = -user_nsize1_(uptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
#ifdef FREED_CHAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    (void)memset(uptr, FREED_CHAR, (size_t)nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
delayed_free_all(const char *file, int line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    for ( i=0; i< MAX_FREE_DELAY_COUNT; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        void *olduptr = free_delay[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        free_delay[i] = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        if ( olduptr!=NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            actual_free(olduptr, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
debug_free(void *uptr, const char *file, int line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    int mid = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    if (uptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        memory_error((void *) NULL, "debug_free", mid, file, line, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
#ifdef MAX_FREE_DELAY_COUNT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    delayed_free(uptr, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    actual_free(uptr, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
/* This function calls malloc(). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
void           *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
debug_malloc(size_t nbytes, const char *file, int line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    void           *mptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    void           *uptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    int mid = id_counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    if ((int)nbytes <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        memory_error((void *) NULL, "debug_malloc", mid, file, line, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /* LINTED */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    mptr = malloc(rbytes_(nbytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    if (mptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        memory_error((void *) NULL, "debug_malloc", mid, file, line, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    setup_space_and_issue_warrant(mptr, nbytes, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    uptr = malloc2user_(mptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
#ifdef ALLOC_CHAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    (void)memset(uptr, ALLOC_CHAR, (size_t)nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    return uptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
void           *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
debug_realloc(void *uptr, size_t nbytes, const char *file, int line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    void           *mptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    void           *oldmptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    void           *newuptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    size_t         oldnbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    int mid = id_counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    oldmptr = user2malloc_(uptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    oldnbytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    if ((int)nbytes <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        memory_error(oldmptr, "debug_realloc", mid, file, line, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    if (uptr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        memory_check(uptr, MID(oldmptr), MFILE(oldmptr), MLINE(oldmptr), file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        oldnbytes = -user_nsize1_(uptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        if ( malloc_watch && remove_warrant(oldmptr)==0 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            memory_check(uptr, MID(oldmptr), MFILE(oldmptr), MLINE(oldmptr), file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    if (uptr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        /* LINTED */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        mptr = malloc(rbytes_(nbytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        /* LINTED */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        mptr = realloc(oldmptr, rbytes_(nbytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    if (mptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        memory_error(oldmptr, "debug_realloc", mid, file, line, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    setup_space_and_issue_warrant(mptr, nbytes, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    newuptr = malloc2user_(mptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
#ifdef ALLOC_CHAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    if (uptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        (void)memset(newuptr, ALLOC_CHAR, (size_t)nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    else if ( nbytes > oldnbytes )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        (void)memset(((char*)newuptr)+oldnbytes, ALLOC_CHAR, (size_t)nbytes-oldnbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    return newuptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
/* This function calls calloc(). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
void           *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
debug_calloc(size_t nelem, size_t elsize, const char *file, int line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    void           *mptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    size_t          nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    int mid = id_counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    nbytes = nelem*elsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    if ((int)nbytes <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        memory_error((void *) NULL, "debug_calloc", mid, file, line, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    /* LINTED */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    mptr = calloc(rbytes_(nbytes),1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    if (mptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        memory_error((void *) NULL, "debug_calloc", mid, file, line, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    setup_space_and_issue_warrant(mptr, nbytes, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    return malloc2user_(mptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
/* This function replaces strdup(). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
char           *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
debug_strdup(const char *s1, const char *file, int line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    void           *mptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    void           *uptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    size_t          nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    int mid = id_counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    if (s1 == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        memory_error((void *) NULL, "debug_strdup", mid, file, line, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    nbytes = strlen(s1)+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    if ((int)nbytes < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        memory_error((void *) NULL, "debug_strdup", mid, file, line, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    /* LINTED */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    mptr = malloc(rbytes_(nbytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    if (mptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        memory_error((void *) NULL, "debug_strdup", mid, file, line, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    setup_space_and_issue_warrant(mptr, nbytes, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    uptr = malloc2user_(mptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    (void)strcpy((char*)uptr, s1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    return (char*)uptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
debug_malloc_verify(const char *file, int line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    void           *mptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
#ifdef MAX_FREE_DELAY_COUNT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    delayed_free_all(file,line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    if (!malloc_watch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    mptr = first_warrant_mptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    if (mptr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        /* Check all this memory first */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            memory_check(malloc2user_(mptr), MID(mptr), MFILE(mptr), MLINE(mptr), file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            mptr = warrant_link_(mptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        } while (mptr != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
/* Report outstanding space warrants to console. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
debug_malloc_police(const char *file, int line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    void           *mptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
#ifdef MAX_FREE_DELAY_COUNT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    delayed_free_all(file,line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    if (!malloc_watch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    mptr = first_warrant_mptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    if (mptr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        debug_malloc_verify(file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        /* Now issue warrants */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        mptr = first_warrant_mptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            error_message("Outstanding space warrant: %p (%d bytes) allocated by %s at line %d, allocation #%d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
               mptr, -nsize1_(mptr), warrant_name_(mptr),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
               warrant_line_(mptr), warrant_id_(mptr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            mptr = warrant_link_(mptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        } while (mptr != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
debug_malloc_verify(const char *file, int line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    file = file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    line = line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
debug_malloc_police(const char *file, int line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    file = file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    line = line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
#endif