init commit

This commit is contained in:
Faynot
2026-07-07 20:35:07 +03:00
commit 38cc34a6e9
33 changed files with 659 additions and 0 deletions

7
src/internal/errno.c Normal file
View File

@@ -0,0 +1,7 @@
#include <errno.h>
#include "libc.h"
int *__errno_location(void)
{
return &__errno;
}

23
src/internal/libc.c Normal file
View File

@@ -0,0 +1,23 @@
#include "libc.h"
/* Initial memory pool — small bump arena for early development.
* When the PM-actor path is wired this will request memory via
* elyz_kcall with ELYZ_OP_ALLOCATE.
*/
#define EARLY_HEAP_SIZE (64UL * 1024 * 1024)
static unsigned char __early_heap[EARLY_HEAP_SIZE];
struct __libc __libc = {
.heap_base = (unsigned long)__early_heap,
.heap_size = EARLY_HEAP_SIZE,
.heap_used = 0,
.threaded = 0,
.progname = 0,
};
int __errno = 0;
void __libc_init(void)
{
/* Future: read startup_info from loader, wire capabilities */
}

28
src/internal/libc.h Normal file
View File

@@ -0,0 +1,28 @@
#ifndef _ELIBC_INTERNAL_LIBC_H
#define _ELIBC_INTERNAL_LIBC_H
/* Internal libc state.
*
* This structure is the single global anchor for elibc. Fields are added
* as the kernel ABI stabilises (e.g. CNode root, PM router channel pool,
* initial capability tokens).
*/
#include <arch/generic/startup_info.h>
struct __libc {
/* Initial pool — bump-allocated until the PM-actor path is wired */
unsigned long heap_base;
unsigned long heap_size;
unsigned long heap_used;
int threaded;
char *progname;
};
extern struct __libc __libc;
extern int __errno;
void __libc_init(void);
#endif

52
src/internal/malloc.c Normal file
View File

@@ -0,0 +1,52 @@
/* Bump allocator on the initial memory pool.
*
* Temporary — until the PM-actor malloc path is implemented.
* Thread-safe: no — __libc.heap_used is guarded by nothing yet.
*/
#include <stddef.h>
#include "libc.h"
void *malloc(size_t size)
{
if (size == 0) size = 1;
unsigned long heap = __libc.heap_base;
unsigned long used = __libc.heap_used;
if (used + size > __libc.heap_size)
return 0;
void *ptr = (void *)(heap + used);
__libc.heap_used = used + size;
return ptr;
}
void free(void *ptr)
{
(void)ptr;
}
void *calloc(size_t nmemb, size_t size)
{
size_t total = nmemb * size;
void *p = malloc(total);
if (!p) return 0;
for (size_t i = 0; i < total; i++)
((unsigned char *)p)[i] = 0;
return p;
}
void *realloc(void *ptr, size_t size)
{
if (!ptr) return malloc(size);
if (size == 0) { free(ptr); return 0; }
/* Bump allocator cannot shrink/grow in place — always allocate + copy */
void *newp = malloc(size);
if (!newp) return 0;
unsigned long old = __libc.heap_used - size;
if (old > __libc.heap_used) old = __libc.heap_used;
for (size_t i = 0; i < old; i++)
((unsigned char *)newp)[i] = ((unsigned char *)ptr)[i];
return newp;
}

11
src/string/memcmp.c Normal file
View File

@@ -0,0 +1,11 @@
#include <string.h>
int memcmp(const void *vl, const void *vr, size_t n)
{
const unsigned char *l = vl, *r = vr;
for (size_t i = 0; i < n; i++) {
if (l[i] != r[i])
return l[i] - r[i];
}
return 0;
}

10
src/string/memcpy.c Normal file
View File

@@ -0,0 +1,10 @@
#include <string.h>
void *memcpy(void *restrict dest, const void *restrict src, size_t n)
{
unsigned char *d = dest;
const unsigned char *s = src;
for (size_t i = 0; i < n; i++)
d[i] = s[i];
return dest;
}

9
src/string/memset.c Normal file
View File

@@ -0,0 +1,9 @@
#include <string.h>
void *memset(void *dest, int c, size_t n)
{
unsigned char *d = dest;
for (size_t i = 0; i < n; i++)
d[i] = (unsigned char)c;
return dest;
}

7
src/string/strcmp.c Normal file
View File

@@ -0,0 +1,7 @@
#include <string.h>
int strcmp(const char *l, const char *r)
{
while (*l && *l == *r) l++, r++;
return *(unsigned char *)l - *(unsigned char *)r;
}

8
src/string/strlen.c Normal file
View File

@@ -0,0 +1,8 @@
#include <string.h>
size_t strlen(const char *s)
{
const char *p = s;
while (*p) p++;
return p - s;
}