-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrjd_strpool.h
165 lines (126 loc) · 4.07 KB
/
rjd_strpool.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#pragma once
#define RJD_STRPOOL_H 1
struct rjd_strpool
{
struct rjd_dict storage;
};
struct rjd_strref;
struct rjd_strpool rjd_strpool_init(struct rjd_mem_allocator* allocator, size_t initial_capacity);
void rjd_strpool_free(struct rjd_strpool* pool);
struct rjd_strref* rjd_strpool_add(struct rjd_strpool* pool, const char* str);
struct rjd_strref* rjd_strpool_addf(struct rjd_strpool* pool, const char* fmt, ...);
struct rjd_strref* rjd_strpool_addv(struct rjd_strpool* pool, const char* fmt, va_list args);
struct rjd_strref* rjd_strpool_addl(struct rjd_strpool* pool, const char* inline_string, size_t length); // for non-null-terminated strings
void rjd_strref_release(struct rjd_strref* ref);
const char* rjd_strref_str(const struct rjd_strref* ref);
uint32_t rjd_strref_length(const struct rjd_strref* ref);
#if RJD_IMPL
struct rjd_strref
{
const char* str;
struct rjd_strpool* owner;
int32_t refcount; // TODO atomic
uint32_t length;
};
static struct rjd_strref* rjd_strpool_addimpl(struct rjd_strpool* pool, const char* str);
struct rjd_strpool rjd_strpool_init(struct rjd_mem_allocator* allocator, size_t initial_capacity)
{
RJD_ASSERT(allocator);
struct rjd_strpool pool = { rjd_dict_init(allocator, initial_capacity * 2) };
return pool;
}
void rjd_strpool_free(struct rjd_strpool* pool)
{
RJD_ASSERT(pool);
void** refs = pool->storage.values;
for (uint32_t i = 0; i < rjd_array_count(refs); ++i) {
if (refs[i]) {
struct rjd_strref* ref = refs[i];
rjd_mem_free(ref); // struct and string are part of the same allocation block
}
}
rjd_dict_free(&pool->storage);
}
struct rjd_strref* rjd_strpool_add(struct rjd_strpool* pool, const char* str)
{
RJD_ASSERT(pool);
return rjd_strpool_addimpl(pool, str);
}
struct rjd_strref* rjd_strpool_addf(struct rjd_strpool* pool, const char* format, ...)
{
RJD_ASSERT(pool);
RJD_ASSERT(format);
struct rjd_strref* ref = NULL;
va_list args;
va_start(args, format);
ref = rjd_strpool_addv(pool, format, args);
va_end(args);
return ref;
}
struct rjd_strref* rjd_strpool_addv(struct rjd_strpool* pool, const char* format, va_list args)
{
RJD_ASSERT(pool);
RJD_ASSERT(format);
struct rjd_strref* ref = NULL;
RJD_STRBUF_SCOPED(buffer, pool->storage.allocator, {
rjd_strbuf_appendv(&buffer, format, args);
ref = rjd_strpool_addimpl(pool, rjd_strbuf_str(&buffer));
});
return ref;
}
struct rjd_strref* rjd_strpool_addl(struct rjd_strpool* pool, const char* inline_string, size_t length)
{
RJD_ASSERT(pool);
RJD_ASSERT(inline_string);
struct rjd_strref* ref = NULL;
RJD_STRBUF_SCOPED(buffer, pool->storage.allocator, {
rjd_strbuf_appendl(&buffer, inline_string, (uint32_t)length);
ref = rjd_strpool_addimpl(pool, rjd_strbuf_str(&buffer));
});
return ref;
}
void rjd_strref_release(struct rjd_strref* ref)
{
RJD_ASSERT(ref);
struct rjd_strpool* pool = ref->owner;
struct rjd_hash64 hash = rjd_hash64_data((const uint8_t*)ref->str, -1);
RJD_ASSERTMSG(rjd_dict_get(&pool->storage, hash) == ref, "ref was not contained in string pool");
--ref->refcount;
if (ref->refcount <= 0) {
rjd_mem_free(ref); // struct and string are part of the same allocation block
rjd_dict_erase(&pool->storage, hash);
}
}
const char* rjd_strref_str(const struct rjd_strref* ref)
{
RJD_ASSERT(ref);
return ref->str;
}
uint32_t rjd_strref_length(const struct rjd_strref* ref)
{
RJD_ASSERT(ref);
return ref->length;
}
static struct rjd_strref* rjd_strpool_addimpl(struct rjd_strpool* pool, const char* str)
{
RJD_ASSERT(pool);
if (!str) {
return NULL;
}
struct rjd_hash64 hash = rjd_hash64_data((const uint8_t*)str, -1);
struct rjd_strref* ref = rjd_dict_get(&pool->storage, hash);
if (!ref) {
uint8_t* mem = rjd_mem_alloc_array(uint8_t, sizeof(struct rjd_strref) + strlen(str) + 1, pool->storage.allocator);
ref = (struct rjd_strref*)mem;
char* copied_str = (char*)(mem + sizeof(struct rjd_strref));
strcpy(copied_str, str);
ref->str = copied_str;
ref->owner = pool;
ref->refcount = 0;
ref->length = (uint32_t)strlen(ref->str);
rjd_dict_insert(&pool->storage, hash, ref);
}
++ref->refcount;
return ref;
}
#endif // RJD_IMPL