aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2020-04-23 21:09:30 -0700
committerNicholas Noll <nbnoll@eml.cc>2020-04-23 21:09:30 -0700
commit4c7870c21b9e645b349ddb77b091543b72c46bf5 (patch)
treef53a77257b62c7ef0729f176daa4c49c5bf57587 /include
parent1a6c99600617f069d6d167fb3d33142a07fe0936 (diff)
feat: made calling signature of interface accepting functions more reliable
Diffstat (limited to 'include')
-rw-r--r--include/libbio.h4
-rw-r--r--include/libn.h78
2 files changed, 59 insertions, 23 deletions
diff --git a/include/libbio.h b/include/libbio.h
index 3cdab4e..2dc3d45 100644
--- a/include/libbio.h
+++ b/include/libbio.h
@@ -25,8 +25,8 @@ typedef struct bio·Tree
} bio·Tree;
/* newick i/o */
-bio·Tree bio·readnewick(io·Peeker stream, mem·Allocator heap);
-error bio·writenewick(bio·Tree tree, io·Putter out);
+bio·Tree bio·readnewick(io·Peeker stream, void*, mem·Allocator heap, void*);
+error bio·writenewick(bio·Tree tree, io·Putter out, void*);
// -----------------------------------------------------------------------
// Sequences
diff --git a/include/libn.h b/include/libn.h
index d2479e4..6547713 100644
--- a/include/libn.h
+++ b/include/libn.h
@@ -1,7 +1,7 @@
#pragma once
// ------------------------------------------------------------------------
-// Standard library
+// standard library
#include <assert.h>
#include <stdlib.h>
@@ -16,7 +16,7 @@
#include <sys/types.h>
// ----------------------------------------------------------------------------
-// Dynamic array.
+// dynamic array
typedef struct bufHdr
{
@@ -48,24 +48,49 @@ void* bufgrow(void*, vlong, vlong);
void _bufpop(void*, int, vlong);
// -----------------------------------------------------------------------------
-// Memory allocation
+// interfaces
+// TODO(nnoll): Think about this idea
+/*
+typedef struct Iface {
+ void* impl;
+ byte fcn[];
+} Iface;
+*/
+// -----------------------------------------------------------------------------
+// memory allocation
+/* allocator interface */
typedef struct mem·Allocator {
- void *(*alloc)(ulong size);
- void *(*realloc)(void *ptr, ulong size);
- void (*free)(void *ptr);
+ void *(*alloc)(void *iface, uint n, ulong size);
+ void (*free)(void *iface, void *ptr);
} mem·Allocator;
-static mem·Allocator mem·sys = { .alloc = &malloc, .realloc = &realloc, .free = &free };
+/* system implementation */
+static
+void ·free(void* _, void* ptr) {
+ return free(ptr);
+}
+
+static
+void *·alloc(void* _, uint n, ulong size) {
+ return malloc(n*size);
+}
+
+// TODO(nnoll): Allow for nil iterfaces?
+static
+mem·Allocator mem·sys = {
+ .alloc = ·alloc,
+ .free = ·free
+};
typedef struct mem·Arena mem·Arena;
-mem·Arena *mem·newarena(mem·Allocator from);
-void *mem·arenaalloc(mem·Arena *A, ulong size);
+mem·Arena *mem·newarena(mem·Allocator from, void*);
+void *mem·arenaalloc(mem·Arena *A, uint n, ulong size);
void mem·freearena(mem·Arena *A);
// -----------------------------------------------------------------------------
-// Co-routines
+// coroutines
typedef struct Coro Coro;
@@ -80,7 +105,7 @@ error coro·free(Coro *c);
#include ".include/str.h"
// -----------------------------------------------------------------------------
-// Maps or dictionaries
+// maps
#include ".include/map.h"
@@ -99,25 +124,25 @@ Stream *io·open(byte *name, byte *mode);
error io·close(Stream *s);
byte io·getbyte(Stream *s);
error io·ungetbyte(Stream *s, byte c);
-vlong io·read(Stream *s, int sz, int n, void *buf);
+int io·read(Stream *s, int sz, int n, void *buf);
int io·readln(Stream *s, int n, byte* buf);
error io·putbyte(Stream *s, byte c);
int io·putstring(Stream *s, string str);
-vlong io·write(Stream *s, int sz, int n, void *buf);
+int io·write(Stream *s, int sz, int n, void *buf);
int io·flush(Stream *s);
int io·seek(Stream *s, long off, enum SeekPos origin);
-/* Generic I/O interfaces */
+/* generic I/O interfaces */
typedef struct io·Reader
{
- int (*read)(int n, void *buf);
+ int (*read)(void*, int sz, int n, void *buf);
} io·Reader;
typedef struct io·Peeker
{
- byte (*get)(void);
- error (*unget)(byte);
+ byte (*get)(void*);
+ error (*unget)(void*, byte);
} io·Peeker;
typedef struct io·FullReader
@@ -128,13 +153,13 @@ typedef struct io·FullReader
typedef struct io·Writer
{
- int (*write)(int n, void *buf);
+ int (*write)(void*, int sz, int n, void *buf);
} io·Writer;
typedef struct io·Putter
{
- error (*put)(byte);
- int (*putstr)(string);
+ error (*put)(void*, byte);
+ int (*putstr)(void*, string);
} io·Putter;
typedef struct io·FullWriter
@@ -150,7 +175,18 @@ typedef struct io·ReadWriter
} io·ReadWriter;
// -----------------------------------------------------------------------------
-// Buffered I/O
+// libflate
+
+typedef struct gz·Reader gz·Reader;
+typedef struct gz·Writer gz·Writer;
+
+gz·Reader *gz·newreader(io·Reader rdr, void* r, mem·Allocator mem, void* m);
+int gz·read(gz·Reader *rdr, int sz, int n, void *buf);
+error gz·freereader(gz·Reader *rdr);
+
+gz·Writer *gz·newwriter(io·Writer wtr, void* w, mem·Allocator mem, void* m);
+error gz·freewriter(gz·Writer *wtr);
+int gz·write(gz·Writer *wtr, int sz, int n, void *buf);
// -----------------------------------------------------------------------------
// Error handling functions.