aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlloyd <[email protected]>2006-12-19 19:57:21 +0000
committerlloyd <[email protected]>2006-12-19 19:57:21 +0000
commita9681ec4dc3e25a6cbd90341f6f3debe7763bd54 (patch)
tree6bdc768730b9c91f070e06a411d820fb46e75e2d
parentfe5db4b1ea0f03dd94822cfc6a55b2093c13fc14 (diff)
Some minor efficiency gains in the memory allocator.
-rw-r--r--include/mem_pool.h8
-rw-r--r--src/mem_pool.cpp38
2 files changed, 21 insertions, 25 deletions
diff --git a/include/mem_pool.h b/include/mem_pool.h
index 0665eac9c..cdd1f8d3b 100644
--- a/include/mem_pool.h
+++ b/include/mem_pool.h
@@ -37,9 +37,10 @@ class Pooling_Allocator : public Allocator
class Memory_Block
{
public:
- Memory_Block(void*, u32bit, u32bit);
+ Memory_Block(void*);
static u32bit bitmap_size() { return BITMAP_SIZE; }
+ static u32bit block_size() { return BLOCK_SIZE; }
bool contains(void*, u32bit) const throw();
byte* alloc(u32bit) throw();
@@ -51,12 +52,13 @@ class Pooling_Allocator : public Allocator
private:
typedef u64bit bitmap_type;
static const u32bit BITMAP_SIZE = 8 * sizeof(bitmap_type);
+ static const u32bit BLOCK_SIZE = 64;
+
bitmap_type bitmap;
byte* buffer, *buffer_end;
- u32bit block_size;
};
- const u32bit PREF_SIZE, BLOCK_SIZE;
+ const u32bit PREF_SIZE;
std::vector<Memory_Block> blocks;
std::vector<Memory_Block>::iterator last_used;
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());