]> Git Repo - secp256k1.git/blob - src/util.h
free the ctx at the end of bench_ecdh
[secp256k1.git] / src / util.h
1 /**********************************************************************
2  * Copyright (c) 2013, 2014 Pieter Wuille                             *
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_UTIL_H
8 #define SECP256K1_UTIL_H
9
10 #if defined HAVE_CONFIG_H
11 #include "libsecp256k1-config.h"
12 #endif
13
14 #include <stdlib.h>
15 #include <stdint.h>
16 #include <stdio.h>
17 #include <limits.h>
18
19 typedef struct {
20     void (*fn)(const char *text, void* data);
21     const void* data;
22 } secp256k1_callback;
23
24 static SECP256K1_INLINE void secp256k1_callback_call(const secp256k1_callback * const cb, const char * const text) {
25     cb->fn(text, (void*)cb->data);
26 }
27
28 #ifdef DETERMINISTIC
29 #define TEST_FAILURE(msg) do { \
30     fprintf(stderr, "%s\n", msg); \
31     abort(); \
32 } while(0);
33 #else
34 #define TEST_FAILURE(msg) do { \
35     fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, msg); \
36     abort(); \
37 } while(0)
38 #endif
39
40 #if SECP256K1_GNUC_PREREQ(3, 0)
41 #define EXPECT(x,c) __builtin_expect((x),(c))
42 #else
43 #define EXPECT(x,c) (x)
44 #endif
45
46 #ifdef DETERMINISTIC
47 #define CHECK(cond) do { \
48     if (EXPECT(!(cond), 0)) { \
49         TEST_FAILURE("test condition failed"); \
50     } \
51 } while(0)
52 #else
53 #define CHECK(cond) do { \
54     if (EXPECT(!(cond), 0)) { \
55         TEST_FAILURE("test condition failed: " #cond); \
56     } \
57 } while(0)
58 #endif
59
60 /* Like assert(), but when VERIFY is defined, and side-effect safe. */
61 #if defined(COVERAGE)
62 #define VERIFY_CHECK(check)
63 #define VERIFY_SETUP(stmt)
64 #elif defined(VERIFY)
65 #define VERIFY_CHECK CHECK
66 #define VERIFY_SETUP(stmt) do { stmt; } while(0)
67 #else
68 #define VERIFY_CHECK(cond) do { (void)(cond); } while(0)
69 #define VERIFY_SETUP(stmt)
70 #endif
71
72 static SECP256K1_INLINE void *checked_malloc(const secp256k1_callback* cb, size_t size) {
73     void *ret = malloc(size);
74     if (ret == NULL) {
75         secp256k1_callback_call(cb, "Out of memory");
76     }
77     return ret;
78 }
79
80 static SECP256K1_INLINE void *checked_realloc(const secp256k1_callback* cb, void *ptr, size_t size) {
81     void *ret = realloc(ptr, size);
82     if (ret == NULL) {
83         secp256k1_callback_call(cb, "Out of memory");
84     }
85     return ret;
86 }
87
88 #if defined(__BIGGEST_ALIGNMENT__)
89 #define ALIGNMENT __BIGGEST_ALIGNMENT__
90 #else
91 /* Using 16 bytes alignment because common architectures never have alignment
92  * requirements above 8 for any of the types we care about. In addition we
93  * leave some room because currently we don't care about a few bytes. */
94 #define ALIGNMENT 16
95 #endif
96
97 #define ROUND_TO_ALIGN(size) (((size + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT)
98
99 /* Assume there is a contiguous memory object with bounds [base, base + max_size)
100  * of which the memory range [base, *prealloc_ptr) is already allocated for usage,
101  * where *prealloc_ptr is an aligned pointer. In that setting, this functions
102  * reserves the subobject [*prealloc_ptr, *prealloc_ptr + alloc_size) of
103  * alloc_size bytes by increasing *prealloc_ptr accordingly, taking into account
104  * alignment requirements.
105  *
106  * The function returns an aligned pointer to the newly allocated subobject.
107  *
108  * This is useful for manual memory management: if we're simply given a block
109  * [base, base + max_size), the caller can use this function to allocate memory
110  * in this block and keep track of the current allocation state with *prealloc_ptr.
111  *
112  * It is VERIFY_CHECKed that there is enough space left in the memory object and
113  * *prealloc_ptr is aligned relative to base.
114  */
115 static SECP256K1_INLINE void *manual_alloc(void** prealloc_ptr, size_t alloc_size, void* base, size_t max_size) {
116     size_t aligned_alloc_size = ROUND_TO_ALIGN(alloc_size);
117     void* ret;
118     VERIFY_CHECK(prealloc_ptr != NULL);
119     VERIFY_CHECK(*prealloc_ptr != NULL);
120     VERIFY_CHECK(base != NULL);
121     VERIFY_CHECK((unsigned char*)*prealloc_ptr >= (unsigned char*)base);
122     VERIFY_CHECK(((unsigned char*)*prealloc_ptr - (unsigned char*)base) % ALIGNMENT == 0);
123     VERIFY_CHECK((unsigned char*)*prealloc_ptr - (unsigned char*)base + aligned_alloc_size <= max_size);
124     ret = *prealloc_ptr;
125     *((unsigned char**)prealloc_ptr) += aligned_alloc_size;
126     return ret;
127 }
128
129 /* Macro for restrict, when available and not in a VERIFY build. */
130 #if defined(SECP256K1_BUILD) && defined(VERIFY)
131 # define SECP256K1_RESTRICT
132 #else
133 # if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) )
134 #  if SECP256K1_GNUC_PREREQ(3,0)
135 #   define SECP256K1_RESTRICT __restrict__
136 #  elif (defined(_MSC_VER) && _MSC_VER >= 1400)
137 #   define SECP256K1_RESTRICT __restrict
138 #  else
139 #   define SECP256K1_RESTRICT
140 #  endif
141 # else
142 #  define SECP256K1_RESTRICT restrict
143 # endif
144 #endif
145
146 #if defined(_WIN32)
147 # define I64FORMAT "I64d"
148 # define I64uFORMAT "I64u"
149 #else
150 # define I64FORMAT "lld"
151 # define I64uFORMAT "llu"
152 #endif
153
154 #if defined(HAVE___INT128)
155 # if defined(__GNUC__)
156 #  define SECP256K1_GNUC_EXT __extension__
157 # else
158 #  define SECP256K1_GNUC_EXT
159 # endif
160 SECP256K1_GNUC_EXT typedef unsigned __int128 uint128_t;
161 #endif
162
163 /* Zero memory if flag == 1. Constant time. */
164 static SECP256K1_INLINE void memczero(void *s, size_t len, int flag) {
165     unsigned char *p;
166     unsigned char mask = -(unsigned char)flag;
167     p = (unsigned char *)s;
168     while (len) {
169         *p ^= *p & mask;
170         p++;
171         len--;
172     }
173 }
174
175 #endif /* SECP256K1_UTIL_H */
This page took 0.030493 seconds and 4 git commands to generate.