📄 brewallocator.h
字号:
/*
* Copyright (C) 2004 Radu Braniste (rbraniste@epicad.com)
* All Rights Reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of the Author nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/ #ifndef _BREW_ALLOCATOR_h_#define _BREW_ALLOCATOR_h_//#include "AEEError.h"#include "BrewVector.h"template <typename T, T CHUNKS , T CHUNK_SZ = 4, size_t POOL_SIZE = CHUNKS*CHUNK_SZ>class BrewAllocator{public: bool isInPool(void* p) { return ((p >= pool_) && ( p < pool_+POOL_SIZE)); } void* allocate(size_t sz) { return findBestFit(sz); } void deallocate(void* p) { restorePool(p); } ~BrewAllocator() { unregisterResources(); } BrewAllocator() : pool_(new char[POOL_SIZE]) { resources_.ensureCapacity(CHUNKS); for(UINT i = 0 ; i < CHUNKS; ++i) { resources_.append(0); } }private: void unregisterResources() { delete[] pool_; } void* findBestFit( size_t sz) { if (!sz) return 0; void* fit = 0; size_t dif = sz/CHUNK_SZ; if ( (dif==0) || ((sz%CHUNK_SZ) > 0) ) dif++; if (dif > CHUNKS) return 0; UINT i = 0; while( i < resources_.size() ) { if (resources_[i] == 0) { int idx = i; for(UINT k = 1 ; k <= dif; ++k) { if (resources_[i] == 0) { if (k == dif) { fit = pool_ + CHUNK_SZ * idx; } else { ++i; } } else { fit = 0; break; } } if (fit) { resources_[idx] = dif; for(UINT j = 1 ; j < dif; ++j) { resources_[idx+j] = 1; } break; } } else { ++i; } } return fit; } void restorePool(void* p) { if (!p) return; int i = ((char*)p - pool_) / CHUNK_SZ; int dif = resources_[i] ; for(int k = 0 ; k < dif; ++k) { resources_[i+k] = 0; } }private: char* pool_; BrewVector<int> resources_;private: BrewAllocator( const BrewAllocator &value ); const BrewAllocator &operator = ( const BrewAllocator &rhs );};typedef BrewAllocator<unsigned char, 128> Allocator;template <size_t POOL_SIZE>class DiscardableHeap{public: void* allocate(size_t sz) { if (allocated_ + sz > POOL_SIZE) return 0; void * p = &pool_[0] + allocated_; allocated_ += sz; return p; } void deallocate() { allocated_ = 0; } ~DiscardableHeap() { } DiscardableHeap() :allocated_(0) { pool_.ensureCapacity(POOL_SIZE); }private: BrewVector<char> pool_; size_t allocated_;private: DiscardableHeap( const DiscardableHeap &value ); const DiscardableHeap &operator = ( const DiscardableHeap &rhs );};typedef DiscardableHeap<1024> TempHeap;#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -