-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathamalloc.c
More file actions
144 lines (117 loc) · 2.79 KB
/
Copy pathamalloc.c
File metadata and controls
144 lines (117 loc) · 2.79 KB
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
/*
* debugging malloc()/realloc()/calloc()/free() that attempts
* to keep track of just what's been allocated today.
*/
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
extern void* memset(void*,int,size_t);
#define MAGIC 0x1f2e3d4c
struct alist { int magic, size, index; int *end; struct alist *next, *last; };
static struct alist list = { 0, 0, 0, 0 };
static int mallocs=0;
static int reallocs=0;
static int frees=0;
static int index = 0;
static void
die(char *msg, int index)
{
fprintf(stderr, msg, index);
abort();
}
void *
acalloc(int count, int size)
{
struct alist *ret;
if ( size > 1 ) {
count *= size;
size = 1;
}
if ( ret = calloc(count + sizeof(struct alist) + sizeof(int), size) ) {
ret->magic = MAGIC;
ret->size = size * count;
ret->index = index ++;
ret->end = (int*)(count + (char*) (ret + 1));
*(ret->end) = ~MAGIC;
if ( list.next ) {
ret->next = list.next;
ret->last = &list;
ret->next->last = ret;
list.next = ret;
}
else {
ret->last = ret->next = &list;
list.next = list.last = ret;
}
++mallocs;
return ret+1;
}
return 0;
}
void*
amalloc(int size)
{
void *ret = acalloc(1, size);
if ( ret ) {
/* explicitally fill the mallocated memory with a nonzero character */
memset(ret, 0x8f, size);
}
return ret;
}
void
afree(void *ptr)
{
struct alist *p2 = ((struct alist*)ptr)-1;
if ( p2->magic == MAGIC ) {
if ( ! (p2->end && *(p2->end) == ~MAGIC) )
die("goddam: corrupted memory block %d in free()!\n", p2->index);
p2->last->next = p2->next;
p2->next->last = p2->last;
++frees;
free(p2);
}
else
free(ptr);
}
void *
arealloc(void *ptr, int size)
{
struct alist *p2 = ((struct alist*)ptr)-1;
struct alist save;
if ( p2->magic == MAGIC ) {
if ( ! (p2->end && *(p2->end) == ~MAGIC) )
die("goddam: corrupted memory block %d in realloc()!\n", p2->index);
save.next = p2->next;
save.last = p2->last;
p2 = realloc(p2, sizeof(int) + sizeof(*p2) + size);
if ( p2 ) {
p2->size = size;
p2->end = (int*)(size + (char*) (p2 + 1));
*(p2->end) = ~MAGIC;
p2->next->last = p2;
p2->last->next = p2;
++reallocs;
return p2+1;
}
else {
save.next->last = save.last;
save.last->next = save.next;
return 0;
}
}
return realloc(ptr, size);
}
void
adump()
{
struct alist *p;
for ( p = list.next; p && (p != &list); p = p->next ) {
fprintf(stderr, "allocated: %d byte%s\n", p->size, (p->size==1) ? "" : "s");
fprintf(stderr, " [%.*s]\n", p->size, (char*)(p+1));
}
if ( getenv("AMALLOC_STATISTICS") ) {
fprintf(stderr, "%d malloc%s\n", mallocs, (mallocs==1)?"":"s");
fprintf(stderr, "%d realloc%s\n", reallocs, (reallocs==1)?"":"s");
fprintf(stderr, "%d free%s\n", frees, (frees==1)?"":"s");
}
}