Move string functions/utils from HAL to std/string

This commit is contained in:
2025-11-11 19:54:09 +01:00
parent f5dae4984d
commit 344952fb5f
27 changed files with 200 additions and 228 deletions

View File

@ -2,6 +2,7 @@
#include <stddef.h> #include <stddef.h>
#include "hal/hal.h" #include "hal/hal.h"
#include "fastlz.h" #include "fastlz.h"
#include "std/string.h"
#define SIXPACK_OK 0 #define SIXPACK_OK 0
#define SIXPACK_ERR_MAGIC -1 #define SIXPACK_ERR_MAGIC -1
@ -93,7 +94,7 @@ int sixpack_decompress_mem(const uint8_t *input, size_t in_size,
/* Stored (uncompressed) */ /* Stored (uncompressed) */
if (written + size > out_cap) if (written + size > out_cap)
return SIXPACK_ERR_NO_SPACE; return SIXPACK_ERR_NO_SPACE;
hal_memcpy(output + written, chunk_data, size); memcpy(output + written, chunk_data, size);
written += size; written += size;
} else if (opts == 1) { } else if (opts == 1) {
/* FastLZ compressed */ /* FastLZ compressed */

View File

@ -46,7 +46,6 @@ SRCFILES += $(call GRABSRC, \
fs/portlfs \ fs/portlfs \
baseimg \ baseimg \
proc \ proc \
hal \
hal/$(ARCH) \ hal/$(ARCH) \
std \ std \
flanterm/src \ flanterm/src \

View File

@ -9,6 +9,7 @@
#include "dlmalloc/malloc.h" #include "dlmalloc/malloc.h"
#include "FastLZ/fastlz.h" #include "FastLZ/fastlz.h"
#include "FastLZ/6unpack_mem.h" #include "FastLZ/6unpack_mem.h"
#include "std/string.h"
#define BASEIMG_DECOMPRESSED (1024*1024*4) #define BASEIMG_DECOMPRESSED (1024*1024*4)
@ -29,7 +30,7 @@ void baseimg_init(void) {
LOG("baseimg", "looking for base image...\n"); LOG("baseimg", "looking for base image...\n");
for (size_t i = 0; i < BOOT_INFO.modules->module_count; i++) { for (size_t i = 0; i < BOOT_INFO.modules->module_count; i++) {
struct limine_file *module = BOOT_INFO.modules->modules[i]; struct limine_file *module = BOOT_INFO.modules->modules[i];
if (hal_strcmp(util_get_filename(module->path), "base.img.6pk") == 0) { if (strcmp(util_get_filename(module->path), "base.img.6pk") == 0) {
baseimg = module; baseimg = module;
break; break;
} }

View File

@ -4,6 +4,7 @@
#include "dev.h" #include "dev.h"
#include "hshtb.h" #include "hshtb.h"
#include "hal/hal.h" #include "hal/hal.h"
#include "std/string.h"
#include "termdev.h" #include "termdev.h"
#include "ps2kbdev.h" #include "ps2kbdev.h"
#include "serialdev.h" #include "serialdev.h"
@ -12,7 +13,7 @@
DevTable DEVTABLE; DevTable DEVTABLE;
void dev_init(void) { void dev_init(void) {
hal_memset(&DEVTABLE, 0, sizeof(DEVTABLE)); memset(&DEVTABLE, 0, sizeof(DEVTABLE));
spinlock_init(&DEVTABLE.spinlock); spinlock_init(&DEVTABLE.spinlock);
termdev_init(); termdev_init();

View File

@ -9,6 +9,7 @@
#include "hal/hal.h" #include "hal/hal.h"
#include "bootinfo/bootinfo.h" #include "bootinfo/bootinfo.h"
#include "errors.h" #include "errors.h"
#include "std/string.h"
int32_t fbdev_getinfo(struct Dev *dev, uint8_t *buffer, size_t len, uint64_t pid) { int32_t fbdev_getinfo(struct Dev *dev, uint8_t *buffer, size_t len, uint64_t pid) {
(void)dev; (void)pid; (void)dev; (void)pid;
@ -24,7 +25,7 @@ int32_t fbdev_getinfo(struct Dev *dev, uint8_t *buffer, size_t len, uint64_t pid
.fontw = 8, .fontw = 8,
.fonth = 16, .fonth = 16,
}; };
hal_memcpy(buffer, &info, sizeof(info)); memcpy(buffer, &info, sizeof(info));
return E_OK; return E_OK;
} }

View File

@ -10,6 +10,7 @@
#include "hal/hal.h" #include "hal/hal.h"
#include "util/util.h" #include "util/util.h"
#include "dev/dev.h" #include "dev/dev.h"
#include "std/string.h"
typedef struct { typedef struct {
uint8_t driveattrs; uint8_t driveattrs;
@ -62,7 +63,7 @@ bool diskpart_mbr_probe(StoreDev *sd) {
LOG("diskpart", "probing for MBR\n"); LOG("diskpart", "probing for MBR\n");
MBR mbr; MBR mbr;
hal_memset(&mbr, 0, sizeof(mbr)); memset(&mbr, 0, sizeof(mbr));
sd->read(sd, (uint8_t *const)&mbr, 0, 0, sizeof(mbr)); sd->read(sd, (uint8_t *const)&mbr, 0, 0, sizeof(mbr));
if (!(mbr.validsign[0] == 0x55 && mbr.validsign[1] == 0xAA)) { if (!(mbr.validsign[0] == 0x55 && mbr.validsign[1] == 0xAA)) {

View File

@ -9,6 +9,7 @@
#include "pmm/pmm.h" #include "pmm/pmm.h"
#include "malloc.h" #include "malloc.h"
#include "bootinfo/bootinfo.h" #include "bootinfo/bootinfo.h"
#include "std/string.h"
#define USE_DL_PREFIX 1 #define USE_DL_PREFIX 1
#define LACKS_SYS_TYPES_H 1 #define LACKS_SYS_TYPES_H 1
@ -67,7 +68,7 @@ void *sbrk(long inc) {
uint64_t blocks = _DIV_ROUNDUP(inc, BITMAP_BLOCK_SIZE); uint64_t blocks = _DIV_ROUNDUP(inc, BITMAP_BLOCK_SIZE);
uint8_t *virt = VIRT(pmm_alloc(blocks)); uint8_t *virt = VIRT(pmm_alloc(blocks));
hal_memset(virt, 0, blocks * BITMAP_BLOCK_SIZE); memset(virt, 0, blocks * BITMAP_BLOCK_SIZE);
_last = (void *)(virt + inc); _last = (void *)(virt + inc);
return virt; return virt;
} }

View File

@ -6,6 +6,7 @@
#include "kprintf.h" #include "kprintf.h"
#include "dlmalloc/malloc.h" #include "dlmalloc/malloc.h"
#include "hal/hal.h" #include "hal/hal.h"
#include "std/string.h"
int32_t littlefs_cleanup(struct VfsMountPoint *vmp) { int32_t littlefs_cleanup(struct VfsMountPoint *vmp) {
dlfree((void *)vmp->fs.littlefs.instance.cfg); dlfree((void *)vmp->fs.littlefs.instance.cfg);
@ -132,7 +133,7 @@ struct VfsObj *littlefs_open(struct VfsMountPoint *vmp, const char *path, uint32
if (vobj == NULL) { if (vobj == NULL) {
return NULL; return NULL;
} }
hal_memset(vobj, 0, sizeof(*vobj)); memset(vobj, 0, sizeof(*vobj));
spinlock_init(&vobj->spinlock); spinlock_init(&vobj->spinlock);
int lfs_flags = 0; int lfs_flags = 0;
@ -214,7 +215,7 @@ int32_t littlefs_fetchdirent(struct VfsMountPoint *vmp, const char *path, FsDire
direntbuf->stat.type = FSSTAT_DIR; direntbuf->stat.type = FSSTAT_DIR;
} }
hal_memcpy(direntbuf->name, entinfo.name, sizeof(direntbuf->name)); memcpy(direntbuf->name, entinfo.name, sizeof(direntbuf->name));
break; break;
} }
i++; i++;

View File

@ -9,17 +9,6 @@ __attribute__((noreturn)) void hal_hang(void);
void hal_init(void); void hal_init(void);
void hal_intr_disable(void); void hal_intr_disable(void);
void hal_intr_enable(void); void hal_intr_enable(void);
void *hal_memset(void *p, int c, size_t n);
void *hal_memcpy(void *dst, const void *src, size_t n);
size_t hal_strlen(char *s);
int hal_memcmp(const void *s1, const void *s2, int len);
int hal_strcmp(const char *a, const char *b);
size_t hal_strcspn(const char *s, const char *reject);
size_t hal_strspn(const char *s, const char *accept);
char *hal_strcpy(char *dest, const char *src);
char *hal_strchr(const char *s, int c);
char *hal_strstr(const char *str, const char *substring);
char *hal_string_combine(char *dest, const char *src);
void hal_wait(uint32_t ms); void hal_wait(uint32_t ms);
int32_t hal_randnum(void); int32_t hal_randnum(void);

View File

@ -1,152 +0,0 @@
#include <stdint.h>
#include <stddef.h>
#include "hal.h"
void *hal_memset(void *p, int c, size_t n) {
char *cp = p;
for (size_t i = 0; i < n; i++) cp[i] = c;
return p;
}
void *hal_memcpy(void *dst, const void *src, size_t n) {
char *a = dst;
const char *b = src;
for (size_t i = 0; i < n; i++) a[i] = b[i];
return dst;
}
size_t hal_strlen(char *s) {
char *s2;
for (s2 = s; *s2; ++s2);
return (s2 - s);
}
// https://aticleworld.com/memcmp-in-c/
int hal_memcmp(const void *s1, const void *s2, int len)
{
unsigned char *p = (unsigned char *)s1;
unsigned char *q = (unsigned char *)s2;
int charCompareStatus = 0;
//If both pointer pointing same memory block
if (s1 == s2)
{
return charCompareStatus;
}
while (len > 0)
{
if (*p != *q)
{ //compare the mismatching character
charCompareStatus = (*p >*q)?1:-1;
break;
}
len--;
p++;
q++;
}
return charCompareStatus;
}
int hal_strcmp(const char *a, const char *b) {
while (*a && (*a == *b)) {
a++, b++;
}
return (unsigned char)*a - (unsigned char)*b;
}
size_t hal_strcspn(const char *s, const char *reject) {
size_t count = 0;
for (; *s != '\0'; ++s) {
const char *r = reject;
while (*r != '\0') {
if (*s == *r) {
return count;
}
r++;
}
count++;
}
return count;
}
size_t hal_strspn(const char *s, const char *accept) {
size_t count = 0;
for (; *s != '\0'; ++s) {
const char *a = accept;
int matched = 0;
while (*a != '\0') {
if (*s == *a) {
matched = 1;
break;
}
a++;
}
if (!matched) {
return count;
}
count++;
}
return count;
}
char *hal_strcpy(char *dest, const char *src) {
char *d = dest;
while ((*d++ = *src++) != '\0') {
;
}
return dest;
}
char *hal_strchr(const char *s, int c) {
char ch = (char)c;
while (*s != '\0') {
if (*s == ch) {
return (char *)s;
}
s++;
}
if (ch == '\0') {
return (char *)s;
}
return NULL;
}
char *hal_strstr(const char *str, const char *substring)
{
const char *a;
const char *b;
b = substring;
if (*b == 0) {
return (char *) str;
}
for ( ; *str != 0; str += 1) {
if (*str != *b) {
continue;
}
a = str;
while (1) {
if (*b == 0) {
return (char *) str;
}
if (*a++ != *b++) {
break;
}
}
b = substring;
}
return NULL;
}
char *hal_string_combine(char *dest, const char *src) {
size_t i, j;
for(i = 0; dest[i] != '\0'; i++);
for(j = 0; src[j] != '\0'; j++) {
dest[i+j] = src[j];
}
dest[i+j] = '\0';
return dest;
}

View File

@ -3,6 +3,7 @@
#include "compiler/attr.h" #include "compiler/attr.h"
#include "hal/hal.h" #include "hal/hal.h"
#include "gdt.h" #include "gdt.h"
#include "std/string.h"
#define GDT_PRESENT 0x80 #define GDT_PRESENT 0x80
#define GDT_TSS 0x89 #define GDT_TSS 0x89
@ -43,7 +44,7 @@ void gdt_setentry(GdtEntry *ent, uint32_t base, uint32_t limit, uint8_t acc, uin
} }
void gdt_init(void) { void gdt_init(void) {
hal_memset(&tss, 0, sizeof(tss)); memset(&tss, 0, sizeof(tss));
tss.iopb_off = sizeof(tss); tss.iopb_off = sizeof(tss);
tss.rsp0 = (uint64_t)(kernelstack + sizeof(kernelstack)); tss.rsp0 = (uint64_t)(kernelstack + sizeof(kernelstack));

View File

@ -8,6 +8,7 @@
#include "proc/proc.h" #include "proc/proc.h"
#include "kprintf.h" #include "kprintf.h"
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "std/string.h"
uint64_t KERNEL_CR3 = 0; uint64_t KERNEL_CR3 = 0;
SpinLock spinlock; SpinLock spinlock;
@ -41,7 +42,7 @@ uint64_t *hal_vmm_nexttable(uint64_t *table, uint64_t ent, bool alloc) {
uint8_t *newphys = pmm_alloc(1); uint8_t *newphys = pmm_alloc(1);
phys = (uint64_t)newphys; phys = (uint64_t)newphys;
hal_memset(VIRT(phys), 0, HAL_PAGE_SIZE); memset(VIRT(phys), 0, HAL_PAGE_SIZE);
table[ent] = phys | HAL_PG_USER | HAL_PG_RW | HAL_PG_PRESENT; table[ent] = phys | HAL_PG_USER | HAL_PG_RW | HAL_PG_PRESENT;
} }
return (uint64_t *)((uint8_t *)VIRT(phys)); return (uint64_t *)((uint8_t *)VIRT(phys));
@ -112,7 +113,7 @@ void hal_vmm_map_kern(uint64_t targetcr3) {
uint64_t hal_vmm_userproc_pml4_phys(void) { uint64_t hal_vmm_userproc_pml4_phys(void) {
uint8_t *cr3phys = pmm_alloc(1); uint8_t *cr3phys = pmm_alloc(1);
uint64_t phys = (uint64_t)cr3phys; uint64_t phys = (uint64_t)cr3phys;
hal_memset(VIRT(phys), 0, HAL_PAGE_SIZE); memset(VIRT(phys), 0, HAL_PAGE_SIZE);
uint64_t *kcr3 = (uint64_t *)VIRT(KERNEL_CR3); uint64_t *kcr3 = (uint64_t *)VIRT(KERNEL_CR3);
uint64_t *pml4 = (uint64_t *)VIRT(phys); uint64_t *pml4 = (uint64_t *)VIRT(phys);

View File

@ -6,6 +6,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "hal/hal.h" #include "hal/hal.h"
#include "util/util.h" #include "util/util.h"
#include "std/string.h"
#define HSHTB_FNV32_OFF 0x811c9dc5u #define HSHTB_FNV32_OFF 0x811c9dc5u
#define HSHTB_FNV32_PRIME 0x01000193u #define HSHTB_FNV32_PRIME 0x01000193u
@ -32,7 +33,7 @@ enum {
#define HSHTB_ALLOC(tb, keyfield, k, out) \ #define HSHTB_ALLOC(tb, keyfield, k, out) \
do { \ do { \
size_t __len = __HSHTB_ARRAY_LEN(tb); \ size_t __len = __HSHTB_ARRAY_LEN(tb); \
uint32_t __h = hshtb_fnv32((k), hal_strlen((k))); \ uint32_t __h = hshtb_fnv32((k), strlen((k))); \
size_t __idx = __h % __len; \ size_t __idx = __h % __len; \
size_t __start = __idx; \ size_t __start = __idx; \
typeof(&(tb)[0]) __tomb = NULL; \ typeof(&(tb)[0]) __tomb = NULL; \
@ -40,13 +41,13 @@ enum {
do { \ do { \
if ((tb)[__idx]._hshtbstate == HSHTB_EMPTY) { \ if ((tb)[__idx]._hshtbstate == HSHTB_EMPTY) { \
typeof(&(tb)[0]) __slot = __tomb ? __tomb : &(tb)[__idx]; \ typeof(&(tb)[0]) __slot = __tomb ? __tomb : &(tb)[__idx]; \
hal_memset(__slot, 0, sizeof(*__slot)); \ memset(__slot, 0, sizeof(*__slot)); \
__slot->_hshtbstate = HSHTB_TAKEN; \ __slot->_hshtbstate = HSHTB_TAKEN; \
hal_memcpy(__slot->keyfield, (k), MIN(sizeof(__slot->keyfield) - 1, hal_strlen((k)))); \ memcpy(__slot->keyfield, (k), MIN(sizeof(__slot->keyfield) - 1, strlen((k)))); \
(out) = __slot; \ (out) = __slot; \
break; \ break; \
} \ } \
if ((tb)[__idx]._hshtbstate == HSHTB_TAKEN && hal_strcmp((tb)[__idx].keyfield, (k)) == 0) { \ if ((tb)[__idx]._hshtbstate == HSHTB_TAKEN && strcmp((tb)[__idx].keyfield, (k)) == 0) { \
(out) = &(tb)[__idx]; \ (out) = &(tb)[__idx]; \
break; \ break; \
} \ } \
@ -60,7 +61,7 @@ enum {
#define HSHTB_GET(tb, keyfield, k, out) \ #define HSHTB_GET(tb, keyfield, k, out) \
do { \ do { \
size_t __len = __HSHTB_ARRAY_LEN(tb); \ size_t __len = __HSHTB_ARRAY_LEN(tb); \
uint32_t __h = hshtb_fnv32((k), hal_strlen((k))); \ uint32_t __h = hshtb_fnv32((k), strlen((k))); \
size_t __idx = __h % __len; \ size_t __idx = __h % __len; \
size_t __start = __idx; \ size_t __start = __idx; \
(out) = NULL; \ (out) = NULL; \
@ -68,7 +69,7 @@ enum {
if ((tb)[__idx]._hshtbstate == HSHTB_EMPTY) { \ if ((tb)[__idx]._hshtbstate == HSHTB_EMPTY) { \
break; \ break; \
} \ } \
if ((tb)[__idx]._hshtbstate == HSHTB_TAKEN && hal_strcmp((tb)[__idx].keyfield, (k)) == 0) { \ if ((tb)[__idx]._hshtbstate == HSHTB_TAKEN && strcmp((tb)[__idx].keyfield, (k)) == 0) { \
(out) = &(tb)[__idx]; \ (out) = &(tb)[__idx]; \
break; \ break; \
} \ } \

View File

@ -7,9 +7,10 @@
#include "errors.h" #include "errors.h"
#include "pipe.h" #include "pipe.h"
#include "kprintf.h" #include "kprintf.h"
#include "std/string.h"
int32_t ipc_pipeinit(IpcPipe *pipe, uint64_t pid) { int32_t ipc_pipeinit(IpcPipe *pipe, uint64_t pid) {
hal_memset(pipe, 0, sizeof(*pipe)); memset(pipe, 0, sizeof(*pipe));
spinlock_init(&pipe->spinlock); spinlock_init(&pipe->spinlock);
pipe->ownerpid = pid; pipe->ownerpid = pid;

View File

@ -1,5 +1,6 @@
#include "path.h" #include "path.h"
#include "hal/hal.h" #include "hal/hal.h"
#include "std/string.h"
void path_parse(const char *in, char *mp, char *path) { void path_parse(const char *in, char *mp, char *path) {
if (in == 0 || *in == 0) { if (in == 0 || *in == 0) {
@ -45,8 +46,8 @@ void path_parse(const char *in, char *mp, char *path) {
return; return;
} }
if (hal_strstr(path, "/../") || hal_strstr(path, "/./") if (strstr(path, "/../") || strstr(path, "/./")
|| hal_strcmp(path, "..") == 0 || hal_strcmp(path, ".") == 0) { || strcmp(path, "..") == 0 || strcmp(path, ".") == 0) {
mp[0] = 0; mp[0] = 0;
path[0] = 0; path[0] = 0;
return; return;

View File

@ -7,6 +7,7 @@
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "util/util.h" #include "util/util.h"
#include "hal/hal.h" #include "hal/hal.h"
#include "std/string.h"
PhysMem PHYS_MEM; PhysMem PHYS_MEM;
@ -37,7 +38,7 @@ void pmm_init(void) {
size_t physbegin = memmap_ent->base; size_t physbegin = memmap_ent->base;
bm->map = (uint8_t *)(physbegin + BOOT_INFO.hhdm_off); bm->map = (uint8_t *)(physbegin + BOOT_INFO.hhdm_off);
hal_memset(bm->map, 0xff, bm->nbytes); memset(bm->map, 0xff, bm->nbytes);
for (size_t i = 0; i < BOOT_INFO.memmap_entrycount; i++) { for (size_t i = 0; i < BOOT_INFO.memmap_entrycount; i++) {
struct limine_memmap_entry *entry = BOOT_INFO.memmap_entries[i]; struct limine_memmap_entry *entry = BOOT_INFO.memmap_entries[i];
if (entry == memmap_ent) { if (entry == memmap_ent) {

View File

@ -16,6 +16,7 @@
#include "sysdefs/proc.h" #include "sysdefs/proc.h"
#include "sysdefs/fs.h" #include "sysdefs/fs.h"
#include "time/time.h" #include "time/time.h"
#include "std/string.h"
#define PROC_REAPER_FREQ 30 #define PROC_REAPER_FREQ 30
@ -53,8 +54,8 @@ ElfAuxval proc_load_elf_segs(Proc *proc, uint8_t *data) {
uint8_t *physaddr = pmm_alloc(blocks); uint8_t *physaddr = pmm_alloc(blocks);
uint8_t *virtaddr = (uint8_t *)(phdr->p_vaddr & ~(HAL_PAGE_SIZE - 1)); uint8_t *virtaddr = (uint8_t *)(phdr->p_vaddr & ~(HAL_PAGE_SIZE - 1));
hal_memset(VIRT(physaddr), 0, blocks * HAL_PAGE_SIZE); memset(VIRT(physaddr), 0, blocks * HAL_PAGE_SIZE);
hal_memcpy(VIRT(physaddr) + off, (data + phdr->p_offset), phdr->p_filesz); memcpy(VIRT(physaddr) + off, (data + phdr->p_offset), phdr->p_filesz);
uint32_t pgflags = HAL_PG_USER | HAL_PG_PRESENT; uint32_t pgflags = HAL_PG_USER | HAL_PG_PRESENT;
if (phdr->p_flags & PF_W) { if (phdr->p_flags & PF_W) {
@ -105,10 +106,10 @@ Proc *proc_spawnuser(char *mountpoint, char *path) {
vfs_close(vobj); vfs_close(vobj);
Proc *proc = dlmalloc(sizeof(*proc)); Proc *proc = dlmalloc(sizeof(*proc));
hal_memset(proc, 0, sizeof(*proc)); memset(proc, 0, sizeof(*proc));
ksprintf(proc->name, "%s:%s", mountpoint, path); ksprintf(proc->name, "%s:%s", mountpoint, path);
hal_memset(&proc->platformdata.trapframe, 0, sizeof(proc->platformdata.trapframe)); memset(&proc->platformdata.trapframe, 0, sizeof(proc->platformdata.trapframe));
proc->platformdata.cr3 = hal_vmm_userproc_pml4_phys(); proc->platformdata.cr3 = hal_vmm_userproc_pml4_phys();
uint8_t *kstackp = (uint8_t *)pmm_alloc(PROC_STACKBLOCKS) + PROC_STACKSIZE; uint8_t *kstackp = (uint8_t *)pmm_alloc(PROC_STACKBLOCKS) + PROC_STACKSIZE;
@ -223,7 +224,7 @@ void proc_sched(void *cpustate) {
hal_intr_disable(); hal_intr_disable();
sched_ticks++; sched_ticks++;
hal_memcpy(&PROCS.current->platformdata.trapframe, cpustate, sizeof(IntrStackFrame)); memcpy(&PROCS.current->platformdata.trapframe, cpustate, sizeof(IntrStackFrame));
PROCS.current = proc_nextready(); PROCS.current = proc_nextready();

View File

@ -10,6 +10,7 @@
#include "sysdefs/proc.h" #include "sysdefs/proc.h"
#include "sysdefs/time.h" #include "sysdefs/time.h"
#include "dev/dev.h" #include "dev/dev.h"
#include "std/string.h"
#define PROC_NAME_MAX 0x100 #define PROC_NAME_MAX 0x100
@ -90,7 +91,7 @@ void proc_kill(Proc *proc);
#define PROC_ARG(proc, str) \ #define PROC_ARG(proc, str) \
do { \ do { \
ProcArg *__arg = dlmalloc(sizeof(*__arg)); \ ProcArg *__arg = dlmalloc(sizeof(*__arg)); \
hal_strcpy(__arg->string, (str)); \ strcpy(__arg->string, (str)); \
LL_APPEND((proc)->procargs.list, __arg); \ LL_APPEND((proc)->procargs.list, __arg); \
(proc)->procargs.len++; \ (proc)->procargs.len++; \
} while(0) } while(0)

View File

@ -1,39 +1,151 @@
#include <stddef.h> #include <stddef.h>
#include "hal/hal.h" #include <stdint.h>
void *memset(void *p, int c, size_t n) { void *memset(void *p, int c, size_t n) {
return hal_memset(p,c,n); char *cp = p;
for (size_t i = 0; i < n; i++) cp[i] = c;
return p;
} }
void *memcpy(void *dst, const void *src, size_t n) { void *memcpy(void *dst, const void *src, size_t n) {
return hal_memcpy(dst,src,n); char *a = dst;
const char *b = src;
for (size_t i = 0; i < n; i++) a[i] = b[i];
return dst;
} }
size_t strlen(char *s) { size_t strlen(char *s) {
return hal_strlen(s); char *s2;
for (s2 = s; *s2; ++s2);
return (s2 - s);
}
// https://aticleworld.com/memcmp-in-c/
int memcmp(const void *s1, const void *s2, int len)
{
unsigned char *p = (unsigned char *)s1;
unsigned char *q = (unsigned char *)s2;
int charCompareStatus = 0;
//If both pointer pointing same memory block
if (s1 == s2)
{
return charCompareStatus;
}
while (len > 0)
{
if (*p != *q)
{ //compare the mismatching character
charCompareStatus = (*p >*q)?1:-1;
break;
}
len--;
p++;
q++;
}
return charCompareStatus;
} }
int strcmp(const char *a, const char *b) { int strcmp(const char *a, const char *b) {
return hal_strcmp(a, b); while (*a && (*a == *b)) {
a++, b++;
}
return (unsigned char)*a - (unsigned char)*b;
} }
size_t strcspn(const char *s, const char *reject) { size_t strcspn(const char *s, const char *reject) {
return hal_strcspn(s, reject); size_t count = 0;
for (; *s != '\0'; ++s) {
const char *r = reject;
while (*r != '\0') {
if (*s == *r) {
return count;
}
r++;
}
count++;
}
return count;
} }
size_t strspn(const char *s, const char *accept) { size_t strspn(const char *s, const char *accept) {
return hal_strspn(s, accept); size_t count = 0;
for (; *s != '\0'; ++s) {
const char *a = accept;
int matched = 0;
while (*a != '\0') {
if (*s == *a) {
matched = 1;
break;
}
a++;
}
if (!matched) {
return count;
}
count++;
}
return count;
} }
char *strcpy(char *dest, const char *src) { char *strcpy(char *dest, const char *src) {
return hal_strcpy(dest, src); char *d = dest;
while ((*d++ = *src++) != '\0') {
;
}
return dest;
} }
char *strchr(const char *s, int c) { char *strchr(const char *s, int c) {
return hal_strchr(s, c); char ch = (char)c;
while (*s != '\0') {
if (*s == ch) {
return (char *)s;
}
s++;
}
if (ch == '\0') {
return (char *)s;
}
return NULL;
} }
int memcmp(const void *s1, const void *s2, int len) { char *strstr(const char *str, const char *substring)
return hal_memcmp(s1, s2, len); {
const char *a;
const char *b;
b = substring;
if (*b == 0) {
return (char *) str;
}
for ( ; *str != 0; str += 1) {
if (*str != *b) {
continue;
}
a = str;
while (1) {
if (*b == 0) {
return (char *) str;
}
if (*a++ != *b++) {
break;
}
}
b = substring;
}
return NULL;
}
char *strcat(char *dest, const char *src) {
size_t i, j;
for(i = 0; dest[i] != '\0'; i++);
for(j = 0; src[j] != '\0'; j++) {
dest[i+j] = src[j];
}
dest[i+j] = '\0';
return dest;
} }

View File

@ -12,5 +12,7 @@ size_t strspn(const char *s, const char *accept);
char *strcpy(char *dest, const char *src); char *strcpy(char *dest, const char *src);
char *strchr(const char *s, int c); char *strchr(const char *s, int c);
int memcmp(const void *s1, const void *s2, int len); int memcmp(const void *s1, const void *s2, int len);
char *strstr(const char *str, const char *substring);
char *strcat(char *dest, const char *src);
#endif // STD_STRING_H_ #endif // STD_STRING_H_

View File

@ -8,6 +8,7 @@
#include "hal/hal.h" #include "hal/hal.h"
#include "util/util.h" #include "util/util.h"
#include "kprintf.h" #include "kprintf.h"
#include "std/string.h"
int32_t ramsd_init(struct StoreDev *sd, void *extra) { int32_t ramsd_init(struct StoreDev *sd, void *extra) {
RamSdInitExtra *e = extra; RamSdInitExtra *e = extra;
@ -26,7 +27,7 @@ int32_t ramsd_init(struct StoreDev *sd, void *extra) {
int32_t ramsd_read(struct StoreDev *sd, uint8_t *const buffer, ptrdiff_t sector, ptrdiff_t off, size_t size) { int32_t ramsd_read(struct StoreDev *sd, uint8_t *const buffer, ptrdiff_t sector, ptrdiff_t off, size_t size) {
RamSd *ramsd = &sd->sd.ramsd; RamSd *ramsd = &sd->sd.ramsd;
spinlock_acquire(&sd->spinlock); spinlock_acquire(&sd->spinlock);
hal_memcpy(buffer, ramsd->buffer + (sector * sd->sectorsize + off), size); memcpy(buffer, ramsd->buffer + (sector * sd->sectorsize + off), size);
spinlock_release(&sd->spinlock); spinlock_release(&sd->spinlock);
return E_OK; return E_OK;
} }
@ -34,7 +35,7 @@ int32_t ramsd_read(struct StoreDev *sd, uint8_t *const buffer, ptrdiff_t sector,
int32_t ramsd_write(struct StoreDev *sd, const uint8_t *const buffer, ptrdiff_t sector, ptrdiff_t off, size_t size) { int32_t ramsd_write(struct StoreDev *sd, const uint8_t *const buffer, ptrdiff_t sector, ptrdiff_t off, size_t size) {
RamSd *ramsd = &sd->sd.ramsd; RamSd *ramsd = &sd->sd.ramsd;
spinlock_acquire(&sd->spinlock); spinlock_acquire(&sd->spinlock);
hal_memcpy(ramsd->buffer + (sector * sd->sectorsize + off), buffer, size); memcpy(ramsd->buffer + (sector * sd->sectorsize + off), buffer, size);
spinlock_release(&sd->spinlock); spinlock_release(&sd->spinlock);
return E_OK; return E_OK;
} }

View File

@ -9,6 +9,7 @@
#include "util/util.h" #include "util/util.h"
#include "hshtb.h" #include "hshtb.h"
#include "dev/dev.h" #include "dev/dev.h"
#include "std/string.h"
int32_t SYSCALL2(sys_dev_gethandle, dev1, devname1) { int32_t SYSCALL2(sys_dev_gethandle, dev1, devname1) {
uint64_t *devh = (uint64_t *)dev1; uint64_t *devh = (uint64_t *)dev1;
@ -87,7 +88,7 @@ int32_t SYSCALL2(sys_dev_stat, devstat1, idx1) {
for (size_t i = 0; i < LEN(DEVTABLE.devs); i++) { for (size_t i = 0; i < LEN(DEVTABLE.devs); i++) {
if (i == idx && DEVTABLE.devs[i]._hshtbstate == HSHTB_TAKEN) { if (i == idx && DEVTABLE.devs[i]._hshtbstate == HSHTB_TAKEN) {
Dev *dev = &DEVTABLE.devs[i]; Dev *dev = &DEVTABLE.devs[i];
hal_memcpy(devstat->name, dev->ident, sizeof(dev->ident)); memcpy(devstat->name, dev->ident, sizeof(dev->ident));
for (size_t j = 0; j < DEV_FNS_MAX; j++) { for (size_t j = 0; j < DEV_FNS_MAX; j++) {
if (dev->fns[j] != NULL) { if (dev->fns[j] != NULL) {
devstat->nfns++; devstat->nfns++;

View File

@ -11,6 +11,7 @@
#include "bootinfo/bootinfo.h" #include "bootinfo/bootinfo.h"
#include "dlmalloc/malloc.h" #include "dlmalloc/malloc.h"
#include "kprintf.h" #include "kprintf.h"
#include "std/string.h"
int32_t SYSCALL5(sys_mman_map, addr1, size1, prot1, flags1, out1) { int32_t SYSCALL5(sys_mman_map, addr1, size1, prot1, flags1, out1) {
uint8_t *addr = (uint8_t *)addr1; uint8_t *addr = (uint8_t *)addr1;
@ -28,7 +29,7 @@ int32_t SYSCALL5(sys_mman_map, addr1, size1, prot1, flags1, out1) {
size_t pages = _DIV_ROUNDUP(size, HAL_PAGE_SIZE); size_t pages = _DIV_ROUNDUP(size, HAL_PAGE_SIZE);
uint8_t *phys = (uint8_t *)pmm_alloc(pages); uint8_t *phys = (uint8_t *)pmm_alloc(pages);
hal_memset(VIRT(phys), 0, pages * HAL_PAGE_SIZE); memset(VIRT(phys), 0, pages * HAL_PAGE_SIZE);
spinlock_acquire(&PROCS.spinlock); spinlock_acquire(&PROCS.spinlock);
Proc *proc = NULL; Proc *proc = NULL;

View File

@ -11,6 +11,7 @@
#include "kprintf.h" #include "kprintf.h"
#include "dlmalloc/malloc.h" #include "dlmalloc/malloc.h"
#include "ipc/pipe/pipe.h" #include "ipc/pipe/pipe.h"
#include "std/string.h"
#define _MP_MAX 0xff #define _MP_MAX 0xff
#define _PATH_MAX VFS_PATH_MAX #define _PATH_MAX VFS_PATH_MAX
@ -178,7 +179,7 @@ int32_t SYSCALL4(sys_proc_argv, pid1, argslen1, argbuf1, maxargs1) {
ret = E_INVALIDARGUMENT; ret = E_INVALIDARGUMENT;
goto done; goto done;
} }
hal_strcpy(argbuf[i], arg->string); strcpy(argbuf[i], arg->string);
} }
*argslen = i; *argslen = i;
@ -214,7 +215,7 @@ int32_t SYSCALL2(sys_proc_stat, pidx, pstat1) {
LL_FOREACH_SAFE_IDX(PROCS.procs, p, ptmp, i) { LL_FOREACH_SAFE_IDX(PROCS.procs, p, ptmp, i) {
if (i == pidx) { if (i == pidx) {
stat->pid = p->pid; stat->pid = p->pid;
hal_strcpy(stat->name, p->name); strcpy(stat->name, p->name);
stat->state = p->state; stat->state = p->state;
VasRange *vas, *vastmp; VasRange *vas, *vastmp;
@ -222,7 +223,7 @@ int32_t SYSCALL2(sys_proc_stat, pidx, pstat1) {
stat->usemem += vas->size; stat->usemem += vas->size;
} }
hal_memcpy(&stat->time, &p->time, sizeof(p->time)); memcpy(&stat->time, &p->time, sizeof(p->time));
break; break;
} }
} }

View File

@ -14,6 +14,7 @@
#include "storedev/storedev.h" #include "storedev/storedev.h"
#include "util/util.h" #include "util/util.h"
#include "hshtb.h" #include "hshtb.h"
#include "std/string.h"
int32_t SYSCALL4(sys_vfsmount, mountpoint1, fstypestr1, devid1, format1) { int32_t SYSCALL4(sys_vfsmount, mountpoint1, fstypestr1, devid1, format1) {
int32_t ret = E_OK; int32_t ret = E_OK;
@ -30,7 +31,7 @@ int32_t SYSCALL4(sys_vfsmount, mountpoint1, fstypestr1, devid1, format1) {
int32_t fstype; int32_t fstype;
if (hal_strcmp(fstypestr, "LittleFS") == 0) { if (strcmp(fstypestr, "LittleFS") == 0) {
fstype = VFS_LITTLEFS; fstype = VFS_LITTLEFS;
} else { } else {
ret = E_INVALIDARGUMENT; ret = E_INVALIDARGUMENT;
@ -76,7 +77,7 @@ int32_t SYSCALL1(sys_vfsavailmounts, availmounts1) {
for (size_t i = 0; i < VFS_MOUNTPOINTS_MAX; i++) { for (size_t i = 0; i < VFS_MOUNTPOINTS_MAX; i++) {
VfsMountPoint *vmp = &VFS_TABLE.mountpoints[i]; VfsMountPoint *vmp = &VFS_TABLE.mountpoints[i];
if (vmp->_hshtbstate == HSHTB_TAKEN) { if (vmp->_hshtbstate == HSHTB_TAKEN) {
hal_memcpy(availmounts->labels[i], vmp->label, VFS_MOUNTPOINT_LABEL_MAX); memcpy(availmounts->labels[i], vmp->label, VFS_MOUNTPOINT_LABEL_MAX);
availmounts->fieldavail[i] = 1; availmounts->fieldavail[i] = 1;
} }
} }
@ -105,7 +106,7 @@ int32_t SYSCALL2(sys_vfsmountstat, statbuf1, label1) {
return E_NOENTRY; return E_NOENTRY;
} }
hal_memcpy(statbuf->label, vmp->label, sizeof(statbuf->label)); memcpy(statbuf->label, vmp->label, sizeof(statbuf->label));
statbuf->fstype = vmp->fstype; statbuf->fstype = vmp->fstype;
StoreDev *vmpsd = vmp->backingsd; StoreDev *vmpsd = vmp->backingsd;
@ -113,7 +114,7 @@ int32_t SYSCALL2(sys_vfsmountstat, statbuf1, label1) {
for (size_t i = 0; i < LEN(DEVTABLE.devs); i++) { for (size_t i = 0; i < LEN(DEVTABLE.devs); i++) {
Dev *dev = &DEVTABLE.devs[i]; Dev *dev = &DEVTABLE.devs[i];
if (dev->extra != NULL && ((StoreDev *)dev->extra)->_magic == STOREDEV_MAGIC && vmpsd == dev->extra) { if (dev->extra != NULL && ((StoreDev *)dev->extra)->_magic == STOREDEV_MAGIC && vmpsd == dev->extra) {
hal_memcpy(statbuf->devname, dev->ident, sizeof(statbuf->devname)); memcpy(statbuf->devname, dev->ident, sizeof(statbuf->devname));
break; break;
} }
} }

View File

@ -2,6 +2,7 @@
#include <stddef.h> #include <stddef.h>
#include "time.h" #include "time.h"
#include "hal/hal.h" #include "hal/hal.h"
#include "std/string.h"
#define CMOS_PORT 0x70 #define CMOS_PORT 0x70
#define CMOS_RETURN 0x71 #define CMOS_RETURN 0x71
@ -48,7 +49,7 @@ void time_get(Time *time) {
continue; continue;
} }
time_fill(&t2); time_fill(&t2);
if (hal_memcmp(&t1, &t2, sizeof(t1)) == 0) { if (memcmp(&t1, &t2, sizeof(t1)) == 0) {
break; break;
} }
} }

View File

@ -12,6 +12,7 @@
#include "storedev/storedev.h" #include "storedev/storedev.h"
#include "baseimg/baseimg.h" #include "baseimg/baseimg.h"
#include "dlmalloc/malloc.h" #include "dlmalloc/malloc.h"
#include "std/string.h"
VfsTable VFS_TABLE; VfsTable VFS_TABLE;
VfsBusyObjs VFS_BUSY_VOBJS; VfsBusyObjs VFS_BUSY_VOBJS;
@ -19,16 +20,16 @@ VfsBusyObjs VFS_BUSY_VOBJS;
#define CHECK_BUSY_VFSOBJ() \ #define CHECK_BUSY_VFSOBJ() \
char *tmpbuf = dlmalloc(VFS_MOUNTPOINT_LABEL_MAX+1+VFS_PATH_MAX); \ char *tmpbuf = dlmalloc(VFS_MOUNTPOINT_LABEL_MAX+1+VFS_PATH_MAX); \
\ \
hal_memset(tmpbuf, 0, VFS_MOUNTPOINT_LABEL_MAX+1+VFS_PATH_MAX); \ memset(tmpbuf, 0, VFS_MOUNTPOINT_LABEL_MAX+1+VFS_PATH_MAX); \
hal_string_combine(tmpbuf, mountpoint); \ strcat(tmpbuf, mountpoint); \
hal_string_combine(tmpbuf, ":"); \ strcat(tmpbuf, ":"); \
hal_string_combine(tmpbuf, path); \ strcat(tmpbuf, path); \
\ \
bool busy = false; \ bool busy = false; \
VfsObj *vobj, *vobjtmp; \ VfsObj *vobj, *vobjtmp; \
spinlock_acquire(&VFS_BUSY_VOBJS.spinlock); \ spinlock_acquire(&VFS_BUSY_VOBJS.spinlock); \
LL_FOREACH_SAFE(VFS_BUSY_VOBJS.vobjs, vobj, vobjtmp) { \ LL_FOREACH_SAFE(VFS_BUSY_VOBJS.vobjs, vobj, vobjtmp) { \
if (hal_strcmp(vobj->path, tmpbuf) == 0) { \ if (strcmp(vobj->path, tmpbuf) == 0) { \
busy = true; \ busy = true; \
break; \ break; \
} \ } \
@ -39,7 +40,7 @@ VfsBusyObjs VFS_BUSY_VOBJS;
void vfs_init_littlefs(VfsMountPoint *mp, bool format) { void vfs_init_littlefs(VfsMountPoint *mp, bool format) {
struct lfs_config *cfg = dlmalloc(sizeof(*cfg)); struct lfs_config *cfg = dlmalloc(sizeof(*cfg));
hal_memset(cfg, 0, sizeof(*cfg)); memset(cfg, 0, sizeof(*cfg));
cfg->context = mp; cfg->context = mp;
cfg->read = &portlfs_read; cfg->read = &portlfs_read;
cfg->prog = &portlfs_prog; cfg->prog = &portlfs_prog;
@ -147,7 +148,7 @@ int32_t vfs_mount(char *mountpoint, int32_t fstype, StoreDev *backingsd, bool fo
return E_NOMEMORY; return E_NOMEMORY;
} }
hal_memcpy(mp->label, mountpoint, hal_strlen(mountpoint)); memcpy(mp->label, mountpoint, strlen(mountpoint));
mp->backingsd = backingsd; mp->backingsd = backingsd;
mp->fstype = fstype; mp->fstype = fstype;
switch (fstype) { switch (fstype) {
@ -178,7 +179,7 @@ int32_t vfs_unmount(char *mountpoint) {
spinlock_release(&mp->spinlock); spinlock_release(&mp->spinlock);
return err; return err;
} }
hal_memset(mp, 0, sizeof(*mp)); memset(mp, 0, sizeof(*mp));
spinlock_release(&mp->spinlock); spinlock_release(&mp->spinlock);
return E_OK; return E_OK;
} }
@ -205,9 +206,9 @@ VfsObj *vfs_open(char *mountpoint, const char *path, uint32_t flags) {
return NULL; return NULL;
} }
hal_string_combine(vobj1->path, mountpoint); strcat(vobj1->path, mountpoint);
hal_string_combine(vobj1->path, ":"); strcat(vobj1->path, ":");
hal_string_combine(vobj1->path, path); strcat(vobj1->path, path);
spinlock_acquire(&VFS_BUSY_VOBJS.spinlock); spinlock_acquire(&VFS_BUSY_VOBJS.spinlock);
LL_APPEND(VFS_BUSY_VOBJS.vobjs, vobj1); LL_APPEND(VFS_BUSY_VOBJS.vobjs, vobj1);
@ -224,9 +225,9 @@ void vfs_close(VfsObj *vobj) {
} }
void vfs_init(void) { void vfs_init(void) {
hal_memset(&VFS_TABLE, 0, sizeof(VFS_TABLE)); memset(&VFS_TABLE, 0, sizeof(VFS_TABLE));
spinlock_init(&VFS_TABLE.spinlock); spinlock_init(&VFS_TABLE.spinlock);
hal_memset(&VFS_BUSY_VOBJS, 0, sizeof(VFS_BUSY_VOBJS)); memset(&VFS_BUSY_VOBJS, 0, sizeof(VFS_BUSY_VOBJS));
spinlock_init(&VFS_BUSY_VOBJS.spinlock); spinlock_init(&VFS_BUSY_VOBJS.spinlock);
{ {