aboutsummaryrefslogtreecommitdiffstats
path: root/src/allocator.c
blob: 44f79857d6143650b44a592044ebf2d1aa9a05ab (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "internals.h"


void
libzahl_realloc(z_t a, size_t need)
{
	size_t i, x;
	zahl_char_t *new;

	/* Find n such that n is a minimal power of 2 ≥ need. */
	if (likely((need & (~need + 1)) != need)) {
		need |= need >> 1;
		need |= need >> 2;
		need |= need >> 4;
		for (i = sizeof(need), x = 8; (i >>= 1); x <<= 1)
			need |= need >> x;
		need += 1;
	}

	i = libzahl_msb_nz_zu(need);

	if (likely(libzahl_pool_n[i])) {
		libzahl_pool_n[i]--;
		new = libzahl_pool[i][libzahl_pool_n[i]];
		zmemcpy(new, a->chars, a->alloced);
		zfree(a);
		a->chars = new;
	} else {
		a->chars = realloc(a->chars, need * sizeof(zahl_char_t));
		if (!a->chars) {
			if (!errno) /* sigh... */
				errno = ENOMEM;
			libzahl_failure(errno);
		}
	}
	a->alloced = need;
}