Skip to content

Commit

Permalink
scratch space: use single allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
apoelstra committed May 25, 2019
1 parent 40839e2 commit 92a48a7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/scratch.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
/* The typedef is used internally; the struct name is used in the public API
* (where it is exposed as a different typedef) */
typedef struct secp256k1_scratch_space_struct {
void *data[SECP256K1_SCRATCH_MAX_FRAMES];
void *data;
void *current_frame;
size_t offset[SECP256K1_SCRATCH_MAX_FRAMES];
size_t frame_size[SECP256K1_SCRATCH_MAX_FRAMES];
size_t frame;
size_t max_size;
const secp256k1_callback* error_callback;
} secp256k1_scratch;

static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t max_size);
Expand Down
15 changes: 7 additions & 8 deletions src/scratch_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* err
secp256k1_scratch* ret = (secp256k1_scratch*)checked_malloc(error_callback, sizeof(*ret));
if (ret != NULL) {
memset(ret, 0, sizeof(*ret));
ret->data = (secp256k1_scratch*)checked_malloc(error_callback, max_size);
ret->max_size = max_size;
ret->error_callback = error_callback;
}
return ret;
}

static void secp256k1_scratch_destroy(secp256k1_scratch* scratch) {
if (scratch != NULL) {
VERIFY_CHECK(scratch->frame == 0);
free(scratch->data);
free(scratch);
}
}
Expand All @@ -44,10 +45,8 @@ static int secp256k1_scratch_allocate_frame(secp256k1_scratch* scratch, size_t n

if (n <= secp256k1_scratch_max_allocation(scratch, objects)) {
n += objects * ALIGNMENT;
scratch->data[scratch->frame] = checked_malloc(scratch->error_callback, n);
if (scratch->data[scratch->frame] == NULL) {
return 0;
}
scratch->current_frame = scratch->data;
scratch->data = (void *) ((char *) scratch->data + n);
scratch->frame_size[scratch->frame] = n;
scratch->offset[scratch->frame] = 0;
scratch->frame++;
Expand All @@ -59,8 +58,8 @@ static int secp256k1_scratch_allocate_frame(secp256k1_scratch* scratch, size_t n

static void secp256k1_scratch_deallocate_frame(secp256k1_scratch* scratch) {
VERIFY_CHECK(scratch->frame > 0);
scratch->frame -= 1;
free(scratch->data[scratch->frame]);
scratch->frame--;
scratch->data = (void *) ((char *) scratch->data - scratch->frame_size[scratch->frame]);
}

static void *secp256k1_scratch_alloc(secp256k1_scratch* scratch, size_t size) {
Expand All @@ -71,7 +70,7 @@ static void *secp256k1_scratch_alloc(secp256k1_scratch* scratch, size_t size) {
if (scratch->frame == 0 || size + scratch->offset[frame] > scratch->frame_size[frame]) {
return NULL;
}
ret = (void *) ((unsigned char *) scratch->data[frame] + scratch->offset[frame]);
ret = (void *) ((char *) scratch->current_frame + scratch->offset[frame]);
memset(ret, 0, size);
scratch->offset[frame] += size;

Expand Down

0 comments on commit 92a48a7

Please sign in to comment.