Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#ifndef SECP256K1_PREALLOCATED_H2#define SECP256K1_PREALLOCATED_H4#include "secp256k1.h"6#ifdef __cplusplus7extern "C" {8#endif10/* The module provided by this header file is intended for settings in which it11* is not possible or desirable to rely on dynamic memory allocation. It provides12* functions for creating, cloning, and destroying secp256k1 context objects in a13* contiguous fixed-size block of memory provided by the caller.14*15* Context objects created by functions in this module can be used like contexts16* objects created by functions in secp256k1.h, i.e., they can be passed to any17* API function that expects a context object (see secp256k1.h for details). The18* only exception is that context objects created by functions in this module19* must be destroyed using secp256k1_context_preallocated_destroy (in this20* module) instead of secp256k1_context_destroy (in secp256k1.h).21*22* It is guaranteed that functions in this module will not call malloc or its23* friends realloc, calloc, and free.24*/26/** Determine the memory size of a secp256k1 context object to be created in27* caller-provided memory.28*29* The purpose of this function is to determine how much memory must be provided30* to secp256k1_context_preallocated_create.31*32* Returns: the required size of the caller-provided memory block33* In: flags: which parts of the context to initialize.34*/35SECP256K1_API size_t secp256k1_context_preallocated_size(36unsigned int flags37) SECP256K1_WARN_UNUSED_RESULT;39/** Create a secp256k1 context object in caller-provided memory.40*41* The caller must provide a pointer to a rewritable contiguous block of memory42* of size at least secp256k1_context_preallocated_size(flags) bytes, suitably43* aligned to hold an object of any type.44*45* The block of memory is exclusively owned by the created context object during46* the lifetime of this context object, which begins with the call to this47* function and ends when a call to secp256k1_context_preallocated_destroy48* (which destroys the context object again) returns. During the lifetime of the49* 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 memory51* contents to a different location or trying to create a second context object52* in the memory. In simpler words, the prealloc pointer (or any pointer derived53* from it) should not be used during the lifetime of the context object.54*55* Returns: pointer to newly created context object.56* In: prealloc: pointer to a rewritable contiguous block of memory of57* size at least secp256k1_context_preallocated_size(flags)58* bytes, as detailed above.59* flags: which parts of the context to initialize.60*61* See secp256k1_context_create (in secp256k1.h) for further details.62*63* See also secp256k1_context_randomize (in secp256k1.h)64* and secp256k1_context_preallocated_destroy.65*/66SECP256K1_API secp256k1_context *secp256k1_context_preallocated_create(67void *prealloc,68unsigned int flags69) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;71/** Determine the memory size of a secp256k1 context object to be copied into72* caller-provided memory.73*74* Returns: the required size of the caller-provided memory block.75* In: ctx: pointer to a context to copy.76*/77SECP256K1_API size_t secp256k1_context_preallocated_clone_size(78const secp256k1_context *ctx79) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;81/** Copy a secp256k1 context object into caller-provided memory.82*83* The caller must provide a pointer to a rewritable contiguous block of memory84* of size at least secp256k1_context_preallocated_size(flags) bytes, suitably85* aligned to hold an object of any type.86*87* The block of memory is exclusively owned by the created context object during88* the lifetime of this context object, see the description of89* secp256k1_context_preallocated_create for details.90*91* Cloning secp256k1_context_static is not possible, and should not be emulated by92* the caller (e.g., using memcpy). Create a new context instead.93*94* Returns: pointer to a newly created context object.95* Args: ctx: pointer to a context to copy (not secp256k1_context_static).96* In: prealloc: pointer to a rewritable contiguous block of memory of97* size at least secp256k1_context_preallocated_size(flags)98* bytes, as detailed above.99*/100SECP256K1_API secp256k1_context *secp256k1_context_preallocated_clone(101const secp256k1_context *ctx,102void *prealloc103) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_WARN_UNUSED_RESULT;105/** Destroy a secp256k1 context object that has been created in106* caller-provided memory.107*108* The context pointer may not be used afterwards.109*110* The context to destroy must have been created using111* secp256k1_context_preallocated_create or secp256k1_context_preallocated_clone.112* If the context has instead been created using secp256k1_context_create or113* secp256k1_context_clone, the behaviour is undefined. In that case,114* secp256k1_context_destroy must be used instead.115*116* If required, it is the responsibility of the caller to deallocate the block117* of memory properly after this function returns, e.g., by calling free on the118* preallocated pointer given to secp256k1_context_preallocated_create or119* secp256k1_context_preallocated_clone.120*121* Args: ctx: pointer to a context to destroy, constructed using122* secp256k1_context_preallocated_create or123* secp256k1_context_preallocated_clone124* (i.e., not secp256k1_context_static).125*/126SECP256K1_API void secp256k1_context_preallocated_destroy(127secp256k1_context *ctx128) SECP256K1_ARG_NONNULL(1);130#ifdef __cplusplus131}132#endif134#endif /* SECP256K1_PREALLOCATED_H */
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.