From 8461d2f7c7685044649b2288bbcee0978d65995b Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Wed, 11 Nov 2015 16:21:04 -0800 Subject: [PATCH] externalize memory management and inline last_chunk --- bindings.cc | 9 +++++---- src/rabin.cc | 36 ++++-------------------------------- src/rabin.h | 14 ++++---------- 3 files changed, 13 insertions(+), 46 deletions(-) diff --git a/bindings.cc b/bindings.cc index 1ad4bba..c6259d4 100644 --- a/bindings.cc +++ b/bindings.cc @@ -1,3 +1,4 @@ +#include #include #include "src/rabin.h" @@ -23,15 +24,15 @@ void get_fingerprints(rabin_t *hasher, Local bufs, Local lengths) len -= remaining; ptr += remaining; - lengths->Set(chunk_idx++, Nan::New(last_chunk.length)); + lengths->Set(chunk_idx++, Nan::New(hasher->chunk_length)); } } } NAN_METHOD(Initialize) { if (instance_counter >= 1024) return Nan::ThrowError("the value of instance_counter is too damn high"); - struct rabin_t *hasher; - hasher = rabin_init(); + struct rabin_t *hasher = (struct rabin_t *) malloc(sizeof(struct rabin_t)); + rabin_init(hasher); instances[instance_counter++] = hasher; info.GetReturnValue().Set(instance_counter - 1); } @@ -59,7 +60,7 @@ NAN_METHOD(End) { instance_counter--; } - delete fingerprint_ptr; + free(fingerprint_ptr); } NAN_MODULE_INIT(InitAll) { diff --git a/src/rabin.cc b/src/rabin.cc index eba7d12..d5c990c 100644 --- a/src/rabin.cc +++ b/src/rabin.cc @@ -6,8 +6,6 @@ #define MASK ((1<pos++; if ((h->count >= MINSIZE && ((h->digest & MASK) == 0)) || h->count >= MAXSIZE) { - last_chunk.start = h->start; - last_chunk.length = h->count; - last_chunk.cut_fingerprint = h->digest; + h->chunk_start = h->start; + h->chunk_length = h->count; + h->chunk_cut_fingerprint = h->digest; - // keep position - uint64_t pos = h->pos; rabin_reset(h); - h->start = pos; - h->pos = pos; - return i+1; } } @@ -133,36 +126,15 @@ int rabin_next_chunk(struct rabin_t *h, uint8_t *buf, uint64_t len) { return -1; } -struct rabin_t *rabin_init(void) { +struct rabin_t *rabin_init(struct rabin_t *h) { if (!tables_initialized) { calc_tables(); tables_initialized = true; } - struct rabin_t *h; - - if ((h = (struct rabin_t *) malloc(sizeof(struct rabin_t))) == NULL) { - errx(1, "malloc()"); - } - h->pos = 0; h->start = 0; rabin_reset(h); return h; } - - -struct chunk_t *rabin_finalize(struct rabin_t *h) { - if (h->count == 0) { - last_chunk.start = 0; - last_chunk.length = 0; - last_chunk.cut_fingerprint = 0; - return NULL; - } - - last_chunk.start = h->start; - last_chunk.length = h->count; - last_chunk.cut_fingerprint = h->digest; - return &last_chunk; -} diff --git a/src/rabin.h b/src/rabin.h index 8168ec1..d9b4649 100644 --- a/src/rabin.h +++ b/src/rabin.h @@ -17,21 +17,15 @@ struct rabin_t { uint64_t pos; uint64_t start; uint64_t digest; + uint64_t chunk_start; + uint64_t chunk_length; + uint64_t chunk_cut_fingerprint; }; -struct chunk_t { - uint64_t start; - uint64_t length; - uint64_t cut_fingerprint; -}; - -extern struct chunk_t last_chunk; - -struct rabin_t *rabin_init(void); +struct rabin_t *rabin_init(struct rabin_t *h); void rabin_reset(struct rabin_t *h); void rabin_slide(struct rabin_t *h, uint8_t b); void rabin_append(struct rabin_t *h, uint8_t b); int rabin_next_chunk(struct rabin_t *h, uint8_t *buf, uint64_t len); -struct chunk_t *rabin_finalize(struct rabin_t *h); #endif