forked from simonsj/fdupes-jody
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_malloc.c
306 lines (254 loc) · 7.8 KB
/
string_malloc.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* String table allocator
* A replacement for malloc() for tables of fixed strings
*
* Copyright (C) 2015-2022 by Jody Bruchon <[email protected]>
* Released under The MIT License
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "string_malloc.h"
/* Size of pages to allocate at once. Must be divisible by uintptr_t.
* The maximum object size is this page size minus about 16 bytes! */
#ifndef SMA_PAGE_SIZE
#define SMA_PAGE_SIZE 262144
#endif
/* Max freed pointers to remember. Increasing this number allows storing
* more free objects but can slow down allocations. Don't increase it if
* the program's total reused freed alloc counter doesn't increase as a
* result or you're slowing allocs down to no benefit. */
#ifndef SMA_MAX_FREE
#define SMA_MAX_FREE 32
#endif
#ifdef DEBUG
uintmax_t sma_allocs = 0;
uintmax_t sma_free_ignored = 0;
uintmax_t sma_free_good = 0;
uintmax_t sma_free_merged = 0;
uintmax_t sma_free_replaced = 0;
uintmax_t sma_free_reclaimed = 0;
uintmax_t sma_free_scanned = 0;
uintmax_t sma_free_tails = 0;
#define DBG(a) a
#else
#define DBG(a)
#endif
/* This is used to bypass string_malloc for debugging */
#ifdef SMA_PASSTHROUGH
void *string_malloc(size_t len) { return malloc(len); }
void string_free(void *ptr) { free(ptr); return; }
void string_malloc_destroy(void) { return; }
#else /* Not SMA_PASSTHROUGH mode */
struct freelist {
void *addr;
size_t size;
};
static void *sma_head = NULL;
static uintptr_t *sma_curpage = NULL;
static unsigned int sma_pages = 0;
static struct freelist sma_freelist[SMA_MAX_FREE];
static int sma_freelist_cnt = 0;
static size_t sma_nextfree = sizeof(uintptr_t);
/* Scan the freed chunk list for a suitably sized object */
static inline void *scan_freelist(const size_t size)
{
size_t *object, *min_p;
size_t sz, min = 0;
int i, used = 0, min_i = -1;
/* Don't bother scanning if the list is empty */
if (sma_freelist_cnt == 0) return NULL;
for (i = 0; i < SMA_MAX_FREE; i++) {
/* Stop scanning once we run out of valid entries */
if (used == sma_freelist_cnt) return NULL;
DBG(sma_free_scanned++;)
object = sma_freelist[i].addr;
/* Skip empty entries */
if (object == NULL) continue;
sz = sma_freelist[i].size;
used++;
/* Skip smaller objects */
if (sz < size) continue;
/* Object is big enough; record if it's the new minimum */
if (min == 0 || sz <= min) {
min = sz;
min_i = i;
/* Always stop scanning if exact sized object found */
if (sz == size) break;
}
}
/* Enhancement TODO: split the free item if it's big enough */
/* Return smallest object found and delete from free list */
if (min_i != -1) {
min_p = sma_freelist[min_i].addr;
*min_p = sma_freelist[min_i].size;
sma_freelist[min_i].addr = NULL;
sma_freelist_cnt--;
min_p++;
return (void *)min_p;
}
/* Fall through - free list search failed */
return NULL;
}
/* malloc() a new page for string_malloc to use */
static inline void *string_malloc_page(void)
{
uintptr_t * restrict pageptr;
/* Allocate page and set up pointers at page starts */
pageptr = (uintptr_t *)malloc(SMA_PAGE_SIZE);
if (pageptr == NULL) return NULL;
*pageptr = (uintptr_t)NULL;
/* Link previous page to this page, if applicable */
if (sma_curpage != NULL) *sma_curpage = (uintptr_t)pageptr;
/* Update last page pointers and total page counter */
sma_curpage = pageptr;
sma_pages++;
return (void *)pageptr;
}
void *string_malloc(size_t len)
{
const void * restrict page = (char *)sma_curpage;
static size_t *address;
/* Calling with no actual length is invalid */
if (len < 1) return NULL;
/* Align objects where possible */
if (len & (sizeof(uintptr_t) - 1)) {
len &= ~(sizeof(uintptr_t) - 1);
len += sizeof(uintptr_t);
}
/* Pass-through allocations larger than maximum object size to malloc() */
if (len > (SMA_PAGE_SIZE - sizeof(uintptr_t) - sizeof(size_t))) {
/* Allocate the space */
address = (size_t *)malloc(len + sizeof(size_t));
if (!address) return NULL;
/* Prefix object with its size */
*address = len;
address++;
DBG(sma_allocs++;)
return (void *)address;
}
/* Initialize on first use */
if (sma_pages == 0) {
/* Initialize the freed object list */
for (int i = 0; i < SMA_MAX_FREE; i++) sma_freelist[i].addr = NULL;
/* Allocate first page and set up for first allocation */
sma_head = string_malloc_page();
if (sma_head == NULL) return NULL;
sma_nextfree = sizeof(uintptr_t);
page = sma_head;
}
/* Allocate objects from the free list first */
address = (size_t *)scan_freelist(len);
if (address != NULL) {
DBG(sma_free_reclaimed++;)
return (void *)address;
}
/* Allocate new page if this object won't fit */
if ((sma_nextfree + len + sizeof(size_t)) > SMA_PAGE_SIZE) {
size_t sz;
size_t *tailaddr;
/* See if page tail has usable remaining capacity */
sz = sma_nextfree + sizeof(size_t) + sizeof(uintptr_t);
/* Try to add page tail to free list rather than waste it */
if (sz <= SMA_PAGE_SIZE) {
sz = SMA_PAGE_SIZE - sma_nextfree - sizeof(size_t);
tailaddr = (size_t *)((uintptr_t)page + sma_nextfree);
*tailaddr = (size_t)sz;
tailaddr++;
string_free(tailaddr);
DBG(sma_free_tails++;)
}
page = string_malloc_page();
if (!page) return NULL;
sma_nextfree = sizeof(uintptr_t);
}
/* Allocate the space */
address = (size_t *)((uintptr_t)page + sma_nextfree);
/* Prefix object with its size */
*address = len;
address++;
sma_nextfree += len + sizeof(size_t);
DBG(sma_allocs++;)
return (void *)address;
}
/* Free an object, adding to free list if possible */
void string_free(void * const addr)
{
int freefull = 0;
struct freelist *emptyslot = NULL;
static uintptr_t before, after;
static size_t *sizeptr;
static size_t size;
/* Do nothing on NULL address */
if (addr == NULL) goto sf_failed;
/* Get address to real start of object and the object size */
sizeptr = (size_t *)addr - 1;
size = *(size_t *)sizeptr;
if (size == 0) goto sf_double_free;
/* Mark the freed object to catch double free attempts */
*(size_t *)sizeptr = 0;
/* Calculate after-block pointer for merge checks */
after = (uintptr_t)addr + size;
/* If free list is full, try to replace a smaller object */
if (sma_freelist_cnt == SMA_MAX_FREE) freefull = 1;
/* Attempt to merge into other free objects */
for (int i = 0; i < SMA_MAX_FREE; i++) {
/* Record first empty slot */
if (emptyslot == NULL && sma_freelist[i].addr == NULL) {
emptyslot = &(sma_freelist[i]);
// break;
} else if (freefull != 0 && sma_freelist[i].size < size) {
/* Replace object if list is full and new one is bigger */
emptyslot = &(sma_freelist[i]);
DBG(sma_free_replaced++;)
break;
} else if ((uintptr_t)(sma_freelist[i].addr) == after) {
/* Merge with a block after this one */
sma_freelist[i].addr = sizeptr;
sma_freelist[i].size += (size + sizeof(size_t *));
DBG(sma_free_good++;)
DBG(sma_free_merged++;)
return;
} else {
before = (uintptr_t)addr + size;
if (before == (uintptr_t)(sma_freelist[i].addr)) {
/* Merge with a block before this one */
sma_freelist[i].size += (size + sizeof(size_t *));
DBG(sma_free_good++;)
DBG(sma_free_merged++;)
}
}
}
/* Merges failed; add to empty slot (if any found) */
if (emptyslot != NULL) {
if (emptyslot->addr == NULL) sma_freelist_cnt++;
emptyslot->addr = sizeptr;
emptyslot->size = size;
DBG(sma_free_good++;)
return;
}
/* Fall through */
sf_failed:
DBG(sma_free_ignored++;)
return;
sf_double_free:
fprintf(stderr, "string_malloc: ERROR: attempt to string_free() already freed object at %p\n", addr);
return;
}
/* Destroy all allocated pages */
void string_malloc_destroy(void)
{
uintptr_t *cur;
uintptr_t *next;
cur = sma_head;
if (sma_head == NULL) return;
while (sma_pages > 0) {
next = (uintptr_t *)*cur;
free(cur);
cur = next;
sma_pages--;
}
sma_head = NULL;
return;
}
#endif /* SMA_PASSTHROUGH */