1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
/*
* FILE: memory.c
* PROGRAM: RAT
* AUTHOR: Isidor Kouvelas + Colin Perkins + Mark Handley + Orion Hodson
*
* $Revision: 1.6 $
* $Date: 1999/05/09 22:22:54 $
*
* Copyright (c) 1995-98 University College London
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, is permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the Computer Science
* Department at University College London
* 4. Neither the name of the University nor of the Department may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "config_unix.h"
#include "config_win32.h"
#include "debug.h"
#include "memory.h"
#include "util.h"
#ifdef DEBUG_MEM
#define MAX_ADDRS 65536
typedef struct s_alloc_blk {
int *addr; /* address */
char *filen; /* file where allocated */
int line; /* line where allocated */
size_t length; /* size of allocation */
int blen; /* size passed to block_alloc (if relevent) */
int est; /* order in all allocation's */
} alloc_blk;
static alloc_blk mem_item[MAX_ADDRS];
static int naddr = 0;
static int tick = 0;
static int init = 0;
#endif
void xdoneinit(void)
{
#ifdef DEBUG_MEM
init = tick++;
#endif
}
void xmemchk(void)
{
#ifdef DEBUG_MEM
int i,j;
if (naddr > MAX_ADDRS) {
printf("ERROR: Too many addresses for xmemchk()!\n");
abort();
}
for (i=0; i<naddr; i++) {
assert(mem_item[i].addr != NULL);
assert((size_t) strlen(mem_item[i].filen) == mem_item[i].length);
if (*mem_item[i].addr != *(mem_item[i].addr + (*mem_item[i].addr / 4) + 2)) {
printf("Memory check failed!\n");
j = i;
for (i=0; i<naddr; i++) {
if (i==j) { /* Because occasionally you can't spy the */
printf("* "); /* deviant block, we'll highlight it. */
} else {
printf(" ");
}
printf("addr: %p", mem_item[i].addr); fflush(stdout);
printf(" size: %6d", *mem_item[i].addr); fflush(stdout);
printf(" size: %6d", *(mem_item[i].addr + (*mem_item[i].addr / 4) + 2)); fflush(stdout);
printf(" age: %6d", tick - mem_item[i].est); fflush(stdout);
printf(" file: %s", mem_item[i].filen); fflush(stdout);
printf(" line: %d", mem_item[i].line); fflush(stdout);
printf("\n");
}
abort();
}
}
#endif
}
void xmemdmp(void)
{
#ifdef DEBUG_MEM
int i;
block_release_all();
if (naddr > MAX_ADDRS) {
printf("ERROR: Too many addresses for xmemchk()!\n");
abort();
}
for (i=0; i<naddr; i++) {
printf("%5d",i); fflush(stdout);
printf(" addr: %p", mem_item[i].addr); fflush(stdout);
printf(" size: %5d", *mem_item[i].addr); fflush(stdout);
printf(" age: %6d", tick - mem_item[i].est); fflush(stdout);
printf(" file: %s", mem_item[i].filen); fflush(stdout);
printf(":%d", mem_item[i].line); fflush(stdout);
if (mem_item[i].blen != 0) {
printf(" \tblen %d", mem_item[i].blen);
fflush(stdout);
}
printf("\n");
}
printf("Program initialisation finished at age %6d\n", tick-init);
#endif
}
/* Because block_alloc recycles blocks we need to know which code
* fragment takes over responsibility for the memory. */
void xclaim(void *addr, const char *filen, int line)
{
#ifdef DEBUG_MEM
int i;
char *x = (char*)addr - 8;
addr = (int*)x;
for (i = 0; i < naddr; i++) {
if (mem_item[i].addr == addr) {
free(mem_item[i].filen);
mem_item[i].filen = strdup(filen);
mem_item[i].length = strlen(filen);
mem_item[i].line = line;
mem_item[i].est = tick++;
break;
}
}
assert(i != naddr); /* Not found */
#else
UNUSED(addr);
UNUSED(filen);
UNUSED(line);
#endif /* DEBUG_MEM */
}
void xfree(void *y)
{
#ifdef DEBUG_MEM
char *x = (char *) y;
int i, j;
int *a, *b, *c;
if (x == NULL) {
printf("ERROR: Attempt to free NULL pointer!\n");
abort();
}
j = 0;
c = (int *) (x - 8);
for (i = 0; i < naddr - j; i++) {
if (mem_item[i].addr == c) {
if (j != 0) {
printf("ERROR: Attempt to free memory twice! (addr=%p)\n", y);
abort();
}
j = 1;
/* Trash the contents of the memory we're about to free... */
b = (int *) (x + *((int *)(x-8)));
for (a = (int *) (x-8); a < b; a++) {
*a = 0xdeadbeef;
}
/* ...and free it! */
free(x - 8);
free(mem_item[i].filen);
}
memmove(mem_item+i,mem_item+i+j,sizeof(alloc_blk));
}
if (j != 1) {
printf("ERROR: Attempt to free memory which was never allocated! (addr=%p)\n", y);
abort();
}
naddr -= j;
xmemchk();
#else
free(y);
#endif
}
void *
_xmalloc(unsigned size, const char *filen, int line)
{
#ifdef DEBUG_MEM
int s = (((size + 7)/8)*8) + 8;
int *p = (int *) malloc(s + 16);
int q;
assert(p != NULL);
assert(filen != NULL);
*p = s;
*(p+(s/4)+2) = s;
for (q = 0; q < s/4; q++) {
*(p+q+2) = rand();
}
mem_item[naddr].addr = p;
mem_item[naddr].filen = (char *) strdup(filen);
mem_item[naddr].line = line;
mem_item[naddr].length = strlen(filen);
mem_item[naddr].est = tick++;
if (++naddr >= MAX_ADDRS) {
printf("ERROR: Allocated too much! Table overflow in xmalloc()\n");
printf(" Do you really need to allocate more than %d items?\n", MAX_ADDRS);
abort();
}
xmemchk();
assert(((char *) (p+2)) != NULL);
return (void *) (p+2);
#else
UNUSED(filen);
UNUSED(line);
return (void *) malloc(size);
#endif
}
void *_xrealloc(void *p, unsigned size, const char *filen, int line)
{
#ifdef DEBUG_MEM
int s = (((size + 7)/8)*8) + 8;
int *q = (int *) p - 2;
int i;
for (i = 0; i < naddr; i++) {
if (mem_item[i].addr == q) {
xfree(mem_item[naddr].filen);
mem_item[naddr].addr = realloc(q, s);
mem_item[naddr].filen = (char *) strdup(filen);
mem_item[naddr].line = line;
mem_item[naddr].length = strlen(filen);
mem_item[naddr].est = tick++;
*q = s;
*(q+(s/4)+2) = s;
return (void *) (q + 2);
}
}
debug_msg("Trying to xrealloc() memory not which is not allocated\n");
abort();
#else
UNUSED(filen);
UNUSED(line);
return (void *) realloc(p, size);
#endif
}
char *_xstrdup(const char *s1, const char *filen, int line)
{
char *s2;
s2 = (char *) _xmalloc(strlen(s1)+1, filen, line);
if (s2 != NULL)
strcpy(s2, s1);
return (s2);
}
|