summaryrefslogtreecommitdiffhomepage
path: root/src/dalist_memfn.c
blob: 0b3199d5f9b7dc52d671d31f126bac208451b132 (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
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
/*****************************************************************************/
/*  dalist: a zero-dependency book-keeping library                           */
/*  Copyright (C) 2013,2014,2015  Z. Gilboa                                  */
/*  Released under GPLv2 and GPLv3; see COPYING.DALIST.                      */
/*****************************************************************************/

#include <dalist/dalist.h>
#include "dalist_impl.h"

/* private prototypes */
static void * dalist_alloc(struct dalist_ex * dlist);

static int dalist_memfn_internal(
	struct dalist_ex *	dlist,
	void **			addr,
	size_t *		alloc_size);


dalist_api
int dalist_get_free_node(
	struct dalist_ex *	dlist,
	void **			fnode)
{
	int			ret;
	struct dalist_node *	nfree;

	/* in case we fail */
	*fnode = (void *)0;

	if (dlist->free)
		/* allocation is not needed */
		nfree  = (struct dalist_node *)dlist->free;
	else
		nfree = (struct dalist_node *)dalist_alloc(dlist);

	if (nfree) {
		*fnode = nfree;
		dlist->free = nfree->next;

		if (nfree->next) /* new head of dlist->free */
			nfree->next->prev = 0;

		/* bookkeeping */
		dlist->info.free_nodes--;

		/* and for good measure */
		nfree->prev = nfree->next = 0;
		ret = DALIST_OK;
	} else
		ret = DALIST_EMEMFN;

	return ret;
}


dalist_api
int dalist_deposit_free_node(
	struct dalist_ex *	dlist,
	void *			fnode)
{
	int			ret;
	struct dalist_node *	nfree;

	if (fnode) {
		nfree       = (struct dalist_node *)fnode;
		nfree->next = (struct dalist_node *)dlist->free;

		/* fnode is now the head free node */
		nfree->prev = 0;
		dlist->free = fnode;

		/* bookkeeping */
		dlist->info.free_nodes++;
		ret = DALIST_OK;
	} else {
		ret = DALIST_ENODE;
	}

	return ret;
}


dalist_api
int dalist_deposit_memory_block(
	struct dalist_ex *	dlist,
	void *			addr,
	size_t			alloc_size)
{
	struct dalist_node *	fnode;
	struct dalist_node *	fnode_next;
	char *			naddr;
	char *			naddr_next;
	char *			naddr_upper;
	size_t			node_size;

	/* node_size: before alignment */
	if (dlist->dblock_size == 0)
		node_size = sizeof(struct dalist_node);
	else
		node_size = (size_t)&((struct dalist_node_ex *)0)->dblock
				+ dlist->dblock_size;

	/* node_size: after alignment */
	node_size += sizeof(uintptr_t)-1;
	node_size |= sizeof(uintptr_t)-1;
	node_size ^= sizeof(uintptr_t)-1;

	/* validate block size */
	if (alloc_size < node_size)
		return DALIST_EMEMBLOCK;

	/* marks */
	naddr       = (char *)addr;
	naddr_next  = naddr + node_size;
	naddr_upper = naddr + alloc_size;

	/* chain of free nodes */
	while ((naddr_next + node_size) < naddr_upper) {
		fnode      = (struct dalist_node *)naddr;
		fnode_next = (struct dalist_node *)naddr_next;

		fnode->next      = fnode_next;
		fnode_next->prev = fnode;

		naddr      += node_size;
		naddr_next += node_size;
	}

	/* free nodes block: head and tail */
	fnode       = (struct dalist_node *)addr;
	fnode_next  = (struct dalist_node *)naddr;

	if (dlist->free) {
		/* link with pre-allocated free nodes */
		fnode_next->next = (struct dalist_node *)dlist->free;
		((struct dalist_node *)dlist->free)->prev = fnode_next;
	} else {
		/* no pre-allocated free nodes */
		fnode_next->next = 0;
	}

	/* fnode is now the head free node */
	fnode->prev = 0;
	dlist->free = fnode;

	/* bookkeeping */
	dlist->info.free_nodes += (alloc_size / node_size);

	return DALIST_OK;
}


static void * __cdecl dalist_alloc(struct dalist_ex * dlist)
{
	int			ret;
	void *			addr;
	size_t			alloc_size;
	memfn_custom *		pmemfn;
	struct dalist_node *	fnode;

	/* pmemfn */
	if (dlist->memfn_method == DALIST_MEMFN_CUSTOM)
		pmemfn = (dalist_memfn_custom *)(uintptr_t)dlist->memfn_ptr;
	else
		pmemfn = dalist_memfn_internal;

	/* allocate: try */
	fnode = 0;
	ret   = pmemfn(dlist,&addr,&alloc_size);

	/* allocate: verify */
	if (ret == DALIST_OK) {
		if (dlist->memfn_method == DALIST_MEMFN_MALLOC)
			fnode = (struct dalist_node *)addr;
		else if (dalist_deposit_memory_block(
				dlist,
				addr,
				alloc_size) == DALIST_OK)
			fnode = (struct dalist_node *)dlist->free;
	}

	return fnode;
}


static int __cdecl dalist_memfn_internal(
	struct dalist_ex *	dlist,
	void **			address,
	size_t *		alloc_size)
{
	int		ret;
	void *		addr;
	size_t		size;

	memfn_mmap *	pfn_mmap;
	memfn_malloc *	pfn_malloc;
	memfn_allocvm * pfn_allocvm;

	switch (dlist->memfn_method) {
		case DALIST_MEMFN_MALLOC:
			if (dlist->dblock_size == 0)
				size = sizeof(struct dalist_node);
			else
				size = (size_t)((struct dalist_node_ex *)0)->dblock
					+ dlist->dblock_size;

			pfn_malloc = (memfn_malloc *)(uintptr_t)dlist->memfn_ptr;
			addr = pfn_malloc(size);

			if (addr) {
				*address = addr;
				*alloc_size = size;
				ret = dlist->memfn_status = DALIST_OK;
			} else {
				ret = DALIST_EMEMFN;
				dlist->memfn_status = dalist_errno(ret);
			}
			break;

		case DALIST_MEMFN_MMAP:
			pfn_mmap = (memfn_mmap * )(uintptr_t)dlist->memfn_ptr;

			addr = pfn_mmap(
				(void *)0,
				dlist->lblock_size,
				PROT_READ | PROT_WRITE,
				MAP_ANON | MAP_SHARED,
				0,0);

			if (addr) {
				*address = addr;
				*alloc_size = dlist->lblock_size;
				ret = dlist->memfn_status = DALIST_OK;
			} else {
				ret = DALIST_EMEMFN;
				dlist->memfn_status = dalist_errno(ret);
			}
			break;

		case DALIST_MEMFN_NT_ALLOCATE_VIRTUAL_MEMORY:
			addr       = (void *)0;
			size = dlist->lblock_size;
			pfn_allocvm = (memfn_allocvm *)(uintptr_t)dlist->memfn_ptr;

			dlist->memfn_status = pfn_allocvm(
				NT_CURRENT_PROCESS_HANDLE,
				&addr,
				0,
				&size,
				NT_MEM_COMMIT,
				NT_PAGE_READWRITE);

			if (dlist->memfn_status == NT_STATUS_SUCCESS) {
				*address = addr;
				*alloc_size = size;
				ret = DALIST_OK;
			} else {
				ret = DALIST_EMEMFN;
			}
			break;
		default:
			ret = DALIST_EINTERNAL;
			break;
	}

	return ret;
}