universalisos/guests/wasm3-app/libc_stub.c

139 lines
3.3 KiB
C

#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
void* memcpy(void* dest, const void* src, size_t n);
/* Minimal Bump Allocator for Wasm3 */
#define HEAP_SIZE (1024 * 1024) /* 1 MB */
static uint8_t heap[HEAP_SIZE];
static size_t heap_offset = 0;
struct alloc_header {
size_t size;
};
void* malloc(size_t size) {
// align to 8 bytes
size = (size + 7) & ~7;
size += sizeof(struct alloc_header);
if (heap_offset + size > HEAP_SIZE) {
return NULL;
}
struct alloc_header* hdr = (struct alloc_header*)&heap[heap_offset];
hdr->size = size;
heap_offset += size;
return (void*)(hdr + 1);
}
void free(void* ptr) {
/* No-op bump allocator free */
}
void* realloc(void* ptr, size_t size) {
if (!ptr) return malloc(size);
if (size == 0) { free(ptr); return NULL; }
struct alloc_header* hdr = (struct alloc_header*)ptr - 1;
size_t old_data_size = hdr->size - sizeof(struct alloc_header);
// If the new size fits in the old allocation, just return it
if (size <= old_data_size) {
return ptr;
}
void* new_ptr = malloc(size);
if (new_ptr) {
memcpy(new_ptr, ptr, old_data_size);
}
return new_ptr;
}
/* Minimal String / Mem functions */
void* memcpy(void* dest, const void* src, size_t n) {
uint8_t* d = (uint8_t*)dest;
const uint8_t* s = (const uint8_t*)src;
while (n--) *d++ = *s++;
return dest;
}
void* memmove(void* dest, const void* src, size_t n) {
uint8_t* d = (uint8_t*)dest;
const uint8_t* s = (const uint8_t*)src;
if (d < s) {
while (n--) *d++ = *s++;
} else {
d += n;
s += n;
while (n--) *(--d) = *(--s);
}
return dest;
}
void* memset(void* s, int c, size_t n) {
uint8_t* p = (uint8_t*)s;
while (n--) *p++ = (uint8_t)c;
return s;
}
size_t strlen(const char* s) {
size_t len = 0;
while (*s++) len++;
return len;
}
int strcmp(const char* s1, const char* s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}
int strncmp(const char* s1, const char* s2, size_t n) {
if (n == 0) return 0;
do {
if (*s1 != *s2++)
return *(const unsigned char*)s1 - *(const unsigned char*)(s2 - 1);
if (*s1++ == 0)
break;
} while (--n != 0);
return 0;
}
/* Other Wasm3 requirements */
double strtod(const char* str, char** endptr) { return 0.0; } // Stub
long strtol(const char* str, char** endptr, int base) { return 0; } // Stub
unsigned long strtoul(const char* str, char** endptr, int base) { return 0; } // Stub
unsigned long long strtoull(const char* str, char** endptr, int base) { return 0; } // Stub
void abort(void) {
while(1);
}
void *calloc(size_t nmemb, size_t size) {
size_t total = nmemb * size;
void *ptr = malloc(total);
if (ptr) {
memset(ptr, 0, total);
}
return ptr;
}
int vsnprintf(char *str, size_t size, const char *format, va_list ap) {
if (size > 0) {
str[0] = '\0';
}
return 0; // Stub
}
int memcmp(const void *s1, const char *s2, size_t n) {
const unsigned char *p1 = s1, *p2 = (const unsigned char *)s2;
while(n--) {
if( *p1 != *p2 ) {
return *p1 - *p2;
} else {
p1++;
p2++;
}
}
return 0;
}