scratch: unify allocations

This commit is contained in:
Andrew Poelstra 2019-03-14 14:37:53 +00:00
parent c2b028a281
commit 5a4bc0bb95
1 changed files with 4 additions and 3 deletions

View File

@ -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);
}
}