diff options
Diffstat (limited to 'botan/src/engine/gnump/gmp_mem.cpp')
-rw-r--r-- | botan/src/engine/gnump/gmp_mem.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/botan/src/engine/gnump/gmp_mem.cpp b/botan/src/engine/gnump/gmp_mem.cpp new file mode 100644 index 0000000..89a1ed2 --- /dev/null +++ b/botan/src/engine/gnump/gmp_mem.cpp @@ -0,0 +1,70 @@ +/* +* GNU MP Memory Handlers +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_gmp.h> +#include <cstring> +#include <gmp.h> + +namespace Botan { + +namespace { + +/* +* Allocator used by GNU MP +*/ +Allocator* gmp_alloc = 0; + +/* +* Allocation Function for GNU MP +*/ +void* gmp_malloc(size_t n) + { + return gmp_alloc->allocate(n); + } + +/* +* Reallocation Function for GNU MP +*/ +void* gmp_realloc(void* ptr, size_t old_n, size_t new_n) + { + void* new_buf = gmp_alloc->allocate(new_n); + std::memcpy(new_buf, ptr, std::min(old_n, new_n)); + gmp_alloc->deallocate(ptr, old_n); + return new_buf; + } + +/* +* Deallocation Function for GNU MP +*/ +void gmp_free(void* ptr, size_t n) + { + gmp_alloc->deallocate(ptr, n); + } + +} + +/* +* Set the GNU MP memory functions +*/ +void GMP_Engine::set_memory_hooks() + { + if(gmp_alloc == 0) + { + gmp_alloc = Allocator::get(true); + mp_set_memory_functions(gmp_malloc, gmp_realloc, gmp_free); + } + } + +/* +* GMP_Engine Constructor +*/ +GMP_Engine::GMP_Engine() + { + set_memory_hooks(); + } + +} |