Changeset View
Changeset View
Standalone View
Standalone View
src/secp256k1/src/util.h
Show First 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | |||||
static SECP256K1_INLINE void *checked_realloc(const secp256k1_callback* cb, void *ptr, size_t size) { | static SECP256K1_INLINE void *checked_realloc(const secp256k1_callback* cb, void *ptr, size_t size) { | ||||
void *ret = realloc(ptr, size); | void *ret = realloc(ptr, size); | ||||
if (ret == NULL) { | if (ret == NULL) { | ||||
secp256k1_callback_call(cb, "Out of memory"); | secp256k1_callback_call(cb, "Out of memory"); | ||||
} | } | ||||
return ret; | return ret; | ||||
} | } | ||||
#if defined(__BIGGEST_ALIGNMENT__) | |||||
#define ALIGNMENT __BIGGEST_ALIGNMENT__ | |||||
#else | |||||
/* Using 16 bytes alignment because common architectures never have alignment | |||||
* requirements above 8 for any of the types we care about. In addition we | |||||
* leave some room because currently we don't care about a few bytes. */ | |||||
#define ALIGNMENT 16 | |||||
#endif | |||||
#define ROUND_TO_ALIGN(size) (((size + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT) | |||||
/* Assume there is a contiguous memory object with bounds [base, base + max_size) | |||||
* of which the memory range [base, *prealloc_ptr) is already allocated for usage, | |||||
* where *prealloc_ptr is an aligned pointer. In that setting, this functions | |||||
* reserves the subobject [*prealloc_ptr, *prealloc_ptr + alloc_size) of | |||||
* alloc_size bytes by increasing *prealloc_ptr accordingly, taking into account | |||||
* alignment requirements. | |||||
* | |||||
* The function returns an aligned pointer to the newly allocated subobject. | |||||
* | |||||
* This is useful for manual memory management: if we're simply given a block | |||||
* [base, base + max_size), the caller can use this function to allocate memory | |||||
* in this block and keep track of the current allocation state with *prealloc_ptr. | |||||
* | |||||
* It is VERIFY_CHECKed that there is enough space left in the memory object and | |||||
* *prealloc_ptr is aligned relative to base. | |||||
*/ | |||||
static SECP256K1_INLINE void *manual_alloc(void** prealloc_ptr, size_t alloc_size, void* base, size_t max_size) { | |||||
size_t aligned_alloc_size = ROUND_TO_ALIGN(alloc_size); | |||||
void* ret; | |||||
VERIFY_CHECK(prealloc_ptr != NULL); | |||||
VERIFY_CHECK(*prealloc_ptr != NULL); | |||||
VERIFY_CHECK(base != NULL); | |||||
VERIFY_CHECK((unsigned char*)*prealloc_ptr >= (unsigned char*)base); | |||||
VERIFY_CHECK(((unsigned char*)*prealloc_ptr - (unsigned char*)base) % ALIGNMENT == 0); | |||||
VERIFY_CHECK((unsigned char*)*prealloc_ptr - (unsigned char*)base + aligned_alloc_size <= max_size); | |||||
ret = *prealloc_ptr; | |||||
*((unsigned char**)prealloc_ptr) += aligned_alloc_size; | |||||
return ret; | |||||
} | |||||
/* Macro for restrict, when available and not in a VERIFY build. */ | /* Macro for restrict, when available and not in a VERIFY build. */ | ||||
#if defined(SECP256K1_BUILD) && defined(VERIFY) | #if defined(SECP256K1_BUILD) && defined(VERIFY) | ||||
# define SECP256K1_RESTRICT | # define SECP256K1_RESTRICT | ||||
#else | #else | ||||
# if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) ) | # if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) ) | ||||
# if SECP256K1_GNUC_PREREQ(3,0) | # if SECP256K1_GNUC_PREREQ(3,0) | ||||
# define SECP256K1_RESTRICT __restrict__ | # define SECP256K1_RESTRICT __restrict__ | ||||
# elif (defined(_MSC_VER) && _MSC_VER >= 1400) | # elif (defined(_MSC_VER) && _MSC_VER >= 1400) | ||||
Show All 27 Lines |