/ src / secp256k1 / src / scratch.h
scratch.h
 1  /***********************************************************************
 2   * Copyright (c) 2017 Andrew Poelstra                                  *
 3   * Distributed under the MIT software license, see the accompanying    *
 4   * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
 5   ***********************************************************************/
 6  
 7  #ifndef SECP256K1_SCRATCH_H
 8  #define SECP256K1_SCRATCH_H
 9  
10  /* The typedef is used internally; the struct name is used in the public API
11   * (where it is exposed as a different typedef) */
12  typedef struct secp256k1_scratch_space_struct {
13      /** guard against interpreting this object as other types */
14      unsigned char magic[8];
15      /** actual allocated data */
16      void *data;
17      /** amount that has been allocated (i.e. `data + offset` is the next
18       *  available pointer)  */
19      size_t alloc_size;
20      /** maximum size available to allocate */
21      size_t max_size;
22  } secp256k1_scratch;
23  
24  typedef struct secp256k1_scratch_space_struct secp256k1_scratch_space;
25  
26  static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t max_size);
27  
28  static void secp256k1_scratch_destroy(const secp256k1_callback* error_callback, secp256k1_scratch* scratch);
29  
30  /** Returns an opaque object used to "checkpoint" a scratch space. Used
31   *  with `secp256k1_scratch_apply_checkpoint` to undo allocations. */
32  static size_t secp256k1_scratch_checkpoint(const secp256k1_callback* error_callback, const secp256k1_scratch* scratch);
33  
34  /** Applies a check point received from `secp256k1_scratch_checkpoint`,
35   *  undoing all allocations since that point. */
36  static void secp256k1_scratch_apply_checkpoint(const secp256k1_callback* error_callback, secp256k1_scratch* scratch, size_t checkpoint);
37  
38  /** Returns the maximum allocation the scratch space will allow */
39  static size_t secp256k1_scratch_max_allocation(const secp256k1_callback* error_callback, const secp256k1_scratch* scratch, size_t n_objects);
40  
41  /** Returns a pointer into the most recently allocated frame, or NULL if there is insufficient available space */
42  static void *secp256k1_scratch_alloc(const secp256k1_callback* error_callback, secp256k1_scratch* scratch, size_t n);
43  
44  #endif