Botan: Botan::MemoryMapping_Allocator Class Reference

Botan 1.10.12

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

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

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

`#include <mmap_mem.h>`

Inheritance diagram for Botan::MemoryMapping_Allocator:

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

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

## Detailed Description

Allocator that uses memory maps backed by disk. We zeroize the map upon deallocation. If swap occurs, the VM will swap to the shared file backing rather than to a swap device, which means we know where it is and can zap it later.

Definition at line 21 of file mmap_mem.h.

## Constructor & Destructor Documentation

Botan::MemoryMapping_Allocator::MemoryMapping_Allocator | ( | Mutex * | mutex | ) |
| inline

Parameters

  mutex | used for internal locking

Definition at line 27 of file mmap_mem.h.

27 : Pooling_Allocator(mutex) {}
Botan::Pooling_Allocator::Pooling_Allocator
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

  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
| ) |  |
| virtualinherited

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 | ( |  | ) |
| 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

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

std::string Botan::MemoryMapping_Allocator::type | ( |  | ) | const
| inlinevirtual

Returns
  name of this allocator type

Implements Botan::Allocator.

Definition at line 28 of file mmap_mem.h.

28 { return "mmap"; }

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

- src/alloc/alloc_mmap/mmap_mem.h
- src/alloc/alloc_mmap/mmap_mem.cpp