Botan: Botan::Pooling_Allocator Class Reference

Botan 1.10.12

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

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

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 }
m
endian little unaligned ok family x86< aliases > ia32 x86 ix86 i86pc x86pc bepc</aliases >< submodels > i386 i486 i586 i686 pentium2 pentium3 pentium4 pentium m prescott k6 athlon atom32</submodels >< submodel_aliases > pentium i586 pentiumpro i686 pentium_pro i686 pentium2 pentium2 pentiumii pentium2 pentium3 pentium3 pentiumiii pentium3 x86family5 i586 x86family6 i686 pentiumm pentium m intelpentiumm pentium m intelpentium4 pentium4 duron athlon athlon xp athlon k7 athlon p2 i686 p3 i686 p4 pentium4 intelcput2250 prescott intelcput2300 prescott intelcput2400 prescott intelcput2500 prescott intelcput2600 prescott intelcput2700 prescott</submodel_aliases >< isa_extn > pentium m
Definition: x86_32.txt:65

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

Definition at line 107 of file mem_pool.cpp.

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

## Member Function Documentation

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

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

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

References 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 }
Botan::round_up
T round_up(T n, T align_to)
Definition: rounding.h:22

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

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

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

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