Skip to content
Permalink
Browse files
Removed compatibility with R13B03 and fixed build on R13B04
R13B03 doesn't provide enif_realloc_binary, so drop support for this
release.

R13B04 enif_alloc_binary and enif_realloc_binary require a first
argument of type ErlNifEnv* in R13B04, which was not being passed and
caused compilation failure on this release.
  • Loading branch information
fdmanana committed Mar 26, 2011
1 parent 003a5cb commit 0857ad1c8b92ae77dad607990256fcdd6e943de9
Showing 1 changed file with 13 additions and 28 deletions.
@@ -22,6 +22,10 @@
#include "google-snappy/snappy.h"
#include "google-snappy/snappy-sinksource.h"

#ifdef OTP_R13B03
#error OTP R13B03 not supported. Upgrade to R13B04 or later.
#endif


const size_t ALLOC_SIZE = 2048;

@@ -30,7 +34,7 @@ class OutOfMem {};

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

if (!enif_realloc_binary(&bin, bin.size + sz)) {
if (!enif_realloc_binary(env, &bin, bin.size + sz)) {
throw OutOfMem();
}
}
@@ -57,7 +61,7 @@ class SnappyNifSink : public snappy::Sink {

ErlNifBinary& getBin() {
if (bin.size > length) {
if (!enif_realloc_binary(&bin, length)) {
if (!enif_realloc_binary(env, &bin, length)) {
// shouldn't happen
throw OutOfMem();
}
@@ -66,30 +70,26 @@ class SnappyNifSink : public snappy::Sink {
}

private:
ErlNifEnv* env;
ErlNifBinary bin;
size_t length;
};


extern "C" {

#ifdef OTP_R13B03
ERL_NIF_TERM snappy_compress(ErlNifEnv* env, ERL_NIF_TERM binary) {
#else
ERL_NIF_TERM snappy_compress(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
ERL_NIF_TERM binary = argv[0];
#endif
ErlNifBinary input;

if (!enif_inspect_binary(env, binary, &input)) {
if (!enif_inspect_binary(env, argv[0], &input)) {
return enif_make_badarg(env);
}

snappy::ByteArraySource source(reinterpret_cast<const char *>(input.data),
input.size);

try {
SnappyNifSink sink;
SnappyNifSink sink(env);

snappy::Compress(&source, &sink);

@@ -104,15 +104,10 @@ extern "C" {
}


#ifdef OTP_R13B03
ERL_NIF_TERM snappy_decompress(ErlNifEnv* env, ERL_NIF_TERM binary) {
#else
ERL_NIF_TERM snappy_decompress(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
ERL_NIF_TERM binary = argv[0];
#endif
ErlNifBinary input;

if (!enif_inspect_binary(env, binary, &input)) {
if (!enif_inspect_binary(env, argv[0], &input)) {
return enif_make_badarg(env);
}

@@ -152,15 +147,10 @@ extern "C" {
}


#ifdef OTP_R13B03
ERL_NIF_TERM snappy_get_uncompressed_length(ErlNifEnv* env, ERL_NIF_TERM binary) {
#else
ERL_NIF_TERM snappy_get_uncompressed_length(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
ERL_NIF_TERM binary = argv[0];
#endif
ErlNifBinary input;

if (!enif_inspect_binary(env, binary, &input)) {
if (!enif_inspect_binary(env, argv[0], &input)) {
return enif_make_badarg(env);
}

@@ -180,15 +170,10 @@ extern "C" {
}


#ifdef OTP_R13B03
ERL_NIF_TERM snappy_is_valid_compressed_buffer(ErlNifEnv* env, ERL_NIF_TERM binary) {
#else
ERL_NIF_TERM snappy_is_valid_compressed_buffer(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
ERL_NIF_TERM binary = argv[0];
#endif
ErlNifBinary input;

if (!enif_inspect_binary(env, binary, &input)) {
if (!enif_inspect_binary(env, argv[0], &input)) {
return enif_make_badarg(env);
}

0 comments on commit 0857ad1

Please sign in to comment.