aboutsummaryrefslogtreecommitdiffstats
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
parentff173a4edb68992d30deed57590fd04c54488a93 (diff)
s/u32bit/size_t/ in alloc
Also handle partial writes in alloc_mmap
-rw-r--r--src/alloc/alloc_mmap/mmap_mem.cpp20
-rw-r--r--src/alloc/alloc_mmap/mmap_mem.h4
-rw-r--r--src/alloc/allocate.h4
-rw-r--r--src/alloc/mem_pool/mem_pool.cpp48
-rw-r--r--src/alloc/mem_pool/mem_pool.h28
-rw-r--r--src/alloc/secmem.h46
-rw-r--r--src/alloc/system_alloc/defalloc.cpp12
-rw-r--r--src/alloc/system_alloc/defalloc.h8
8 files changed, 91 insertions, 79 deletions
diff --git a/src/alloc/alloc_mmap/mmap_mem.cpp b/src/alloc/alloc_mmap/mmap_mem.cpp
index 206b620e4..78177bcdd 100644
--- a/src/alloc/alloc_mmap/mmap_mem.cpp
+++ b/src/alloc/alloc_mmap/mmap_mem.cpp
@@ -15,6 +15,7 @@
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
+#include <errno.h>
#ifndef MAP_FAILED
#define MAP_FAILED -1
@@ -39,7 +40,7 @@ class BOTAN_DLL MemoryMapping_Failed : public Exception
/*
* Memory Map a File into Memory
*/
-void* MemoryMapping_Allocator::alloc_block(u32bit n)
+void* MemoryMapping_Allocator::alloc_block(size_t n)
{
class TemporaryFile
{
@@ -86,8 +87,19 @@ void* MemoryMapping_Allocator::alloc_block(u32bit n)
std::vector<byte> zeros(n);
- if(::write(file.get_fd(), &zeros[0], zeros.size()) != zeros.size())
- throw MemoryMapping_Failed("Could not write to file");
+ ssize_t remaining = n;
+
+ while(remaining)
+ {
+ ssize_t wrote_here = ::write(file.get_fd(),
+ &zeros[0],
+ remaining);
+
+ if(wrote_here == -1 && errno != EINTR)
+ throw MemoryMapping_Failed("Could not write to file");
+
+ remaining -= wrote_here;
+ }
#ifndef MAP_NOSYNC
#define MAP_NOSYNC 0
@@ -107,7 +119,7 @@ void* MemoryMapping_Allocator::alloc_block(u32bit n)
/*
* Remove a Memory Mapping
*/
-void MemoryMapping_Allocator::dealloc_block(void* ptr, u32bit n)
+void MemoryMapping_Allocator::dealloc_block(void* ptr, size_t n)
{
if(ptr == 0)
return;
diff --git a/src/alloc/alloc_mmap/mmap_mem.h b/src/alloc/alloc_mmap/mmap_mem.h
index 521d85ea9..fa4e5e715 100644
--- a/src/alloc/alloc_mmap/mmap_mem.h
+++ b/src/alloc/alloc_mmap/mmap_mem.h
@@ -27,8 +27,8 @@ class MemoryMapping_Allocator : public Pooling_Allocator
MemoryMapping_Allocator(Mutex* mutex) : Pooling_Allocator(mutex) {}
std::string type() const { return "mmap"; }
private:
- void* alloc_block(u32bit);
- void dealloc_block(void*, u32bit);
+ void* alloc_block(size_t);
+ void dealloc_block(void*, size_t);
};
}
diff --git a/src/alloc/allocate.h b/src/alloc/allocate.h
index 5bce1f205..b8574be1e 100644
--- a/src/alloc/allocate.h
+++ b/src/alloc/allocate.h
@@ -33,14 +33,14 @@ class BOTAN_DLL Allocator
* @param n how many bytes to allocate
* @return pointer to n bytes of memory
*/
- virtual void* allocate(u32bit n) = 0;
+ virtual void* allocate(size_t n) = 0;
/**
* Deallocate memory allocated with allocate()
* @param ptr the pointer returned by allocate()
* @param n the size of the block pointed to by ptr
*/
- virtual void deallocate(void* ptr, u32bit n) = 0;
+ virtual void deallocate(void* ptr, size_t n) = 0;
/**
* @return name of this allocator type
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;
};
diff --git a/src/alloc/secmem.h b/src/alloc/secmem.h
index bec20b131..cbc4354ad 100644
--- a/src/alloc/secmem.h
+++ b/src/alloc/secmem.h
@@ -26,7 +26,7 @@ class MemoryRegion
* contains.
* @return size of the buffer
*/
- u32bit size() const { return used; }
+ size_t size() const { return used; }
/**
* Find out whether this buffer is empty.
@@ -48,8 +48,8 @@ class MemoryRegion
operator const T* () const { return buf; }
#else
- T& operator[](u32bit n) { return buf[n]; }
- const T& operator[](u32bit n) const { return buf[n]; }
+ T& operator[](size_t n) { return buf[n]; }
+ const T& operator[](size_t n) const { return buf[n]; }
#endif
@@ -121,7 +121,7 @@ class MemoryRegion
* @param in the array to copy the contents from
* @param n the length of in
*/
- void copy(const T in[], u32bit n)
+ void copy(const T in[], size_t n)
{
copy_mem(buf, in, std::min(n, size()));
}
@@ -135,7 +135,7 @@ class MemoryRegion
* @param in the array to copy the contents from
* @param n the length of in
*/
- void copy(u32bit off, const T in[], u32bit n)
+ void copy(size_t off, const T in[], size_t n)
{
copy_mem(buf + off, in, std::min(n, size() - off));
}
@@ -146,7 +146,7 @@ class MemoryRegion
* @param in the array of objects of type T to copy the contents from
* @param n the size of array in
*/
- void set(const T in[], u32bit n) { resize(n); copy(in, n); }
+ void set(const T in[], size_t n) { resize(n); copy(in, n); }
/**
* Append a single element.
@@ -169,7 +169,7 @@ class MemoryRegion
* set or otherwise zero-initialized
* @param n length of the new buffer
*/
- void resize(u32bit n);
+ void resize(size_t n);
/**
* Swap this buffer with another object.
@@ -196,21 +196,21 @@ class MemoryRegion
* @param locking should we use a locking allocator
* @param length the initial length to use
*/
- void init(bool locking, u32bit length = 0)
+ void init(bool locking, size_t length = 0)
{ alloc = Allocator::get(locking); resize(length); }
private:
- T* allocate(u32bit n)
+ T* allocate(size_t n)
{
return static_cast<T*>(alloc->allocate(sizeof(T)*n));
}
- void deallocate(T* p, u32bit n)
+ void deallocate(T* p, size_t n)
{ if(alloc && p && n) alloc->deallocate(p, sizeof(T)*n); }
T* buf;
- u32bit used;
- u32bit allocated;
+ size_t used;
+ size_t allocated;
Allocator* alloc;
};
@@ -218,11 +218,11 @@ class MemoryRegion
* Change the size of the buffer
*/
template<typename T>
-void MemoryRegion<T>::resize(u32bit n)
+void MemoryRegion<T>::resize(size_t n)
{
if(n <= allocated)
{
- u32bit zap = std::min(used, n);
+ size_t zap = std::min(used, n);
clear_mem(buf + zap, allocated - zap);
used = n;
}
@@ -242,10 +242,10 @@ void MemoryRegion<T>::resize(u32bit n)
template<typename T>
bool MemoryRegion<T>::operator<(const MemoryRegion<T>& other) const
{
- const u32bit min_size = std::min(size(), other.size());
+ const size_t min_size = std::min(size(), other.size());
// This should probably be rewritten to run in constant time
- for(u32bit i = 0; i != min_size; ++i)
+ for(size_t i = 0; i != min_size; ++i)
{
if(buf[i] < other[i])
return true;
@@ -293,7 +293,7 @@ class MemoryVector : public MemoryRegion<T>
* Create a buffer of the specified length.
* @param n the length of the buffer to create.
*/
- MemoryVector(u32bit n = 0) { this->init(false, n); }
+ MemoryVector(size_t n = 0) { this->init(false, n); }
/**
* Create a buffer with the specified contents.
@@ -301,7 +301,7 @@ class MemoryVector : public MemoryRegion<T>
* into the newly created buffer
* @param n the size of the arry in
*/
- MemoryVector(const T in[], u32bit n)
+ MemoryVector(const T in[], size_t n)
{ this->init(false); this->set(in, n); }
/**
@@ -333,7 +333,7 @@ class SecureVector : public MemoryRegion<T>
* Create a buffer of the specified length.
* @param n the length of the buffer to create.
*/
- SecureVector(u32bit n = 0) { this->init(true, n); }
+ SecureVector(size_t n = 0) { this->init(true, n); }
/**
* Create a buffer with the specified contents.
@@ -341,7 +341,7 @@ class SecureVector : public MemoryRegion<T>
* into the newly created buffer
* @param n the size of the array in
*/
- SecureVector(const T in[], u32bit n)
+ SecureVector(const T in[], size_t n)
{
this->init(true);
this->set(&in[0], n);
@@ -363,7 +363,7 @@ template<typename T>
MemoryRegion<T>& operator+=(MemoryRegion<T>& out,
const MemoryRegion<T>& in)
{
- const u32bit copy_offset = out.size();
+ const size_t copy_offset = out.size();
out.resize(out.size() + in.size());
copy_mem(&out[copy_offset], &in[0], in.size());
return out;
@@ -381,7 +381,7 @@ template<typename T, typename L>
MemoryRegion<T>& operator+=(MemoryRegion<T>& out,
const std::pair<const T*, L>& in)
{
- const u32bit copy_offset = out.size();
+ const size_t copy_offset = out.size();
out.resize(out.size() + in.second);
copy_mem(&out[copy_offset], in.first, in.second);
return out;
@@ -391,7 +391,7 @@ template<typename T, typename L>
MemoryRegion<T>& operator+=(MemoryRegion<T>& out,
const std::pair<T*, L>& in)
{
- const u32bit copy_offset = out.size();
+ const size_t copy_offset = out.size();
out.resize(out.size() + in.second);
copy_mem(&out[copy_offset], in.first, in.second);
return out;
diff --git a/src/alloc/system_alloc/defalloc.cpp b/src/alloc/system_alloc/defalloc.cpp
index 3161716b9..8e178bb14 100644
--- a/src/alloc/system_alloc/defalloc.cpp
+++ b/src/alloc/system_alloc/defalloc.cpp
@@ -18,7 +18,7 @@ namespace {
/*
* Perform Memory Allocation
*/
-void* do_malloc(u32bit n, bool do_lock)
+void* do_malloc(size_t n, bool do_lock)
{
void* ptr = std::malloc(n);
@@ -35,7 +35,7 @@ void* do_malloc(u32bit n, bool do_lock)
/*
* Perform Memory Deallocation
*/
-void do_free(void* ptr, u32bit n, bool do_lock)
+void do_free(void* ptr, size_t n, bool do_lock)
{
if(!ptr)
return;
@@ -52,7 +52,7 @@ void do_free(void* ptr, u32bit n, bool do_lock)
/*
* Malloc_Allocator's Allocation
*/
-void* Malloc_Allocator::allocate(u32bit n)
+void* Malloc_Allocator::allocate(size_t n)
{
void* ptr = do_malloc(n, false);
if(!ptr)
@@ -63,7 +63,7 @@ void* Malloc_Allocator::allocate(u32bit n)
/*
* Malloc_Allocator's Deallocation
*/
-void Malloc_Allocator::deallocate(void* ptr, u32bit n)
+void Malloc_Allocator::deallocate(void* ptr, size_t n)
{
do_free(ptr, n, false);
}
@@ -71,7 +71,7 @@ void Malloc_Allocator::deallocate(void* ptr, u32bit n)
/*
* Locking_Allocator's Allocation
*/
-void* Locking_Allocator::alloc_block(u32bit n)
+void* Locking_Allocator::alloc_block(size_t n)
{
return do_malloc(n, true);
}
@@ -79,7 +79,7 @@ void* Locking_Allocator::alloc_block(u32bit n)
/*
* Locking_Allocator's Deallocation
*/
-void Locking_Allocator::dealloc_block(void* ptr, u32bit n)
+void Locking_Allocator::dealloc_block(void* ptr, size_t n)
{
do_free(ptr, n, true);
}
diff --git a/src/alloc/system_alloc/defalloc.h b/src/alloc/system_alloc/defalloc.h
index dcf552b8b..6fba0e511 100644
--- a/src/alloc/system_alloc/defalloc.h
+++ b/src/alloc/system_alloc/defalloc.h
@@ -18,8 +18,8 @@ namespace Botan {
class Malloc_Allocator : public Allocator
{
public:
- void* allocate(u32bit);
- void deallocate(void*, u32bit);
+ void* allocate(size_t);
+ void deallocate(void*, size_t);
std::string type() const { return "malloc"; }
};
@@ -37,8 +37,8 @@ class Locking_Allocator : public Pooling_Allocator
std::string type() const { return "locking"; }
private:
- void* alloc_block(u32bit);
- void dealloc_block(void*, u32bit);
+ void* alloc_block(size_t);
+ void dealloc_block(void*, size_t);
};
}