scratch space: use single allocation

This commit is contained in:
Andrew Poelstra 2019-03-13 22:19:41 +00:00
parent 40839e21b9
commit 92a48a764d
2 changed files with 9 additions and 10 deletions

View File

@ -12,12 +12,12 @@
/* The typedef is used internally; the struct name is used in the public API /* The typedef is used internally; the struct name is used in the public API
* (where it is exposed as a different typedef) */ * (where it is exposed as a different typedef) */
typedef struct secp256k1_scratch_space_struct { 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 offset[SECP256K1_SCRATCH_MAX_FRAMES];
size_t frame_size[SECP256K1_SCRATCH_MAX_FRAMES]; size_t frame_size[SECP256K1_SCRATCH_MAX_FRAMES];
size_t frame; size_t frame;
size_t max_size; size_t max_size;
const secp256k1_callback* error_callback;
} secp256k1_scratch; } secp256k1_scratch;
static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t max_size); static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t max_size);

View File

@ -14,8 +14,8 @@ static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* err
secp256k1_scratch* ret = (secp256k1_scratch*)checked_malloc(error_callback, sizeof(*ret)); secp256k1_scratch* ret = (secp256k1_scratch*)checked_malloc(error_callback, sizeof(*ret));
if (ret != NULL) { if (ret != NULL) {
memset(ret, 0, sizeof(*ret)); memset(ret, 0, sizeof(*ret));
ret->data = (secp256k1_scratch*)checked_malloc(error_callback, max_size);
ret->max_size = max_size; ret->max_size = max_size;
ret->error_callback = error_callback;
} }
return ret; return ret;
} }
@ -23,6 +23,7 @@ static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* err
static void secp256k1_scratch_destroy(secp256k1_scratch* scratch) { static void secp256k1_scratch_destroy(secp256k1_scratch* scratch) {
if (scratch != NULL) { if (scratch != NULL) {
VERIFY_CHECK(scratch->frame == 0); VERIFY_CHECK(scratch->frame == 0);
free(scratch->data);
free(scratch); free(scratch);
} }
} }
@ -44,10 +45,8 @@ static int secp256k1_scratch_allocate_frame(secp256k1_scratch* scratch, size_t n
if (n <= secp256k1_scratch_max_allocation(scratch, objects)) { if (n <= secp256k1_scratch_max_allocation(scratch, objects)) {
n += objects * ALIGNMENT; n += objects * ALIGNMENT;
scratch->data[scratch->frame] = checked_malloc(scratch->error_callback, n); scratch->current_frame = scratch->data;
if (scratch->data[scratch->frame] == NULL) { scratch->data = (void *) ((char *) scratch->data + n);
return 0;
}
scratch->frame_size[scratch->frame] = n; scratch->frame_size[scratch->frame] = n;
scratch->offset[scratch->frame] = 0; scratch->offset[scratch->frame] = 0;
scratch->frame++; scratch->frame++;
@ -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) { static void secp256k1_scratch_deallocate_frame(secp256k1_scratch* scratch) {
VERIFY_CHECK(scratch->frame > 0); VERIFY_CHECK(scratch->frame > 0);
scratch->frame -= 1; scratch->frame--;
free(scratch->data[scratch->frame]); scratch->data = (void *) ((char *) scratch->data - scratch->frame_size[scratch->frame]);
} }
static void *secp256k1_scratch_alloc(secp256k1_scratch* scratch, size_t size) { static void *secp256k1_scratch_alloc(secp256k1_scratch* scratch, size_t size) {
@ -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]) { if (scratch->frame == 0 || size + scratch->offset[frame] > scratch->frame_size[frame]) {
return NULL; 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); memset(ret, 0, size);
scratch->offset[frame] += size; scratch->offset[frame] += size;