/
esnappy_nif.cpp
450 lines (347 loc) · 10.2 KB
/
esnappy_nif.cpp
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include <erl_nif.h>
#include <cstdio>
#include "exc.hpp"
#include "async_queue.h"
#include "snappy.h"
#include "snappy-sinksource.h"
static const size_t ALLOC_SIZE = 2048;
class Sink: public snappy::Sink {
public:
Sink(): used(0)
{
if (!enif_alloc_binary(0, &bin)) {
enif_release_binary(&bin);
THROW_EXC("enif_alloc_binary() failed");
}
}
void Append(const char *data, size_t n)
{
if (data != reinterpret_cast<const char*>(bin.data + used)) {
memcpy(bin.data + used, data, n);
}
used += n;
}
char* GetAppendBuffer(size_t len, char*)
{
size_t allocated = bin.size;
if ((used + len) > allocated) {
size_t sz = len > ALLOC_SIZE ? len + ALLOC_SIZE - (len % ALLOC_SIZE) : ALLOC_SIZE;
if (!enif_realloc_binary(&bin, allocated + sz)) {
enif_release_binary(&bin);
THROW_EXC("enif_realloc_binary() failed");
}
}
return reinterpret_cast<char*>(bin.data + used);
}
ErlNifBinary& GetBinary()
{
size_t allocated = bin.size;
if (allocated > used) {
if (!enif_realloc_binary(&bin, used)) {
enif_release_binary(&bin);
THROW_EXC("enif_realloc_binary() failed");
}
}
return bin;
}
private:
ErlNifBinary bin;
size_t used;
};
extern "C" {
typedef struct {
async_queue_t *queue;
ErlNifThreadOpts *topts;
ErlNifTid tid;
} ctx_t;
typedef enum {
UNKNOWN,
COMPRESS,
DECOMPRESS,
SHUTDOWN
} task_type_t;
typedef struct {
task_type_t type;
ErlNifEnv *env;
ErlNifPid pid;
ERL_NIF_TERM ref;
ErlNifBinary data;
} task_t;
static ErlNifResourceType *res_type;
static ERL_NIF_TERM atom_ok;
static ERL_NIF_TERM atom_error;
static void cleanup_task(task_t **task);
static task_t* init_task(task_type_t type, ERL_NIF_TERM ref, ErlNifPid pid,
ERL_NIF_TERM orig_term);
static task_t* init_empty_task(task_type_t type);
static ctx_t* init_ctx();
static ERL_NIF_TERM compress(task_t *task);
static ERL_NIF_TERM decompress(task_t *task);
static void* worker(void *arg);
void resource_dtor(ErlNifEnv *env, void *obj);
int load(ErlNifEnv *env, void **priv, ERL_NIF_TERM load_info);
void
cleanup_task(task_t **task)
{
if ((*task)->env != NULL) {
enif_free_env((*task)->env);
}
enif_free(*task);
*task = NULL;
}
task_t*
init_empty_task(task_type_t type)
{
task_t *task;
task = static_cast<task_t*>(enif_alloc(sizeof(*task)));
if (task == NULL) {
goto done;
}
memset(task, 0, sizeof(*task));
task->type = type;
done:
return task;
}
task_t*
init_task(task_type_t type, ERL_NIF_TERM ref, ErlNifPid pid,
ERL_NIF_TERM orig_term)
{
ERL_NIF_TERM term;
task_t *task;
task = init_empty_task(type);
task->pid = pid;
task->env = enif_alloc_env();
if (task->env == NULL) {
cleanup_task(&task);
goto done;
}
term = enif_make_copy(task->env, orig_term);
if (!enif_inspect_iolist_as_binary(task->env, term, &task->data)) {
cleanup_task(&task);
goto done;
}
task->ref = enif_make_copy(task->env, ref);
done:
return task;
}
ERL_NIF_TERM
compress(task_t *task)
{
ERL_NIF_TERM result;
snappy::ByteArraySource source(reinterpret_cast<const char*>(task->data.data), task->data.size);
Sink sink;
try {
snappy::Compress(&source, &sink);
result = enif_make_tuple3(task->env, atom_ok, task->ref,
enif_make_binary(task->env, &sink.GetBinary()));
} catch (std::exception &e) {
result = enif_make_tuple2(task->env, atom_error,
enif_make_string(task->env, "Failed to compress",
ERL_NIF_LATIN1));
}
return result;
}
ERL_NIF_TERM
decompress(task_t *task)
{
size_t len;
bool status;
ERL_NIF_TERM result;
ErlNifBinary bin;
len = -1;
status = snappy::GetUncompressedLength(
reinterpret_cast<const char *>(task->data.data),
task->data.size, &len);
if (!status) {
result = enif_make_tuple2(task->env, atom_error,
enif_make_string(task->env, "Data is not compressed",
ERL_NIF_LATIN1));
goto done;
}
if (!enif_alloc_binary(len, &bin)) {
result = enif_make_tuple2(task->env, atom_error,
enif_make_string(task->env, "Couldn't allocate memory",
ERL_NIF_LATIN1));
goto done;
}
status = snappy::RawUncompress(
reinterpret_cast<const char*>(task->data.data),
task->data.size, reinterpret_cast<char*>(bin.data));
if (!status) {
result = enif_make_tuple2(task->env, atom_error,
enif_make_string(task->env, "Failed to decompress",
ERL_NIF_LATIN1));
goto done;
}
result = enif_make_tuple3(task->env, atom_ok, task->ref,
enif_make_binary(task->env, &bin));
done:
return result;
}
void*
worker(void *arg)
{
ctx_t *ctx;
task_t *task;
ERL_NIF_TERM result;
ctx = static_cast<ctx_t*>(arg);
while (true) {
task = static_cast<task_t*>(async_queue_pop(ctx->queue));
if (task->type == COMPRESS) {
result = compress(task);
} else if (task->type == DECOMPRESS) {
result = decompress(task);
} else if (task->type == SHUTDOWN) {
break;
} else {
errx(1, "Unexpected task type: %i", task->type);
}
enif_send(NULL, &task->pid, task->env, result);
cleanup_task(&task);
}
cleanup_task(&task);
return NULL;
}
ctx_t*
init_ctx()
{
int status;
ctx_t *ctx = static_cast<ctx_t*>(enif_alloc_resource(res_type, sizeof(*ctx)));
if (ctx == NULL) {
goto done;
}
ctx->queue = async_queue_create();
ctx->topts = enif_thread_opts_create(const_cast<char*>("snappy_thread_opts"));
status = enif_thread_create(const_cast<char*>("worker"),
&ctx->tid, worker, ctx, ctx->topts);
if (status != 0) {
enif_release_resource(ctx);
ctx = NULL;
goto done;
}
done:
return ctx;
}
void
resource_dtor(ErlNifEnv*, void *obj)
{
ctx_t *ctx = static_cast<ctx_t*>(obj);
task_t *task = init_empty_task(SHUTDOWN);
void *result = NULL;
async_queue_push(ctx->queue, static_cast<void*>(task));
enif_thread_join(ctx->tid, &result);
async_queue_destroy(ctx->queue);
enif_thread_opts_destroy(ctx->topts);
}
int
load(ErlNifEnv *env, void **, ERL_NIF_TERM)
{
const char *mod = "erlang-snappy";
const char *name = "nif_resource";
ErlNifResourceFlags flags = static_cast<ErlNifResourceFlags>(ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER);
res_type = enif_open_resource_type(env, mod, name, resource_dtor, flags, NULL);
if (res_type == NULL) {
return -1;
}
atom_ok = enif_make_atom(env, "ok");
atom_error = enif_make_atom(env, "error");
return 0;
}
ERL_NIF_TERM
snappy_create_ctx(ErlNifEnv *env, int, const ERL_NIF_TERM[])
{
ERL_NIF_TERM rv;
ctx_t *ctx;
ctx = init_ctx();
if (ctx == NULL) {
return enif_make_tuple2(env, atom_error,
enif_make_string(env, "Failed to create context",
ERL_NIF_LATIN1));
}
rv = enif_make_resource(env, ctx);
enif_release_resource(ctx);
return enif_make_tuple2(env, atom_ok, rv);
}
ERL_NIF_TERM
snappy_compress_impl(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
ctx_t *ctx;
task_t *task;
ErlNifPid pid;
if (argc != 4) {
return enif_make_badarg(env);
}
if (!enif_get_resource(env, argv[0], res_type,
reinterpret_cast<void**>(&ctx))) {
return enif_make_badarg(env);
}
if (!enif_is_ref(env, argv[1])) {
return enif_make_tuple2(env, atom_error,
enif_make_string(env, "Second arg. is not a reference",
ERL_NIF_LATIN1));
}
if (!enif_get_local_pid(env, argv[2], &pid)) {
return enif_make_tuple2(env, atom_error,
enif_make_string(env, "Third arg. is not a pid of local process",
ERL_NIF_LATIN1));
}
if (!(enif_is_binary(env, argv[3]) ||
enif_is_list(env, argv[3]))) {
return enif_make_tuple2(env, atom_error,
enif_make_string(env, "Forth arg. is not a binary or a list",
ERL_NIF_LATIN1));
}
task = init_task(COMPRESS, argv[1], pid, argv[3]);
if (!task) {
return enif_make_tuple2(env, atom_error,
enif_make_string(env, "Failed to create a task",
ERL_NIF_LATIN1));
}
async_queue_push(ctx->queue, static_cast<void*>(task));
return atom_ok;
}
ERL_NIF_TERM
snappy_decompress_impl(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
ctx_t *ctx;
task_t *task;
ErlNifPid pid;
if (argc != 4) {
return enif_make_badarg(env);
}
if (!enif_get_resource(env, argv[0], res_type,
reinterpret_cast<void**>(&ctx))) {
return enif_make_badarg(env);
}
if (!enif_is_ref(env, argv[1])) {
return enif_make_tuple2(env, atom_error,
enif_make_string(env, "Second arg. is not a reference",
ERL_NIF_LATIN1));
}
if (!enif_get_local_pid(env, argv[2], &pid)) {
return enif_make_tuple2(env, atom_error,
enif_make_string(env, "Third arg. is not a pid of local process",
ERL_NIF_LATIN1));
}
if (!(enif_is_binary(env, argv[3]) ||
enif_is_list(env, argv[3]))) {
return enif_make_tuple2(env, atom_error,
enif_make_string(env, "Forth arg. is not a binary",
ERL_NIF_LATIN1));
}
task = init_task(DECOMPRESS, argv[1], pid, argv[3]);
if (!task) {
return enif_make_tuple2(env, atom_error,
enif_make_string(env, "Failed to create a task",
ERL_NIF_LATIN1));
}
async_queue_push(ctx->queue, static_cast<void*>(task));
return atom_ok;
}
static ErlNifFunc nif_funcs[] = {
{"create_ctx", 0, snappy_create_ctx},
{"compress_impl", 4, snappy_compress_impl},
{"decompress_impl", 4, snappy_decompress_impl}
};
ERL_NIF_INIT(esnappy, nif_funcs, &load, NULL, NULL, NULL);
} // extern "C"