Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

scratch_impl.h
Go to the documentation of this file.
1 /**********************************************************************
2  * Copyright (c) 2017 Andrew Poelstra *
3  * Distributed under the MIT software license, see the accompanying *
4  * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5  **********************************************************************/
6 
7 #ifndef _SECP256K1_SCRATCH_IMPL_H_
8 #define _SECP256K1_SCRATCH_IMPL_H_
9 
10 #include "scratch.h"
11 
12 /* Using 16 bytes alignment because common architectures never have alignment
13  * requirements above 8 for any of the types we care about. In addition we
14  * leave some room because currently we don't care about a few bytes.
15  * TODO: Determine this at configure time. */
16 #define ALIGNMENT 16
17 
18 static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t max_size) {
19  secp256k1_scratch* ret = (secp256k1_scratch*)checked_malloc(error_callback, sizeof(*ret));
20  if (ret != NULL) {
21  memset(ret, 0, sizeof(*ret));
22  ret->max_size = max_size;
23  ret->error_callback = error_callback;
24  }
25  return ret;
26 }
27 
29  if (scratch != NULL) {
30  VERIFY_CHECK(scratch->frame == 0);
31  free(scratch);
32  }
33 }
34 
35 static size_t secp256k1_scratch_max_allocation(const secp256k1_scratch* scratch, size_t objects) {
36  size_t i = 0;
37  size_t allocated = 0;
38  for (i = 0; i < scratch->frame; i++) {
39  allocated += scratch->frame_size[i];
40  }
41  if (scratch->max_size - allocated <= objects * ALIGNMENT) {
42  return 0;
43  }
44  return scratch->max_size - allocated - objects * ALIGNMENT;
45 }
46 
47 static int secp256k1_scratch_allocate_frame(secp256k1_scratch* scratch, size_t n, size_t objects) {
49 
50  if (n <= secp256k1_scratch_max_allocation(scratch, objects)) {
51  n += objects * ALIGNMENT;
52  scratch->data[scratch->frame] = checked_malloc(scratch->error_callback, n);
53  if (scratch->data[scratch->frame] == NULL) {
54  return 0;
55  }
56  scratch->frame_size[scratch->frame] = n;
57  scratch->offset[scratch->frame] = 0;
58  scratch->frame++;
59  return 1;
60  } else {
61  return 0;
62  }
63 }
64 
66  VERIFY_CHECK(scratch->frame > 0);
67  scratch->frame -= 1;
68  free(scratch->data[scratch->frame]);
69 }
70 
71 static void *secp256k1_scratch_alloc(secp256k1_scratch* scratch, size_t size) {
72  void *ret;
73  size_t frame = scratch->frame - 1;
74  size = ((size + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT;
75 
76  if (scratch->frame == 0 || size + scratch->offset[frame] > scratch->frame_size[frame]) {
77  return NULL;
78  }
79  ret = (void *) ((unsigned char *) scratch->data[frame] + scratch->offset[frame]);
80  memset(ret, 0, size);
81  scratch->offset[frame] += size;
82 
83  return ret;
84 }
85 
86 #endif
#define VERIFY_CHECK(cond)
Definition: util.h:67
void * data[SECP256K1_SCRATCH_MAX_FRAMES]
Definition: scratch.h:15
const secp256k1_callback * error_callback
Definition: scratch.h:20
static secp256k1_scratch * secp256k1_scratch_create(const secp256k1_callback *error_callback, size_t max_size)
Definition: scratch_impl.h:18
#define SECP256K1_SCRATCH_MAX_FRAMES
Definition: scratch.h:10
static void secp256k1_scratch_destroy(secp256k1_scratch *scratch)
Definition: scratch_impl.h:28
size_t frame_size[SECP256K1_SCRATCH_MAX_FRAMES]
Definition: scratch.h:17
static size_t secp256k1_scratch_max_allocation(const secp256k1_scratch *scratch, size_t objects)
Definition: scratch_impl.h:35
size_t offset[SECP256K1_SCRATCH_MAX_FRAMES]
Definition: scratch.h:16
static SECP256K1_INLINE void * checked_malloc(const secp256k1_callback *cb, size_t size)
Definition: util.h:71
#define ALIGNMENT
Definition: scratch_impl.h:16
static void secp256k1_scratch_deallocate_frame(secp256k1_scratch *scratch)
Definition: scratch_impl.h:65
static int secp256k1_scratch_allocate_frame(secp256k1_scratch *scratch, size_t n, size_t objects)
Definition: scratch_impl.h:47
static void * secp256k1_scratch_alloc(secp256k1_scratch *scratch, size_t size)
Definition: scratch_impl.h:71
Released under the MIT license