summaryrefslogtreecommitdiffstats
path: root/strstorage.c
blob: 3cc98b7ea1b4fa20c5ddec41a31d1c36945d70e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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;
}