Skip to content

Commit b4d7c15

Browse files
Explain caller's obligations for preallocated memory
1 parent e424fd7 commit b4d7c15

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

include/secp256k1_preallocated.h

+39-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ extern "C" {
1212
* functions for creating, cloning, and destroying secp256k1 context objects in a
1313
* contiguous fixed-size block of memory provided by the caller.
1414
*
15-
* It is guaranteed that functions in this module will not call malloc or its
15+
* Context objects created by functions in this module can be used like contexts
16+
* objects created by functions in secp256k1.h, i.e., they can be passed to any
17+
* API function that excepts a context object (see secp256k1.h for details). The
18+
* only exception is that context objects created by functions in this module
19+
* must be destroyed using secp256k1_context_preallocated_destroy (in this
20+
* module) instead of secp256k1_context_destroy (in secp256k1.h).
21+
*
22+
* It is guaranteed that functions in by this module will not call malloc or its
1623
* friends realloc, calloc, and free.
1724
*/
1825

@@ -30,15 +37,29 @@ SECP256K1_API size_t secp256k1_context_preallocated_size(
3037
) SECP256K1_WARN_UNUSED_RESULT;
3138

3239
/** Create a secp256k1 context object in caller-provided memory.
40+
*
41+
* The caller must provide a pointer to a rewritable contiguous block of memory
42+
* of size at least secp256k1_context_preallocated_size(flags) bytes, suitably
43+
* aligned to hold an object of any type.
44+
*
45+
* The block of memory is exclusively owned by the created context object during
46+
* the lifetime of this context object, which begins with the call to this
47+
* function and ends when a call to secp256k1_context_preallocated_destroy
48+
* (which destroys the context object again) returns. During the lifetime of the
49+
* context object, the caller is obligated not to access this block of memory,
50+
* i.e., the caller may not read or write the memory, e.g., by copying the memory
51+
* contents to a different location or trying to create a second context object
52+
* in the memory. In simpler words, the prealloc pointer (or any pointer derived
53+
* from it) should not be used during the lifetime of the context object.
3354
*
3455
* Returns: a newly created context object.
3556
* In: prealloc: a pointer to a rewritable contiguous block of memory of
3657
* size at least secp256k1_context_preallocated_size(flags)
37-
* bytes, suitably aligned to hold an object of any type
38-
* (cannot be NULL)
58+
* bytes, as detailed above (cannot be NULL)
3959
* flags: which parts of the context to initialize.
4060
*
41-
* See also secp256k1_context_randomize.
61+
* See also secp256k1_context_randomize (in secp256k1.h)
62+
* and secp256k1_context_preallocated_destroy.
4263
*/
4364
SECP256K1_API secp256k1_context* secp256k1_context_preallocated_create(
4465
void* prealloc,
@@ -48,9 +69,6 @@ SECP256K1_API secp256k1_context* secp256k1_context_preallocated_create(
4869
/** Determine the memory size of a secp256k1 context object to be copied into
4970
* caller-provided memory.
5071
*
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-
*
5472
* Returns: the required size of the caller-provided memory block.
5573
* In: ctx: an existing context to copy (cannot be NULL)
5674
*/
@@ -59,13 +77,20 @@ SECP256K1_API size_t secp256k1_context_preallocated_clone_size(
5977
) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
6078

6179
/** Copy a secp256k1 context object into caller-provided memory.
80+
*
81+
* The caller must provide a pointer to a rewritable contiguous block of memory
82+
* of size at least secp256k1_context_preallocated_size(flags) bytes, suitably
83+
* aligned to hold an object of any type.
84+
*
85+
* The block of memory is exclusively owned by the created context object during
86+
* the lifetime of this context object, see the description of
87+
* secp256k1_context_preallocated_create for details.
6288
*
6389
* Returns: a newly created context object.
6490
* Args: ctx: an existing context to copy (cannot be NULL)
6591
* In: prealloc: a pointer to a rewritable contiguous block of memory of
6692
* size at least secp256k1_context_preallocated_size(flags)
67-
* bytes, suitably aligned to hold an object of any type
68-
* (cannot be NULL)
93+
* bytes, as detailed above (cannot be NULL)
6994
*/
7095
SECP256K1_API secp256k1_context* secp256k1_context_preallocated_clone(
7196
const secp256k1_context* ctx,
@@ -83,6 +108,11 @@ SECP256K1_API secp256k1_context* secp256k1_context_preallocated_clone(
83108
* secp256k1_context_clone, the behaviour is undefined. In that case,
84109
* secp256k1_context_destroy must be used instead.
85110
*
111+
* If required, it is the responsibility of the caller to deallocate the block
112+
* of memory properly after this function returns, e.g., by calling free on the
113+
* preallocated pointer given to secp256k1_context_preallocated_create or
114+
* secp256k1_context_preallocated_clone.
115+
*
86116
* Args: ctx: an existing context to destroy, constructed using
87117
* secp256k1_context_preallocated_create or
88118
* secp256k1_context_preallocated_clone (cannot be NULL)

0 commit comments

Comments
 (0)