📄 defalloc.cpp
字号:
/************************************************** Default Allocator Source File ** (C) 1999-2002 The Botan Project **************************************************/#include <botan/defalloc.h>#include <cstdlib>#include <cstring>namespace Botan {/************************************************** Allocation **************************************************/void* Default_Allocator::alloc_block(u32bit n) const { if(n == PREF_SIZE) { for(u32bit j = 0; j != CACHE_SIZE; j++) if(cache[j]) { void* ptr = cache[j]; cache[j] = 0; std::memset(ptr, 0, n); return ptr; } } void* ptr = std::malloc(n); if(ptr) std::memset(ptr, 0, n); return ptr; }/************************************************** Deallocation **************************************************/void Default_Allocator::dealloc_block(void* ptr, u32bit n) const { if(n == PREF_SIZE) { for(u32bit j = 0; j != CACHE_SIZE; j++) if(cache[j] == 0) { cache[j] = ptr; return; } } std::memset(ptr, 0, n); std::free(ptr); }/************************************************** Constructor **************************************************/Default_Allocator::Default_Allocator() : ManagedAllocator(true, PREF_SIZE) { for(u32bit j = 0; j != CACHE_SIZE; j++) { cache[j] = std::malloc(PREF_SIZE); if(!cache[j]) throw Memory_Exhaustion(); std::memset(cache[j], 0, PREF_SIZE); } }/************************************************** Destructor **************************************************/Default_Allocator::~Default_Allocator() { for(u32bit j = 0; j != CACHE_SIZE; j++) if(cache[j]) { std::memset(cache[j], 0, PREF_SIZE); std::free(cache[j]); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -