1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
/* Distributed Checksum Clearinghouse heap debugging
*
* Copyright (c) 2005 by Rhyolite Software
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE
* BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*
* Rhyolite Software DCC 1.2.74-1.13 $Revision$
*/
#include "dcc_defs.h"
#include "dcc_heap_debug.h"
#include <stdio.h>
void dcc_malloc_lock(void);
void dcc_malloc_unlock(void);
#ifdef UNIX
#include <unistd.h>
#if defined(FreeBSD) || defined(linux)
#define NO_MALLOC_OPTIONS
#endif
char dcc_malloc_options[] = "A";
#else /* !UNIX */
#include "malloc.h"
#endif /* !UNIX */
typedef u_long SENTINEL;
#define HEAD_SENTINEL_VALUE 0xdeadbeaf
#define TAIL_SENTINEL_VALUE 0xbeafdead
typedef struct mdbg{
SENTINEL head;
struct mdbg *fwd, *bak;
SENTINEL *tail;
} MDBG;
u_int dcc_num_mdbg; /* global to suppress warnings */
MDBG *dcc_mdbg_chain; /* when not used */
MDBG *bad_mp;
int bad_i;
/* leave a few names outside to make some `ar`s happy */
#ifdef DEBUG_HEAP
/* this needs to do as little as possible to avoid calling malloc()
* global and no NRATTRIB to avoid confusion in dumps */
void
dcc_heap_abort(const char *m)
{
#ifdef UNIX
write(STDERR_FILENO, m, strlen(m));
abort();
#else
bad_message_box("heap_debug", 1, m);
#endif
}
char *
dcc_strdup(const char *s)
{
char *p;
p = dcc_malloc(strlen(s)+1);
strcpy(p, s);
return p;
}
void
dcc_malloc_check(void)
{
MDBG *mp;
u_int i;
i = dcc_num_mdbg;
if (!i)
return;
mp = dcc_mdbg_chain;
for (;;) {
if (mp->head != HEAD_SENTINEL_VALUE
|| *mp->tail != TAIL_SENTINEL_VALUE)
dcc_heap_abort("trashed heap sentinel");
if (mp->bak->fwd != mp
|| mp->fwd->bak != mp) {
bad_mp = mp;
bad_i = i;
dcc_heap_abort("malloc chain trashed");
}
if (--i == 0) {
if (mp->fwd != dcc_mdbg_chain) {
bad_mp = mp;
bad_i = i;
dcc_heap_abort("wrong malloc chain too long");
}
#ifndef UNIX
i = _heapchk();
if (i != _HEAPOK && i != _HEAPEMPTY)
dcc_heap_abort("heapchk() failed");
#endif
return;
} else if (mp->fwd == dcc_mdbg_chain) {
bad_mp = mp;
bad_i = i;
dcc_heap_abort("wrong malloc chain too short");
}
mp = mp->fwd;
}
}
void *
dcc_malloc(size_t len)
{
MDBG *mp;
#if defined(UNIX) && !defined(NO_MALLOC_OPTIONS)
if (!malloc_options) {
malloc_options = dcc_malloc_options;
} else if (malloc_options != dcc_malloc_options) {
dcc_heap_abort("malloc_options already set");
}
#endif
dcc_malloc_lock();
dcc_malloc_check();
if (!len)
dcc_heap_abort("malloc(0)");
len += sizeof(MDBG) + sizeof(mp->tail);
len += (sizeof(mp->tail) - len) & (sizeof(mp->tail)-1); /* align tail */
mp = malloc(len);
if (!mp)
return mp;
if (!dcc_num_mdbg) {
mp->fwd = mp->bak = mp;
} else {
mp->bak = dcc_mdbg_chain;
mp->fwd = dcc_mdbg_chain->fwd;
mp->bak->fwd = mp;
mp->fwd->bak = mp;
}
dcc_mdbg_chain = mp;
mp->head = HEAD_SENTINEL_VALUE;
mp->tail = (SENTINEL *)((u_char *)mp+len-sizeof(mp->tail));
*mp->tail = TAIL_SENTINEL_VALUE;
dcc_num_mdbg++;
dcc_malloc_unlock();
return (mp+1);
}
void *
dcc_calloc(size_t n, size_t s)
{
void *p;
s *= n;
if (s == 0)
dcc_heap_abort("zero calloc() size");
p = dcc_malloc(s);
if (!p)
return p;
memset(p, 0, s);
return p;
}
void
dcc_free(void *p)
{
int i;
MDBG *mp;
dcc_malloc_lock();
dcc_malloc_check();
i = dcc_num_mdbg;
mp = dcc_mdbg_chain;
for (;;) {
if (!i)
dcc_heap_abort("freeing non-free");
if (mp+1 == p)
break;
mp = mp->fwd;
i--;
}
if (dcc_mdbg_chain == mp)
dcc_mdbg_chain = mp->fwd;
mp->bak->fwd = mp->fwd;
mp->fwd->bak = mp->bak;
dcc_num_mdbg--;
memset(mp, 0xf1, (u_char *)mp->tail+sizeof(mp->tail)-(u_char *)mp);
dcc_malloc_check();
dcc_malloc_unlock();
free(mp);
}
#endif /* DEBUG_HEAP */
|