Skip to content

Commit 92a48a7

Browse files
committed
scratch space: use single allocation
1 parent 40839e2 commit 92a48a7

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

src/scratch.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
/* The typedef is used internally; the struct name is used in the public API
1313
* (where it is exposed as a different typedef) */
1414
typedef struct secp256k1_scratch_space_struct {
15-
void *data[SECP256K1_SCRATCH_MAX_FRAMES];
15+
void *data;
16+
void *current_frame;
1617
size_t offset[SECP256K1_SCRATCH_MAX_FRAMES];
1718
size_t frame_size[SECP256K1_SCRATCH_MAX_FRAMES];
1819
size_t frame;
1920
size_t max_size;
20-
const secp256k1_callback* error_callback;
2121
} secp256k1_scratch;
2222

2323
static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t max_size);

src/scratch_impl.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* err
1414
secp256k1_scratch* ret = (secp256k1_scratch*)checked_malloc(error_callback, sizeof(*ret));
1515
if (ret != NULL) {
1616
memset(ret, 0, sizeof(*ret));
17+
ret->data = (secp256k1_scratch*)checked_malloc(error_callback, max_size);
1718
ret->max_size = max_size;
18-
ret->error_callback = error_callback;
1919
}
2020
return ret;
2121
}
2222

2323
static void secp256k1_scratch_destroy(secp256k1_scratch* scratch) {
2424
if (scratch != NULL) {
2525
VERIFY_CHECK(scratch->frame == 0);
26+
free(scratch->data);
2627
free(scratch);
2728
}
2829
}
@@ -44,10 +45,8 @@ static int secp256k1_scratch_allocate_frame(secp256k1_scratch* scratch, size_t n
4445

4546
if (n <= secp256k1_scratch_max_allocation(scratch, objects)) {
4647
n += objects * ALIGNMENT;
47-
scratch->data[scratch->frame] = checked_malloc(scratch->error_callback, n);
48-
if (scratch->data[scratch->frame] == NULL) {
49-
return 0;
50-
}
48+
scratch->current_frame = scratch->data;
49+
scratch->data = (void *) ((char *) scratch->data + n);
5150
scratch->frame_size[scratch->frame] = n;
5251
scratch->offset[scratch->frame] = 0;
5352
scratch->frame++;
@@ -59,8 +58,8 @@ static int secp256k1_scratch_allocate_frame(secp256k1_scratch* scratch, size_t n
5958

6059
static void secp256k1_scratch_deallocate_frame(secp256k1_scratch* scratch) {
6160
VERIFY_CHECK(scratch->frame > 0);
62-
scratch->frame -= 1;
63-
free(scratch->data[scratch->frame]);
61+
scratch->frame--;
62+
scratch->data = (void *) ((char *) scratch->data - scratch->frame_size[scratch->frame]);
6463
}
6564

6665
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) {
7170
if (scratch->frame == 0 || size + scratch->offset[frame] > scratch->frame_size[frame]) {
7271
return NULL;
7372
}
74-
ret = (void *) ((unsigned char *) scratch->data[frame] + scratch->offset[frame]);
73+
ret = (void *) ((char *) scratch->current_frame + scratch->offset[frame]);
7574
memset(ret, 0, size);
7675
scratch->offset[frame] += size;
7776

0 commit comments

Comments
 (0)