Skip to content

Commit 8b9df48

Browse files
committed
Simplifying openbios allocation scenario.
1 parent a658a18 commit 8b9df48

File tree

1 file changed

+10
-29
lines changed

1 file changed

+10
-29
lines changed

src/mips/openbios/kernel/alloc.c

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -64,39 +64,21 @@ typedef struct allocated_block_ {
6464
_Static_assert(sizeof(empty_block) == (2 * sizeof(void *)), "empty_block is of the wrong size");
6565
_Static_assert(sizeof(allocated_block) == (2 * sizeof(void *)), "allocated_block is of the wrong size");
6666

67-
static void *user_heap_start = NULL;
68-
static void *user_heap_end = NULL;
69-
static void *kern_heap_start = NULL;
70-
static void *kern_heap_end = NULL;
71-
7267
static empty_block *user_heap_head = NULL;
7368
static empty_block *kern_heap_head = NULL;
7469

75-
static empty_block marker;
70+
static empty_block marker = { .next = NULL, .size = 0 };
7671

7772
enum heap { HEAP_USER, HEAP_KERNEL };
7873

79-
static __attribute__((section(".ramtext"))) void *multi_malloc(size_t size_, enum heap heap) {
74+
static __attribute__((section(".ramtext"))) void *multi_malloc(size_t size_, const enum heap heap) {
8075
empty_block *curr = heap == HEAP_USER ? user_heap_head : kern_heap_head;
8176
empty_block *prev = NULL;
8277
empty_block *best_fit = NULL;
8378
empty_block *best_fit_prev = NULL;
8479

8580
size_t size = ALIGN_TO(size_ + sizeof(allocated_block));
8681

87-
if (curr == NULL) {
88-
marker.next = NULL;
89-
marker.size = 0;
90-
curr = (empty_block *)ALIGN_TO(heap == HEAP_USER ? user_heap_start : kern_heap_start);
91-
if (heap == HEAP_USER) {
92-
user_heap_head = curr;
93-
} else {
94-
kern_heap_head = curr;
95-
}
96-
curr->next = ▮
97-
curr->size = ALIGN_TO(((size_t)(heap == HEAP_USER ? user_heap_end : kern_heap_end)) - sizeof(empty_block));
98-
}
99-
10082
size_t curr_size = 0;
10183
while ((curr_size != size) && (curr != &marker)) {
10284
curr_size = curr->size;
@@ -147,7 +129,7 @@ static __attribute__((section(".ramtext"))) void *multi_malloc(size_t size_, enu
147129
return ptr;
148130
}
149131

150-
static __attribute__((section(".ramtext"))) void multi_free(void *ptr_, enum heap heap) {
132+
static __attribute__((section(".ramtext"))) void multi_free(void *ptr_, const enum heap heap) {
151133
if (ptr_ == NULL) {
152134
return;
153135
}
@@ -218,7 +200,7 @@ static __attribute__((section(".ramtext"))) void multi_free(void *ptr_, enum hea
218200
}
219201
}
220202

221-
static __attribute__((section(".ramtext"))) void *multi_realloc(void *ptr_, size_t size_, enum heap heap) {
203+
static __attribute__((section(".ramtext"))) void *multi_realloc(void *ptr_, size_t size_, const enum heap heap) {
222204
if (ptr_ == NULL) {
223205
return multi_malloc(size_, heap);
224206
}
@@ -341,7 +323,6 @@ static __attribute__((section(".ramtext"))) void *multi_realloc(void *ptr_, size
341323
if (new_ptr == NULL) {
342324
return NULL;
343325
}
344-
//__builtin_memcpy(new_ptr, ptr_, old_size - sizeof(empty_block));
345326
uint32_t * src = (uint32_t *)ptr_;
346327
uint32_t * dst = (uint32_t *)new_ptr;
347328
uint32_t size_to_copy = old_size - sizeof(empty_block);
@@ -359,9 +340,9 @@ __attribute__((section(".ramtext"))) void *user_realloc(void *ptr, size_t size)
359340
return multi_realloc(ptr, size, HEAP_USER);
360341
}
361342
__attribute__((section(".ramtext"))) void user_initheap(void *base, size_t size) {
362-
user_heap_start = base;
363-
user_heap_end = ((char *)base) + size;
364-
user_heap_head = NULL;
343+
user_heap_head = (empty_block*)ALIGN_TO(base);
344+
user_heap_head->next = ▮
345+
user_heap_head->size = ALIGN_TO(size - sizeof(empty_block));
365346
}
366347

367348
__attribute__((section(".ramtext"))) void *kern_malloc(size_t size) { return multi_malloc(size, HEAP_KERNEL); }
@@ -370,7 +351,7 @@ __attribute__((section(".ramtext"))) void *kern_realloc(void *ptr, size_t size)
370351
return multi_realloc(ptr, size, HEAP_KERNEL);
371352
}
372353
__attribute__((section(".ramtext"))) void kern_initheap(void *base, size_t size) {
373-
kern_heap_start = base;
374-
kern_heap_end = ((char *)base) + size;
375-
kern_heap_head = NULL;
354+
kern_heap_head = (empty_block*)ALIGN_TO(base);
355+
kern_heap_head->next = ▮
356+
kern_heap_head->size = ALIGN_TO(size - sizeof(empty_block));
376357
}

0 commit comments

Comments
 (0)