Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5a4bc0b

Browse files
committedMay 25, 2019
scratch: unify allocations
1 parent c2b028a commit 5a4bc0b

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed
 

‎src/scratch_impl.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
#include "scratch.h"
1212

1313
static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t max_size) {
14-
secp256k1_scratch* ret = (secp256k1_scratch*)checked_malloc(error_callback, sizeof(*ret));
14+
const size_t base_alloc = ((sizeof(secp256k1_scratch) + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT;
15+
void *alloc = checked_malloc(error_callback, base_alloc + max_size);
16+
secp256k1_scratch* ret = (secp256k1_scratch *)alloc;
1517
if (ret != NULL) {
1618
memset(ret, 0, sizeof(*ret));
1719
memcpy(ret->magic, "scratch", 8);
18-
ret->data = (secp256k1_scratch*)checked_malloc(error_callback, max_size);
20+
ret->data = (void *) ((char *) alloc + base_alloc);
1921
ret->max_size = max_size;
2022
}
2123
return ret;
@@ -29,7 +31,6 @@ static void secp256k1_scratch_destroy(const secp256k1_callback* error_callback,
2931
return;
3032
}
3133
memset(scratch->magic, 0, sizeof(scratch->magic));
32-
free(scratch->data);
3334
free(scratch);
3435
}
3536
}

0 commit comments

Comments
 (0)
Please sign in to comment.