kmalloc: bugfixes and performance improvements
This commit is contained in:
parent
d4ee4e5953
commit
347bb5cc9c
2 changed files with 62 additions and 11 deletions
|
|
@ -19,7 +19,7 @@
|
|||
* doesn't have paging:
|
||||
* <https://git.bsd.gay/fef/ardix/src/commit/c767d551d3301fc30f9fce30eda8f04e2f9a42ab/kernel/mm.c>
|
||||
* As a matter of fact, this allocator is merely an extension of the one from
|
||||
* Ardix with the only difference being that the heap can grow upwards.
|
||||
* Ardix with the only difference being that the heap can be extended upwards.
|
||||
*/
|
||||
|
||||
/**
|
||||
|
|
@ -46,7 +46,7 @@ struct memblk {
|
|||
/** @brief Used as the return value for `kmalloc()` */
|
||||
u8 data[0];
|
||||
/**
|
||||
* @brief Used to get the copy of the low_size field at the end of
|
||||
* @brief Used to get the copy of the size at the end of
|
||||
* the block, right after the last byte of `data`
|
||||
*/
|
||||
usize high_size[0];
|
||||
|
|
@ -274,7 +274,7 @@ static struct memblk *blk_try_merge(struct memblk *blk)
|
|||
if (blk_get_size(cursor) >= blk_get_size(blk))
|
||||
break;
|
||||
}
|
||||
clist_add_first(&cursor->clink, &blk->clink);
|
||||
clist_add(&cursor->clink, &blk->clink);
|
||||
|
||||
return blk;
|
||||
}
|
||||
|
|
@ -292,13 +292,14 @@ static struct memblk *blk_merge(struct memblk *bottom, struct memblk *top)
|
|||
|
||||
static struct memblk *blk_slice(struct memblk *blk, usize slice_size)
|
||||
{
|
||||
struct memblk *cursor = clist_prev_entry(blk, clink);
|
||||
clist_del(&blk->clink);
|
||||
|
||||
/*
|
||||
* If the remaining low_size is less than the minimum allocation unit, we
|
||||
* If the remaining size is less than the minimum allocation unit, we
|
||||
* hand out the entire block. Additionally, we must add an underflow
|
||||
* check which happens if the slice low_size is less than OVERHEAD smaller
|
||||
* than the full block low_size.
|
||||
* check which happens if the slice size is less than OVERHEAD smaller
|
||||
* than the full block size.
|
||||
*/
|
||||
usize rest_size = blk_get_size(blk) - slice_size - OVERHEAD;
|
||||
if (rest_size < MIN_SIZE || rest_size + OVERHEAD < rest_size) {
|
||||
|
|
@ -316,8 +317,7 @@ static struct memblk *blk_slice(struct memblk *blk, usize slice_size)
|
|||
blk_set_alloc(blk);
|
||||
blk_clear_border_end(blk);
|
||||
|
||||
struct memblk *cursor;
|
||||
clist_foreach_entry(&blocks, cursor, clink) {
|
||||
clist_foreach_entry_rev_continue(&blocks, cursor, clink) {
|
||||
if (blk_get_size(cursor) <= rest_size)
|
||||
break;
|
||||
}
|
||||
|
|
@ -348,6 +348,11 @@ static inline struct memblk *blk_next(struct memblk *blk)
|
|||
|
||||
static inline usize blk_get_size(struct memblk *blk)
|
||||
{
|
||||
# ifdef DEBUG
|
||||
usize index = blk->low_size[0] / sizeof(blk->low_size[0]);
|
||||
if ((blk->low_size[0] & SIZE_MASK) != (blk->high_size[index] & SIZE_MASK))
|
||||
kprintf("Memory corruption in block %p detected!\n", blk);
|
||||
# endif
|
||||
return blk->low_size[0] & SIZE_MASK;
|
||||
}
|
||||
|
||||
|
|
@ -356,7 +361,7 @@ static void blk_set_size(struct memblk *blk, usize size)
|
|||
/* don't affect flags */
|
||||
blk->low_size[0] &= ~SIZE_MASK;
|
||||
# ifdef DEBUG
|
||||
if (size & SIZE_MASK)
|
||||
if (size & ~SIZE_MASK)
|
||||
kprintf("Unaligned size in blk_set_size()\n");
|
||||
# endif
|
||||
blk->low_size[0] |= size & SIZE_MASK;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue