You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

secp256k1_preallocated.h 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef SECP256K1_PREALLOCATED_H
  2. #define SECP256K1_PREALLOCATED_H
  3. #include "secp256k1.h"
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. /* The module provided by this header file is intended for settings in which it
  8. * is not possible or desirable to rely on dynamic memory allocation. It provides
  9. * functions for creating, cloning, and destroying secp256k1 context objects in a
  10. * contiguous fixed-size block of memory provided by the caller.
  11. *
  12. * Context objects created by functions in this module can be used like contexts
  13. * objects created by functions in secp256k1.h, i.e., they can be passed to any
  14. * API function that expects a context object (see secp256k1.h for details). The
  15. * only exception is that context objects created by functions in this module
  16. * must be destroyed using secp256k1_context_preallocated_destroy (in this
  17. * module) instead of secp256k1_context_destroy (in secp256k1.h).
  18. *
  19. * It is guaranteed that functions in this module will not call malloc or its
  20. * friends realloc, calloc, and free.
  21. */
  22. /** Determine the memory size of a secp256k1 context object to be created in
  23. * caller-provided memory.
  24. *
  25. * The purpose of this function is to determine how much memory must be provided
  26. * to secp256k1_context_preallocated_create.
  27. *
  28. * Returns: the required size of the caller-provided memory block
  29. * In: flags: which parts of the context to initialize.
  30. */
  31. SECP256K1_API size_t secp256k1_context_preallocated_size(
  32. unsigned int flags
  33. ) SECP256K1_WARN_UNUSED_RESULT;
  34. /** Create a secp256k1 context object in caller-provided memory.
  35. *
  36. * The caller must provide a pointer to a rewritable contiguous block of memory
  37. * of size at least secp256k1_context_preallocated_size(flags) bytes, suitably
  38. * aligned to hold an object of any type.
  39. *
  40. * The block of memory is exclusively owned by the created context object during
  41. * the lifetime of this context object, which begins with the call to this
  42. * function and ends when a call to secp256k1_context_preallocated_destroy
  43. * (which destroys the context object again) returns. During the lifetime of the
  44. * context object, the caller is obligated not to access this block of memory,
  45. * i.e., the caller may not read or write the memory, e.g., by copying the memory
  46. * contents to a different location or trying to create a second context object
  47. * in the memory. In simpler words, the prealloc pointer (or any pointer derived
  48. * from it) should not be used during the lifetime of the context object.
  49. *
  50. * Returns: a newly created context object.
  51. * In: prealloc: a pointer to a rewritable contiguous block of memory of
  52. * size at least secp256k1_context_preallocated_size(flags)
  53. * bytes, as detailed above (cannot be NULL)
  54. * flags: which parts of the context to initialize.
  55. *
  56. * See also secp256k1_context_randomize (in secp256k1.h)
  57. * and secp256k1_context_preallocated_destroy.
  58. */
  59. SECP256K1_API secp256k1_context* secp256k1_context_preallocated_create(
  60. void* prealloc,
  61. unsigned int flags
  62. ) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
  63. /** Determine the memory size of a secp256k1 context object to be copied into
  64. * caller-provided memory.
  65. *
  66. * Returns: the required size of the caller-provided memory block.
  67. * In: ctx: an existing context to copy (cannot be NULL)
  68. */
  69. SECP256K1_API size_t secp256k1_context_preallocated_clone_size(
  70. const secp256k1_context* ctx
  71. ) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
  72. /** Copy a secp256k1 context object into caller-provided memory.
  73. *
  74. * The caller must provide a pointer to a rewritable contiguous block of memory
  75. * of size at least secp256k1_context_preallocated_size(flags) bytes, suitably
  76. * aligned to hold an object of any type.
  77. *
  78. * The block of memory is exclusively owned by the created context object during
  79. * the lifetime of this context object, see the description of
  80. * secp256k1_context_preallocated_create for details.
  81. *
  82. * Returns: a newly created context object.
  83. * Args: ctx: an existing context to copy (cannot be NULL)
  84. * In: prealloc: a pointer to a rewritable contiguous block of memory of
  85. * size at least secp256k1_context_preallocated_size(flags)
  86. * bytes, as detailed above (cannot be NULL)
  87. */
  88. SECP256K1_API secp256k1_context* secp256k1_context_preallocated_clone(
  89. const secp256k1_context* ctx,
  90. void* prealloc
  91. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_WARN_UNUSED_RESULT;
  92. /** Destroy a secp256k1 context object that has been created in
  93. * caller-provided memory.
  94. *
  95. * The context pointer may not be used afterwards.
  96. *
  97. * The context to destroy must have been created using
  98. * secp256k1_context_preallocated_create or secp256k1_context_preallocated_clone.
  99. * If the context has instead been created using secp256k1_context_create or
  100. * secp256k1_context_clone, the behaviour is undefined. In that case,
  101. * secp256k1_context_destroy must be used instead.
  102. *
  103. * If required, it is the responsibility of the caller to deallocate the block
  104. * of memory properly after this function returns, e.g., by calling free on the
  105. * preallocated pointer given to secp256k1_context_preallocated_create or
  106. * secp256k1_context_preallocated_clone.
  107. *
  108. * Args: ctx: an existing context to destroy, constructed using
  109. * secp256k1_context_preallocated_create or
  110. * secp256k1_context_preallocated_clone (cannot be NULL)
  111. */
  112. SECP256K1_API void secp256k1_context_preallocated_destroy(
  113. secp256k1_context* ctx
  114. );
  115. #ifdef __cplusplus
  116. }
  117. #endif
  118. #endif /* SECP256K1_PREALLOCATED_H */