Botan 1.10.17
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

◆ Locking_Allocator()

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

References Botan::Pooling_Allocator::Pooling_Allocator().

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

Member Function Documentation

◆ allocate()

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 133 of file mem_pool.cpp.

134 {
135 const size_t BITMAP_SIZE = Memory_Block::bitmap_size();
136 const size_t BLOCK_SIZE = Memory_Block::block_size();
137
138 Mutex_Holder lock(mutex);
139
140 if(n <= BITMAP_SIZE * BLOCK_SIZE)
141 {
142 const size_t block_no = round_up(n, BLOCK_SIZE) / BLOCK_SIZE;
143
144 byte* mem = allocate_blocks(block_no);
145 if(mem)
146 return mem;
147
148 get_more_core(BOTAN_MEM_POOL_CHUNK_SIZE);
149
150 mem = allocate_blocks(block_no);
151 if(mem)
152 return mem;
153
154 throw Memory_Exhaustion();
155 }
156
157 void* new_buf = alloc_block(n);
158 if(new_buf)
159 return new_buf;
160
161 throw Memory_Exhaustion();
162 }
T round_up(T n, T align_to)
Definition rounding.h:22

References Botan::round_up().

◆ deallocate()

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 167 of file mem_pool.cpp.

168 {
169 const size_t BITMAP_SIZE = Memory_Block::bitmap_size();
170 const size_t BLOCK_SIZE = Memory_Block::block_size();
171
172 if(ptr == 0 || n == 0)
173 return;
174
175 Mutex_Holder lock(mutex);
176
177 if(n > BITMAP_SIZE * BLOCK_SIZE)
178 dealloc_block(ptr, n);
179 else
180 {
181 const size_t block_no = round_up(n, BLOCK_SIZE) / BLOCK_SIZE;
182
183 std::vector<Memory_Block>::iterator i =
184 std::lower_bound(blocks.begin(), blocks.end(), Memory_Block(ptr));
185
186 if(i == blocks.end() || !i->contains(ptr, block_no))
187 throw Invalid_State("Pointer released to the wrong allocator");
188
189 i->free(ptr, block_no);
190 }
191 }
Invalid_State(const std::string &err)
Definition exceptn.h:27

References Botan::Invalid_State::Invalid_State(), and Botan::round_up().

◆ destroy()

void Botan::Pooling_Allocator::destroy ( )
virtualinherited

Shutdown the allocator

Reimplemented from Botan::Allocator.

Definition at line 119 of file mem_pool.cpp.

120 {
121 Mutex_Holder lock(mutex);
122
123 blocks.clear();
124
125 for(size_t j = 0; j != allocated.size(); ++j)
126 dealloc_block(allocated[j].first, allocated[j].second);
127 allocated.clear();
128 }

◆ get()

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.

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 }
virtual std::string type() const =0
Allocator * get_allocator(const std::string &name="") const
Definition libstate.cpp:67
Library_State & global_state()
Internal_Error(const std::string &err)
Definition exceptn.h:47

References Botan::Library_State::get_allocator(), Botan::global_state(), Botan::Internal_Error::Internal_Error(), and type().

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

◆ init()

virtual void Botan::Allocator::init ( )
inlinevirtualinherited

Initialize the allocator

Definition at line 53 of file allocate.h.

53{}

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

◆ type()

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: