aboutsummaryrefslogtreecommitdiff
path: root/lib/alloc/arena_realloc.c
blob: 6877ae17b4aabb67befab1b966522f7d257ae7aa (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <errno.h>
#include <stdckdint.h>
#include <string.h>

#include "alloc.h"
#include "macros.h"

void *
arena_realloc(arena *a, void *ptr, size_t old, size_t new, size_t elemsz,
              size_t align)
{
	ASSUME(a != nullptr);

	if (ptr == nullptr)
		return arena_alloc(a, new, elemsz, align);

	if (old == new)
		return ptr;

	struct _region *cur = a->_head;
	while (cur != nullptr) {
		if (ptr >= cur->data && (char *)ptr < (char *)cur->data + cur->cap)
			break;
		cur = cur->next;
	}
	if (cur == nullptr)
		cur = a->_head;

	/* cur now points to the region containing ‘ptr’ */

	/* If we are shrinking the buffer, then we don’t need to move our allocation
	   and can just return it directly.  As a minor optimization if the
	   allocation we are shrinking is the last allocation in the current region,
	   we can decrease the region length to make space for more future
	   allocations. */
	if (old > new) {
		if (ptr == cur->last)
			cur->len -= (old - new) * elemsz;
		return ptr;
	}

	ASSUME(old < new);

	/* If we need to grow the given allocation, but it was the last allocation
	   made in a region, then we first see if we can just eat more trailing free
	   space in the region to avoid a memcpy(). */
	if (ptr == cur->last) {
		size_t need, free = cur->cap - cur->len;
		if (ckd_mul(&need, new - old, elemsz)) {
			errno = EOVERFLOW;
			return nullptr;
		}
		if (need <= free) {
			cur->len += need;
			return ptr;
		}
	}

	/* At this point we just make a new allocation and copy the data over */
	void *dst = arena_alloc(a, new, elemsz, align);
	return dst == nullptr ? nullptr : memcpy(dst, ptr, old * elemsz);
}