@@ -64,39 +64,21 @@ typedef struct allocated_block_ {
64
64
_Static_assert (sizeof (empty_block ) == (2 * sizeof (void * )), "empty_block is of the wrong size" );
65
65
_Static_assert (sizeof (allocated_block ) == (2 * sizeof (void * )), "allocated_block is of the wrong size" );
66
66
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
-
72
67
static empty_block * user_heap_head = NULL ;
73
68
static empty_block * kern_heap_head = NULL ;
74
69
75
- static empty_block marker ;
70
+ static empty_block marker = { . next = NULL , . size = 0 } ;
76
71
77
72
enum heap { HEAP_USER , HEAP_KERNEL };
78
73
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 ) {
80
75
empty_block * curr = heap == HEAP_USER ? user_heap_head : kern_heap_head ;
81
76
empty_block * prev = NULL ;
82
77
empty_block * best_fit = NULL ;
83
78
empty_block * best_fit_prev = NULL ;
84
79
85
80
size_t size = ALIGN_TO (size_ + sizeof (allocated_block ));
86
81
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 = & marker ;
97
- curr -> size = ALIGN_TO (((size_t )(heap == HEAP_USER ? user_heap_end : kern_heap_end )) - sizeof (empty_block ));
98
- }
99
-
100
82
size_t curr_size = 0 ;
101
83
while ((curr_size != size ) && (curr != & marker )) {
102
84
curr_size = curr -> size ;
@@ -147,7 +129,7 @@ static __attribute__((section(".ramtext"))) void *multi_malloc(size_t size_, enu
147
129
return ptr ;
148
130
}
149
131
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 ) {
151
133
if (ptr_ == NULL ) {
152
134
return ;
153
135
}
@@ -218,7 +200,7 @@ static __attribute__((section(".ramtext"))) void multi_free(void *ptr_, enum hea
218
200
}
219
201
}
220
202
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 ) {
222
204
if (ptr_ == NULL ) {
223
205
return multi_malloc (size_ , heap );
224
206
}
@@ -341,7 +323,6 @@ static __attribute__((section(".ramtext"))) void *multi_realloc(void *ptr_, size
341
323
if (new_ptr == NULL ) {
342
324
return NULL ;
343
325
}
344
- //__builtin_memcpy(new_ptr, ptr_, old_size - sizeof(empty_block));
345
326
uint32_t * src = (uint32_t * )ptr_ ;
346
327
uint32_t * dst = (uint32_t * )new_ptr ;
347
328
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)
359
340
return multi_realloc (ptr , size , HEAP_USER );
360
341
}
361
342
__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 = & marker ;
345
+ user_heap_head -> size = ALIGN_TO ( size - sizeof ( empty_block )) ;
365
346
}
366
347
367
348
__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)
370
351
return multi_realloc (ptr , size , HEAP_KERNEL );
371
352
}
372
353
__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 = & marker ;
356
+ kern_heap_head -> size = ALIGN_TO ( size - sizeof ( empty_block )) ;
376
357
}
0 commit comments