-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttlmap.c
270 lines (237 loc) · 7.84 KB
/
ttlmap.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "ttlmap.h"
#define TTLMAP_LOCK(map) do {if ((map)->safe == 0) pthread_mutex_lock(&((map)->hlock));} while(0);
#define TTLMAP_UNLOCK(map) do {if ((map)->safe == 0) pthread_mutex_unlock(&((map)->hlock));} while(0);
ttlmap *_ttlmap_new(size_t elsize, size_t cap,
uint64_t seed0, uint64_t seed1,
uint64_t (*hash)(const void *item,
uint64_t seed0, uint64_t seed1),
int (*compare)(const void *a, const void *b,
void *udata),
void (*elfree)(void *item),
void *udata,
timewheel_t *twptr, int safe)
{
ttlmap *map = (ttlmap*)malloc(sizeof(ttlmap));
map->hmap = hashmap_new(elsize, cap, seed0, seed1, hash, compare, elfree, udata);
map->elsize = elsize;
if (twptr == NULL) {
map->tw = tw_new();
tw_runthread(map->tw);
} else {
pthread_mutex_lock(&twptr->ref_lock);
twptr->ref_count++;
pthread_mutex_unlock(&twptr->ref_lock);
map->tw = twptr;
}
if (safe == 0) {
map->safe = 0;
} else {
map->safe = 1;
pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
memcpy(&map->hlock, &init_mutex, sizeof(init_mutex));
}
return map;
}
ttlmap *_ttlmap_new_with_allocator(
void *(*malloc)(size_t),
void *(*realloc)(void *, size_t),
void (*free)(void*),
size_t elsize, size_t cap,
uint64_t seed0, uint64_t seed1,
uint64_t (*hash)(const void *item,
uint64_t seed0, uint64_t seed1),
int (*compare)(const void *a, const void *b,
void *udata),
void (*elfree)(void *item),
void *udata,
timewheel_t *twptr, int safe)
{
ttlmap *map = (ttlmap*)malloc(sizeof(ttlmap));
map->hmap = hashmap_new_with_allocator(malloc, realloc, free, elsize, cap, seed0, seed1, hash, compare, elfree, udata);
if (twptr == NULL) {
map->tw = tw_new();
tw_runthread(map->tw);
} else {
pthread_mutex_lock(&twptr->ref_lock);
twptr->ref_count++;
pthread_mutex_unlock(&twptr->ref_lock);
map->tw = twptr;
}
if (safe == 0) {
map->safe = 0;
} else {
map->safe = 1;
pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
memcpy(&map->hlock, &init_mutex, sizeof(init_mutex));
}
return map;
}
ttlmap *ttlmap_new(size_t elsize, size_t cap,
uint64_t seed0, uint64_t seed1,
uint64_t (*hash)(const void *item,
uint64_t seed0, uint64_t seed1),
int (*compare)(const void *a, const void *b,
void *udata),
void (*elfree)(void *item),
void *udata,
timewheel_t *twptr)
{
return _ttlmap_new(elsize, cap, seed0, seed1, hash, compare, elfree, udata, twptr, 1);
}
ttlmap *ttlmap_new_with_allocator(
void *(*malloc)(size_t),
void *(*realloc)(void *, size_t),
void (*free)(void*),
size_t elsize, size_t cap,
uint64_t seed0, uint64_t seed1,
uint64_t (*hash)(const void *item,
uint64_t seed0, uint64_t seed1),
int (*compare)(const void *a, const void *b,
void *udata),
void (*elfree)(void *item),
void *udata,
timewheel_t *twptr)
{
return _ttlmap_new_with_allocator(malloc, realloc, free, elsize, cap, seed0, seed1, hash, compare, elfree, udata, twptr, 1);
}
ttlmap *ttlmap_new_threadunsafe(size_t elsize, size_t cap,
uint64_t seed0, uint64_t seed1,
uint64_t (*hash)(const void *item,
uint64_t seed0, uint64_t seed1),
int (*compare)(const void *a, const void *b,
void *udata),
void (*elfree)(void *item),
void *udata,
timewheel_t *twptr)
{
return _ttlmap_new(elsize, cap, seed0, seed1, hash, compare, elfree, udata, twptr, 0);
}
ttlmap *ttlmap_new_with_allocator_threadunsafe(
void *(*malloc)(size_t),
void *(*realloc)(void *, size_t),
void (*free)(void*),
size_t elsize, size_t cap,
uint64_t seed0, uint64_t seed1,
uint64_t (*hash)(const void *item,
uint64_t seed0, uint64_t seed1),
int (*compare)(const void *a, const void *b,
void *udata),
void (*elfree)(void *item),
void *udata,
timewheel_t *twptr)
{
return _ttlmap_new_with_allocator(malloc, realloc, free, elsize, cap, seed0, seed1, hash, compare, elfree, udata, twptr, 0);
}
void ttlmap_free(ttlmap *map)
{
hashmap_free(map->hmap);
pthread_mutex_destroy(&map->hlock);
int shouldbefree = 0;
pthread_mutex_lock(&map->tw->ref_lock);
if (--map->tw->ref_count == 0) {
shouldbefree = 1;
}
pthread_mutex_unlock(&map->tw->ref_lock);
if (shouldbefree) {
tw_free(map->tw);
}
free(map);
}
void ttlmap_clear(ttlmap *map, bool update_cap)
{
TTLMAP_LOCK(map);
hashmap_clear(map->hmap, update_cap);
TTLMAP_UNLOCK(map);
}
size_t ttlmap_count(ttlmap *map)
{
size_t ret;
TTLMAP_LOCK(map);
hashmap_count(map->hmap);
TTLMAP_UNLOCK(map);
}
bool ttlmap_oom(ttlmap *map)
{
bool ret;
TTLMAP_LOCK(map);
ret = hashmap_oom(map->hmap);
TTLMAP_UNLOCK(map);
return ret;
}
void *ttlmap_get(ttlmap *map, const void *item)
{
void *ret;
TTLMAP_LOCK(map);
ret = hashmap_get(map->hmap, item);
TTLMAP_UNLOCK(map);
return ret;
}
struct _delitemarg {
ttlmap *map;
void *item;
};
void _deleteitem(void *arg) {
// printf("call delete\n");
struct _delitemarg *darg = arg;
ttlmap_delete(darg->map, darg->item);
free(darg->item);
free(darg);
}
void *ttlmap_set(ttlmap *map, const void *item, int ttl_ms)
{
void *ret;
TTLMAP_LOCK(map);
ret = hashmap_set(map->hmap, item);
TTLMAP_UNLOCK(map);
if (ttl_ms > 0) {
struct _delitemarg *darg = (struct _delitemarg*)malloc(sizeof(struct _delitemarg));
darg->item = malloc(map->elsize);
memcpy(darg->item, item, map->elsize);
darg->map = map;
tw_addtask(map->tw, ttl_ms, _deleteitem, darg);
}
return ret;
}
void *ttlmap_delete(ttlmap *map, void *item)
{
void *ret;
TTLMAP_LOCK(map);
ret = hashmap_delete(map->hmap, item);
TTLMAP_UNLOCK(map);
return ret;
}
void *ttlmap_probe(ttlmap *map, uint64_t position)
{
void *ret;
TTLMAP_LOCK(map);
ret = hashmap_probe(map->hmap, position);
TTLMAP_UNLOCK(map);
return ret;
}
bool ttlmap_scan(ttlmap *map,
bool (*iter)(const void *item, void *udata), void *udata)
{
bool ret;
TTLMAP_LOCK(map);
ret = hashmap_scan(map->hmap, iter, udata);
TTLMAP_UNLOCK(map);
return ret;
}
bool ttlmap_iter(ttlmap *map, size_t *i, void **item)
{
bool ret;
TTLMAP_LOCK(map);
ret = hashmap_iter(map->hmap, i, item);
TTLMAP_UNLOCK(map);
return ret;
}
uint64_t ttlmap_sip(const void *data, size_t len,
uint64_t seed0, uint64_t seed1)
{
return hashmap_sip(data, len, seed0, seed1);
}
uint64_t ttlmap_murmur(const void *data, size_t len,
uint64_t seed0, uint64_t seed1)
{
return hashmap_murmur(data, len, seed0, seed1);
}