Skip to content
Permalink
Browse files
On realloc error, release previously allocated memory
  • Loading branch information
fdmanana committed Mar 27, 2011
1 parent 722f4ae commit 97d1aa21e2b9b53173478528856bfff625b78697
Showing 2 changed files with 8 additions and 0 deletions.
@@ -41,6 +41,7 @@ extern "C" {
#define enif_release_resource_compat enif_release_resource
#define enif_alloc_binary_compat enif_alloc_binary
#define enif_alloc_compat enif_alloc
#define enif_release_binary_compat enif_release_binary
#define enif_free_compat enif_free
#define enif_get_atom_compat enif_get_atom
#define enif_priv_data_compat enif_get_data
@@ -59,6 +60,7 @@ extern "C" {
#define enif_release_resource_compat enif_release_resource
#define enif_alloc_binary_compat enif_alloc_binary
#define enif_realloc_binary_compat enif_realloc_binary
#define enif_release_binary_compat enif_release_binary
#define enif_alloc_compat enif_alloc
#define enif_free_compat enif_free
#define enif_get_atom_compat enif_get_atom
@@ -87,6 +89,9 @@ extern "C" {
#define enif_realloc_binary_compat(E, S, B) \
enif_realloc_binary(S, B)

#define enif_release_binary_compat(E, B) \
enif_release_binary(B)

#define enif_alloc_compat(E, S) \
enif_alloc(S)

@@ -36,6 +36,7 @@ class SnappyNifSink : public snappy::Sink {
public:
SnappyNifSink(ErlNifEnv* e) : env(e), length(0) {
if (!enif_alloc_binary_compat(env, 0, &bin)) {
enif_release_binary_compat(env, &bin);
throw OutOfMem();
}
}
@@ -52,6 +53,7 @@ class SnappyNifSink : public snappy::Sink {
size_t sz = len > ALLOC_SIZE ? len + ALLOC_SIZE - (len % ALLOC_SIZE) : ALLOC_SIZE;

if (!enif_realloc_binary_compat(env, &bin, bin.size + sz)) {
enif_release_binary_compat(env, &bin);
throw OutOfMem();
}
}
@@ -63,6 +65,7 @@ class SnappyNifSink : public snappy::Sink {
if (bin.size > length) {
if (!enif_realloc_binary_compat(env, &bin, length)) {
// shouldn't happen
enif_release_binary_compat(env, &bin);
throw OutOfMem();
}
}

0 comments on commit 97d1aa2

Please sign in to comment.