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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
// bump.c --- bump allocator -*- C -*-
//
// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
//
// SPDX-License-Identifier: MIT
//
// This file is part of COSMOS Codebase.
//
// Commentary:
//
// Code:
#include "bump.h"
#include "dbg.h"
#include "gpa.h"
#include "macros.h"
#include "bits.h"
#include <string.h> // TEMP(cmmm): remove libc dep
#ifndef BUMP_NO_FEAT_GPA
void *
gpa_fun_bump (void *_ctx, void *ptr, usz old_sz, usz new_sz, usz spec)
{
DBG_ASSERT_NN (_ctx);
struct buf *buf = _ctx;
DBG_ASSERT_BUF (*buf);
// drop
if (!new_sz) {
// reclaim if last allocation
if (ptr + old_sz == buf->ptr)
buf->ptr = ptr;
return ptr;
}
// in-place resize
if (ptr + old_sz == buf->ptr) {
void *new_head = buf->ptr + new_sz - old_sz;
if (UNLIKELY (new_head > (void *) buf->end))
return NULL;
buf->ptr = new_head;
return ptr;
}
void *new_head = ALIGN_UPTO (buf->ptr, GPA_ALIGN_MASK & spec);
void *ret = new_head;
new_head += new_sz;
if (UNLIKELY (new_head > (void *) buf->end))
return NULL;
buf->ptr = new_head;
if (ptr) {
if (new_sz > old_sz) {
memcpy (ret, ptr, old_sz);
if (spec & GPA_ZI)
memset (ret + old_sz, 0, new_sz - old_sz);
} else {
memcpy (ret, ptr, new_sz);
}
} else if (spec & GPA_ZI) {
memset (ret, 0, new_sz);
}
return ret;
}
struct gpa
gpa_from_bump (struct bump *bump)
{
DBG_ASSERT_NN ( bump);
// DBG_ASSERT_BUMP (*bump);
return gpa_from (gpa_fun_bump, bump);
}
#endif // BUMP_NO_FEAT_GPA
// bump.c ends here
|