@@ -12,7 +12,14 @@ extern "C" {
12
12
* functions for creating, cloning, and destroying secp256k1 context objects in a
13
13
* contiguous fixed-size block of memory provided by the caller.
14
14
*
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
16
23
* friends realloc, calloc, and free.
17
24
*/
18
25
@@ -30,15 +37,29 @@ SECP256K1_API size_t secp256k1_context_preallocated_size(
30
37
) SECP256K1_WARN_UNUSED_RESULT ;
31
38
32
39
/** 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.
33
54
*
34
55
* Returns: a newly created context object.
35
56
* In: prealloc: a pointer to a rewritable contiguous block of memory of
36
57
* 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)
39
59
* flags: which parts of the context to initialize.
40
60
*
41
- * See also secp256k1_context_randomize.
61
+ * See also secp256k1_context_randomize (in secp256k1.h)
62
+ * and secp256k1_context_preallocated_destroy.
42
63
*/
43
64
SECP256K1_API secp256k1_context * secp256k1_context_preallocated_create (
44
65
void * prealloc ,
@@ -48,9 +69,6 @@ SECP256K1_API secp256k1_context* secp256k1_context_preallocated_create(
48
69
/** Determine the memory size of a secp256k1 context object to be copied into
49
70
* caller-provided memory.
50
71
*
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
72
* Returns: the required size of the caller-provided memory block.
55
73
* In: ctx: an existing context to copy (cannot be NULL)
56
74
*/
@@ -59,13 +77,20 @@ SECP256K1_API size_t secp256k1_context_preallocated_clone_size(
59
77
) SECP256K1_ARG_NONNULL (1 ) SECP256K1_WARN_UNUSED_RESULT ;
60
78
61
79
/** 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.
62
88
*
63
89
* Returns: a newly created context object.
64
90
* Args: ctx: an existing context to copy (cannot be NULL)
65
91
* In: prealloc: a pointer to a rewritable contiguous block of memory of
66
92
* 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)
69
94
*/
70
95
SECP256K1_API secp256k1_context * secp256k1_context_preallocated_clone (
71
96
const secp256k1_context * ctx ,
@@ -83,6 +108,11 @@ SECP256K1_API secp256k1_context* secp256k1_context_preallocated_clone(
83
108
* secp256k1_context_clone, the behaviour is undefined. In that case,
84
109
* secp256k1_context_destroy must be used instead.
85
110
*
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
+ *
86
116
* Args: ctx: an existing context to destroy, constructed using
87
117
* secp256k1_context_preallocated_create or
88
118
* secp256k1_context_preallocated_clone (cannot be NULL)
0 commit comments