aboutsummaryrefslogtreecommitdiff
path: root/src/arena.c
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-06-22 13:25:24 +0200
committerThomas Voss <mail@thomasvoss.com> 2024-06-22 13:26:22 +0200
commit0c83d031ff793f364b86bcf557584580edfe66a5 (patch)
treebeef319096e8545d7dbf0091e46a57626c27ea1f /src/arena.c
parent9e127d92246d6577792122b013088466e3688da1 (diff)
Rename lots of things to make my life easier
Diffstat (limited to 'src/arena.c')
-rw-r--r--src/arena.c30
1 files changed, 18 insertions, 12 deletions
diff --git a/src/arena.c b/src/arena.c
index 5b50404..e7091fb 100644
--- a/src/arena.c
+++ b/src/arena.c
@@ -26,14 +26,20 @@
#define IS_POW_2(n) ((n) != 0 && ((n) & ((n)-1)) == 0)
struct _arena {
+ /* DATA points to the start of the block’s memory while FREE points
+ to the beginning of the unused data in the block */
+ void *data, *free;
size_t len, cap;
- void *data, *last;
struct _arena *next;
};
-static struct _arena *mkblk(size_t)
+/* Return a new arena block of size SZ */
+static struct _arena *mkblk(size_t sz)
__attribute__((returns_nonnull));
-static inline size_t pad(size_t, size_t)
+
+/* Return the padding required to properly align an allocation with
+ alignment ALIGN at offset OFF */
+static inline size_t pad(size_t off, size_t align)
__attribute__((const, always_inline));
void *
@@ -42,8 +48,8 @@ arena_alloc(struct _arena **a, size_t nmemb, size_t size, size_t align)
assert(IS_POW_2(align));
assert(nmemb * size != 0);
- if (size > SIZE_MAX / nmemb) {
- errno = EOVERFLOW;
+ if (unlikely(size > SIZE_MAX / nmemb)) {
+ errno = ENOMEM;
err("%s:", __func__);
}
@@ -57,7 +63,7 @@ arena_alloc(struct _arena **a, size_t nmemb, size_t size, size_t align)
if (nlen <= p->cap) {
void *ret = (char *)p->data + off;
p->len = nlen;
- p->last = ret;
+ p->free = ret;
return ret;
}
}
@@ -71,27 +77,27 @@ arena_alloc(struct _arena **a, size_t nmemb, size_t size, size_t align)
}
void *
-_arena_grow(arena *a, void *ptr, size_t old_nmemb, size_t new_nmemb,
+_arena_grow(arena_t *a, void *ptr, size_t old_nmemb, size_t new_nmemb,
size_t size, size_t align)
{
assert(IS_POW_2(align));
assert(new_nmemb * size != 0);
- if (size > SIZE_MAX / new_nmemb) {
- errno = EOVERFLOW;
+ if (unlikely(size > SIZE_MAX / new_nmemb)) {
+ errno = ENOMEM;
err("%s:", __func__);
}
size *= new_nmemb;
for (struct _arena *p = *a; p != NULL; p = p->next) {
- if (ptr < p->data || ptr > p->last)
+ if (ptr < p->data || ptr > p->free)
continue;
/* 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 == p->last) {
+ if (ptr == p->free) {
size_t rest = p->cap - p->len;
size_t need = (new_nmemb - old_nmemb) * size;
if (need <= rest) {
@@ -134,7 +140,7 @@ mkblk(size_t cap)
a->data = mmap(NULL, cap, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
if (a->data == MAP_FAILED)
err("mmap:");
- a->last = a->data;
+ a->free = a->data;
return a;
}