Skip to content

Commit

Permalink
scratch: save a couple bytes of unnecessarily-allocated memory
Browse files Browse the repository at this point in the history
  • Loading branch information
apoelstra committed May 25, 2019
1 parent a7a164f commit 7623cf2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/scratch_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ static size_t secp256k1_scratch_max_allocation(const secp256k1_callback* error_c
for (i = 0; i < scratch->frame; i++) {
allocated += scratch->frame_size[i];
}
if (scratch->max_size - allocated <= objects * ALIGNMENT) {
if (scratch->max_size - allocated <= objects * (ALIGNMENT - 1)) {
return 0;
}
return scratch->max_size - allocated - objects * ALIGNMENT;
return scratch->max_size - allocated - objects * (ALIGNMENT - 1);
}

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

if (n <= secp256k1_scratch_max_allocation(error_callback, scratch, objects)) {
n += objects * ALIGNMENT;
n += objects * (ALIGNMENT - 1);
scratch->current_frame = scratch->data;
scratch->data = (void *) ((char *) scratch->data + n);
scratch->frame_size[scratch->frame] = n;
Expand Down
2 changes: 1 addition & 1 deletion src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ void run_scratch_tests(void) {

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

Expand Down

0 comments on commit 7623cf2

Please sign in to comment.