Debian Codegraph @

src/third-party/ksw2/kalloc.c

raw
1 #include <pbcopper/third-party/ksw2/kalloc.h>
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 /* In kalloc, a *core* is a large chunk of contiguous memory. Each core is
8 * associated with a master header, which keeps the size of the current core
9 * and the pointer to next core. Kalloc allocates small *blocks* of memory from
10 * the cores and organizes free memory blocks in a circular single-linked list.
11 *
12 * In the following diagram, "@" stands for the header of a free block (of type
13 * header_t), "#" for the header of an allocated block (of type size_t), "-"
14 * for free memory, and "+" for allocated memory.
15 *
16 * master This region is core 1. master This region is core 2.
17 * | |
18 * *@-------#++++++#++++++++++++@-------- *@----------#++++++++++++#+++++++@------------
19 * | | | |
20 * p=p->ptr->ptr->ptr->ptr p->ptr p->ptr->ptr p->ptr->ptr->ptr
21 */
22
23 #define MIN_CORE_SIZE 0x80000
24
25 typedef struct header_t
26 {
27 size_t size;
28 struct header_t* ptr;
29 } header_t;
30
31 typedef struct
32 {
33 header_t base, *loop_head, *core_head; /* base is a zero-sized block always kept in the loop */
34 } kmem_t;
35
36 static void panic(const char* s)
37 {
38 fprintf(stderr, "%s\n", s);
39 abort();
40 }
41
42 void* km_init(void) { return calloc(1, sizeof(kmem_t)); }
43
44 void km_destroy(void* _km)
45 {
46 kmem_t* km = (kmem_t*)_km;
47 header_t *p, *q;
48 if (km == NULL) return;
49 for (p = km->core_head; p != NULL;) {
50 q = p->ptr;
51 free(p);
52 p = q;
53 }
54 free(km);
55 }
56
57 static header_t* morecore(kmem_t* km, size_t nu)
58 {
59 header_t* q;
60 size_t bytes, *p;
61 nu = (nu + 1 + (MIN_CORE_SIZE - 1)) / MIN_CORE_SIZE *
62 MIN_CORE_SIZE; /* the first +1 for core header */
63 bytes = nu * sizeof(header_t);
64 q = (header_t*)malloc(bytes);
65 if (!q) panic("[morecore] insufficient memory");
66 q->ptr = km->core_head, q->size = nu, km->core_head = q;
67 p = (size_t*)(q + 1);
68 *p = nu -
69 1; /* the size of the free block; -1 because the first unit is used for the core header */
70 kfree(km, p + 1); /* initialize the new "core"; NB: the core header is not looped. */
71 return km->loop_head;
72 }
73
74 void kfree(void* _km, void* ap) /* kfree() also adds a new core to the circular list */
75 {
76 header_t *p, *q;
77 kmem_t* km = (kmem_t*)_km;
78
79 if (!ap) return;
80 if (km == NULL) {
81 free(ap);
82 return;
83 }
84 p = (header_t*)((size_t*)ap - 1);
85 p->size = *((size_t*)ap - 1);
86 /* Find the pointer that points to the block to be freed. The following loop can stop on two conditions:
87 *
88 * a) "p>q && p<q->ptr": @------#++++++++#+++++++@------- @---------------#+++++++@-------
89 * (can also be in | | | -> | |
90 * two cores) q p q->ptr q q->ptr
91 *
92 * @-------- #+++++++++@-------- @-------- @------------------
93 * | | | -> | |
94 * q p q->ptr q q->ptr
95 *
96 * b) "q>=q->ptr && (p>q || p<q->ptr)": @-------#+++++ @--------#+++++++ @-------#+++++ @----------------
97 * | | | -> | |
98 * q->ptr q p q->ptr q
99 *
100 * #+++++++@----- #++++++++@------- @------------- #++++++++@-------
101 * | | | -> | |
102 * p q->ptr q q->ptr q
103 */
104 for (q = km->loop_head; !(p > q && p < q->ptr); q = q->ptr)
105 if (q >= q->ptr && (p > q || p < q->ptr)) break;
106 if (p + p->size ==
107 q->ptr) { /* two adjacent blocks, merge p and q->ptr (the 2nd and 4th cases) */
108 p->size += q->ptr->size;
109 p->ptr = q->ptr->ptr;
110 } else if (p + p->size > q->ptr && q->ptr >= p) {
111 panic("[kfree] The end of the allocated block enters a free block.");
112 } else
113 p->ptr = q->ptr; /* backup q->ptr */
114
115 if (q + q->size == p) { /* two adjacent blocks, merge q and p (the other two cases) */
116 q->size += p->size;
117 q->ptr = p->ptr;
118 km->loop_head = q;
119 } else if (q + q->size > p && p >= q) {
120 panic("[kfree] The end of a free block enters the allocated block.");
121 } else
122 km->loop_head = p,
123 q->ptr = p; /* in two cores, cannot be merged; create a new block in the list */
124 }
125
126 void* kmalloc(void* _km, size_t n_bytes)
127 {
128 kmem_t* km = (kmem_t*)_km;
129 size_t n_units;
130 header_t *p, *q;
131
132 if (n_bytes == 0) return 0;
133 if (km == NULL) return malloc(n_bytes);
134 n_units = (n_bytes + sizeof(size_t) + sizeof(header_t) - 1) / sizeof(header_t) + 1;
135
136 if (!(q = km->loop_head)) /* the first time when kmalloc() is called, intialize it */
137 q = km->loop_head = km->base.ptr = &km->base;
138 for (p = q->ptr;; q = p, p = p->ptr) { /* search for a suitable block */
139 if (p->size >=
140 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)
142 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,
169 size_t n_bytes) // TODO: this can be made more efficient in principle
170 {
171 kmem_t* km = (kmem_t*)_km;
172 size_t n_units, *p, *q;
173
174 if (n_bytes == 0) {
175 kfree(km, ap);
176 return 0;
177 }
178 if (km == NULL) return realloc(ap, n_bytes);
179 if (ap == NULL) return kmalloc(km, n_bytes);
180 n_units = (n_bytes + sizeof(size_t) + sizeof(header_t) - 1) / sizeof(header_t);
181 p = (size_t*)ap - 1;
182 if (*p >= n_units) return ap; /* TODO: this prevents shrinking */
183 q = (size_t*)kmalloc(km, n_bytes);
184 memcpy(q, ap, (*p - 1) * sizeof(header_t));
185 kfree(km, ap);
186 return q;
187 }
188
189 void km_stat(const void* _km, km_stat_t* s)
190 {
191 kmem_t* km = (kmem_t*)_km;
192 header_t* p;
193 memset(s, 0, sizeof(km_stat_t));
194 if (km == NULL || km->loop_head == NULL) return;
195 for (p = km->loop_head;; p = p->ptr) {
196 s->available += p->size * sizeof(header_t);
197 if (p->size != 0)
198 ++s->n_blocks; /* &kmem_t::base is always one of the cores. It is zero-sized. */
199 if (p->ptr > p && p + p->size > p->ptr)
200 panic("[km_stat] The end of a free block enters another free block.");
201 if (p->ptr == km->loop_head) break;
202 }
203 for (p = km->core_head; p != NULL; p = p->ptr) {
204 size_t size = p->size * sizeof(header_t);
205 ++s->n_cores;
206 s->capacity += size;
207 s->largest = s->largest > size ? s->largest : size;
208 }
209 }