|
| 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 init_size, size_t max_size) { |
| 19 | + secp256k1_scratch* ret = (secp256k1_scratch*)checked_malloc(error_callback, sizeof(*ret)); |
| 20 | + if (ret != NULL) { |
| 21 | + ret->data = checked_malloc(error_callback, init_size); |
| 22 | + if (ret->data == NULL) { |
| 23 | + free (ret); |
| 24 | + return NULL; |
| 25 | + } |
| 26 | + ret->offset = 0; |
| 27 | + ret->init_size = init_size; |
| 28 | + ret->max_size = max_size; |
| 29 | + ret->error_callback = error_callback; |
| 30 | + } |
| 31 | + return ret; |
| 32 | +} |
| 33 | + |
| 34 | +static void secp256k1_scratch_destroy(secp256k1_scratch* scratch) { |
| 35 | + if (scratch != NULL) { |
| 36 | + free(scratch->data); |
| 37 | + free(scratch); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +static size_t secp256k1_scratch_max_allocation(const secp256k1_scratch* scratch, size_t objects) { |
| 42 | + if (scratch->max_size <= objects * ALIGNMENT) { |
| 43 | + return 0; |
| 44 | + } |
| 45 | + return scratch->max_size - objects * ALIGNMENT; |
| 46 | +} |
| 47 | + |
| 48 | +static int secp256k1_scratch_resize(secp256k1_scratch* scratch, size_t n, size_t objects) { |
| 49 | + n += objects * ALIGNMENT; |
| 50 | + if (n > scratch->init_size && n <= scratch->max_size) { |
| 51 | + void *tmp = checked_realloc(scratch->error_callback, scratch->data, n); |
| 52 | + if (tmp == NULL) { |
| 53 | + return 0; |
| 54 | + } |
| 55 | + scratch->init_size = n; |
| 56 | + scratch->data = tmp; |
| 57 | + } |
| 58 | + return n <= scratch->max_size; |
| 59 | +} |
| 60 | + |
| 61 | +static void *secp256k1_scratch_alloc(secp256k1_scratch* scratch, size_t size) { |
| 62 | + void *ret; |
| 63 | + size = ((size + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT; |
| 64 | + if (size + scratch->offset > scratch->init_size) { |
| 65 | + return NULL; |
| 66 | + } |
| 67 | + ret = (void *) ((unsigned char *) scratch->data + scratch->offset); |
| 68 | + memset(ret, 0, size); |
| 69 | + scratch->offset += size; |
| 70 | + return ret; |
| 71 | +} |
| 72 | + |
| 73 | +static void secp256k1_scratch_reset(secp256k1_scratch* scratch) { |
| 74 | + scratch->offset = 0; |
| 75 | +} |
| 76 | + |
| 77 | +#endif |
0 commit comments