aboutsummaryrefslogtreecommitdiffstats
path: root/src/alloc/mem_pool
diff options
context:
space:
mode:
authorlloyd <[email protected]>2010-10-12 15:40:31 +0000
committerlloyd <[email protected]>2010-10-12 15:40:31 +0000
commit18a58396b7bb2925a33c57b17f1820b386b54c2d (patch)
tree42b2707148ffc2a869f7bb44507ec575e0067d10 /src/alloc/mem_pool
parentff173a4edb68992d30deed57590fd04c54488a93 (diff)
s/u32bit/size_t/ in alloc
Also handle partial writes in alloc_mmap
Diffstat (limited to 'src/alloc/mem_pool')
-rw-r--r--src/alloc/mem_pool/mem_pool.cpp48
-rw-r--r--src/alloc/mem_pool/mem_pool.h28
2 files changed, 38 insertions, 38 deletions
diff --git a/src/alloc/mem_pool/mem_pool.cpp b/src/alloc/mem_pool/mem_pool.cpp
index 4180d2602..f32eb9604 100644
--- a/src/alloc/mem_pool/mem_pool.cpp
+++ b/src/alloc/mem_pool/mem_pool.cpp
@@ -29,7 +29,7 @@ Pooling_Allocator::Memory_Block::Memory_Block(void* buf)
* See if ptr is contained by this block
*/
bool Pooling_Allocator::Memory_Block::contains(void* ptr,
- u32bit length) const
+ size_t length) const
{
return ((buffer <= ptr) &&
(buffer_end >= static_cast<byte*>(ptr) + length * BLOCK_SIZE));
@@ -38,7 +38,7 @@ bool Pooling_Allocator::Memory_Block::contains(void* ptr,
/*
* Allocate some memory, if possible
*/
-byte* Pooling_Allocator::Memory_Block::alloc(u32bit n)
+byte* Pooling_Allocator::Memory_Block::alloc(size_t n)
{
if(n == 0 || n > BITMAP_SIZE)
return 0;
@@ -55,7 +55,7 @@ byte* Pooling_Allocator::Memory_Block::alloc(u32bit n)
}
bitmap_type mask = (static_cast<bitmap_type>(1) << n) - 1;
- u32bit offset = 0;
+ size_t offset = 0;
while(bitmap & mask)
{
@@ -78,17 +78,17 @@ byte* Pooling_Allocator::Memory_Block::alloc(u32bit n)
/*
* Mark this memory as free, if we own it
*/
-void Pooling_Allocator::Memory_Block::free(void* ptr, u32bit blocks)
+void Pooling_Allocator::Memory_Block::free(void* ptr, size_t blocks)
{
clear_mem(static_cast<byte*>(ptr), blocks * BLOCK_SIZE);
- const u32bit offset = (static_cast<byte*>(ptr) - buffer) / BLOCK_SIZE;
+ const size_t offset = (static_cast<byte*>(ptr) - buffer) / BLOCK_SIZE;
if(offset == 0 && blocks == BITMAP_SIZE)
bitmap = ~bitmap;
else
{
- for(u32bit j = 0; j != blocks; ++j)
+ for(size_t j = 0; j != blocks; ++j)
bitmap &= ~(static_cast<bitmap_type>(1) << (j+offset));
}
}
@@ -120,7 +120,7 @@ void Pooling_Allocator::destroy()
blocks.clear();
- for(u32bit j = 0; j != allocated.size(); ++j)
+ for(size_t j = 0; j != allocated.size(); ++j)
dealloc_block(allocated[j].first, allocated[j].second);
allocated.clear();
}
@@ -128,16 +128,16 @@ void Pooling_Allocator::destroy()
/*
* Allocation
*/
-void* Pooling_Allocator::allocate(u32bit n)
+void* Pooling_Allocator::allocate(size_t n)
{
- const u32bit BITMAP_SIZE = Memory_Block::bitmap_size();
- const u32bit BLOCK_SIZE = Memory_Block::block_size();
+ const size_t BITMAP_SIZE = Memory_Block::bitmap_size();
+ const size_t BLOCK_SIZE = Memory_Block::block_size();
Mutex_Holder lock(mutex);
if(n <= BITMAP_SIZE * BLOCK_SIZE)
{
- const u32bit block_no = round_up(n, BLOCK_SIZE) / BLOCK_SIZE;
+ const size_t block_no = round_up(n, BLOCK_SIZE) / BLOCK_SIZE;
byte* mem = allocate_blocks(block_no);
if(mem)
@@ -162,10 +162,10 @@ void* Pooling_Allocator::allocate(u32bit n)
/*
* Deallocation
*/
-void Pooling_Allocator::deallocate(void* ptr, u32bit n)
+void Pooling_Allocator::deallocate(void* ptr, size_t n)
{
- const u32bit BITMAP_SIZE = Memory_Block::bitmap_size();
- const u32bit BLOCK_SIZE = Memory_Block::block_size();
+ const size_t BITMAP_SIZE = Memory_Block::bitmap_size();
+ const size_t BLOCK_SIZE = Memory_Block::block_size();
if(ptr == 0 && n == 0)
return;
@@ -176,7 +176,7 @@ void Pooling_Allocator::deallocate(void* ptr, u32bit n)
dealloc_block(ptr, n);
else
{
- const u32bit block_no = round_up(n, BLOCK_SIZE) / BLOCK_SIZE;
+ const size_t block_no = round_up(n, BLOCK_SIZE) / BLOCK_SIZE;
std::vector<Memory_Block>::iterator i =
std::lower_bound(blocks.begin(), blocks.end(), Memory_Block(ptr));
@@ -191,7 +191,7 @@ void Pooling_Allocator::deallocate(void* ptr, u32bit n)
/*
* Try to get some memory from an existing block
*/
-byte* Pooling_Allocator::allocate_blocks(u32bit n)
+byte* Pooling_Allocator::allocate_blocks(size_t n)
{
if(blocks.empty())
return 0;
@@ -219,18 +219,18 @@ byte* Pooling_Allocator::allocate_blocks(u32bit n)
/*
* Allocate more memory for the pool
*/
-void Pooling_Allocator::get_more_core(u32bit in_bytes)
+void Pooling_Allocator::get_more_core(size_t in_bytes)
{
- const u32bit BITMAP_SIZE = Memory_Block::bitmap_size();
- const u32bit BLOCK_SIZE = Memory_Block::block_size();
+ const size_t BITMAP_SIZE = Memory_Block::bitmap_size();
+ const size_t BLOCK_SIZE = Memory_Block::block_size();
- const u32bit TOTAL_BLOCK_SIZE = BLOCK_SIZE * BITMAP_SIZE;
+ const size_t TOTAL_BLOCK_SIZE = BLOCK_SIZE * BITMAP_SIZE;
// upper bound on allocation is 1 MiB
- in_bytes = std::min<u32bit>(in_bytes, 1024 * 1024);
+ in_bytes = std::min<size_t>(in_bytes, 1024 * 1024);
- const u32bit in_blocks = round_up(in_bytes, BLOCK_SIZE) / TOTAL_BLOCK_SIZE;
- const u32bit to_allocate = in_blocks * TOTAL_BLOCK_SIZE;
+ const size_t in_blocks = round_up(in_bytes, BLOCK_SIZE) / TOTAL_BLOCK_SIZE;
+ const size_t to_allocate = in_blocks * TOTAL_BLOCK_SIZE;
void* ptr = alloc_block(to_allocate);
if(ptr == 0)
@@ -238,7 +238,7 @@ void Pooling_Allocator::get_more_core(u32bit in_bytes)
allocated.push_back(std::make_pair(ptr, to_allocate));
- for(u32bit j = 0; j != in_blocks; ++j)
+ for(size_t j = 0; j != in_blocks; ++j)
{
byte* byte_ptr = static_cast<byte*>(ptr);
blocks.push_back(Memory_Block(byte_ptr + j * TOTAL_BLOCK_SIZE));
diff --git a/src/alloc/mem_pool/mem_pool.h b/src/alloc/mem_pool/mem_pool.h
index a67e641e2..28d4dd903 100644
--- a/src/alloc/mem_pool/mem_pool.h
+++ b/src/alloc/mem_pool/mem_pool.h
@@ -22,8 +22,8 @@ namespace Botan {
class Pooling_Allocator : public Allocator
{
public:
- void* allocate(u32bit);
- void deallocate(void*, u32bit);
+ void* allocate(size_t);
+ void deallocate(void*, size_t);
void destroy();
@@ -33,23 +33,23 @@ class Pooling_Allocator : public Allocator
Pooling_Allocator(Mutex* mutex);
~Pooling_Allocator();
private:
- void get_more_core(u32bit);
- byte* allocate_blocks(u32bit);
+ void get_more_core(size_t);
+ byte* allocate_blocks(size_t);
- virtual void* alloc_block(u32bit) = 0;
- virtual void dealloc_block(void*, u32bit) = 0;
+ virtual void* alloc_block(size_t) = 0;
+ virtual void dealloc_block(void*, size_t) = 0;
class Memory_Block
{
public:
Memory_Block(void*);
- static u32bit bitmap_size() { return BITMAP_SIZE; }
- static u32bit block_size() { return BLOCK_SIZE; }
+ static size_t bitmap_size() { return BITMAP_SIZE; }
+ static size_t block_size() { return BLOCK_SIZE; }
- bool contains(void*, u32bit) const;
- byte* alloc(u32bit);
- void free(void*, u32bit);
+ bool contains(void*, size_t) const;
+ byte* alloc(size_t);
+ void free(void*, size_t);
bool operator<(const Memory_Block& other) const
{
@@ -59,8 +59,8 @@ 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;
+ static const size_t BITMAP_SIZE = 8 * sizeof(bitmap_type);
+ static const size_t BLOCK_SIZE = 64;
bitmap_type bitmap;
byte* buffer, *buffer_end;
@@ -68,7 +68,7 @@ class Pooling_Allocator : public Allocator
std::vector<Memory_Block> blocks;
std::vector<Memory_Block>::iterator last_used;
- std::vector<std::pair<void*, u32bit> > allocated;
+ std::vector<std::pair<void*, size_t> > allocated;
Mutex* mutex;
};