Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix non-deterministic to_bin #20

Merged
merged 2 commits into from
Jan 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion c_src/xor_filter_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
#include "erl_nif.h"
#include "ewok.h"

#define malloc(size) enif_alloc(size)
// This forward declaration is needed because we're using this
// function to override xorfilter's use of malloc.
void * xor_nif_zalloc(size_t size);
#define malloc(size) xor_nif_zalloc(size)
#define free(size) enif_free(size)
#include "xorfilter.h"

Expand Down Expand Up @@ -46,6 +49,24 @@ pack_le_u64(uint8_t * dst, uint64_t val) {
dst[7] = (val >> 56) & 0xff;
}

// Allocates 'size' zeroized bytes from the VM.
//
// Erlang does not provide a malloc like function which returns
// zeroized memory. That's not usually a problem, as you can always
// call memset after allocation memory. However, we're overriding the
// xorfilter's use malloc by redefining it before including
// xorfilter.h, leaving us no other option than to the VMs allocator
// and memset into this function.
void *
xor_nif_zalloc(size_t size)
{
void * mem = enif_alloc(size);
if (mem) {
memset(mem, 0, size);
}
return mem;
}

void
destroy_exor_t_resource(ErlNifEnv* env, void* obj)
{
Expand Down
10 changes: 8 additions & 2 deletions test/exor_filter_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ xor8_serialization() ->
?assertEqual(false, xor8:contain(BinFilter, "test4")),
Filter2 = xor8:from_bin(BinFilter),
?assertEqual(true, xor8:contain(Filter2, "test1")),
?assertEqual(false, xor8:contain(Filter2, "test4")).
?assertEqual(false, xor8:contain(Filter2, "test4")),
Filter3 = xor8:new(["test1", "test2", "test3"]),
BinFilter2 = xor8:to_bin(Filter3),
?assertEqual(BinFilter, BinFilter2).

xor8_incremental_builder() ->
Filter0 = xor8:new_empty(),
Expand Down Expand Up @@ -403,7 +406,10 @@ xor16_serialization() ->
?assertEqual(false, xor16:contain(BinFilter, "test4")),
Filter2 = xor16:from_bin(BinFilter),
?assertEqual(true, xor16:contain(Filter2, "test1")),
?assertEqual(false, xor16:contain(Filter2, "test4")).
?assertEqual(false, xor16:contain(Filter2, "test4")),
Filter3 = xor16:new(["test1", "test2", "test3"]),
BinFilter2 = xor16:to_bin(Filter3),
?assertEqual(BinFilter, BinFilter2).

xor16_incremental_builder() ->
Filter0 = xor16:new_empty(),
Expand Down