Debian Codegraph @

src/kalloc.c

raw
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 km->min_core_size = min_core_size > 0? min_core_size : 0x80000;
44 return (void*)km;
45 }
46
47 void *km_init(void) { return km_init2(0, 0); }
48
49 void km_destroy(void *_km)
50 {
51 kmem_t *km = (kmem_t*)_km;
52 void *km_par;
53 header_t *p, *q;
54 if (km == NULL) return;
55 km_par = km->par;
56 for (p = km->core_head; p != NULL;) {
57 q = p->ptr;
58 kfree(km_par, p);
59 p = q;
60 }
61 kfree(km_par, km);
62 }
63
64 static header_t *morecore(kmem_t *km, size_t nu)
65 {
66 header_t *q;
67 size_t bytes, *p;
68 nu = (nu + 1 + (km->min_core_size - 1)) / km->min_core_size * km->min_core_size; /* the first +1 for core header */
69 bytes = nu * sizeof(header_t);
70 q = (header_t*)kmalloc(km->par, bytes);
71 if (!q) panic("[morecore] insufficient memory");
72 q->ptr = km->core_head, q->size = nu, km->core_head = q;
73 p = (size_t*)(q + 1);
74 *p = nu - 1; /* the size of the free block; -1 because the first unit is used for the core header */
75 kfree(km, p + 1); /* initialize the new "core"; NB: the core header is not looped. */
76 return km->loop_head;
77 }
78
79 void kfree(void *_km, void *ap) /* kfree() also adds a new core to the circular list */
80 {
81 header_t *p, *q;
82 kmem_t *km = (kmem_t*)_km;
83
84 if (!ap) return;
85 if (km == NULL) {
86 free(ap);
87 return;
88 }
89 p = (header_t*)((size_t*)ap - 1);
90 p->size = *((size_t*)ap - 1);
91 /* Find the pointer that points to the block to be freed. The following loop can stop on two conditions:
92 *
93 * a) "p>q && p<q->ptr": @------#++++++++#+++++++@------- @---------------#+++++++@-------
94 * (can also be in | | | -> | |
95 * two cores) q p q->ptr q q->ptr
96 *
97 * @-------- #+++++++++@-------- @-------- @------------------
98 * | | | -> | |
99 * q p q->ptr q q->ptr
100 *
101 * b) "q>=q->ptr && (p>q || p<q->ptr)": @-------#+++++ @--------#+++++++ @-------#+++++ @----------------
102 * | | | -> | |
103 * q->ptr q p q->ptr q
104 *
105 * #+++++++@----- #++++++++@------- @------------- #++++++++@-------
106 * | | | -> | |
107 * p q->ptr q q->ptr q
108 */
109 for (q = km->loop_head; !(p > q && p < q->ptr); q = q->ptr)
110 if (q >= q->ptr && (p > q || p < q->ptr)) break;
111 if (p + p->size == q->ptr) { /* two adjacent blocks, merge p and q->ptr (the 2nd and 4th cases) */
112 p->size += q->ptr->size;
113 p->ptr = q->ptr->ptr;
114 } else if (p + p->size > q->ptr && q->ptr >= p) {
115 panic("[kfree] The end of the allocated block enters a free block.");
116 } else p->ptr = q->ptr; /* backup q->ptr */
117
118 if (q + q->size == p) { /* two adjacent blocks, merge q and p (the other two cases) */
119 q->size += p->size;
120 q->ptr = p->ptr;
121 km->loop_head = q;
122 } else if (q + q->size > p && p >= q) {
123 panic("[kfree] The end of a free block enters the allocated block.");
124 } else km->loop_head = p, q->ptr = p; /* in two cores, cannot be merged; create a new block in the list */
125 }
126
127 void *kmalloc(void *_km, size_t n_bytes)
128 {
129 kmem_t *km = (kmem_t*)_km;
130 size_t n_units;
131 header_t *p, *q;
132
133 if (n_bytes == 0) return 0;
134 if (km == NULL) return malloc(n_bytes);
135 n_units = (n_bytes + sizeof(size_t) + sizeof(header_t) - 1) / sizeof(header_t); /* header+n_bytes requires at least this number of units */
136
137 if (!(q = km->loop_head)) /* the first time when kmalloc() is called, intialize it */
138 q = km->loop_head = km->base.ptr = &km->base;
139 for (p = q->ptr;; q = p, p = p->ptr) { /* search for a suitable block */
140 if (p->size >= n_units) { /* p->size if the size of current block. This line means the current block is large enough. */
141 if (p->size == n_units) q->ptr = p->ptr; /* no need to split the block */
142 else { /* split the block. NB: memory is allocated at the end of the block! */
143 p->size -= n_units; /* reduce the size of the free block */
144 p += p->size; /* p points to the allocated block */
145 *(size_t*)p = n_units; /* set the size */
146 }
147 km->loop_head = q; /* set the end of chain */
148 return (size_t*)p + 1;
149 }
150 if (p == km->loop_head) { /* then ask for more "cores" */
151 if ((p = morecore(km, n_units)) == 0) return 0;
152 }
153 }
154 }
155
156 void *kcalloc(void *_km, size_t count, size_t size)
157 {
158 kmem_t *km = (kmem_t*)_km;
159 void *p;
160 if (size == 0 || count == 0) return 0;
161 if (km == NULL) return calloc(count, size);
162 p = kmalloc(km, count * size);
163 memset(p, 0, count * size);
164 return p;
165 }
166
167 void *krealloc(void *_km, void *ap, size_t n_bytes) // TODO: this can be made more efficient in principle
168 {
169 kmem_t *km = (kmem_t*)_km;
170 size_t cap, *p, *q;
171
172 if (n_bytes == 0) {
173 kfree(km, ap); return 0;
174 }
175 if (km == NULL) return realloc(ap, n_bytes);
176 if (ap == NULL) return kmalloc(km, n_bytes);
177 p = (size_t*)ap - 1;
178 cap = (*p) * sizeof(header_t) - sizeof(size_t);
179 if (cap >= n_bytes) return ap; /* TODO: this prevents shrinking */
180 q = (size_t*)kmalloc(km, n_bytes);
181 memcpy(q, ap, cap);
182 kfree(km, ap);
183 return q;
184 }
185
186 void km_stat(const void *_km, km_stat_t *s)
187 {
188 kmem_t *km = (kmem_t*)_km;
189 header_t *p;
190 memset(s, 0, sizeof(km_stat_t));
191 if (km == NULL || km->loop_head == NULL) return;
192 for (p = km->loop_head;; p = p->ptr) {
193 s->available += p->size * sizeof(header_t);
194 if (p->size != 0) ++s->n_blocks; /* &kmem_t::base is always one of the cores. It is zero-sized. */
195 if (p->ptr > p && p + p->size > p->ptr)
196 panic("[km_stat] The end of a free block enters another free block.");
197 if (p->ptr == km->loop_head) break;
198 }
199 for (p = km->core_head; p != NULL; p = p->ptr) {
200 size_t size = p->size * sizeof(header_t);
201 ++s->n_cores;
202 s->capacity += size;
203 s->largest = s->largest > size? s->largest : size;
204 }
205 }