]>
Commit | Line | Data |
---|---|---|
238305fd TR |
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 | * | |
0522caac TR |
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 | |
b1e68cb8 | 17 | * API function that expects a context object (see secp256k1.h for details). The |
0522caac TR |
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 | * | |
b1e68cb8 | 22 | * It is guaranteed that functions in this module will not call malloc or its |
238305fd TR |
23 | * friends realloc, calloc, and free. |
24 | */ | |
25 | ||
26 | /** Determine the memory size of a secp256k1 context object to be created in | |
27 | * caller-provided memory. | |
28 | * | |
29 | * The purpose of this function is to determine how much memory must be provided | |
30 | * to secp256k1_context_preallocated_create. | |
31 | * | |
32 | * Returns: the required size of the caller-provided memory block | |
33 | * In: flags: which parts of the context to initialize. | |
34 | */ | |
35 | SECP256K1_API size_t secp256k1_context_preallocated_size( | |
36 | unsigned int flags | |
37 | ) SECP256K1_WARN_UNUSED_RESULT; | |
38 | ||
39 | /** Create a secp256k1 context object in caller-provided memory. | |
0522caac TR |
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. | |
238305fd TR |
54 | * |
55 | * Returns: a newly created context object. | |
56 | * In: prealloc: a pointer to a rewritable contiguous block of memory of | |
57 | * size at least secp256k1_context_preallocated_size(flags) | |
0522caac | 58 | * bytes, as detailed above (cannot be NULL) |
238305fd TR |
59 | * flags: which parts of the context to initialize. |
60 | * | |
0522caac TR |
61 | * See also secp256k1_context_randomize (in secp256k1.h) |
62 | * and secp256k1_context_preallocated_destroy. | |
238305fd TR |
63 | */ |
64 | SECP256K1_API secp256k1_context* secp256k1_context_preallocated_create( | |
65 | void* prealloc, | |
66 | unsigned int flags | |
67 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT; | |
68 | ||
69 | /** Determine the memory size of a secp256k1 context object to be copied into | |
70 | * caller-provided memory. | |
71 | * | |
238305fd TR |
72 | * Returns: the required size of the caller-provided memory block. |
73 | * In: ctx: an existing context to copy (cannot be NULL) | |
74 | */ | |
75 | SECP256K1_API size_t secp256k1_context_preallocated_clone_size( | |
76 | const secp256k1_context* ctx | |
77 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT; | |
78 | ||
79 | /** Copy a secp256k1 context object into caller-provided memory. | |
0522caac TR |
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. | |
238305fd TR |
88 | * |
89 | * Returns: a newly created context object. | |
90 | * Args: ctx: an existing context to copy (cannot be NULL) | |
91 | * In: prealloc: a pointer to a rewritable contiguous block of memory of | |
92 | * size at least secp256k1_context_preallocated_size(flags) | |
0522caac | 93 | * bytes, as detailed above (cannot be NULL) |
238305fd TR |
94 | */ |
95 | SECP256K1_API secp256k1_context* secp256k1_context_preallocated_clone( | |
96 | const secp256k1_context* ctx, | |
97 | void* prealloc | |
98 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_WARN_UNUSED_RESULT; | |
99 | ||
100 | /** Destroy a secp256k1 context object that has been created in | |
101 | * caller-provided memory. | |
102 | * | |
103 | * The context pointer may not be used afterwards. | |
104 | * | |
105 | * The context to destroy must have been created using | |
106 | * secp256k1_context_preallocated_create or secp256k1_context_preallocated_clone. | |
107 | * If the context has instead been created using secp256k1_context_create or | |
108 | * secp256k1_context_clone, the behaviour is undefined. In that case, | |
109 | * secp256k1_context_destroy must be used instead. | |
110 | * | |
0522caac TR |
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 | * | |
238305fd TR |
116 | * Args: ctx: an existing context to destroy, constructed using |
117 | * secp256k1_context_preallocated_create or | |
118 | * secp256k1_context_preallocated_clone (cannot be NULL) | |
119 | */ | |
120 | SECP256K1_API void secp256k1_context_preallocated_destroy( | |
121 | secp256k1_context* ctx | |
122 | ); | |
123 | ||
124 | #ifdef __cplusplus | |
125 | } | |
126 | #endif | |
127 | ||
128 | #endif /* SECP256K1_PREALLOCATED_H */ |