-
Notifications
You must be signed in to change notification settings - Fork 9
/
dalloc.c
64 lines (54 loc) · 1.37 KB
/
dalloc.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
// Copyright (c) 2009 David Caldwell, All Rights Reserved.
#include "dalloc.h"
struct dalloc_memory {
struct dalloc_memory *next;
void *mem;
};
struct dalloc_head {
struct dalloc_head *next;
struct dalloc_memory *list;
};
static struct dalloc_head *dalloc_head_list;
void dalloc_start()
{
struct dalloc_head *head = xcalloc(1, sizeof(*head));
head->next = dalloc_head_list;
dalloc_head_list = head;
}
void *dalloc_remember(void *mem)
{
if (!mem) return mem;
struct dalloc_memory *m = xmalloc(sizeof(*m));
m->mem = mem;
m->next = dalloc_head_list->list;
dalloc_head_list->list = m;
return mem;
}
void dalloc_free()
{
struct dalloc_head *head = dalloc_head_list;
dalloc_head_list = dalloc_head_list->next;
for (struct dalloc_memory *m=head->list, *next; m; m=next) {
next = m->next;
free(m->mem);
free(m);
}
free(head);
}
void *drealloc(void *old, size_t count)
{
for (struct dalloc_memory *m=dalloc_head_list->list; m; m=m->next)
if (m->mem == old) m->mem = NULL; // Taking it out of the list is hard. Let's go shopping.
return dalloc_remember(xrealloc(old,count));
}
#include <stdarg.h>
#include <stdio.h>
char *dsprintf(char *format, ...)
{
char *out;
va_list ap;
va_start(ap, format);
vxsprintf(&out, format, ap);
va_end(ap);
return dalloc_remember(out);
}