summaryrefslogtreecommitdiff
path: root/xcache/xc_allocator_bestfit.c
blob: 7b4a3174a7f1015e8ed5a4ce53f34e22ac97b6fa (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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#define _xc_allocator_t _xc_allocator_bestfit_t
#define _xc_allocator_block_t _xc_allocator_bestfit_block_t
#include "xc_allocator.h"
#undef _xc_allocator_t
#undef _xc_allocator_block_t

#ifdef TEST
#	include <limits.h>
#	include <stdio.h>
#	define XCACHE_DEBUG
typedef int zend_bool;
#	define ZEND_ATTRIBUTE_PTR_FORMAT(a, b, c)
#	define zend_error(type, error) fprintf(stderr, "%s", error)
#else
#	include <php.h>
#endif

#ifdef XCACHE_DEBUG
#	define ALLOC_DEBUG_BLOCK_CHECK
#endif


#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "xc_shm.h"
#include "util/xc_align.h"
#include "util/xc_trace.h"

#if 0
#undef ALLOC_DEBUG_BLOCK_CHECK
#endif

#define CHAR_PTR(p) ((char *) (p))
#define PADD(p, a) (CHAR_PTR(p) + a)
#define PSUB(p1, p2) (CHAR_PTR(p1) - CHAR_PTR(p2))

/* {{{ allocator */
typedef struct _xc_allocator_bestfit_block_t xc_allocator_bestfit_block_t;
struct _xc_allocator_bestfit_block_t {
#ifdef ALLOC_DEBUG_BLOCK_CHECK
	unsigned int magic;
#endif
	xc_memsize_t size; /* reserved even after alloc */
	xc_allocator_bestfit_block_t *next;  /* not used after alloc */
};

typedef struct _xc_allocator_bestfit_t {
	const xc_allocator_vtable_t *vtable;
	xc_shm_t *shm;
	xc_memsize_t size;
	xc_memsize_t avail;       /* total free */
	xc_allocator_bestfit_block_t headblock[1];  /* just as a pointer to first block*/
} xc_allocator_bestfit_t;

#include <stddef.h>

#define SizeOf(type, field) sizeof( ((type *) 0)->field )
#define BLOCK_HEADER_SIZE() (ALIGN( offsetof(xc_allocator_bestfit_block_t, size) + SizeOf(xc_allocator_bestfit_block_t, size) ))

#define BLOCK_MAGIC ((unsigned int) 0x87655678)

/* }}} */
static inline void xc_block_setup(xc_allocator_bestfit_block_t *b, xc_memsize_t size, xc_allocator_bestfit_block_t *next) /* {{{ */
{
#ifdef ALLOC_DEBUG_BLOCK_CHECK
	b->magic = BLOCK_MAGIC;
#endif
	b->size = size;
	b->next = next;
}
/* }}} */
#ifdef ALLOC_DEBUG_BLOCK_CHECK
static void xc_block_check(xc_allocator_bestfit_block_t *b) /* {{{ */
{
	if (b->magic != BLOCK_MAGIC) {
		fprintf(stderr, "0x%X != 0x%X magic wrong \n", b->magic, BLOCK_MAGIC);
	}
}
/* }}} */
#else
#	define xc_block_check(b) do { } while(0)
#endif


static XC_ALLOCATOR_MALLOC(xc_allocator_bestfit_malloc) /* {{{ */
{
	xc_allocator_bestfit_block_t *prev, *cur;
	xc_allocator_bestfit_block_t *newb, *b;
	xc_memsize_t realsize;
	xc_memsize_t minsize;
	void *p;
	/* [xc_allocator_bestfit_block_t:size|size] */
	realsize = BLOCK_HEADER_SIZE() + size;
	/* realsize is ALIGNed so next block start at ALIGNed address */
	realsize = ALIGN(realsize);

	TRACE("avail: %lu (%luKB). Allocate size: %lu realsize: %lu (%luKB)"
			, allocator->avail, allocator->avail / 1024
			, size
			, realsize, realsize / 1024
			);
	do {
		p = NULL;
		if (allocator->avail < realsize) {
			TRACE("%s", " oom");
			break;
		}

		b = NULL;
		minsize = ULONG_MAX;

		/* prev|cur */

		for (prev = allocator->headblock; prev->next; prev = cur) {
			/* while (prev->next != 0) { */
			cur = prev->next;
			xc_block_check(cur);
			if (cur->size == realsize) {
				/* found a perfect fit, stop searching */
				b = prev;
				break;
			}
			/* make sure we can split on the block */
			else if (cur->size > (sizeof(xc_allocator_bestfit_block_t) + realsize) &&
					cur->size < minsize) {
				/* cur is acceptable and memller */
				b = prev;
				minsize = cur->size;
			}
			prev = cur;
		}

		if (b == NULL) {
			TRACE("%s", " no fit chunk");
			break;
		}

		prev = b;

		cur = prev->next;
		p = PADD(cur, BLOCK_HEADER_SIZE());

		/* update the block header */
		allocator->avail -= realsize;

		/* perfect fit, just unlink */
		if (cur->size == realsize) {
			prev->next = cur->next;
			TRACE(" perfect fit. Got: %p", p);
			break;
		}

		/* make new free block after alloced space */

		/* save, as it might be overwrited by newb (cur->size is ok) */
		b = cur->next;

		/* prev|cur     |next=b */

		newb = (xc_allocator_bestfit_block_t *)PADD(cur, realsize);
		xc_block_setup(newb, cur->size - realsize, b);
		cur->size = realsize;
		/* prev|cur|newb|next
		 *            `--^
		 */

		TRACE(" -> avail: %lu (%luKB). new next: %p offset: %lu %luKB. Got: %p"
				, allocator->avail, allocator->avail / 1024
				, (void *) newb
				, PSUB(newb, allocator), PSUB(newb, allocator) / 1024
				, p
				);
		prev->next = newb;
		/* prev|cur|newb|next
		 *    `-----^
		 */

	} while (0);

	return p;
}
/* }}} */
static XC_ALLOCATOR_FREE(xc_allocator_bestfit_free) /* {{{ return block size freed */
{
	xc_allocator_bestfit_block_t *cur, *b;
	int size;

	cur = (xc_allocator_bestfit_block_t *) (CHAR_PTR(p) - BLOCK_HEADER_SIZE());
	TRACE("freeing: %p, size=%lu", p, cur->size);
	xc_block_check(cur);
	assert((char*)allocator < (char*)cur);
	assert((char*)cur < (char*)allocator + allocator->size);

	/* find free block right before the p */
	b = allocator->headblock;
	while (b->next != 0 && b->next < cur) {
		b = b->next;
	}

	/* restore block */
	cur->next = b->next;
	b->next = cur;
	size = cur->size;

	TRACE(" avail %lu (%luKB)", allocator->avail, allocator->avail / 1024);
	allocator->avail += size;

	/* combine prev|cur */
	if (PADD(b, b->size) == (char *)cur) {
		b->size += cur->size;
		b->next = cur->next;
		cur = b;
		TRACE("%s", " combine prev");
	}

	/* combine cur|next */
	b = cur->next;
	if (PADD(cur, cur->size) == (char *)b) {
		cur->size += b->size;
		cur->next = b->next;
		TRACE("%s", " combine next");
	}
	TRACE(" -> avail %lu (%luKB)", allocator->avail, allocator->avail / 1024);
	return size;
}
/* }}} */
static XC_ALLOCATOR_CALLOC(xc_allocator_bestfit_calloc) /* {{{ */
{
	xc_memsize_t realsize = memb * size;
	void *p = xc_allocator_bestfit_malloc(allocator, realsize);

	if (p) {
		memset(p, 0, realsize);
	}
	return p;
}
/* }}} */
static XC_ALLOCATOR_REALLOC(xc_allocator_bestfit_realloc) /* {{{ */
{
	void *newp = xc_allocator_bestfit_malloc(allocator, size);
	if (p && newp) {
		memcpy(newp, p, size);
		xc_allocator_bestfit_free(allocator, p);
	}
	return newp;
}
/* }}} */

static XC_ALLOCATOR_AVAIL(xc_allocator_bestfit_avail) /* {{{ */
{
	return allocator->avail;
}
/* }}} */
static XC_ALLOCATOR_SIZE(xc_allocator_bestfit_size) /* {{{ */
{
	return allocator->size;
}
/* }}} */

static XC_ALLOCATOR_FREEBLOCK_FIRST(xc_allocator_bestfit_freeblock_first) /* {{{ */
{
	return allocator->headblock->next;
}
/* }}} */
static XC_ALLOCATOR_FREEBLOCK_NEXT(xc_allocator_bestfit_freeblock_next) /* {{{ */
{
	return block->next;
}
/* }}} */
static XC_ALLOCATOR_BLOCK_SIZE(xc_allocator_bestfit_block_size) /* {{{ */
{
	return block->size;
}
/* }}} */
static XC_ALLOCATOR_BLOCK_OFFSET(xc_allocator_bestfit_block_offset) /* {{{ */
{
	return ((char *) block) - ((char *) allocator);
}
/* }}} */

static XC_ALLOCATOR_INIT(xc_allocator_bestfit_init) /* {{{ */
{
	xc_allocator_bestfit_block_t *b;
#define MINSIZE (ALIGN(sizeof(xc_allocator_bestfit_t)) + sizeof(xc_allocator_bestfit_block_t))
	/* requires at least the header and 1 tail block */
	if (size < MINSIZE) {
		fprintf(stderr, "xc_allocator_bestfit_init requires %lu bytes at least\n", (unsigned long) MINSIZE);
		return NULL;
	}
	TRACE("size=%lu", size);
	allocator->shm = shm;
	allocator->size = size;
	allocator->avail = size - MINSIZE;

	/* pointer to first block, right after ALIGNed header */
	b = allocator->headblock;
	xc_block_setup(b, 0, (xc_allocator_bestfit_block_t *) PADD(allocator, ALIGN(sizeof(xc_allocator_bestfit_t))));

	/* first block*/
	b = b->next;
	xc_block_setup(b, allocator->avail, 0);
#undef MINSIZE

	return allocator;
}
/* }}} */
static XC_ALLOCATOR_DESTROY(xc_allocator_bestfit_destroy) /* {{{ */
{
}
/* }}} */

static xc_allocator_vtable_t xc_allocator_bestfit = XC_ALLOCATOR_VTABLE(allocator_bestfit);
void xc_allocator_bestfit_register() /* {{{ */
{
	if (xc_allocator_register("bestfit", &xc_allocator_bestfit) == 0) {
		zend_error(E_ERROR, "XCache: failed to register allocator 'bestfit'");
	}
}
/* }}} */