-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmem.c
200 lines (169 loc) · 4.66 KB
/
mem.c
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
/* Copyright 2008-2022 Carsten Elton Sorensen
This file is part of ASMotor.
ASMotor is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ASMotor is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ASMotor. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "util.h"
#include "mem.h"
#include "lists.h"
#if __STDC_VERSION__ > 201112L
// C11
# define ALIGN _Alignof(long double)
#else
# define ALIGN 16
#endif
#define HEADERSIZE ((sizeof(SMemoryChunk) + (ALIGN - 1)) & -ALIGN)
typedef struct MemoryChunk {
list_Data(struct MemoryChunk);
size_t size;
#if defined(_DEBUG)
const char* filename;
int lineNumber;
#endif
} SMemoryChunk;
#if defined(ASMOTOR_FAKE_ALLOC)
uint32_t g_memory[1024*1024/4];
uint8_t* g_fakeMalloc = (uint8_t*) g_memory;
extern void*
mem_AllocImpl(size_t size, const char* filename, int lineNumber) {
void* r = g_fakeMalloc;
g_fakeMalloc += (size + 3) & -4;
return r;
}
#endif
#if !defined(ASMOTOR_INLINE_MEMORY) && !defined(ASMOTOR_FAKE_ALLOC)
static SMemoryChunk* g_memoryList = NULL;
#endif
#if !defined(ASMOTOR_INLINE_MEMORY) && !defined(ASMOTOR_FAKE_ALLOC)
static void*
#if defined(_DEBUG)
CheckMemPointer(SMemoryChunk* chunk, size_t size, const char* filename, int lineNumber) {
#else
checkMemPointer(SMemoryChunk* chunk, size_t size) {
#endif
if (!chunk) {
fprintf(stderr, "Unable to allocate memory. Critical error, exiting.\n");
exit(EXIT_FAILURE);
}
chunk->size = size;
#if defined(_DEBUG)
chunk->filename = filename;
chunk->lineNumber = lineNumber;
#endif
list_Insert(g_memoryList, chunk);
return (char*) chunk + HEADERSIZE;
}
#endif
#if defined(_DEBUG) && !defined(ASMOTOR_INLINE_MEMORY) && !defined(ASMOTOR_FAKE_ALLOC)
void*
mem_AllocImpl(size_t size, const char* filename, int lineNumber) {
assert (size != 0);
uint8_t* mem = CheckMemPointer(malloc(size + HEADERSIZE), size, filename, lineNumber);
for (size_t i = 0; i < size; ++i) {
switch (i & 3) {
case 0: mem[i] = 0xDE; break;
case 1: mem[i] = 0xAD; break;
case 2: mem[i] = 0xBE; break;
case 3: mem[i] = 0xEF; break;
}
}
return mem;
}
#elif !defined(ASMOTOR_INLINE_MEMORY) && !defined(ASMOTOR_FAKE_ALLOC)
void*
mem_Alloc(size_t size) {
assert (size != 0);
return checkMemPointer(malloc(size + HEADERSIZE), size);
}
#endif
#if !defined(ASMOTOR_INLINE_MEMORY) && !defined(ASMOTOR_FAKE_ALLOC)
void*
#if defined(_DEBUG)
mem_ReallocImpl(void* memory, size_t size, const char* filename, int lineNumber) {
#else
mem_Realloc(void* memory, size_t size) {
#endif
if (memory == NULL) {
#if defined(_DEBUG)
return mem_AllocImpl(size, filename, lineNumber);
#else
return mem_Alloc(size);
#endif
} else if (size == 0) {
mem_Free(memory);
return NULL;
} else {
SMemoryChunk* chunk = (SMemoryChunk*) ((char*) memory - HEADERSIZE);
list_Remove(g_memoryList, chunk);
#if defined(_DEBUG)
return CheckMemPointer(realloc(chunk, size + HEADERSIZE), size, filename, lineNumber);
#else
return checkMemPointer(realloc(chunk, size + HEADERSIZE), size);
#endif
}
}
#endif
#if !defined(ASMOTOR_INLINE_MEMORY) && !defined(ASMOTOR_FAKE_ALLOC)
void
mem_Free(void* memory) {
if (memory != NULL) {
SMemoryChunk* chunk = (SMemoryChunk*) ((char*) memory - HEADERSIZE);
assert (chunk->size != 0);
list_Remove(g_memoryList, chunk);
chunk->size = 0;
free(chunk);
}
}
#endif
void
hexDumpLine(const uint8_t* data, size_t count) {
for (size_t i = 0; i < count; ++i) {
printf("%02X ", data[i]);
}
for (size_t i = 16 * 3 + 1; i > count * 3; --i) {
printf(" ");
}
for (size_t i = 0; i < count; ++i) {
if (isprint(data[i])) {
printf("%c", data[i]);
} else {
printf(".");
}
}
printf("\n");
}
void
mem_HexDump(const uint8_t* data, size_t count) {
while (count > 0) {
size_t toDump = count;
if (toDump > 16)
toDump = 16;
hexDumpLine(data, toDump);
count -= toDump;
data += toDump;
}
}
#if !defined(ASMOTOR_INLINE_MEMORY) && !defined(ASMOTOR_FAKE_ALLOC)
void
mem_ShowLeaks(void) {
#if defined(_DEBUG)
for (SMemoryChunk* chunk = g_memoryList; chunk != NULL; chunk = list_GetNext(chunk)) {
uint8_t* data = ((uint8_t*) chunk) + sizeof(SMemoryChunk);
printf("Leak %s:%d (%p)\n", chunk->filename, chunk->lineNumber, (void *) data);
mem_HexDump(data, 32);
}
#endif
}
#endif