Botan: Botan::Pooling_Allocator Class Reference

Botan 1.10.16

Classes | Public Member Functions | Static Public Member Functions | List of all members
Botan::Pooling_Allocator Class Referenceabstract

`#include <mem_pool.h>`

Inheritance diagram for Botan::Pooling_Allocator:

## Public Member Functions
void * | allocate (size_t)
void | deallocate (void *, size_t)
void | destroy ()
virtual void | init ()
| Pooling_Allocator (Mutex *mutex)
virtual std::string | type () const =0
| ~Pooling_Allocator ()

## Static Public Member Functions
static Allocator * | get (bool locking)

## Detailed Description

Pooling Allocator

Definition at line 22 of file mem_pool.h.

## Constructor & Destructor Documentation

## ◆ Pooling_Allocator()

Botan::Pooling_Allocator::Pooling_Allocator | ( | Mutex * | mutex | ) |

Parameters

  mutex | used for internal locking

Definition at line 99 of file mem_pool.cpp.

99 : mutex(m)
100 {
101 last_used = blocks.begin();
102 }

## ◆ ~Pooling_Allocator()

Botan::Pooling_Allocator::~Pooling_Allocator | ( |  | ) |

Definition at line 107 of file mem_pool.cpp.

108 {
109 delete mutex;
110 #if 0
111 if(blocks.size())
112 throw Invalid_State("Pooling_Allocator: Never released memory");
113 #endif
114 }

## Member Function Documentation

## ◆ allocate()

void * Botan::Pooling_Allocator::allocate | ( | size_t | n | ) |
| virtual

Allocate a block of memory

Parameters

  n | how 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 }
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::round_up
T round_up(T n, T align_to)
Definition: rounding.h:22

## ◆ deallocate()

void Botan::Pooling_Allocator::deallocate | ( | void * | ptr,
|  | size_t | n
| ) |  |
| virtual

Deallocate memory allocated with allocate()

Parameters

  ptr | the pointer returned by allocate()
  n | the 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 }
Botan::round_up
T round_up(T n, T align_to)
Definition: rounding.h:22

## ◆ destroy()

void Botan::Pooling_Allocator::destroy | ( |  | ) |
| virtual

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

  locking | is 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::Malloc_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 }
Botan::Library_State::get_allocator
Allocator * get_allocator(const std::string &name="") const
Definition: libstate.cpp:67
Botan::Global_State_Management::global_state
Library_State & global_state()
Definition: global_state.cpp:32
Botan::Allocator::type
virtual std::string type() const =0
alloc
Allocator * alloc
Definition: bzip2.cpp:29

## ◆ init()

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 {}

## ◆ type()

virtual std::string Botan::Allocator::type | ( |  | ) | const
| pure virtualinherited

Returns
  name of this allocator type

Implemented in Botan::Locking_Allocator, Botan::MemoryMapping_Allocator, and Botan::Malloc_Allocator.

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

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

- src/alloc/mem_pool/mem_pool.h
- src/alloc/mem_pool/mem_pool.cpp