From 7623cf2b97ab276c5250f641b37158e02280137d Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Fri, 15 Mar 2019 15:41:09 +0000 Subject: [PATCH] scratch: save a couple bytes of unnecessarily-allocated memory --- src/scratch_impl.h | 6 +++--- src/tests.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/scratch_impl.h b/src/scratch_impl.h index 3a3991035a09f..4b36da88b299c 100644 --- a/src/scratch_impl.h +++ b/src/scratch_impl.h @@ -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) { @@ -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; diff --git a/src/tests.c b/src/tests.c index 75cf2b0a721f3..d47d713af0fff 100644 --- a/src/tests.c +++ b/src/tests.c @@ -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);