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