Skip to content

Commit 238305f

Browse files
Move _preallocated functions to separate header
1 parent 695feb6 commit 238305f

File tree

5 files changed

+105
-77
lines changed

5 files changed

+105
-77
lines changed

Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ else
88
JNI_LIB =
99
endif
1010
include_HEADERS = include/secp256k1.h
11+
include_HEADERS += include/secp256k1_preallocated.h
1112
noinst_HEADERS =
1213
noinst_HEADERS += src/scalar.h
1314
noinst_HEADERS += src/scalar_4x64.h

include/secp256k1.h

+4-77
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ SECP256K1_API extern const secp256k1_context *secp256k1_context_no_precomp;
191191
/** Create a secp256k1 context object (in dynamically allocated memory).
192192
*
193193
* This function uses malloc to allocate memory. It is guaranteed that malloc is
194-
* called at most once for every call of this function.
194+
* called at most once for every call of this function. If you need to avoid dynamic
195+
* memory allocation entirely, see the functions in secp256k1_preallocated.h.
195196
*
196197
* Returns: a newly created context object.
197198
* In: flags: which parts of the context to initialize.
@@ -205,7 +206,8 @@ SECP256K1_API secp256k1_context* secp256k1_context_create(
205206
/** Copy a secp256k1 context object (into dynamically allocated memory).
206207
*
207208
* This function uses malloc to allocate memory. It is guaranteed that malloc is
208-
* called at most once for every call of this function.
209+
* called at most once for every call of this function. If you need to avoid dynamic
210+
* memory allocation entirely, see the functions in secp256k1_preallocated.h.
209211
*
210212
* Returns: a newly created context object.
211213
* Args: ctx: an existing context to copy (cannot be NULL)
@@ -230,81 +232,6 @@ SECP256K1_API secp256k1_context* secp256k1_context_clone(
230232
SECP256K1_API void secp256k1_context_destroy(
231233
secp256k1_context* ctx
232234
);
233-
/** Determine the memory size of a secp256k1 context object to be created in
234-
* caller-provided memory.
235-
*
236-
* The purpose of this function is to determine how much memory must be provided
237-
* to secp256k1_context_preallocated_create.
238-
*
239-
* Returns: the required size of the caller-provided memory block
240-
* In: flags: which parts of the context to initialize.
241-
*/
242-
243-
SECP256K1_API size_t secp256k1_context_preallocated_size(
244-
unsigned int flags
245-
) SECP256K1_WARN_UNUSED_RESULT;
246-
247-
/** Create a secp256k1 context object in caller-provided memory.
248-
*
249-
* Returns: a newly created context object.
250-
* In: prealloc: a pointer to a rewritable contiguous block of memory of
251-
* size at least secp256k1_context_preallocated_size(flags)
252-
* bytes, suitably aligned to hold an object of any type
253-
* (cannot be NULL)
254-
* flags: which parts of the context to initialize.
255-
*
256-
* See also secp256k1_context_randomize.
257-
*/
258-
SECP256K1_API secp256k1_context* secp256k1_context_preallocated_create(
259-
void* prealloc,
260-
unsigned int flags
261-
) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
262-
263-
/** Determine the memory size of a secp256k1 context object to be copied into
264-
* caller-provided memory.
265-
*
266-
* The purpose of this function is to determine how much memory must be provided
267-
* to secp256k1_context_preallocated_clone when copying the context ctx.
268-
*
269-
* Returns: the required size of the caller-provided memory block.
270-
* In: ctx: an existing context to copy (cannot be NULL)
271-
*/
272-
SECP256K1_API size_t secp256k1_context_preallocated_clone_size(
273-
const secp256k1_context* ctx
274-
) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
275-
276-
/** Copy a secp256k1 context object into caller-provided memory.
277-
*
278-
* Returns: a newly created context object.
279-
* Args: ctx: an existing context to copy (cannot be NULL)
280-
* In: prealloc: a pointer to a rewritable contiguous block of memory of
281-
* size at least secp256k1_context_preallocated_size(flags)
282-
* bytes, suitably aligned to hold an object of any type
283-
* (cannot be NULL)
284-
*/
285-
SECP256K1_API secp256k1_context* secp256k1_context_preallocated_clone(
286-
const secp256k1_context* ctx,
287-
void* prealloc
288-
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_WARN_UNUSED_RESULT;
289-
290-
/** Destroy a secp256k1 context object that has been created in
291-
* caller-provided memory.
292-
*
293-
* The context pointer may not be used afterwards.
294-
*
295-
* The context to destroy must have been created using
296-
* secp256k1_context_preallocated_create or secp256k1_context_preallocated_clone.
297-
* If the context has instead been created using secp256k1_context_create or
298-
* secp256k1_context_clone, the behaviour is undefined. In that case,
299-
* secp256k1_context_destroy must be used instead.
300-
*
301-
* Args: ctx: an existing context to destroy, constructed using
302-
* secp256k1_context_preallocated_create or
303-
* secp256k1_context_preallocated_clone (cannot be NULL)
304-
*/
305-
SECP256K1_API void secp256k1_context_preallocated_destroy(
306-
secp256k1_context* ctx
307-
);
308235

309236
/** Set a callback function to be called when an illegal argument is passed to
310237
* an API call. It will only trigger for violations that are mentioned

include/secp256k1_preallocated.h

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#ifndef SECP256K1_PREALLOCATED_H
2+
#define SECP256K1_PREALLOCATED_H
3+
4+
#include "secp256k1.h"
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
/* The module provided by this header file is intended for settings in which it
11+
* is not possible or desirable to rely on dynamic memory allocation. It provides
12+
* functions for creating, cloning, and destroying secp256k1 context objects in a
13+
* contiguous fixed-size block of memory provided by the caller.
14+
*
15+
* It is guaranteed that functions in this module will not call malloc or its
16+
* friends realloc, calloc, and free.
17+
*/
18+
19+
/** Determine the memory size of a secp256k1 context object to be created in
20+
* caller-provided memory.
21+
*
22+
* The purpose of this function is to determine how much memory must be provided
23+
* to secp256k1_context_preallocated_create.
24+
*
25+
* Returns: the required size of the caller-provided memory block
26+
* In: flags: which parts of the context to initialize.
27+
*/
28+
SECP256K1_API size_t secp256k1_context_preallocated_size(
29+
unsigned int flags
30+
) SECP256K1_WARN_UNUSED_RESULT;
31+
32+
/** Create a secp256k1 context object in caller-provided memory.
33+
*
34+
* Returns: a newly created context object.
35+
* In: prealloc: a pointer to a rewritable contiguous block of memory of
36+
* size at least secp256k1_context_preallocated_size(flags)
37+
* bytes, suitably aligned to hold an object of any type
38+
* (cannot be NULL)
39+
* flags: which parts of the context to initialize.
40+
*
41+
* See also secp256k1_context_randomize.
42+
*/
43+
SECP256K1_API secp256k1_context* secp256k1_context_preallocated_create(
44+
void* prealloc,
45+
unsigned int flags
46+
) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
47+
48+
/** Determine the memory size of a secp256k1 context object to be copied into
49+
* caller-provided memory.
50+
*
51+
* The purpose of this function is to determine how much memory must be provided
52+
* to secp256k1_context_preallocated_clone when copying the context ctx.
53+
*
54+
* Returns: the required size of the caller-provided memory block.
55+
* In: ctx: an existing context to copy (cannot be NULL)
56+
*/
57+
SECP256K1_API size_t secp256k1_context_preallocated_clone_size(
58+
const secp256k1_context* ctx
59+
) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
60+
61+
/** Copy a secp256k1 context object into caller-provided memory.
62+
*
63+
* Returns: a newly created context object.
64+
* Args: ctx: an existing context to copy (cannot be NULL)
65+
* In: prealloc: a pointer to a rewritable contiguous block of memory of
66+
* size at least secp256k1_context_preallocated_size(flags)
67+
* bytes, suitably aligned to hold an object of any type
68+
* (cannot be NULL)
69+
*/
70+
SECP256K1_API secp256k1_context* secp256k1_context_preallocated_clone(
71+
const secp256k1_context* ctx,
72+
void* prealloc
73+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_WARN_UNUSED_RESULT;
74+
75+
/** Destroy a secp256k1 context object that has been created in
76+
* caller-provided memory.
77+
*
78+
* The context pointer may not be used afterwards.
79+
*
80+
* The context to destroy must have been created using
81+
* secp256k1_context_preallocated_create or secp256k1_context_preallocated_clone.
82+
* If the context has instead been created using secp256k1_context_create or
83+
* secp256k1_context_clone, the behaviour is undefined. In that case,
84+
* secp256k1_context_destroy must be used instead.
85+
*
86+
* Args: ctx: an existing context to destroy, constructed using
87+
* secp256k1_context_preallocated_create or
88+
* secp256k1_context_preallocated_clone (cannot be NULL)
89+
*/
90+
SECP256K1_API void secp256k1_context_preallocated_destroy(
91+
secp256k1_context* ctx
92+
);
93+
94+
#ifdef __cplusplus
95+
}
96+
#endif
97+
98+
#endif /* SECP256K1_PREALLOCATED_H */

src/secp256k1.c

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
**********************************************************************/
66

77
#include "include/secp256k1.h"
8+
#include "include/secp256k1_preallocated.h"
89

910
#include "util.h"
1011
#include "num_impl.h"

src/tests.c

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "secp256k1.c"
1818
#include "include/secp256k1.h"
19+
#include "include/secp256k1_preallocated.h"
1920
#include "testrand_impl.h"
2021

2122
#ifdef ENABLE_OPENSSL_TESTS

0 commit comments

Comments
 (0)