libc: add a, bad, malloc

This commit is contained in:
Mathieu Maret 2023-03-21 22:41:17 +01:00 committed by Mathieu Maret
parent 1f844d6a35
commit 4c523bf956
2 changed files with 27 additions and 3 deletions

View File

@ -536,7 +536,7 @@ char readc()
return syscall0(SYSCALL_ID_READ);
}
char readcBlock()
char getchar()
{
char c = 0;
do {
@ -549,7 +549,7 @@ int readline(char *buf, int size)
{
int i = 0;
for (; i < size - 1; i++) {
char key = readcBlock();
char key = getchar();
if (key == '\n')
break;
@ -570,3 +570,24 @@ void *brk(void *addr)
{
return (void *)syscall1(SYSCALL_ID_BRK, (unsigned int)addr);
}
void *malloc(size_t size)
{
void *heapTop = 0;
static void *free = 0;
if (heapTop == 0) {
heapTop = brk(0);
free = heapTop;
} else {
heapTop = brk(0);
}
if (free + size > heapTop) {
if (brk(heapTop + size))
return NULL;
}
free += size;
return (free - size);
}

View File

@ -10,6 +10,8 @@
(((c) == ' ') || ((c) == '\t') || ((c) == '\f') || ((c) == '\n') || ((c) == '\r') || \
((c) == '\v'))
#define isprint(c) ((' ' <= (c)) && ((c) <= '~'))
#define EOF (-1)
int memcmp(const void *s1, const void *s2, size_t n);
void *memcpy(void *dest, const void *src, size_t n);
@ -43,6 +45,7 @@ void _exit(int status);
void yolo();
int testSycall5(uint arg1, uint arg2, uint arg3, uint arg4, uint arg5);
char readc();
char readcBlock();
char getchar();
int readline(char *buf, int size);
void *brk(void *addr);
void *malloc(size_t size);