aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / mm / heap.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* heap.c -- see heap.h. */ |
| 18 | #include "heap.h" |
| 19 | |
| 20 | #define HDR 16u |
| 21 | #define MAGIC_USED 0xa1e05ed0u |
| 22 | #define MAGIC_FREE 0xa1e0f4eeu |
| 23 | |
| 24 | struct blk { |
| 25 | uint64_t size; /* whole block incl. header, multiple of 16 */ |
| 26 | uint32_t magic; |
| 27 | uint32_t pad; |
| 28 | }; |
| 29 | |
| 30 | static struct blk *at(const struct ck_heap *h, size_t off) { return (struct blk *)(h->base + off); } |
| 31 | |
| 32 | int ck_heap_init(struct ck_heap *h, void *base, size_t size) |
| 33 | { |
| 34 | uintptr_t b = ((uintptr_t)base + 15) & ~(uintptr_t)15; |
| 35 | size_t lost = b - (uintptr_t)base; |
| 36 | if (size < lost + 2 * HDR) |
| 37 | return -1; |
| 38 | h->base = (uint8_t *)b; |
| 39 | h->size = (size - lost) & ~(size_t)15; |
| 40 | struct blk *f = at(h, 0); |
| 41 | f->size = h->size; |
| 42 | f->magic = MAGIC_FREE; |
| 43 | f->pad = 0; |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | void *ck_heap_alloc(struct ck_heap *h, size_t bytes) |
| 48 | { |
| 49 | if (!h->base || bytes > h->size) |
| 50 | return 0; |
| 51 | size_t need = ((bytes + 15) & ~(size_t)15) + HDR; |
| 52 | if (need < 2 * HDR) |
| 53 | need = 2 * HDR; |
| 54 | for (size_t off = 0; off < h->size;) { |
| 55 | struct blk *b = at(h, off); |
| 56 | if (b->magic == MAGIC_FREE && b->size >= need) { |
| 57 | if (b->size - need >= 2 * HDR) { |
| 58 | struct blk *rest = at(h, off + need); |
| 59 | rest->size = b->size - need; |
| 60 | rest->magic = MAGIC_FREE; |
| 61 | rest->pad = 0; |
| 62 | b->size = need; |
| 63 | } |
| 64 | b->magic = MAGIC_USED; |
| 65 | uint8_t *p = (uint8_t *)b + HDR; |
| 66 | for (size_t i = 0; i < b->size - HDR; i++) |
| 67 | p[i] = 0; |
| 68 | return p; |
| 69 | } |
| 70 | if (!b->size) |
| 71 | return 0; /* corrupted */ |
| 72 | off += b->size; |
| 73 | } |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | int ck_heap_free(struct ck_heap *h, void *p) |
| 78 | { |
| 79 | uint8_t *u = p; |
| 80 | if (!p || u < h->base + HDR || u >= h->base + h->size || ((uintptr_t)u & 15)) |
| 81 | return -1; |
| 82 | /* Find the block by walking, so a forged pointer is never trusted. */ |
| 83 | size_t prev = (size_t)-1; |
| 84 | for (size_t off = 0; off < h->size;) { |
| 85 | struct blk *b = at(h, off); |
| 86 | if (!b->size) |
| 87 | return -1; |
| 88 | if ((uint8_t *)b + HDR == u) { |
| 89 | if (b->magic != MAGIC_USED) |
| 90 | return -1; |
| 91 | b->magic = MAGIC_FREE; |
| 92 | size_t next = off + b->size; |
| 93 | if (next < h->size && at(h, next)->magic == MAGIC_FREE) |
| 94 | b->size += at(h, next)->size; |
| 95 | if (prev != (size_t)-1 && at(h, prev)->magic == MAGIC_FREE) |
| 96 | at(h, prev)->size += b->size; |
| 97 | return 0; |
| 98 | } |
| 99 | prev = off; |
| 100 | off += b->size; |
| 101 | } |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | size_t ck_heap_free_bytes(const struct ck_heap *h) |
| 106 | { |
| 107 | size_t s = 0; |
| 108 | for (size_t off = 0; off < h->size;) { |
| 109 | struct blk *b = at(h, off); |
| 110 | if (!b->size) |
| 111 | break; |
| 112 | if (b->magic == MAGIC_FREE) |
| 113 | s += b->size - HDR; |
| 114 | off += b->size; |
| 115 | } |
| 116 | return s; |
| 117 | } |
| 118 | |
| 119 | int ck_heap_check(const struct ck_heap *h) |
| 120 | { |
| 121 | size_t off = 0; |
| 122 | while (off < h->size) { |
| 123 | struct blk *b = at(h, off); |
| 124 | if (b->size < 2 * HDR || (b->size & 15) || (b->magic != MAGIC_USED && b->magic != MAGIC_FREE)) |
| 125 | return -1; |
| 126 | off += b->size; |
| 127 | } |
| 128 | return off == h->size ? 0 : -1; |
| 129 | } |