From b48327d357e0818d1a6ae2a064cfa7d1567e1242 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Sun, 5 Dec 2021 15:17:44 -0800 Subject: feat(huge): huge refactor (in progress). Commented out libc includes to uncover all explicit dependencies. A large fraction has now been ported over (no testing). I did not port over the command line tools, such as the rc shell. These will be done independently - as of now I just want the library to stand independent. Compilation currently fails due to the lack of math functions. --- include/base/fs.h | 2 +- include/base/io.h | 3 +++ include/base/macro/map.h | 20 ++++++++++---------- include/base/memory.h | 19 +++++++++++-------- include/base/string.h | 4 ++++ include/base/utf.h | 4 +++- 6 files changed, 32 insertions(+), 20 deletions(-) (limited to 'include/base') diff --git a/include/base/fs.h b/include/base/fs.h index dc6ef9e..0c77f3a 100644 --- a/include/base/fs.h +++ b/include/base/fs.h @@ -36,6 +36,6 @@ void fs·fini(fs·Walker *); void fs·walk(fs·Walker *); /* small utilities */ -int fs·exists(byte *path, int flag); +int fs·access(byte *path, int flag); byte *fs·dirname(byte *path); byte *fs·basename(byte *path); diff --git a/include/base/io.h b/include/base/io.h index 04b596f..8b26761 100644 --- a/include/base/io.h +++ b/include/base/io.h @@ -124,3 +124,6 @@ intptr io·offset(io·Header *io); int io·print(io·Header *io, char *fmt, ...); int io·vprint(io·Header *io, char *fmt, va_list args); + +/* simple i/o */ +int io·puts(char *s); diff --git a/include/base/macro/map.h b/include/base/macro/map.h index d7b9e89..1f07eab 100644 --- a/include/base/macro/map.h +++ b/include/base/macro/map.h @@ -44,7 +44,7 @@ static const double HASHFILL = 0.77; #define MAP_RESET(map) \ if((map) && (map)->flag){ \ - memset((map)->flag, 0xAA, map·flagsize((map)->cap)*sizeof(int32)); \ + mem·set((map)->flag, map·flagsize((map)->cap)*sizeof(int32), 0xAA); \ (map)->len = (map)->occ = 0; \ } @@ -85,14 +85,14 @@ static const double HASHFILL = 0.77; else{ \ new_flags = (mem).alloc((heap), map·flagsize(newcap), sizeof(int32)); \ if(!new_flags) return -1; \ - memset(new_flags, 0xaa, map·flagsize(newcap) * sizeof(int32)); \ + mem·set(new_flags, map·flagsize(newcap) * sizeof(int32), 0xAA); \ if((map)->cap < newcap){ /* expand */ \ key_t *new_keys = (mem).alloc((heap), newcap, sizeof(key_t)); \ if(!new_keys){ \ (mem).free((heap), new_flags); \ return -1; \ } \ - memcpy(new_keys, (map)->key, sizeof(key_t) * (map)->cap); \ + mem·copy(new_keys, sizeof(key_t) * (map)->cap, (map)->key); \ (mem).free((heap), (map)->key); \ (map)->key = new_keys; \ val_t *new_vals = (mem).alloc((heap), newcap, sizeof(val_t)); \ @@ -100,7 +100,7 @@ static const double HASHFILL = 0.77; (mem).free((heap), new_flags); \ return -1; \ } \ - memcpy(new_vals, (map)->val, sizeof(val_t) * (map)->cap); \ + mem·copy(new_vals, sizeof(val_t) * (map)->cap, (map)->val); \ (mem).free((heap), (map)->val); \ (map)->val = new_vals; \ } \ @@ -144,12 +144,12 @@ static const double HASHFILL = 0.77; } \ if((map)->cap > newcap){ /* shrink the hash table */ \ key_t *new_keys = (mem).alloc((heap), newcap, sizeof(key_t)); \ - memcpy(new_keys, (map)->key, sizeof(key_t) * (map)->cap); \ + mem·copy(new_keys, sizeof(key_t) * (map)->cap, (map)->key); \ (mem).free((heap), (map)->key); \ (map)->key = new_keys; \ \ val_t *new_vals = (mem).alloc((heap), newcap, sizeof(val_t)); \ - memcpy(new_vals, (map)->val, sizeof(val_t) * (map)->cap); \ + mem·copy(new_vals, sizeof(val_t) * (map)->cap, (map)->val); \ (mem).free((heap), (map)->val); \ (map)->val = new_vals; \ } \ @@ -240,7 +240,7 @@ static const double HASHFILL = 0.77; #define SET_RESET(set) \ if ((set) && (set)->flag) { \ - memset((set)->flag, 0xAA, map·flagsize((set)->cap) * sizeof(int32)); \ + mem·set((set)->flag, map·flagsize((set)->cap) * sizeof(int32), 0xAA); \ (set)->len = (set)->occ = 0; \ } @@ -281,14 +281,14 @@ static const double HASHFILL = 0.77; else{ \ new_flags = mem.alloc((heap), map·flagsize(newcap), sizeof(int32)); \ if(!new_flags) return -1; \ - memset(new_flags, 0xaa, map·flagsize(newcap) * sizeof(int32)); \ + mem·set(new_flags, 0xaa, map·flagsize(newcap) * sizeof(int32)); \ if((set)->cap < newcap) { /* expand */ \ key_t *new_keys = mem.alloc((heap), newcap, sizeof(key_t)); \ if(!new_keys){ \ mem.free((heap), new_flags); \ return -1; \ } \ - memcpy(new_keys, (set)->key, sizeof(key_t) * (set)->cap); \ + mem·copy(new_keys, sizeof(key_t) * (set)->cap, (set)->key); \ mem.free((heap), (set)->key); \ (set)->key = new_keys; \ } \ @@ -324,7 +324,7 @@ static const double HASHFILL = 0.77; } \ if((set)->cap > newcap) { /* shrink the hash table */ \ key_t *new_keys = mem.alloc((heap), newcap, sizeof(key_t)); \ - memcpy(new_keys, (set)->key, sizeof(key_t) * (set)->cap); \ + mem·copy(new_keys, sizeof(key_t) * (set)->cap, (set)->key); \ mem.free((heap), (set)->key); \ (set)->key = new_keys; \ } \ diff --git a/include/base/memory.h b/include/base/memory.h index 767e948..6bf3cbc 100644 --- a/include/base/memory.h +++ b/include/base/memory.h @@ -8,6 +8,7 @@ int mem·move(void *dst, uintptr size, void *src); int mem·copy(void *dst, uintptr size, void *src); int mem·compare(void *, uintptr size, void *); void *mem·findc(void *dst, uintptr len, int c); +void *mem·rfindc(void *dst, uintptr len, int c); int mem·set(void *dst, uintptr size, int val); int mem·set64(void *dst, uintptr size, uint64 val); @@ -49,23 +50,25 @@ typedef struct mem·Pool mem·Pool; typedef struct mem·Arena mem·Arena; typedef struct mem·Allocator { - void *(*alloc)(void *heap, uint n, ulong size); - void *(*realloc)(void *iface, void *ptr, uint n, ulong size); + void *(*alloc)(void *heap, long n, uintptr size); + void *(*realloc)(void *heap, void *ptr, long n, uintptr size); void (*free)(void *heap, void *ptr); } mem·Allocator; -extern mem·Allocator base·Memory; /* standard allocator */ +/* standard allocator */ +extern mem·Allocator base·Memory; /* pool allocator (manages free list) */ -mem·Pool *mem·makepool(mem·Allocator from, void*); -void mem·freepool(mem·Pool); +mem·Pool *mem·makepool(mem·Allocator from, void*, char *name, int flags, intptr maxsize, intptr mincore); +void mem·freepool(mem·Pool *); -void *mem·poolalloc(mem·Pool *, uintptr n, uintptr size); -void *mem·poolrealloc(mem·Pool *, void *, uintptr n, uintptr size); +void *mem·poolalloc(mem·Pool *, long n, uintptr size); +void *mem·poolrealloc(mem·Pool *, void *, long n, uintptr size); void mem·poolfree(mem·Pool *, void *); /* simple arena allocator (heterogeneous bump) */ mem·Arena *mem·makearena(mem·Allocator from, void*); -void *mem·arenaalloc(mem·Arena *A, uint n, ulong size); +void *mem·arenaalloc(mem·Arena *A, long n, ulong size); void mem·freearena(mem·Arena *A); + extern mem·Allocator mem·ArenaAllocator; diff --git a/include/base/string.h b/include/base/string.h index a6d6612..238ebf9 100644 --- a/include/base/string.h +++ b/include/base/string.h @@ -9,6 +9,10 @@ char *str·findc(char *s, int c); char *str·nfindc(char *s, intptr n, int c); char *str·efindc(char *s, char *e, int c); +char *str·rfindc(char *s, int c); +char *str·nrfindc(char *s, intptr n, int c); +char *str·erfindc(char *s, char *e, int c); + char *str·find(char *s, char *sub); char *str·nfind(char *s, intptr n, char *sub); char *str·efind(char *s, char *e, char *sub); diff --git a/include/base/utf.h b/include/base/utf.h index 0419d1a..19f345f 100644 --- a/include/base/utf.h +++ b/include/base/utf.h @@ -21,7 +21,7 @@ int utf8·encode(rune *, char *); // encode 1 rune from rune stream, s int utf8·decodeprev(char *s, rune *r); // decode 1 rune from char stream, reading backwards, store into rune, return number of bytes char *utf8·find(char *s, rune); // find rune in char stream -char *utf8·findlast(char* s, rune); // find last rune in char stream +char *utf8·rfind(char* s, rune); // find last rune in char stream int utf8·fullrune(char *, int); // XXX: odd function... @@ -30,6 +30,8 @@ int utf8·isdigit(rune r); int utf8·isspace(rune r); int utf8·istitle(rune r); int utf8·ispunct(rune r); +int utf8·isupper(rune r); +int utf8·islower(rune r); rune utf8·toupper(rune r); rune utf8·tolower(rune r); -- cgit v1.2.1