libc: refactor a couple of string routines

This is just a minor overhaul of several utility
functions, in part because it kept bothering me
and in part because i was bored.
This commit is contained in:
anna 2021-10-12 23:24:17 +02:00
parent afbb3743d5
commit 904584ccc0
Signed by: fef
GPG key ID: EC22E476DC2D3D84
15 changed files with 195 additions and 69 deletions

View file

@ -2,6 +2,8 @@
#include <arch/page.h>
#include <gay/arith.h>
#include <gay/cdefs.h>
#include <gay/clist.h>
#include <gay/config.h>
#include <gay/errno.h>
@ -205,8 +207,29 @@ void kfree(void *ptr)
blk_try_merge(blk);
}
/*
* These wrappers are used for linking libc against the kernel itself.
* This is a "temporary" hack because i haven't figured out the whole C flags
* thingy for properly producing two versions of libc (one static one for the
* kernel and a shared one for user space).
*/
__weak void *malloc(usize size)
{
return kmalloc(size, MM_KERNEL);
}
__weak void free(void *ptr)
{
kfree(ptr);
}
static inline struct memblk *blk_create(usize num_pages)
{
usize blksize;
if (mul_overflow(&blksize, num_pages, PAGE_SIZE))
return NULL;
/*
* heap_end points to the first address that is not part of the heap
* anymore, so that's where the new block starts when we add pages
@ -215,7 +238,7 @@ static inline struct memblk *blk_create(usize num_pages)
if (grow_heap(num_pages) != num_pages)
return NULL; /* OOM :( */
blk_set_size(blk, num_pages * PAGE_SIZE - OVERHEAD);
blk_set_size(blk, blksize - OVERHEAD);
blk_clear_alloc(blk);
blk_set_border_end(blk);
@ -302,7 +325,8 @@ static struct memblk *blk_slice(struct memblk *blk, usize slice_size)
* 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) {
bool carry = sub_underflow(&rest_size, blk_get_size(blk), slice_size + OVERHEAD);
if (rest_size < MIN_SIZE || carry) {
blk_set_alloc(blk);
return blk;
}