forked from jarjin/tolua_runtime_V2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringpool.c
60 lines (53 loc) · 1.33 KB
/
stringpool.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "alloc.h"
#include <stdlib.h>
#include <string.h>
#define PAGE_SIZE 256
struct _stringpool {
char * buffer;
size_t len;
struct _stringpool *next;
};
struct _stringpool *
_pbcS_new(void) {
struct _stringpool * ret = (struct _stringpool *)malloc(sizeof(struct _stringpool) + PAGE_SIZE);
ret->buffer = (char *)(ret + 1);
ret->len = 0;
ret->next = NULL;
return ret;
}
void
_pbcS_delete(struct _stringpool *pool) {
while(pool) {
struct _stringpool *next = pool->next;
free(pool);
pool = next;
}
}
const char *
_pbcS_build(struct _stringpool *pool, const char * str , int sz) {
size_t s = sz + 1;
if (s < PAGE_SIZE - pool->len) {
char * ret = pool->buffer + pool->len;
memcpy(pool->buffer + pool->len, str, s);
pool->len += s;
return ret;
}
if (s > PAGE_SIZE) {
struct _stringpool * next = (struct _stringpool *)malloc(sizeof(struct _stringpool) + s);
next->buffer = (char *)(next + 1);
memcpy(next->buffer, str, s);
next->len = s;
next->next = pool->next;
pool->next = next;
return next->buffer;
}
struct _stringpool *next = (struct _stringpool *)malloc(sizeof(struct _stringpool) + PAGE_SIZE);
next->buffer = pool->buffer;
next->next = pool->next;
next->len = pool->len;
pool->next = next;
pool->buffer = (char *)(next + 1);
memcpy(pool->buffer, str, s);
pool->len = s;
return pool->buffer;
}