mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
110 lines
2.2 KiB
C++
110 lines
2.2 KiB
C++
#include <stdlib.h>
|
|
#include <mman.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include <alloca.h>
|
|
#include <Kernel/Syscall.h>
|
|
#include <AK/Assertions.h>
|
|
|
|
extern "C" {
|
|
|
|
// FIXME: This is a temporary malloc() implementation. It never frees anything,
|
|
// and you can't allocate more than 128 kB total.
|
|
static const size_t mallocBudget = 131072;
|
|
|
|
static byte* nextptr = nullptr;
|
|
static byte* endptr = nullptr;
|
|
|
|
void __malloc_init()
|
|
{
|
|
nextptr = (byte*)mmap(nullptr, mallocBudget);
|
|
endptr = nextptr + mallocBudget;
|
|
int rc = set_mmap_name(nextptr, mallocBudget, "malloc");
|
|
if (rc < 0)
|
|
perror("set_mmap_name failed");
|
|
}
|
|
|
|
void* malloc(size_t size)
|
|
{
|
|
if ((nextptr + size) > endptr) {
|
|
volatile char* crashme = (char*)0xc007d00d;
|
|
*crashme = 0;
|
|
}
|
|
byte* ret = nextptr;
|
|
nextptr += size;
|
|
nextptr += 16;
|
|
nextptr = (byte*)((dword)nextptr & 0xfffffff0);
|
|
return ret;
|
|
}
|
|
|
|
void free(void* ptr)
|
|
{
|
|
if (!ptr)
|
|
return;
|
|
#if 0
|
|
munmap(ptr, 4096);
|
|
#endif
|
|
}
|
|
|
|
void* calloc(size_t nmemb, size_t)
|
|
{
|
|
ASSERT_NOT_REACHED();
|
|
return nullptr;
|
|
}
|
|
|
|
void* realloc(void *ptr, size_t)
|
|
{
|
|
ASSERT_NOT_REACHED();
|
|
return nullptr;
|
|
}
|
|
|
|
void exit(int status)
|
|
{
|
|
Syscall::invoke(Syscall::SC_exit, (dword)status);
|
|
}
|
|
|
|
void abort()
|
|
{
|
|
// FIXME: Implement proper abort().
|
|
exit(253);
|
|
}
|
|
|
|
char* getenv(const char* name)
|
|
{
|
|
for (size_t i = 0; environ[i]; ++i) {
|
|
const char* decl = environ[i];
|
|
char* eq = strchr(decl, '=');
|
|
if (!eq)
|
|
continue;
|
|
size_t varLength = eq - decl;
|
|
char* var = (char*)alloca(varLength + 1);
|
|
memcpy(var, decl, varLength);
|
|
var[varLength] = '\0';
|
|
if (!strcmp(var, name)) {
|
|
char* value = eq + 1;
|
|
return value;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
int atoi(const char* str)
|
|
{
|
|
ssize_t len = strlen(str);
|
|
int value = 0;
|
|
bool isNegative = false;
|
|
for (size_t i = 0; i < len; ++i) {
|
|
if (i == 0 && str[0] == '-') {
|
|
isNegative = true;
|
|
continue;
|
|
}
|
|
if (str[i] < '0' || str[i] > '9')
|
|
return 0;
|
|
value = value * 10;
|
|
value += str[i] - '0';
|
|
}
|
|
return isNegative ? -value : value;
|
|
}
|
|
|
|
}
|