diff options
author | lloyd <[email protected]> | 2006-09-11 00:44:28 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2006-09-11 00:44:28 +0000 |
commit | 31170c2210d98c2ea8f3fac56ff5f31e27f741af (patch) | |
tree | b43349234dfb36ae0b6e3ea97923e7f5f52eb06e | |
parent | a563159b999eea2fee0ea94d0baaea06df0ca1df (diff) |
Correctly deal with allocators added post-initialization. In particular,
handle the case where an allocator is added that has the same name as one
already registered.
Flush the cached allocator pointer when the default is changed.
Mark comparison operations in Pooling_Allocator::Memory_Block as inline;
this seems to help the STL sort and binary search algorithms tremendously.
-rw-r--r-- | include/allocate.h | 3 | ||||
-rw-r--r-- | include/libstate.h | 4 | ||||
-rw-r--r-- | include/mem_pool.h | 4 | ||||
-rw-r--r-- | include/secmem.h | 4 | ||||
-rw-r--r-- | src/allocate.cpp | 23 | ||||
-rw-r--r-- | src/defalloc.cpp | 17 | ||||
-rw-r--r-- | src/libstate.cpp | 38 | ||||
-rw-r--r-- | src/mem_pool.cpp | 10 |
8 files changed, 49 insertions, 54 deletions
diff --git a/include/allocate.h b/include/allocate.h index d30e4e0bc..6472924d5 100644 --- a/include/allocate.h +++ b/include/allocate.h @@ -17,6 +17,8 @@ namespace Botan { class Allocator { public: + static Allocator* get(bool); + virtual void* allocate(u32bit) = 0; virtual void deallocate(void*, u32bit) = 0; @@ -31,7 +33,6 @@ class Allocator /************************************************* * Get an allocator * *************************************************/ -Allocator* get_allocator(const std::string& = ""); } diff --git a/include/libstate.h b/include/libstate.h index 5bfb4018d..ece05609a 100644 --- a/include/libstate.h +++ b/include/libstate.h @@ -31,7 +31,8 @@ class Library_State friend class Engine_Iterator; Allocator* get_allocator(const std::string& = "") const; - void add_allocator(Allocator*, bool = false); + void add_allocator(Allocator*); + void set_default_allocator(const std::string&) const; void set_prng(RandomNumberGenerator*); void randomize(byte[], u32bit); @@ -78,6 +79,7 @@ class Library_State class Charset_Transcoder* transcoder; RandomNumberGenerator* rng; + std::vector<Allocator*> allocators; std::vector<EntropySource*> entropy_sources; std::vector<class Engine*> engines; }; diff --git a/include/mem_pool.h b/include/mem_pool.h index 59599d65c..e365cd29d 100644 --- a/include/mem_pool.h +++ b/include/mem_pool.h @@ -46,9 +46,9 @@ class Pooling_Allocator : public Allocator byte* alloc(u32bit) throw(); void free(void*, u32bit) throw(); - bool cmp_mem(const void* x) const { return (*this) < x; } bool operator<(const void*) const; - bool operator<(const Memory_Block&) const; + bool operator<(const Memory_Block& other) const + { return (buffer < other.buffer); } private: typedef u64bit bitmap_type; static const u32bit BITMAP_SIZE = 8 * sizeof(bitmap_type); diff --git a/include/secmem.h b/include/secmem.h index 84c821ec7..7873c8b1f 100644 --- a/include/secmem.h +++ b/include/secmem.h @@ -75,8 +75,8 @@ class MemoryRegion set(copy.buf, copy.used); } - void init(bool lock, u32bit size = 0) - { alloc = get_allocator(lock ? "" : "malloc"); create(size); } + void init(bool locking, u32bit size = 0) + { alloc = Allocator::get(locking); create(size); } private: T* allocate(u32bit n) const { return (T*)alloc->allocate(sizeof(T)*n); } void deallocate(T* p, u32bit n) const diff --git a/src/allocate.cpp b/src/allocate.cpp deleted file mode 100644 index 72b1f873a..000000000 --- a/src/allocate.cpp +++ /dev/null @@ -1,23 +0,0 @@ -/************************************************* -* Allocator Factory Source File * -* (C) 1999-2006 The Botan Project * -*************************************************/ - -#include <botan/allocate.h> -#include <botan/libstate.h> - -namespace Botan { - -/************************************************* -* Get an allocator * -*************************************************/ -Allocator* get_allocator(const std::string& type) - { - Allocator* alloc = global_state().get_allocator(type); - if(alloc) - return alloc; - - throw Exception("Couldn't find an allocator to use in get_allocator"); - } - -} diff --git a/src/defalloc.cpp b/src/defalloc.cpp index ad54fb3ca..5b61e7fac 100644 --- a/src/defalloc.cpp +++ b/src/defalloc.cpp @@ -4,6 +4,7 @@ *************************************************/ #include <botan/defalloc.h> +#include <botan/libstate.h> #include <botan/util.h> #include <cstdlib> #include <cstring> @@ -78,4 +79,20 @@ void Locking_Allocator::dealloc_block(void* ptr, u32bit n) do_free(ptr, n, true); } +/************************************************* +* Get an allocator * +*************************************************/ +Allocator* Allocator::get(bool locking) + { + std::string type = ""; + if(!locking) + type = "malloc"; + + Allocator* alloc = global_state().get_allocator(type); + if(alloc) + return alloc; + + throw Exception("Couldn't find an allocator to use in get_allocator"); + } + } diff --git a/src/libstate.cpp b/src/libstate.cpp index 1bb27e74a..a782f4c60 100644 --- a/src/libstate.cpp +++ b/src/libstate.cpp @@ -110,21 +110,28 @@ Allocator* Library_State::get_allocator(const std::string& type) const /************************************************* * Create a new name to object mapping * *************************************************/ -void Library_State::add_allocator(Allocator* allocator, - bool set_as_default) +void Library_State::add_allocator(Allocator* allocator) { Named_Mutex_Holder lock("allocator"); allocator->init(); - const std::string type = allocator->type(); - if(alloc_factory[type]) - delete alloc_factory[type]; + allocators.push_back(allocator); + alloc_factory[allocator->type()] = allocator; + } - alloc_factory[type] = allocator; +/************************************************* +* Set the default allocator type * +*************************************************/ +void Library_State::set_default_allocator(const std::string& type) const + { + Named_Mutex_Holder lock("allocator"); - if(set_as_default) - config().set("conf", "base/default_allocator", type); + if(type == "") + return; + + config().set("conf", "base/default_allocator", type); + cached_default_allocator = 0; } /************************************************* @@ -307,11 +314,11 @@ void Library_State::load(Modules& modules) set_timer(modules.timer()); set_transcoder(modules.transcoder()); - std::vector<Allocator*> allocators = modules.allocators(); + std::vector<Allocator*> mod_allocs = modules.allocators(); + for(u32bit j = 0; j != mod_allocs.size(); j++) + add_allocator(mod_allocs[j]); - for(u32bit j = 0; j != allocators.size(); j++) - add_allocator(allocators[j], - allocators[j]->type() == modules.default_allocator()); + set_default_allocator(modules.default_allocator()); std::vector<Engine*> engines = modules.engines(); for(u32bit j = 0; j != engines.size(); ++j) @@ -361,11 +368,10 @@ Library_State::~Library_State() cached_default_allocator = 0; - for(std::map<std::string, Allocator*>::iterator j = alloc_factory.begin(); - j != alloc_factory.end(); ++j) + for(u32bit j = 0; j != allocators.size(); j++) { - j->second->destroy(); - delete j->second; + allocators[j]->destroy(); + delete allocators[j]; } std::for_each(locks.begin(), locks.end(), diff --git a/src/mem_pool.cpp b/src/mem_pool.cpp index 00a9f2d06..24866d172 100644 --- a/src/mem_pool.cpp +++ b/src/mem_pool.cpp @@ -52,7 +52,7 @@ Pooling_Allocator::Memory_Block::Memory_Block(void* buf, u32bit map_size, /************************************************* * Compare a Memory_Block with a void pointer * *************************************************/ -bool Pooling_Allocator::Memory_Block::operator<(const void* other) const +inline bool Pooling_Allocator::Memory_Block::operator<(const void* other) const { if(buffer <= other && other < buffer_end) return false; @@ -60,14 +60,6 @@ bool Pooling_Allocator::Memory_Block::operator<(const void* other) const } /************************************************* -* Compare two Memory_Block objects * -*************************************************/ -bool Pooling_Allocator::Memory_Block::operator<(const Memory_Block& blk) const - { - return (buffer < blk.buffer); - } - -/************************************************* * See if ptr is contained by this block * *************************************************/ bool Pooling_Allocator::Memory_Block::contains(void* ptr, |