Prefer binary realloc to self-managed buffers

Use erlang's allocator and simplify the code by writing into the
buffer we're going to give back anyway.

Also introduce a function to NUL-terminate iolist buffers, which can
avoid memory copying.
This commit is contained in:
Carlos Martín Nieto
2013-05-02 13:09:51 +02:00
parent ef7d2d5490
commit 616a358dc7
6 changed files with 33 additions and 89 deletions

View File

@@ -123,6 +123,16 @@ geef_oom(ErlNifEnv *env)
return enif_make_tuple2(env, atoms.error, atoms.enomem);
}
int geef_terminate_binary(ErlNifBinary *bin)
{
if (!enif_realloc_binary(bin, bin->size + 1))
return 0;
bin->data[bin->size - 1] = '\0';
return 1;
}
static ErlNifFunc geef_funcs[] =
{
{"repository_init", 2, geef_repository_init},