Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 1 | #include "alloc.h" |
| 2 | |
| 3 | #include "dlog.h" |
| 4 | #include "spinlock.h" |
| 5 | |
| 6 | static size_t alloc_base; |
| 7 | static size_t alloc_limit; |
| 8 | static struct spinlock alloc_lock = SPINLOCK_INIT; |
| 9 | |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 10 | /** |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 11 | * Initializes the allocator. |
| 12 | */ |
| 13 | void halloc_init(size_t base, size_t size) |
| 14 | { |
| 15 | alloc_base = base; |
| 16 | alloc_limit = base + size; |
| 17 | } |
| 18 | |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 19 | /** |
| 20 | * Allocates the requested amount of memory. Returns NULL when there isn't |
| 21 | * enough free memory. |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 22 | */ |
| 23 | void *halloc(size_t size) |
| 24 | { |
| 25 | return halloc_aligned(size, 2 * sizeof(size_t)); |
| 26 | } |
| 27 | |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 28 | /** |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 29 | * Frees the provided memory. |
| 30 | * |
| 31 | * Currently unimplemented. |
| 32 | */ |
| 33 | void hfree(void *ptr) |
| 34 | { |
| 35 | dlog("Attempted to free pointer %p\n", ptr); |
| 36 | } |
| 37 | |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 38 | /** |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 39 | * Allocates the requested amount of memory, with the requested alignment. |
| 40 | * |
| 41 | * Alignment must be a power of two. Returns NULL when there isn't enough free |
| 42 | * memory. |
| 43 | */ |
| 44 | void *halloc_aligned(size_t size, size_t align) |
| 45 | { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 46 | void *ret; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 47 | |
| 48 | sl_lock(&alloc_lock); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 49 | ret = halloc_aligned_nosync(size, align); |
| 50 | sl_unlock(&alloc_lock); |
| 51 | |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Allocates the requested amount of memory, with the requested alignment, but |
| 57 | * no synchronisation with other CPUs. The caller is responsible for serialising |
| 58 | * all such calls. |
| 59 | * |
| 60 | * Alignment must be a power of two. Returns NULL when there isn't enough free |
| 61 | * memory. |
| 62 | */ |
| 63 | void *halloc_aligned_nosync(size_t size, size_t align) |
| 64 | { |
| 65 | size_t begin; |
| 66 | size_t end; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 67 | |
| 68 | begin = (alloc_base + align - 1) & ~(align - 1); |
| 69 | end = begin + size; |
| 70 | |
| 71 | /* Check for overflows, and that there is enough free mem. */ |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 72 | if (end > begin && begin >= alloc_base && end <= alloc_limit) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 73 | alloc_base = end; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 74 | } else { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 75 | begin = 0; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 76 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 77 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 78 | return (void *)begin; |
| 79 | } |