Botan  1.10.9
Public Member Functions | Static Public Member Functions | List of all members
Botan::Locking_Allocator Class Reference

#include <defalloc.h>

Inheritance diagram for Botan::Locking_Allocator:
Botan::Pooling_Allocator Botan::Allocator

Public Member Functions

void * allocate (size_t)
 
void deallocate (void *, size_t)
 
void destroy ()
 
virtual void init ()
 
 Locking_Allocator (Mutex *mutex)
 
std::string type () const
 

Static Public Member Functions

static Allocatorget (bool locking)
 

Detailed Description

Allocator using malloc plus locking

Definition at line 30 of file defalloc.h.

Constructor & Destructor Documentation

Botan::Locking_Allocator::Locking_Allocator ( Mutex mutex)
inline
Parameters
mutexused for internal locking

Definition at line 36 of file defalloc.h.

36 : Pooling_Allocator(mutex) {}
Pooling_Allocator(Mutex *mutex)
Definition: mem_pool.cpp:99

Member Function Documentation

void * Botan::Pooling_Allocator::allocate ( size_t  n)
virtualinherited

Allocate a block of memory

Parameters
nhow many bytes to allocate
Returns
pointer to n bytes of memory

Implements Botan::Allocator.

Definition at line 131 of file mem_pool.cpp.

References block_size, and Botan::round_up().

132  {
133  const size_t BITMAP_SIZE = Memory_Block::bitmap_size();
134  const size_t BLOCK_SIZE = Memory_Block::block_size();
135 
136  Mutex_Holder lock(mutex);
137 
138  if(n <= BITMAP_SIZE * BLOCK_SIZE)
139  {
140  const size_t block_no = round_up(n, BLOCK_SIZE) / BLOCK_SIZE;
141 
142  byte* mem = allocate_blocks(block_no);
143  if(mem)
144  return mem;
145 
146  get_more_core(BOTAN_MEM_POOL_CHUNK_SIZE);
147 
148  mem = allocate_blocks(block_no);
149  if(mem)
150  return mem;
151 
152  throw Memory_Exhaustion();
153  }
154 
155  void* new_buf = alloc_block(n);
156  if(new_buf)
157  return new_buf;
158 
159  throw Memory_Exhaustion();
160  }
BigInt n
Definition: numthry.cpp:26
size_t block_size
Definition: ossl_md.cpp:41
unsigned char byte
Definition: types.h:22
T round_up(T n, T align_to)
Definition: rounding.h:22
void Botan::Pooling_Allocator::deallocate ( void *  ptr,
size_t  n 
)
virtualinherited

Deallocate memory allocated with allocate()

Parameters
ptrthe pointer returned by allocate()
nthe size of the block pointed to by ptr

Implements Botan::Allocator.

Definition at line 165 of file mem_pool.cpp.

References block_size, and Botan::round_up().

166  {
167  const size_t BITMAP_SIZE = Memory_Block::bitmap_size();
168  const size_t BLOCK_SIZE = Memory_Block::block_size();
169 
170  if(ptr == 0 || n == 0)
171  return;
172 
173  Mutex_Holder lock(mutex);
174 
175  if(n > BITMAP_SIZE * BLOCK_SIZE)
176  dealloc_block(ptr, n);
177  else
178  {
179  const size_t block_no = round_up(n, BLOCK_SIZE) / BLOCK_SIZE;
180 
181  std::vector<Memory_Block>::iterator i =
182  std::lower_bound(blocks.begin(), blocks.end(), Memory_Block(ptr));
183 
184  if(i == blocks.end() || !i->contains(ptr, block_no))
185  throw Invalid_State("Pointer released to the wrong allocator");
186 
187  i->free(ptr, block_no);
188  }
189  }
BigInt n
Definition: numthry.cpp:26
size_t block_size
Definition: ossl_md.cpp:41
T round_up(T n, T align_to)
Definition: rounding.h:22
void Botan::Pooling_Allocator::destroy ( )
virtualinherited

Shutdown the allocator

Reimplemented from Botan::Allocator.

Definition at line 117 of file mem_pool.cpp.

118  {
119  Mutex_Holder lock(mutex);
120 
121  blocks.clear();
122 
123  for(size_t j = 0; j != allocated.size(); ++j)
124  dealloc_block(allocated[j].first, allocated[j].second);
125  allocated.clear();
126  }
Allocator * Botan::Allocator::get ( bool  locking)
staticinherited

Acquire a pointer to an allocator

Parameters
lockingis true if the allocator should attempt to secure the memory (eg for using to store keys)
Returns
pointer to an allocator; ownership remains with library, so do not delete

Definition at line 90 of file defalloc.cpp.

References alloc, Botan::Library_State::get_allocator(), Botan::Global_State_Management::global_state(), and Botan::Allocator::type().

Referenced by Botan::GMP_Engine::GMP_Engine(), and Botan::MemoryRegion< word >::init().

91  {
92  std::string type = "";
93  if(!locking)
94  type = "malloc";
95 
96  Allocator* alloc = global_state().get_allocator(type);
97  if(alloc)
98  return alloc;
99 
100  throw Internal_Error("Couldn't find an allocator to use in get_allocator");
101  }
Library_State & global_state()
Allocator * get_allocator(const std::string &name="") const
Definition: libstate.cpp:67
virtual std::string type() const =0
Allocator * alloc
Definition: bzip2.cpp:29
virtual void Botan::Allocator::init ( )
inlinevirtualinherited

Initialize the allocator

Definition at line 53 of file allocate.h.

Referenced by Botan::Library_State::add_allocator().

53 {}
std::string Botan::Locking_Allocator::type ( ) const
inlinevirtual
Returns
name of this allocator type

Implements Botan::Allocator.

Definition at line 38 of file defalloc.h.

38 { return "locking"; }

The documentation for this class was generated from the following files: