From 5a4bc0bb95d8be4774873655f556e57fade503ed Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Thu, 14 Mar 2019 14:37:53 +0000 Subject: [PATCH] scratch: unify allocations --- src/scratch_impl.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/scratch_impl.h b/src/scratch_impl.h index a1db9d4541ea0..cbe119f2fc442 100644 --- a/src/scratch_impl.h +++ b/src/scratch_impl.h @@ -11,11 +11,13 @@ #include "scratch.h" static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t max_size) { - secp256k1_scratch* ret = (secp256k1_scratch*)checked_malloc(error_callback, sizeof(*ret)); + const size_t base_alloc = ((sizeof(secp256k1_scratch) + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT; + void *alloc = checked_malloc(error_callback, base_alloc + max_size); + secp256k1_scratch* ret = (secp256k1_scratch *)alloc; if (ret != NULL) { memset(ret, 0, sizeof(*ret)); memcpy(ret->magic, "scratch", 8); - ret->data = (secp256k1_scratch*)checked_malloc(error_callback, max_size); + ret->data = (void *) ((char *) alloc + base_alloc); ret->max_size = max_size; } return ret; @@ -29,7 +31,6 @@ static void secp256k1_scratch_destroy(const secp256k1_callback* error_callback, return; } memset(scratch->magic, 0, sizeof(scratch->magic)); - free(scratch->data); free(scratch); } }