klib
[Ported from github.com/attractivechaos/klib] added block arena
Brought by drakestapleton
klib / kalloc.c
| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include "kalloc.h" |
| 5 | |
| 6 | /* In kalloc, a *core* is a large chunk of contiguous memory. Each core is |
| 7 | * associated with a master header, which keeps the size of the current core |
| 8 | * and the pointer to next core. Kalloc allocates small *blocks* of memory from |
| 9 | * the cores and organizes free memory blocks in a circular single-linked list. |
| 10 | * |
| 11 | * In the following diagram, "@" stands for the header of a free block (of type |
| 12 | * header_t), "#" for the header of an allocated block (of type size_t), "-" |
| 13 | * for free memory, and "+" for allocated memory. |
| 14 | * |
| 15 | * master This region is core 1. master This region is core 2. |
| 16 | * | | |
| 17 | * *@-------#++++++#++++++++++++@-------- *@----------#++++++++++++#+++++++@------------ |
| 18 | * | | | | |
| 19 | * p=p->ptr->ptr->ptr->ptr p->ptr p->ptr->ptr p->ptr->ptr->ptr |
| 20 | */ |
| 21 | typedef struct header_t { |
| 22 | size_t size; |
| 23 | struct header_t *ptr; |
| 24 | } header_t; |
| 25 | |
| 26 | typedef struct { |
| 27 | void *par; |
| 28 | size_t min_core_size; |
| 29 | header_t base, *loop_head, *core_head; /* base is a zero-sized block always kept in the loop */ |
| 30 | } kmem_t; |
| 31 | |
| 32 | static void panic(const char *s) |
| 33 | { |
| 34 | fprintf(stderr, "%s\n", s); |
| 35 | abort(); |
| 36 | } |
| 37 | |
| 38 | void *km_init2(void *km_par, size_t min_core_size) |
| 39 | { |
| 40 | kmem_t *km; |
| 41 | km = (kmem_t*)kcalloc(km_par, 1, sizeof(kmem_t)); |
| 42 | km->par = km_par; |
| 43 | if (km_par) km->min_core_size = min_core_size > 0? min_core_size : ((kmem_t*)km_par)->min_core_size - 2; |
| 44 | else km->min_core_size = min_core_size > 0? min_core_size : 0x80000; |
| 45 | return (void*)km; |
| 46 | } |
| 47 | |
| 48 | void *km_init(void) { return km_init2(0, 0); } |
| 49 | |
| 50 | void km_destroy(void *_km) |
| 51 | { |
| 52 | kmem_t *km = (kmem_t*)_km; |
| 53 | void *km_par; |
| 54 | header_t *p, *q; |
| 55 | if (km == NULL) return; |
| 56 | km_par = km->par; |
| 57 | for (p = km->core_head; p != NULL;) { |
| 58 | q = p->ptr; |
| 59 | kfree(km_par, p); |
| 60 | p = q; |
| 61 | } |
| 62 | kfree(km_par, km); |
| 63 | } |
| 64 | |
| 65 | static header_t *morecore(kmem_t *km, size_t nu) |
| 66 | { |
| 67 | header_t *q; |
| 68 | size_t bytes, *p; |
| 69 | nu = (nu + 1 + (km->min_core_size - 1)) / km->min_core_size * km->min_core_size; /* the first +1 for core header */ |
| 70 | bytes = nu * sizeof(header_t); |
| 71 | q = (header_t*)kmalloc(km->par, bytes); |
| 72 | if (!q) panic("[morecore] insufficient memory"); |
| 73 | q->ptr = km->core_head, q->size = nu, km->core_head = q; |
| 74 | p = (size_t*)(q + 1); |
| 75 | *p = nu - 1; /* the size of the free block; -1 because the first unit is used for the core header */ |
| 76 | kfree(km, p + 1); /* initialize the new "core"; NB: the core header is not looped. */ |
| 77 | return km->loop_head; |
| 78 | } |
| 79 | |
| 80 | void kfree(void *_km, void *ap) /* kfree() also adds a new core to the circular list */ |
| 81 | { |
| 82 | header_t *p, *q; |
| 83 | kmem_t *km = (kmem_t*)_km; |
| 84 | |
| 85 | if (!ap) return; |
| 86 | if (km == NULL) { |
| 87 | free(ap); |
| 88 | return; |
| 89 | } |
| 90 | p = (header_t*)((size_t*)ap - 1); |
| 91 | p->size = *((size_t*)ap - 1); |
| 92 | /* Find the pointer that points to the block to be freed. The following loop can stop on two conditions: |
| 93 | * |
| 94 | * a) "p>q && p<q->ptr": @------#++++++++#+++++++@------- @---------------#+++++++@------- |
| 95 | * (can also be in | | | -> | | |
| 96 | * two cores) q p q->ptr q q->ptr |
| 97 | * |
| 98 | * @-------- #+++++++++@-------- @-------- @------------------ |
| 99 | * | | | -> | | |
| 100 | * q p q->ptr q q->ptr |
| 101 | * |
| 102 | * b) "q>=q->ptr && (p>q || p<q->ptr)": @-------#+++++ @--------#+++++++ @-------#+++++ @---------------- |
| 103 | * | | | -> | | |
| 104 | * q->ptr q p q->ptr q |
| 105 | * |
| 106 | * #+++++++@----- #++++++++@------- @------------- #++++++++@------- |
| 107 | * | | | -> | | |
| 108 | * p q->ptr q q->ptr q |
| 109 | */ |
| 110 | for (q = km->loop_head; !(p > q && p < q->ptr); q = q->ptr) |
| 111 | if (q >= q->ptr && (p > q || p < q->ptr)) break; |
| 112 | if (p + p->size == q->ptr) { /* two adjacent blocks, merge p and q->ptr (the 2nd and 4th cases) */ |
| 113 | p->size += q->ptr->size; |
| 114 | p->ptr = q->ptr->ptr; |
| 115 | } else if (p + p->size > q->ptr && q->ptr >= p) { |
| 116 | panic("[kfree] The end of the allocated block enters a free block."); |
| 117 | } else p->ptr = q->ptr; /* backup q->ptr */ |
| 118 | |
| 119 | if (q + q->size == p) { /* two adjacent blocks, merge q and p (the other two cases) */ |
| 120 | q->size += p->size; |
| 121 | q->ptr = p->ptr; |
| 122 | km->loop_head = q; |
| 123 | } else if (q + q->size > p && p >= q) { |
| 124 | panic("[kfree] The end of a free block enters the allocated block."); |
| 125 | } else km->loop_head = p, q->ptr = p; /* in two cores, cannot be merged; create a new block in the list */ |
| 126 | } |
| 127 | |
| 128 | void *kmalloc(void *_km, size_t n_bytes) |
| 129 | { |
| 130 | kmem_t *km = (kmem_t*)_km; |
| 131 | size_t n_units; |
| 132 | header_t *p, *q; |
| 133 | |
| 134 | if (n_bytes == 0) return 0; |
| 135 | if (km == NULL) return malloc(n_bytes); |
| 136 | n_units = (n_bytes + sizeof(size_t) + sizeof(header_t) - 1) / sizeof(header_t); /* header+n_bytes requires at least this number of units */ |
| 137 | |
| 138 | if (!(q = km->loop_head)) /* the first time when kmalloc() is called, intialize it */ |
| 139 | q = km->loop_head = km->base.ptr = &km->base; |
| 140 | for (p = q->ptr;; q = p, p = p->ptr) { /* search for a suitable block */ |
| 141 | if (p->size >= n_units) { /* p->size if the size of current block. This line means the current block is large enough. */ |
| 142 | if (p->size == n_units) q->ptr = p->ptr; /* no need to split the block */ |
| 143 | else { /* split the block. NB: memory is allocated at the end of the block! */ |
| 144 | p->size -= n_units; /* reduce the size of the free block */ |
| 145 | p += p->size; /* p points to the allocated block */ |
| 146 | *(size_t*)p = n_units; /* set the size */ |
| 147 | } |
| 148 | km->loop_head = q; /* set the end of chain */ |
| 149 | return (size_t*)p + 1; |
| 150 | } |
| 151 | if (p == km->loop_head) { /* then ask for more "cores" */ |
| 152 | if ((p = morecore(km, n_units)) == 0) return 0; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void *kcalloc(void *_km, size_t count, size_t size) |
| 158 | { |
| 159 | kmem_t *km = (kmem_t*)_km; |
| 160 | void *p; |
| 161 | if (size == 0 || count == 0) return 0; |
| 162 | if (km == NULL) return calloc(count, size); |
| 163 | p = kmalloc(km, count * size); |
| 164 | memset(p, 0, count * size); |
| 165 | return p; |
| 166 | } |
| 167 | |
| 168 | void *krealloc(void *_km, void *ap, size_t n_bytes) // TODO: this can be made more efficient in principle |
| 169 | { |
| 170 | kmem_t *km = (kmem_t*)_km; |
| 171 | size_t cap, *p, *q; |
| 172 | |
| 173 | if (n_bytes == 0) { |
| 174 | kfree(km, ap); return 0; |
| 175 | } |
| 176 | if (km == NULL) return realloc(ap, n_bytes); |
| 177 | if (ap == NULL) return kmalloc(km, n_bytes); |
| 178 | p = (size_t*)ap - 1; |
| 179 | cap = (*p) * sizeof(header_t) - sizeof(size_t); |
| 180 | if (cap >= n_bytes) return ap; /* TODO: this prevents shrinking */ |
| 181 | q = (size_t*)kmalloc(km, n_bytes); |
| 182 | memcpy(q, ap, cap); |
| 183 | kfree(km, ap); |
| 184 | return q; |
| 185 | } |
| 186 | |
| 187 | void *krelocate(void *km, void *ap, size_t n_bytes) |
| 188 | { |
| 189 | void *p; |
| 190 | if (km == 0 || ap == 0) return ap; |
| 191 | p = kmalloc(km, n_bytes); |
| 192 | memcpy(p, ap, n_bytes); |
| 193 | kfree(km, ap); |
| 194 | return p; |
| 195 | } |
| 196 | |
| 197 | void km_stat(const void *_km, km_stat_t *s) |
| 198 | { |
| 199 | kmem_t *km = (kmem_t*)_km; |
| 200 | header_t *p; |
| 201 | memset(s, 0, sizeof(km_stat_t)); |
| 202 | if (km == NULL || km->loop_head == NULL) return; |
| 203 | for (p = km->loop_head;; p = p->ptr) { |
| 204 | s->available += p->size * sizeof(header_t); |
| 205 | if (p->size != 0) ++s->n_blocks; /* &kmem_t::base is always one of the cores. It is zero-sized. */ |
| 206 | if (p->ptr > p && p + p->size > p->ptr) |
| 207 | panic("[km_stat] The end of a free block enters another free block."); |
| 208 | if (p->ptr == km->loop_head) break; |
| 209 | } |
| 210 | for (p = km->core_head; p != NULL; p = p->ptr) { |
| 211 | size_t size = p->size * sizeof(header_t); |
| 212 | ++s->n_cores; |
| 213 | s->capacity += size; |
| 214 | s->largest = s->largest > size? s->largest : size; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void km_stat_print(const void *km) |
| 219 | { |
| 220 | km_stat_t st; |
| 221 | km_stat(km, &st); |
| 222 | fprintf(stderr, "[km_stat] cap=%ld, avail=%ld, largest=%ld, n_core=%ld, n_block=%ld\n", |
| 223 | st.capacity, st.available, st.largest, st.n_blocks, st.n_cores); |
| 224 | } |