aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-06 02:56:27 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-06 07:09:55 +0000
commit0a1b15b307c3194c8da703a3ba832ed9277b4c72 (patch)
tree3599426ae4363bd5d41dd4aa510305f4708fc3d7 /src
parentfeat(arena): add arena allocator (diff)
downloadcodebase-0a1b15b307c3194c8da703a3ba832ed9277b4c72.tar.gz
feat(bump): add bump allocator
Diffstat (limited to 'src')
-rw-r--r--src/bump.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/bump.c b/src/bump.c
new file mode 100644
index 0000000..f1522ab
--- /dev/null
+++ b/src/bump.c
@@ -0,0 +1,82 @@
1// bump.c --- bump allocator -*- C -*-
2//
3// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
4//
5// SPDX-License-Identifier: MIT
6//
7// This file is part of COSMOS Codebase.
8//
9// Commentary:
10//
11// Code:
12
13#include "bump.h"
14#include "dbg.h"
15#include "gpa.h"
16#include "macros.h"
17#include "bits.h"
18
19#include <string.h> // TEMP(cmmm): remove libc dep
20
21#ifndef BUMP_NO_FEAT_GPA
22
23void *
24gpa_fun_bump (void *_ctx, void *ptr, usz old_sz, usz new_sz, usz spec)
25{
26 DBG_ASSERT_NN (_ctx);
27
28 struct buf *buf = _ctx;
29 DBG_ASSERT_BUF (*buf);
30
31 // drop
32 if (!new_sz) {
33 // reclaim if last allocation
34 if (ptr + old_sz == buf->ptr)
35 buf->ptr = ptr;
36
37 return ptr;
38 }
39
40 // in-place resize
41 if (ptr + old_sz == buf->ptr) {
42 void *new_head = buf->ptr + new_sz - old_sz;
43 if (UNLIKELY (new_head > (void *) buf->end))
44 return NULL;
45 buf->ptr = new_head;
46 return ptr;
47 }
48
49 void *new_head = ALIGN_UPTO (buf->ptr, GPA_ALIGN_MASK & spec);
50 void *ret = new_head;
51 new_head += new_sz;
52 if (UNLIKELY (new_head > (void *) buf->end))
53 return NULL;
54
55 buf->ptr = new_head;
56
57 if (ptr) {
58 if (new_sz > old_sz) {
59 memcpy (ret, ptr, old_sz);
60 if (spec & GPA_ZI)
61 memset (ret + old_sz, 0, new_sz - old_sz);
62 } else {
63 memcpy (ret, ptr, new_sz);
64 }
65 } else if (spec & GPA_ZI) {
66 memset (ret, 0, new_sz);
67 }
68
69 return ret;
70}
71
72struct gpa
73gpa_from_bump (struct bump *bump)
74{
75 DBG_ASSERT_NN ( bump);
76 // DBG_ASSERT_BUMP (*bump);
77 return gpa_from (gpa_fun_bump, bump);
78}
79
80#endif // BUMP_NO_FEAT_GPA
81
82// bump.c ends here