diff options
author | Nicholas Noll <nbnoll@eml.cc> | 2021-12-05 09:47:21 -0800 |
---|---|---|
committer | Nicholas Noll <nbnoll@eml.cc> | 2021-12-05 10:54:20 -0800 |
commit | 521d01e8ad87e931af3e9a763cc84a6cf7fe5ee3 (patch) | |
tree | f544119060c3eefc7b0fec6cff1740a362541213 /include/base/mem.h | |
parent | 158d9b84f14457136379f42e7c071eb79d87ee6b (diff) |
Feat: basic string and memory functions
Continue filling out the basic standard lib functions.
Included prototypes of the str* and mem* families.
Plan to add e(str|mem) and n(str|mem) variants as well.
Diffstat (limited to 'include/base/mem.h')
-rw-r--r-- | include/base/mem.h | 65 |
1 files changed, 0 insertions, 65 deletions
diff --git a/include/base/mem.h b/include/base/mem.h deleted file mode 100644 index 6f14c8e..0000000 --- a/include/base/mem.h +++ /dev/null @@ -1,65 +0,0 @@ -#pragma once - -// ---------------------------------------------------------------------------- -// dynamic array - -typedef struct BufHdr -{ - vlong len; - vlong cap; - byte buf[]; -} BufHdr; - -#define bufhdr(b) ((BufHdr*)((uint8*)(b)-offsetof(BufHdr, buf))) -#define buflen(b) ((b) ? (bufhdr(b)->len) : 0) -#define bufcap(b) ((b) ? (bufhdr(b)->cap) : 0) -#define bufend(b) ((b) + buflen(b)) -#define bufsize(b) ((b) ? (buflen(b) * sizeof((b)[0])) : 0) - -#define buffree(b) ((b) ? (free(bufhdr(b)), (b) = nil) : 0) -#define buffit(b, n) ((n) <= bufcap(b) ? 0 : ((b) = ·bufgrow((b), (n), sizeof(*(b))))) - -#define bufpush(b, ...) (buffit((b), 1 + buflen(b)), (b)[bufhdr(b)->len++] = (__VA_ARGS__)) -#define bufaddn(b, n) (buffit(b, buflen(b)+n), bufhdr(b)->len += n, b+bufhdr(b)->len-n) - -#define bufpop(b) ((b)[--bufhdr(b)->len]) -#define bufdel(b, i) bufdeln((b), (i), 1) -#define bufdeln(b, i, n) (memmove((b)+(i), (b)+(i)+(n), sizeof(*(b))*(bufhdr(b)->len-(n)-(i)), bufhdr(b)->len -= (n)) -#define bufdelswap(b, i) ((b)[i] = bufend(b)[-1], bufhdr(b)->len-=1) - -void* ·bufgrow(void*, vlong, vlong); - -// ----------------------------------------------------------------------------- -// memory allocation - -/* allocator interface */ -typedef struct mem·Allocator { - void *(*alloc)(void *heap, uint n, ulong size); - void (*free)(void *heap, void *ptr); -} mem·Allocator; - -extern mem·Allocator sys·Memory; - -typedef struct mem·Reallocator { - void *(*alloc)(void *iface, uint n, ulong size); - void *(*realloc)(void *iface, void *ptr, uint n, ulong size); - void (*free)(void *iface, void *ptr); -} mem·Reallocator; - -extern mem·Reallocator sys·FullMemory; - -/* simple memory arena */ -typedef struct mem·Arena mem·Arena; - -mem·Arena *mem·makearena(mem·Allocator from, void*); -void *mem·arenaalloc(mem·Arena *A, uint n, ulong size); -void mem·freearena(mem·Arena *A); - -extern mem·Allocator mem·ArenaAllocator; - -/* mem functions */ -int mem·move(void *dst, void *src, uintptr size); -void *mem·findc(void *dst, uintptr len, int c); -int mem·set(void *dst, int val, uintptr size); -int mem·set64(void *dst, uint64 val, uintptr size); - |