diff options
Diffstat (limited to 'base.c')
| -rw-r--r-- | base.c | 172 |
1 files changed, 0 insertions, 172 deletions
| @@ -1,172 +0,0 @@ | |||
| 1 | #include <sys/mman.h> | ||
| 2 | #include <stddef.h> | ||
| 3 | #include <stdint.h> | ||
| 4 | #include <string.h> | ||
| 5 | #include <assert.h> | ||
| 6 | |||
| 7 | #define OK 0 | ||
| 8 | #define ERR_IO 1 | ||
| 9 | #define ERR_PARSE 2 | ||
| 10 | #define ERR_PERM 3 | ||
| 11 | #define ERR_INVALID 4 | ||
| 12 | |||
| 13 | enum | ||
| 14 | { | ||
| 15 | BUFFER_SIZE_SMALL = 128, | ||
| 16 | BUFFER_SIZE_DEFAULT = 256, | ||
| 17 | BUFFER_SIZE_LARGE = 512, | ||
| 18 | PATH_MAX_LEN = 4096 | ||
| 19 | }; | ||
| 20 | |||
| 21 | typedef uint64_t u64; | ||
| 22 | typedef uint32_t u32; | ||
| 23 | typedef uint16_t u16; | ||
| 24 | typedef uint8_t u8; | ||
| 25 | |||
| 26 | typedef int8_t i8; | ||
| 27 | typedef int16_t i16; | ||
| 28 | typedef int32_t i32; | ||
| 29 | typedef int64_t i64; | ||
| 30 | |||
| 31 | typedef i16 b16; | ||
| 32 | typedef i32 b32; | ||
| 33 | |||
| 34 | static inline u64 | ||
| 35 | KiB(u64 n) | ||
| 36 | { | ||
| 37 | return n << 10; | ||
| 38 | } | ||
| 39 | static inline u64 | ||
| 40 | MiB(u64 n) | ||
| 41 | { | ||
| 42 | return n << 20; | ||
| 43 | } | ||
| 44 | static inline u64 | ||
| 45 | GiB(u64 n) | ||
| 46 | { | ||
| 47 | return n << 30; | ||
| 48 | } | ||
| 49 | |||
| 50 | typedef struct mem_arena mem_arena; | ||
| 51 | mem_arena * | ||
| 52 | arena_create(u64 capacity); | ||
| 53 | void | ||
| 54 | arena_destroy(mem_arena *arena); | ||
| 55 | void | ||
| 56 | arena_clear(mem_arena *arena); | ||
| 57 | |||
| 58 | /** | ||
| 59 | * Arena Helper macro's | ||
| 60 | * */ | ||
| 61 | |||
| 62 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | ||
| 63 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) | ||
| 64 | #define ALIGN_UP_POW2(n, p) (((u64)(n) + ((u64)(p) - 1)) & (~((u64)(p) - 1))) | ||
| 65 | |||
| 66 | /* | ||
| 67 | * Represents a disk partition with major/minor device numbers and block count. | ||
| 68 | */ | ||
| 69 | |||
| 70 | /** | ||
| 71 | * replacing malloc/free with arena allocaters | ||
| 72 | * | ||
| 73 | * */ | ||
| 74 | |||
| 75 | #define ARENA_BASE_POS (sizeof(mem_arena)) | ||
| 76 | // void * for the size of a pointer on the machine, 64/32bit comp | ||
| 77 | #define ARENA_ALIGN (sizeof(void *)) | ||
| 78 | |||
| 79 | struct mem_arena | ||
| 80 | { | ||
| 81 | u64 capacity; | ||
| 82 | u64 pos; | ||
| 83 | }; | ||
| 84 | |||
| 85 | // arena prototypes | ||
| 86 | mem_arena * | ||
| 87 | arena_create(u64 capacity); | ||
| 88 | // make it a void pointer to allow implicit conversion | ||
| 89 | void | ||
| 90 | arena_destroy(mem_arena *arena); | ||
| 91 | |||
| 92 | void * | ||
| 93 | arena_push(mem_arena *arena, u64 size, b32 non_zero); | ||
| 94 | |||
| 95 | void | ||
| 96 | arena_pop(mem_arena *arena, u64 size); | ||
| 97 | |||
| 98 | void | ||
| 99 | arena_pop_to(mem_arena *arena, u64 pos); | ||
| 100 | |||
| 101 | void | ||
| 102 | arena_clear(mem_arena *arena); | ||
| 103 | |||
| 104 | mem_arena * | ||
| 105 | arena_create(u64 capacity) | ||
| 106 | { | ||
| 107 | mem_arena *arena = mmap(0, capacity, PROT_READ | PROT_WRITE | PROT_EXEC, | ||
| 108 | MAP_SHARED | MAP_ANONYMOUS, -1, 0); | ||
| 109 | if (arena == MAP_FAILED) | ||
| 110 | { | ||
| 111 | assert(0); | ||
| 112 | } | ||
| 113 | |||
| 114 | arena->capacity = capacity; | ||
| 115 | arena->pos = ARENA_BASE_POS; | ||
| 116 | |||
| 117 | return arena; | ||
| 118 | } | ||
| 119 | |||
| 120 | // make it a void pointer to allow implicit conversion | ||
| 121 | void | ||
| 122 | arena_destroy(mem_arena *arena) | ||
| 123 | { | ||
| 124 | munmap(arena, arena->capacity); | ||
| 125 | } | ||
| 126 | |||
| 127 | void * | ||
| 128 | arena_push(mem_arena *arena, u64 size, b32 non_zero) | ||
| 129 | { | ||
| 130 | u64 pos_aligned = ALIGN_UP_POW2(arena->pos, ARENA_ALIGN); | ||
| 131 | u64 new_pos = pos_aligned + size; | ||
| 132 | |||
| 133 | if (new_pos > arena->capacity) | ||
| 134 | { | ||
| 135 | assert(0); | ||
| 136 | return NULL; | ||
| 137 | } | ||
| 138 | |||
| 139 | arena->pos = new_pos; | ||
| 140 | // cast to u8 to be able to do pointer arithemtic | ||
| 141 | u8 *out = (u8 *)arena + pos_aligned; | ||
| 142 | |||
| 143 | if (!non_zero) | ||
| 144 | { | ||
| 145 | memset(out, 0, size); | ||
| 146 | } | ||
| 147 | return out; | ||
| 148 | } | ||
| 149 | void | ||
| 150 | arena_pop(mem_arena *arena, u64 size) | ||
| 151 | { | ||
| 152 | size = MIN(size, arena->pos - ARENA_BASE_POS); | ||
| 153 | arena->pos -= size; | ||
| 154 | } | ||
| 155 | |||
| 156 | void | ||
| 157 | arena_pop_to(mem_arena *arena, u64 pos) | ||
| 158 | { | ||
| 159 | u64 size = pos < arena->pos ? arena->pos - pos : 0; | ||
| 160 | arena_pop(arena, size); | ||
| 161 | } | ||
| 162 | |||
| 163 | void | ||
| 164 | arena_clear(mem_arena *arena) | ||
| 165 | { | ||
| 166 | arena_pop_to(arena, ARENA_BASE_POS); | ||
| 167 | } | ||
| 168 | |||
| 169 | #define PUSH_STRUCT(arena, T) (T *)arena_push((arena), sizeof(T), 0) | ||
| 170 | #define PUSH_STRUCT_NZ(arena, T) (T *)arena_push((arena), sizeof(T), 1) | ||
| 171 | #define PUSH_ARRAY(arena, T, n) (T *)arena_push((arena), sizeof(T) * (n), 0) | ||
| 172 | #define PUSH_ARRAY_NZ(arena, T, n) (T *)arena_push((arena), sizeof(T) * (n), 1) | ||
