defalloc.cpp
来自「含有多种公开密钥算法、多种块加密、多种数据流加密、多种HASH函数、多种Chec」· C++ 代码 · 共 80 行
CPP
80 行
/************************************************** 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 + =
减小字号Ctrl + -
显示快捷键?