Skip to content

Commit ac49361

Browse files
prealloc: Get rid of manual memory management for prealloc contexts
1 parent 6573c08 commit ac49361

File tree

2 files changed

+5
-40
lines changed

2 files changed

+5
-40
lines changed

src/secp256k1.c

+5-8
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static const secp256k1_context secp256k1_context_no_precomp_ = {
6060
const secp256k1_context *secp256k1_context_no_precomp = &secp256k1_context_no_precomp_;
6161

6262
size_t secp256k1_context_preallocated_size(unsigned int flags) {
63-
size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context));
63+
size_t ret = sizeof(secp256k1_context);
6464
/* A return value of 0 is reserved as an indicator for errors when we call this function internally. */
6565
VERIFY_CHECK(ret != 0);
6666

@@ -74,13 +74,12 @@ size_t secp256k1_context_preallocated_size(unsigned int flags) {
7474
}
7575

7676
size_t secp256k1_context_preallocated_clone_size(const secp256k1_context* ctx) {
77-
size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context));
77+
size_t ret = sizeof(secp256k1_context);
7878
VERIFY_CHECK(ctx != NULL);
7979
return ret;
8080
}
8181

8282
secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigned int flags) {
83-
void* const base = prealloc;
8483
size_t prealloc_size;
8584
secp256k1_context* ret;
8685

@@ -93,7 +92,7 @@ secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigne
9392
return NULL;
9493
}
9594
VERIFY_CHECK(prealloc != NULL);
96-
ret = (secp256k1_context*)manual_alloc(&prealloc, sizeof(secp256k1_context), base, prealloc_size);
95+
ret = (secp256k1_context*)prealloc;
9796
ret->illegal_callback = default_illegal_callback;
9897
ret->error_callback = default_error_callback;
9998

@@ -102,7 +101,7 @@ secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigne
102101
secp256k1_ecmult_gen_context_build(&ret->ecmult_gen_ctx);
103102
ret->declassify = !!(flags & SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY);
104103

105-
return (secp256k1_context*) ret;
104+
return ret;
106105
}
107106

108107
secp256k1_context* secp256k1_context_create(unsigned int flags) {
@@ -117,14 +116,12 @@ secp256k1_context* secp256k1_context_create(unsigned int flags) {
117116
}
118117

119118
secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context* ctx, void* prealloc) {
120-
size_t prealloc_size;
121119
secp256k1_context* ret;
122120
VERIFY_CHECK(ctx != NULL);
123121
ARG_CHECK(prealloc != NULL);
124122

125-
prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
126123
ret = (secp256k1_context*)prealloc;
127-
memcpy(ret, ctx, prealloc_size);
124+
*ret = *ctx;
128125
return ret;
129126
}
130127

src/util.h

-32
Original file line numberDiff line numberDiff line change
@@ -142,38 +142,6 @@ static SECP256K1_INLINE void *checked_realloc(const secp256k1_callback* cb, void
142142

143143
#define ROUND_TO_ALIGN(size) ((((size) + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT)
144144

145-
/* Assume there is a contiguous memory object with bounds [base, base + max_size)
146-
* of which the memory range [base, *prealloc_ptr) is already allocated for usage,
147-
* where *prealloc_ptr is an aligned pointer. In that setting, this functions
148-
* reserves the subobject [*prealloc_ptr, *prealloc_ptr + alloc_size) of
149-
* alloc_size bytes by increasing *prealloc_ptr accordingly, taking into account
150-
* alignment requirements.
151-
*
152-
* The function returns an aligned pointer to the newly allocated subobject.
153-
*
154-
* This is useful for manual memory management: if we're simply given a block
155-
* [base, base + max_size), the caller can use this function to allocate memory
156-
* in this block and keep track of the current allocation state with *prealloc_ptr.
157-
*
158-
* It is VERIFY_CHECKed that there is enough space left in the memory object and
159-
* *prealloc_ptr is aligned relative to base.
160-
*/
161-
static SECP256K1_INLINE void *manual_alloc(void** prealloc_ptr, size_t alloc_size, void* base, size_t max_size) {
162-
size_t aligned_alloc_size = ROUND_TO_ALIGN(alloc_size);
163-
void* ret;
164-
VERIFY_CHECK(prealloc_ptr != NULL);
165-
VERIFY_CHECK(*prealloc_ptr != NULL);
166-
VERIFY_CHECK(base != NULL);
167-
VERIFY_CHECK((unsigned char*)*prealloc_ptr >= (unsigned char*)base);
168-
VERIFY_CHECK(((unsigned char*)*prealloc_ptr - (unsigned char*)base) % ALIGNMENT == 0);
169-
VERIFY_CHECK((unsigned char*)*prealloc_ptr - (unsigned char*)base + aligned_alloc_size <= max_size);
170-
/* Avoid unused parameter warnings when building without VERIFY */
171-
(void) base; (void) max_size;
172-
ret = *prealloc_ptr;
173-
*prealloc_ptr = (unsigned char*)*prealloc_ptr + aligned_alloc_size;
174-
return ret;
175-
}
176-
177145
/* Macro for restrict, when available and not in a VERIFY build. */
178146
#if defined(SECP256K1_BUILD) && defined(VERIFY)
179147
# define SECP256K1_RESTRICT

0 commit comments

Comments
 (0)