-
Notifications
You must be signed in to change notification settings - Fork 13
/
stringpool.h
58 lines (44 loc) · 1.76 KB
/
stringpool.h
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
#ifndef WP_STRINGPOOL_H_
#define WP_STRINGPOOL_H_
// whistlepig string pool
// (c) 2011 William Morgan. See COPYING for license terms.
//
// a string pool. adds strings to a big blob and returns an int which can be
// used to look them up later. in other words, an int->string mapping, where
// you provide the string and we'll give you an int.
//
// does no duplicate detection, if you add the same string twice, you will
// get two different ints and you will have wasted memory.
//
// this is used by stringmap to maintain a bidirectional string<->int mapping
// and is not really used directly.
//
// int 0 is a special case for the null string. passing in invalid ints (i.e.
// ints i didn't return) will result in garbage data.
//
// like termhash and stringmap, has a slightly funny API that is designed to
// work on a pre-allocated chunk of memory rather than allocate any of its own.
#include <stdint.h>
#define INITIAL_POOL_SIZE 2048
typedef struct stringpool {
uint32_t size, next;
char pool[];
} stringpool;
// API methods
// public: create a stringpool
void stringpool_init(stringpool* p);
// public: add a string, returning an int
uint32_t stringpool_add(stringpool* p, const char* s);
// public: does this stringpool need to be increased?
int stringpool_needs_bump(stringpool* p);
// public: increase the size of the stringpool
void stringpool_bump_size(stringpool* p);
// public: given an id, return the string
char* stringpool_lookup(stringpool* p, uint32_t id);
// public: returns the byte size of the pool
uint32_t stringpool_size(stringpool* p);
// public: returns the initial byte size for an empty pool
uint32_t stringpool_initial_size();
// public: returns the byte size for the next larger version of a pool
uint32_t stringpool_next_size(stringpool* p);
#endif