summary refs log tree commit diff
path: root/zone.h
blob: 0a6db89bffbb3a077f7ba85ab2184b0276317f6f (plain)
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
#ifndef ZONE_H
#define ZONE_H

#include <stdint.h>

#ifndef ZONE_MIN_CAPACITY
#define ZONE_MIN_CAPACITY 1024
#endif

#define ZONE_USE_MALLOC 0
#define ZONE_USE_MMAP   1

typedef struct ZoneFrame ZoneFrame;
struct ZoneFrame {
	ZoneFrame *prev, *next;
	size_t length, capacity;
	uintptr_t data[];
};

typedef struct {
	ZoneFrame *tail, *here;
} Zone;

typedef struct {
	ZoneFrame *zf;
	size_t length;
} ZoneState;

void zn_free(Zone *z);
void zn_reset(Zone *z);
void zn_save(Zone *z, ZoneState *m);
void zn_load(Zone *z, ZoneState *m);

void *zn_alloc(Zone *z, size_t n);
void *zn_zf_alloc(Zone *z, ZoneFrame **zf, size_t n);
void *zn_zf_realloc(Zone *z, ZoneFrame **zf, void *ptr, size_t oldsz, size_t newsz);
char *zn_strdup(Zone *z, const char *s);

#ifdef ZONE_IMPL

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "zone.h"

#ifndef ZONE_BACKEND
	#ifdef __unix__
		#define ZONE_BACKEND ZONE_USE_MMAP
	#else
		#define ZONE_BACKEND ZONE_USE_MALLOC
	#endif
#endif

#if ZONE_BACKEND == ZONE_USE_MMAP

	#include <sys/mman.h>
	#include <unistd.h>

	static ZoneFrame *zn_zf_allocate(size_t capacity) {
		ZoneFrame *zf = mmap(NULL, sizeof *zf + capacity * sizeof(uintptr_t), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
		if (!zf) {
			fprintf(stderr, "failed to allocate memory zone frame\n");
			abort();
		}
		return zf;
	}
	static void zn_zf_free(ZoneFrame *zf) {
		munmap(zf, sizeof *zf + zf->capacity * sizeof(uintptr_t));
	}
	static size_t zn_zf_capacity(size_t cap) {
		if (cap < ZONE_MIN_CAPACITY) cap = ZONE_MIN_CAPACITY;
		size_t pg_size = sysconf(_SC_PAGE_SIZE) / sizeof(uintptr_t);
		size_t hdr_size = sizeof(ZoneFrame) / sizeof(uintptr_t);
		cap += pg_size - (cap % pg_size);
		return cap - hdr_size;
	}

#elif ZONE_BACKEND == ZONE_USE_MALLOC

	static ZoneFrame *zn_zf_allocate(size_t capacity) {
		ZoneFrame *zf = malloc(sizeof *zf + capacity * sizeof(uintptr_t));
		if (!zf) {
			fprintf(stderr, "failed to allocate memory zone frame\n");
			abort();
		}
		return zf;
	}
	static void zn_zf_free(ZoneFrame *zf) {
		free(zf);
	}
	static size_t zn_zf_capacity(size_t cap) {
		if (cap < ZONE_MIN_CAPACITY) cap = ZONE_MIN_CAPACITY;
		return cap;
	}

#else

	#error "unknown or unsupported zone backend"

#endif

static ZoneFrame *zn_zf_new(size_t capacity) {
	ZoneFrame *zf = zn_zf_allocate(capacity);
	zf->prev = NULL;
	zf->next = NULL;
	zf->length = 0;
	zf->capacity = capacity;
	return zf;
}

void *zn_alloc(Zone *z, size_t n) {
	return zn_zf_alloc(z, NULL, n);
}

void *zn_zf_alloc(Zone *z, ZoneFrame **zfo, size_t n) {
	ZoneFrame *zf = NULL;
	size_t wordsz = (n + sizeof(uintptr_t) - 1) / sizeof(uintptr_t);
	for (ZoneFrame *f = z->here; f; f = f->next) {
		if (f->capacity - f->length >= wordsz) {
			zf = f;
			break;
		}
	}
	if (!zf) {
		zf = zn_zf_new(zn_zf_capacity(wordsz));
		zf->prev = z->tail;
		if (z->tail) z->tail->next = zf;
		z->tail = zf;
	}
	void *ptr = &zf->data[zf->length];
	zf->length += wordsz;
	z->here = zf;
	if (zfo) *zfo = zf;
	return ptr;
}

void *zn_zf_realloc(Zone *z, ZoneFrame **zfo, void *old, size_t oldsz, size_t newsz) {
	ZoneFrame *zf = *zfo;
	size_t old_wordsz = (oldsz + sizeof(uintptr_t) - 1) / sizeof(uintptr_t);
	size_t new_wordsz = (newsz + sizeof(uintptr_t) - 1) / sizeof(uintptr_t);
	if (&zf->data[zf->length - old_wordsz] == old && zf->length + new_wordsz - old_wordsz <= zf->capacity) {
		zf->length = zf->length + new_wordsz - old_wordsz;
		return old;
	} else {
		void *new = zn_zf_alloc(z, zfo, newsz);
		zf->length -= old_wordsz;
		memcpy(new, old, oldsz);
		return new;
	}
}

void zn_free(Zone *z) {
	ZoneFrame *p, *t;
	t = z->tail;
	while (t) {
		p = t->prev;
		zn_zf_free(t);
		t = p;
	}
}

void zn_reset(Zone *z) {
	ZoneFrame *zf = z->here;
	if (!zf) return;
	for (;;) {
		zf->length = 0;
		if (zf->prev) zf = zf->prev;
		else break;
	}
	z->tail = zf;
}

char *zn_strdup(Zone *z, const char *s) {
	size_t n = strlen(s) + 1;
	char *d = zn_alloc(z, n);
	memcpy(d, s, n);
	return d;
}

void zn_save(Zone *z, ZoneState *m) {
	m->zf = z->here;
	m->length = z->here ? z->tail->length : 0;
}

void zn_load(Zone *z, ZoneState *m) {
	if (m->zf) {
		while (z->here != m->zf) {
			z->here->length = 0;
			z->here = z->here->prev;
		}
		m->zf->length = m->length;
	} else {
		zn_reset(z);
	}
}

#endif
#endif