aboutsummaryrefslogtreecommitdiffstats
path: root/src/mem_pool.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mem_pool.cpp')
-rw-r--r--src/mem_pool.cpp38
1 files changed, 16 insertions, 22 deletions
diff --git a/src/mem_pool.cpp b/src/mem_pool.cpp
index 870365412..35f6d07e7 100644
--- a/src/mem_pool.cpp
+++ b/src/mem_pool.cpp
@@ -10,6 +10,8 @@
#include <botan/util.h>
#include <algorithm>
+#include <assert.h>
+
namespace Botan {
namespace {
@@ -34,19 +36,11 @@ u32bit choose_pref_size(u32bit provided)
/*************************************************
* Memory_Block Constructor *
*************************************************/
-Pooling_Allocator::Memory_Block::Memory_Block(void* buf, u32bit map_size,
- u32bit block_size)
+Pooling_Allocator::Memory_Block::Memory_Block(void* buf)
{
buffer = static_cast<byte*>(buf);
bitmap = 0;
- this->block_size = block_size;
-
- buffer_end = buffer + (block_size * BITMAP_SIZE);
-
- clear_mem(buffer, block_size * BITMAP_SIZE);
-
- if(map_size != BITMAP_SIZE)
- throw Invalid_Argument("Memory_Block: Bad bitmap size");
+ buffer_end = buffer + (BLOCK_SIZE * BITMAP_SIZE);
}
/*************************************************
@@ -66,7 +60,7 @@ bool Pooling_Allocator::Memory_Block::contains(void* ptr,
u32bit length) const throw()
{
return ((buffer <= ptr) &&
- (buffer_end >= (byte*)ptr + length * block_size));
+ (buffer_end >= (byte*)ptr + length * BLOCK_SIZE));
}
/*************************************************
@@ -106,7 +100,7 @@ byte* Pooling_Allocator::Memory_Block::alloc(u32bit n) throw()
return 0;
bitmap |= mask;
- return buffer + offset * block_size;
+ return buffer + offset * BLOCK_SIZE;
}
/*************************************************
@@ -114,9 +108,9 @@ byte* Pooling_Allocator::Memory_Block::alloc(u32bit n) throw()
*************************************************/
void Pooling_Allocator::Memory_Block::free(void* ptr, u32bit blocks) throw()
{
- clear_mem((byte*)ptr, blocks * block_size);
+ clear_mem((byte*)ptr, blocks * BLOCK_SIZE);
- const u32bit offset = ((byte*)ptr - buffer) / block_size;
+ const u32bit offset = ((byte*)ptr - buffer) / BLOCK_SIZE;
if(offset == 0 && blocks == BITMAP_SIZE)
bitmap = ~bitmap;
@@ -131,7 +125,7 @@ void Pooling_Allocator::Memory_Block::free(void* ptr, u32bit blocks) throw()
* Pooling_Allocator Constructor *
*************************************************/
Pooling_Allocator::Pooling_Allocator(u32bit p_size, bool) :
- PREF_SIZE(choose_pref_size(p_size)), BLOCK_SIZE(64)
+ PREF_SIZE(choose_pref_size(p_size))
{
mutex = global_state().get_mutex();
last_used = blocks.begin();
@@ -167,6 +161,7 @@ void Pooling_Allocator::destroy()
void* Pooling_Allocator::allocate(u32bit n)
{
const u32bit BITMAP_SIZE = Memory_Block::bitmap_size();
+ const u32bit BLOCK_SIZE = Memory_Block::block_size();
Mutex_Holder lock(mutex);
@@ -200,6 +195,7 @@ void* Pooling_Allocator::allocate(u32bit n)
void Pooling_Allocator::deallocate(void* ptr, u32bit n)
{
const u32bit BITMAP_SIZE = Memory_Block::bitmap_size();
+ const u32bit BLOCK_SIZE = Memory_Block::block_size();
if(ptr == 0 && n == 0)
return;
@@ -215,10 +211,10 @@ void Pooling_Allocator::deallocate(void* ptr, u32bit n)
std::vector<Memory_Block>::iterator i =
std::lower_bound(blocks.begin(), blocks.end(), ptr);
- if(i != blocks.end() && i->contains((byte*)ptr, block_no))
- i->free(ptr, block_no);
- else
+ if(i == blocks.end() || !i->contains(ptr, block_no))
throw Invalid_State("Pointer released to the wrong allocator");
+
+ i->free(ptr, block_no);
}
}
@@ -256,6 +252,7 @@ byte* Pooling_Allocator::allocate_blocks(u32bit n)
void Pooling_Allocator::get_more_core(u32bit in_bytes)
{
const u32bit BITMAP_SIZE = Memory_Block::bitmap_size();
+ const u32bit BLOCK_SIZE = Memory_Block::block_size();
const u32bit TOTAL_BLOCK_SIZE = BLOCK_SIZE * BITMAP_SIZE;
@@ -271,10 +268,7 @@ void Pooling_Allocator::get_more_core(u32bit in_bytes)
for(u32bit j = 0; j != in_blocks; ++j)
{
byte* byte_ptr = static_cast<byte*>(ptr);
-
- blocks.push_back(
- Memory_Block(byte_ptr + j * TOTAL_BLOCK_SIZE, BITMAP_SIZE, BLOCK_SIZE)
- );
+ blocks.push_back(Memory_Block(byte_ptr + j * TOTAL_BLOCK_SIZE));
}
std::sort(blocks.begin(), blocks.end());