diff options
| author | 2026-11-06 02:56:27 +0000 | |
|---|---|---|
| committer | 2026-11-06 07:09:55 +0000 | |
| commit | 0a1b15b307c3194c8da703a3ba832ed9277b4c72 (patch) | |
| tree | 3599426ae4363bd5d41dd4aa510305f4708fc3d7 | |
| parent | feat(arena): add arena allocator (diff) | |
| download | codebase-0a1b15b307c3194c8da703a3ba832ed9277b4c72.tar.gz | |
feat(bump): add bump allocator
| -rw-r--r-- | include/bump.h | 34 | ||||
| -rw-r--r-- | src/bump.c | 82 |
2 files changed, 116 insertions, 0 deletions
diff --git a/include/bump.h b/include/bump.h new file mode 100644 index 0000000..49dbc64 --- /dev/null +++ b/include/bump.h | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | // bump.h --- 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 | // HOLD(cmmm): add direct bump API. | ||
| 12 | // | ||
| 13 | // Code: | ||
| 14 | |||
| 15 | #ifndef INCLUDED_COSMOS_BUMP_H | ||
| 16 | #define INCLUDED_COSMOS_BUMP_H | ||
| 17 | |||
| 18 | #include "buf.h" | ||
| 19 | |||
| 20 | struct bump { | ||
| 21 | struct buf buf; //!< Backing memory buffer. | ||
| 22 | }; | ||
| 23 | |||
| 24 | #ifndef BUMP_NO_FEAT_GPA | ||
| 25 | #include "gpa.h" | ||
| 26 | |||
| 27 | void *gpa_fun_bump (void *ctx, void *ptr, usz old_sz, usz new_sz, usz align); | ||
| 28 | struct gpa gpa_from_bump (struct bump *); | ||
| 29 | |||
| 30 | #endif // BUMP_NO_FEAT_GPA | ||
| 31 | |||
| 32 | #endif // INCLUDED_COSMOS_BUMP_H | ||
| 33 | |||
| 34 | // bump.h ends here | ||
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 | |||
| 23 | void * | ||
| 24 | gpa_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 | |||
| 72 | struct gpa | ||
| 73 | gpa_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 | ||
