-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmemory.c
200 lines (164 loc) · 3.42 KB
/
memory.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
/* memory.c
* By Ron
* Created January, 2010
*
* (See LICENSE.txt)
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "memory.h"
typedef struct entry
{
char *file;
int line;
void *memory;
size_t size;
struct entry *next;
} entry_t;
#ifdef TESTMEMORY
static entry_t *first = NULL;
#endif
void add_entry(char *file, int line, void *memory, size_t size)
{
#ifdef TESTMEMORY
entry_t *current = (entry_t*) malloc(sizeof(entry_t));
if(!current)
DIE_MEM();
/* Put the new entry at the front of the list. */
current->next = first;
first = current;
current->file = file;
current->line = line;
current->memory = memory;
current->size = size;
#endif
}
void update_entry(void *old_memory, void *new_memory, int new_size)
{
#ifdef TESTMEMORY
entry_t *current = first;
while(current)
{
if(current->memory == old_memory)
{
current->memory = new_memory;
current->size = new_size;
return;
}
current = current->next;
}
DIE("Tried to re-allocate memory that doesn't exist.");
#endif
}
void remove_entry(void *memory)
{
#ifdef TESTMEMORY
entry_t *last = NULL;
entry_t *current = first;
while(current)
{
if(current->memory == memory)
{
if(current == first)
{
/* Beginning of the list. */
first = first->next;
free(current);
}
else
{
/* Anywhere else in the list. */
last->next = current->next;
free(current);
}
return;
}
last = current;
current = current->next;
}
DIE("Tried to free memory that we didn't own");
#endif
}
void print_memory()
{
#ifdef TESTMEMORY
if(first == NULL)
{
fprintf(stderr, "No allocated memory. Congratulations!\n");
}
else
{
entry_t *current = first;
fprintf(stderr, "Allocated memory:\n");
while(current)
{
fprintf(stderr, "%p: 0x%08x bytes allocated at %s:%d\n", current->memory, (unsigned int)current->size, current->file, current->line);
current = current->next;
}
}
#endif
}
void *safe_malloc_internal(size_t size, char *file, int line)
{
void *ret = malloc(size);
if(!ret)
DIE_MEM();
memset(ret, 0, size);
add_entry(file, line, ret, size);
return ret;
}
void *safe_realloc_internal(void *ptr, size_t size, char *file, int line)
{
void *ret = realloc(ptr, size);
if(!ret)
DIE_MEM();
update_entry(ptr, ret, size);
return ret;
}
char *safe_strdup_internal(const char *str, char *file, int line)
{
char *ret;
if(strlen(str) + 1 < strlen(str))
DIE("Overflow.");
ret = safe_malloc_internal(strlen(str) + 1, file, line);
memcpy(ret, str, strlen(str) + 1);
return ret;
}
void safe_free(void *ptr)
{
free(ptr);
remove_entry(ptr);
}
char *unicode_alloc(const char *string)
{
size_t i;
char *unicode;
size_t unicode_length = (strlen(string) + 1) * 2;
if(unicode_length < strlen(string))
DIE("Overflow.");
unicode = safe_malloc(unicode_length);
memset(unicode, 0, unicode_length);
for(i = 0; i < strlen(string); i++)
{
unicode[(i * 2)] = string[i];
}
return unicode;
}
char *unicode_alloc_upper(const char *string)
{
size_t i;
char *unicode;
size_t unicode_length = (strlen(string) + 1) * 2;
if(unicode_length < strlen(string))
DIE("Overflow.");
unicode = safe_malloc(unicode_length);
memset(unicode, 0, unicode_length);
for(i = 0; i < strlen(string); i++)
{
/* Note: int typecase fixes compile warning on cygwin. */
unicode[(i * 2)] = toupper((int)string[i]);
}
return unicode;
}