summaryrefslogtreecommitdiffstats
path: root/strstorage.c
diff options
context:
space:
mode:
Diffstat (limited to 'strstorage.c')
-rw-r--r--strstorage.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/strstorage.c b/strstorage.c
new file mode 100644
index 0000000..3cc98b7
--- /dev/null
+++ b/strstorage.c
@@ -0,0 +1,29 @@
+#include <stdlib.h>
+#include "byte.h"
+#include "strstorage.h"
+
+#define PAGESIZE 4096
+
+const char* strstorage_add(const char* s,size_t n) {
+ static char* page=0;
+ static size_t leftonpage=0;
+ if (leftonpage>=n) {
+copyit:
+ byte_copy(page,n,s);
+ s=page;
+ page+=n;
+ leftonpage-=n;
+ } else {
+ if (n>=PAGESIZE/2) {
+ char* tmp=malloc(n);
+ if (!tmp) return 0;
+ byte_copy(tmp,n,s);
+ s=tmp;
+ } else {
+ if (!(page=malloc(PAGESIZE))) return 0;
+ leftonpage=PAGESIZE;
+ goto copyit;
+ }
+ }
+ return s;
+}