Skip to content

Commit 7623cf2

Browse files
committed
scratch: save a couple bytes of unnecessarily-allocated memory
1 parent a7a164f commit 7623cf2

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/scratch_impl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ static size_t secp256k1_scratch_max_allocation(const secp256k1_callback* error_c
4545
for (i = 0; i < scratch->frame; i++) {
4646
allocated += scratch->frame_size[i];
4747
}
48-
if (scratch->max_size - allocated <= objects * ALIGNMENT) {
48+
if (scratch->max_size - allocated <= objects * (ALIGNMENT - 1)) {
4949
return 0;
5050
}
51-
return scratch->max_size - allocated - objects * ALIGNMENT;
51+
return scratch->max_size - allocated - objects * (ALIGNMENT - 1);
5252
}
5353

5454
static int secp256k1_scratch_allocate_frame(const secp256k1_callback* error_callback, secp256k1_scratch* scratch, size_t n, size_t objects) {
@@ -60,7 +60,7 @@ static int secp256k1_scratch_allocate_frame(const secp256k1_callback* error_call
6060
}
6161

6262
if (n <= secp256k1_scratch_max_allocation(error_callback, scratch, objects)) {
63-
n += objects * ALIGNMENT;
63+
n += objects * (ALIGNMENT - 1);
6464
scratch->current_frame = scratch->data;
6565
scratch->data = (void *) ((char *) scratch->data + n);
6666
scratch->frame_size[scratch->frame] = n;

src/tests.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ void run_scratch_tests(void) {
356356

357357
/* ...but pushing a new stack frame does affect the max allocation */
358358
CHECK(secp256k1_scratch_allocate_frame(&none->error_callback, scratch, 500, 1) == 1);
359-
CHECK(secp256k1_scratch_max_allocation(&none->error_callback, scratch, 1) < 500); /* 500 - ALIGNMENT */
359+
CHECK(secp256k1_scratch_max_allocation(&none->error_callback, scratch, 1) < 500); /* 500 - (ALIGNMENT - 1) */
360360
CHECK(secp256k1_scratch_alloc(&none->error_callback, scratch, 500) != NULL);
361361
CHECK(secp256k1_scratch_alloc(&none->error_callback, scratch, 500) == NULL);
362362

0 commit comments

Comments
 (0)