📄 allocate.cpp
字号:
/************************************************** Allocator Factory Source File ** (C) 1999-2002 The Botan Project **************************************************/#include <botan/allocate.h>#include <botan/secalloc.h>#include <botan/defalloc.h>#include <botan/exceptn.h>#include <map>namespace Botan {namespace {class AllocatorFactory { public: SecureAllocator* get(const std::string& type) const { std::map<std::string, SecureAllocator*>::const_iterator iter; iter = alloc.find(type); if(iter == alloc.end()) return 0; return iter->second; } void add(const std::string& type, SecureAllocator* allocator) { alloc[type] = allocator; } ~AllocatorFactory() { std::map<std::string, SecureAllocator*>::iterator iter; for(iter = alloc.begin(); iter != alloc.end(); iter++) delete iter->second; } private: std::map<std::string, SecureAllocator*> alloc; };AllocatorFactory* factory;std::string default_type = "default";/************************************************** Try to get an allocator of the specified type **************************************************/SecureAllocator* try_alloc(const std::string& type) { if(!factory) throw Exception("LibraryInitializer has not been created, or it failed"); SecureAllocator* alloc = factory->get(type); if(alloc) return alloc->clone(); return 0; }}/************************************************** Get an allocator **************************************************/SecureAllocator* get_allocator(const std::string& type) { SecureAllocator* alloc; alloc = try_alloc(type); if(alloc) return alloc; alloc = try_alloc(default_type); if(alloc) return alloc; alloc = try_alloc("default"); if(alloc) return alloc; throw Exception("Couldn't find an allocator to use in get_allocator"); }/************************************************** Release an allocator **************************************************/void release_allocator(SecureAllocator* alloc) { if(!alloc->single_copy()) delete alloc; }/************************************************** Set the default allocator type **************************************************/std::string set_default_allocator(const std::string& type) { std::string old_default = default_type; default_type = type; return old_default; }/************************************************** Add new allocator type **************************************************/bool add_allocator_type(const std::string& type, SecureAllocator* alloc) { if(type == "" || factory->get(type)) return false; factory->add(type, alloc); return true; }/************************************************** Initalize the memory subsystem **************************************************/void initalize_memory_subsystem() { factory = new AllocatorFactory; factory->add("default", new Default_Allocator); }/************************************************** Shut down the memory subsystem **************************************************/void shutdown_memory_subsystem() { delete factory; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -