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); return syscall0(SYSCALL_ID_READ);
} }
char readcBlock() char getchar()
{ {
char c = 0; char c = 0;
do { do {
@ -549,7 +549,7 @@ int readline(char *buf, int size)
{ {
int i = 0; int i = 0;
for (; i < size - 1; i++) { for (; i < size - 1; i++) {
char key = readcBlock(); char key = getchar();
if (key == '\n') if (key == '\n')
break; break;
@ -570,3 +570,24 @@ void *brk(void *addr)
{ {
return (void *)syscall1(SYSCALL_ID_BRK, (unsigned int)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) == ' ') || ((c) == '\t') || ((c) == '\f') || ((c) == '\n') || ((c) == '\r') || \
((c) == '\v')) ((c) == '\v'))
#define isprint(c) ((' ' <= (c)) && ((c) <= '~')) #define isprint(c) ((' ' <= (c)) && ((c) <= '~'))
#define EOF (-1)
int memcmp(const void *s1, const void *s2, size_t n); int memcmp(const void *s1, const void *s2, size_t n);
void *memcpy(void *dest, const void *src, size_t n); void *memcpy(void *dest, const void *src, size_t n);
@ -43,6 +45,7 @@ void _exit(int status);
void yolo(); void yolo();
int testSycall5(uint arg1, uint arg2, uint arg3, uint arg4, uint arg5); int testSycall5(uint arg1, uint arg2, uint arg3, uint arg4, uint arg5);
char readc(); char readc();
char readcBlock(); char getchar();
int readline(char *buf, int size); int readline(char *buf, int size);
void *brk(void *addr); void *brk(void *addr);
void *malloc(size_t size);