diff options
author | aqua <aqua@iserlohn-fortress.net> | 2023-02-18 10:12:24 +0200 |
---|---|---|
committer | aqua <aqua@iserlohn-fortress.net> | 2023-02-18 10:12:24 +0200 |
commit | 41ee6b43c89ce67808a684ba67f69e964b0636fa (patch) | |
tree | fadee7301456711d567df030793c568a745bb522 /lib/stdlib | |
parent | Generate dependency files for source code (diff) | |
download | kernel-41ee6b43c89ce67808a684ba67f69e964b0636fa.tar.xz |
Move C stdlib to lib/libk
Diffstat (limited to 'lib/stdlib')
-rw-r--r-- | lib/stdlib/linked_list_allocator.c | 77 | ||||
-rw-r--r-- | lib/stdlib/memcpy.c | 13 | ||||
-rw-r--r-- | lib/stdlib/memset.c | 7 |
3 files changed, 0 insertions, 97 deletions
diff --git a/lib/stdlib/linked_list_allocator.c b/lib/stdlib/linked_list_allocator.c deleted file mode 100644 index 66c63d1..0000000 --- a/lib/stdlib/linked_list_allocator.c +++ /dev/null @@ -1,77 +0,0 @@ -#include <stdint.h> -#include <stdlib.h> - -struct Chunk { - struct Chunk *prev; - struct Chunk *next; - int used; - size_t size; -}; - -void -Chunk_ctor(struct Chunk *self, size_t size) -{ - self->prev = NULL; - self->next = NULL; - self->used = 0; - self->size = size - sizeof(struct Chunk); -} - -static struct Chunk *begin = NULL; - -void -alloc_init(void *mem, size_t size) -{ - if (size < sizeof(struct Chunk)) return; - - begin = (struct Chunk *)mem; - Chunk_ctor(begin, size); -} - -void * -malloc(size_t size) -{ - if (begin == NULL) return NULL; - - // find free chunk that is at least (size + sizeof(struct Chunk)) - for (struct Chunk *iter = begin; iter != NULL; iter = iter->next) { - if (iter->used != 0 || iter->size < size) continue; - - // if there's at least sizeof(struct Chunk) bytes left over, create a new Chunk - if (iter->size >= (size + 2 * sizeof(struct Chunk))) { - struct Chunk *next = (struct Chunk *)((uintptr_t)iter + sizeof(struct Chunk) + size); - Chunk_ctor(next, iter->size - size - sizeof(struct Chunk)); - next->prev = iter; - - iter->size = size; - iter->next = next; - } - - iter->used = 1; - return (void *)((uintptr_t)iter + sizeof(struct Chunk)); - } - - return NULL; -} - -void -free(void *ptr) -{ - if (ptr == NULL) return; - struct Chunk *chunk = (struct Chunk *)((uintptr_t)ptr - sizeof(struct Chunk)); - chunk->used = 0; - - // merge next chunk - if (chunk->next != NULL && chunk->next->used == 0) { - chunk->size += chunk->next->size + sizeof(struct Chunk); - chunk->next = chunk->next->next; - if (chunk->next != NULL) chunk->next->prev = chunk; - } - - // merge into prev chunk - if (chunk->prev != NULL && chunk->prev->used == 0) { - chunk->prev->size += chunk->size + sizeof(struct Chunk); - chunk->prev->next = chunk->next; - if (chunk->next != NULL) chunk->next->prev = chunk->prev; - } -} diff --git a/lib/stdlib/memcpy.c b/lib/stdlib/memcpy.c deleted file mode 100644 index 90470d5..0000000 --- a/lib/stdlib/memcpy.c +++ /dev/null @@ -1,13 +0,0 @@ -void * -memcpy(void *restrict dest, const void *restrict src, long unsigned n) -{ - char *pDest = (char *)dest; - const char *pSrc = (const char *)src; - - while (n) { - *(pDest++) = *(pSrc++); - --n; - } - - return dest; -} diff --git a/lib/stdlib/memset.c b/lib/stdlib/memset.c deleted file mode 100644 index a16bd05..0000000 --- a/lib/stdlib/memset.c +++ /dev/null @@ -1,7 +0,0 @@ -void * -memset(void *s, int c, long unsigned n) -{ - char *pDest = (char *)s; - for (unsigned i = 0; i < n; ++i) pDest[i] = (char)c; - return s; -} |