summaryrefslogtreecommitdiffstats
path: root/old/botan/src/engine/gnump/gmp_mem.cpp
blob: 89a1ed2d47a42052c85ab28b0b617c891f34f695 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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();
   }

}