Implemented stdlib.h

This commit is contained in:
2024-10-25 12:01:54 +02:00
parent a797d4366a
commit 18d1b7de6a
8 changed files with 156 additions and 24 deletions
+23 -2
View File
@@ -3,7 +3,28 @@
#include <stddef.h>
void* malloc(const size_t size);
void free(const void* ptr);
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
#define RAND_MAX 0x7FFFFFF
/* Memory allocation */
void* malloc(size_t size);
void* calloc(size_t count, size_t size);
void* realloc(void* ptr, size_t size);
void free(void* ptr);
/* Process controll */
void _Exit(int status);
void exit(int status);
// TODO: move atoi() here
/* Random number generator */
int rand(void);
void srand(unsigned int seed);
/* Sort & search */
void qsort(void* base, size_t nmemb, size_t size, int (*compar)(const void*, const void*));
void* bsearch(const void* key, const void* base, size_t nmemb, size_t size, int (*compar)(const void*, const void*));
#endif
-10
View File
@@ -17,16 +17,6 @@ typedef enum {
SYS_SLEEP,
} SYSCALL_NUMBERS;
typedef enum {
O_CREAT = 0x01, // Create if does not exist
O_RDONLY = 0x02, // Read only
O_WRONLY = 0x04, // Write only
O_RDWR = 0x08, // Read and write
O_APPEND = 0x10, // Always writes at the end (appends)
O_TRUNC = 0x20, // Truncate file size and position to 0 on Open
O_BIN = 0x40, // Read bytes, not text
} OFLAGS;
int syscall(int nr, int arg0, int arg1, int arg2);
/* Syscall wrappers */
+41
View File
@@ -0,0 +1,41 @@
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <syscall.h>
void* malloc(size_t size) {
return (void*)syscall(SYS_MALLOC, size, 0, 0);
}
void* calloc(size_t count, size_t size) {
size_t total = count * size;
void* ptr = malloc(total);
if (!ptr) { return NULL; }
memset(ptr, 0, total);
return ptr;
}
void* realloc(void* ptr, size_t size) {
if (size == 0) {
free(ptr);
return NULL;
}
if (ptr == NULL) { return malloc(size); }
void* newptr = malloc(size);
if (!newptr) { return NULL; }
// TODO: retrieve the old size
memcpy(newptr, ptr, size);
free(ptr);
return newptr;
}
void free(void* ptr) {
syscall(SYS_FREE, (int)ptr, 0, 0);
}
+15
View File
@@ -0,0 +1,15 @@
#include <stdlib.h>
#include <syscall.h>
// Exits without cleaning up
void _Exit(int status) {
syscall(SYS_EXIT, status, 0, 0);
}
void exit(int status) {
// TODO: Close open files
// TODO: Free malloced buffers
_Exit(status);
}
+15
View File
@@ -0,0 +1,15 @@
#include <stdlib.h>
// taken from glibc
#define RAND_A 1103515245
#define RAND_C 12345
static size_t _next = 1;
int rand(void) {
_next = (RAND_A * _next + RAND_C) & RAND_MAX;
return (int)(_next % (size_t)(RAND_MAX + 1));
}
void srand(size_t seed) { _next = seed; }
+25
View File
@@ -0,0 +1,25 @@
#include <stdlib.h>
#include <stddef.h>
void* bsearch(const void* key, const void* base, size_t nitem, size_t size, int (*compar)(const void*, const void*)) {
size_t left = 0;
size_t right = nitem;
while (left < right) {
size_t mid = left + (right - left) / 2;
void* midEl = (char*)base + mid * size;
int cmp = compar(key, midEl);
if (cmp < 0) {
right = mid;
} else if (cmp > 0) {
left = mid + 1;
} else {
return midEl;
}
}
return NULL;
}
+37
View File
@@ -0,0 +1,37 @@
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
void swap(void* a, void* b, size_t size) {
char temp[size];
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
}
void* partition(void* base, size_t nitem, size_t size, int (*compar)(const void*, const void*)) {
char* pivot = (char*)base + (nitem - 1) * size;
char* i = (char*)base;
for (char* j = (char*)base; j < pivot; j += size) {
if (compar(j, pivot) < 0) {
swap(i, j, size);
i += size;
}
}
swap(i, pivot, size);
return i;
}
void qsort(void* base, size_t nitem, size_t size, int (*compar)(const void*, const void*)) {
if (nitem <= 1) { return; }
char* pivot = (char*)partition(base, nitem, size, compar);
size_t lsize = (pivot - (char*)base) / size;
size_t rsize = nitem - lsize - 1;
qsort(base, lsize, size, compar);
qsort(pivot + size, rsize, size, compar);
}
-12
View File
@@ -1,12 +0,0 @@
#include <stdint.h>
#include <syscall.h>
#include <stdlib.h>
void* malloc(const uint32_t size) {
return (void*)syscall(SYS_MALLOC, size, 0, 0);
}
void free(const void* ptr) {
syscall(SYS_FREE, (int)ptr, 0, 0);
}