Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
externalize memory management and inline last_chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Nov 12, 2015
1 parent 2bceea3 commit 8461d2f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 46 deletions.
9 changes: 5 additions & 4 deletions bindings.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdlib.h>
#include <nan.h>
#include "src/rabin.h"

Expand All @@ -23,15 +24,15 @@ void get_fingerprints(rabin_t *hasher, Local<Array> bufs, Local<Array> lengths)
len -= remaining;
ptr += remaining;

lengths->Set(chunk_idx++, Nan::New<Number>(last_chunk.length));
lengths->Set(chunk_idx++, Nan::New<Number>(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);
}
Expand Down Expand Up @@ -59,7 +60,7 @@ NAN_METHOD(End) {
instance_counter--;
}

delete fingerprint_ptr;
free(fingerprint_ptr);
}

NAN_MODULE_INIT(InitAll) {
Expand Down
36 changes: 4 additions & 32 deletions src/rabin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#define MASK ((1<<AVERAGE_BITS)-1)
#define POL_SHIFT (POLYNOMIAL_DEGREE-8)

struct chunk_t last_chunk;

static bool tables_initialized = false;
static uint64_t mod_table[256];
static uint64_t out_table[256];
Expand Down Expand Up @@ -116,53 +114,27 @@ int rabin_next_chunk(struct rabin_t *h, uint8_t *buf, uint64_t len) {
h->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;
}
}

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;
}
14 changes: 4 additions & 10 deletions src/rabin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8461d2f

Please sign in to comment.