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 **********************************************************************/
7 #ifndef SECP256K1_UTIL_H
8 #define SECP256K1_UTIL_H
10 #if defined HAVE_CONFIG_H
11 #include "libsecp256k1-config.h"
19 void (*fn)(const char *text, void* data);
23 static SECP256K1_INLINE void secp256k1_callback_call(const secp256k1_callback * const cb, const char * const text) {
24 cb->fn(text, (void*)cb->data);
28 #define TEST_FAILURE(msg) do { \
29 fprintf(stderr, "%s\n", msg); \
33 #define TEST_FAILURE(msg) do { \
34 fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, msg); \
39 #if SECP256K1_GNUC_PREREQ(3, 0)
40 #define EXPECT(x,c) __builtin_expect((x),(c))
42 #define EXPECT(x,c) (x)
46 #define CHECK(cond) do { \
47 if (EXPECT(!(cond), 0)) { \
48 TEST_FAILURE("test condition failed"); \
52 #define CHECK(cond) do { \
53 if (EXPECT(!(cond), 0)) { \
54 TEST_FAILURE("test condition failed: " #cond); \
59 /* Like assert(), but when VERIFY is defined, and side-effect safe. */
61 #define VERIFY_CHECK(check)
62 #define VERIFY_SETUP(stmt)
64 #define VERIFY_CHECK CHECK
65 #define VERIFY_SETUP(stmt) do { stmt; } while(0)
67 #define VERIFY_CHECK(cond) do { (void)(cond); } while(0)
68 #define VERIFY_SETUP(stmt)
71 static SECP256K1_INLINE void *checked_malloc(const secp256k1_callback* cb, size_t size) {
72 void *ret = malloc(size);
74 secp256k1_callback_call(cb, "Out of memory");
79 static SECP256K1_INLINE void *checked_realloc(const secp256k1_callback* cb, void *ptr, size_t size) {
80 void *ret = realloc(ptr, size);
82 secp256k1_callback_call(cb, "Out of memory");
87 #if defined(__BIGGEST_ALIGNMENT__)
88 #define ALIGNMENT __BIGGEST_ALIGNMENT__
90 /* Using 16 bytes alignment because common architectures never have alignment
91 * requirements above 8 for any of the types we care about. In addition we
92 * leave some room because currently we don't care about a few bytes. */
96 #define ROUND_TO_ALIGN(size) (((size + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT)
98 /* Assume there is a contiguous memory object with bounds [base, base + max_size)
99 * of which the memory range [base, *prealloc_ptr) is already allocated for usage,
100 * where *prealloc_ptr is an aligned pointer. In that setting, this functions
101 * reserves the subobject [*prealloc_ptr, *prealloc_ptr + alloc_size) of
102 * alloc_size bytes by increasing *prealloc_ptr accordingly, taking into account
103 * alignment requirements.
105 * The function returns an aligned pointer to the newly allocated subobject.
107 * This is useful for manual memory management: if we're simply given a block
108 * [base, base + max_size), the caller can use this function to allocate memory
109 * in this block and keep track of the current allocation state with *prealloc_ptr.
111 * It is VERIFY_CHECKed that there is enough space left in the memory object and
112 * *prealloc_ptr is aligned relative to base.
114 static SECP256K1_INLINE void *manual_alloc(void** prealloc_ptr, size_t alloc_size, void* base, size_t max_size) {
115 size_t aligned_alloc_size = ROUND_TO_ALIGN(alloc_size);
117 VERIFY_CHECK(prealloc_ptr != NULL);
118 VERIFY_CHECK(*prealloc_ptr != NULL);
119 VERIFY_CHECK(base != NULL);
120 VERIFY_CHECK((unsigned char*)*prealloc_ptr >= (unsigned char*)base);
121 VERIFY_CHECK(((unsigned char*)*prealloc_ptr - (unsigned char*)base) % ALIGNMENT == 0);
122 VERIFY_CHECK((unsigned char*)*prealloc_ptr - (unsigned char*)base + aligned_alloc_size <= max_size);
124 *((unsigned char**)prealloc_ptr) += aligned_alloc_size;
128 /* Macro for restrict, when available and not in a VERIFY build. */
129 #if defined(SECP256K1_BUILD) && defined(VERIFY)
130 # define SECP256K1_RESTRICT
132 # if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) )
133 # if SECP256K1_GNUC_PREREQ(3,0)
134 # define SECP256K1_RESTRICT __restrict__
135 # elif (defined(_MSC_VER) && _MSC_VER >= 1400)
136 # define SECP256K1_RESTRICT __restrict
138 # define SECP256K1_RESTRICT
141 # define SECP256K1_RESTRICT restrict
146 # define I64FORMAT "I64d"
147 # define I64uFORMAT "I64u"
149 # define I64FORMAT "lld"
150 # define I64uFORMAT "llu"
153 #if defined(HAVE___INT128)
154 # if defined(__GNUC__)
155 # define SECP256K1_GNUC_EXT __extension__
157 # define SECP256K1_GNUC_EXT
159 SECP256K1_GNUC_EXT typedef unsigned __int128 uint128_t;
162 #endif /* SECP256K1_UTIL_H */