#include<iostream>
#include<map>
#include "MemoryManager.hpp"
#include "DOMDocumentImpl.hpp"
class DOMAttr{
public:
DOMAttr(int a, int b):_a(a),_b(b){
std::cout<<"constructor of DOMAttr _a="<<_a<<", _b"<<_b<<std::endl;
}
~DOMAttr(){
std::cout<<"deconstructor of DOMAttr"<<std::endl;
}
private:
int _a;
int _b;
};
class DOMElement{
public:
DOMElement(int a, int b, int c, int d):_a(a),_b(b),_c(c),_d(d){
std::cout<<"constructor of DOMElement _a="<<_a<<std::endl;
}
~DOMElement(){
std::cout<<"deconstructor of DOMElement"<<std::endl;
}
private:
int _a;
int _b;
int _c;
int _d;
};
//
// ---------------------------------------------------------------------------//
// Operator new. Global overloaded version, lets any object be allocated on
// the heap owned by a document.
//
// ---------------------------------------------------------------------------
void * operator new(size_t amt, DOMDocumentImpl * doc, NodeObjectType type)
{
std::cout<<"call void * operator new(long unsigned int amt, DOMDocumentImpl * doc, NodeObje ctType type)"<<std::endl;
void* p = doc->allocate(amt, type);
return p;
}
// ---------------------------------------------------------------------------
// For DOM:
// Bypass compiler warning:
// no matching operator delete found; memory will not be freed if initialization throws an exception
// ---------------------------------------------------------------------------
#if !defined(XERCES_NO_MATCHING_DELETE_OPERATOR)
inline void operator delete(void* /*ptr*/, DOMDocumentImpl * /*doc*/)
{
return;
}
#endif
unsigned int alignPointerForNewBlockAllocation(unsigned int ptrSize)
{
// Macro XML_PLATFORM_NEW_BLOCK_ALIGNMENT may be defined
// as needed to dictate alignment requirements on a
// per-architecture basis. In the absense of that we
// take an educated guess.
#ifdef XML_PLATFORM_NEW_BLOCK_ALIGNMENT
static const unsigned int alignment = XML_PLATFORM_NEW_BLOCK_ALIGNMENT;
#else
static const unsigned int alignment = (sizeof(void*) >= sizeof(double)) ? sizeof(void*) : sizeof(double);
#endif
// Calculate current alignment of pointer
unsigned int current = ptrSize % alignment;
// Adjust pointer alignment as needed
return (current == 0)
? ptrSize
: (ptrSize + alignment - current);
}
// The chunk size to allocate from the system allocator.
static unsigned int kInitialHeapAllocSize = 0x40;
static unsigned int kMaxHeapAllocSize = 0x800;
static unsigned int kMaxSubAllocationSize = 0x0100;
// Any request for more bytes than this will be handled by allocating directly with system
DOMDocumentImpl::DOMDocumentImpl(MemoryManager* pmm)
:fMemoryManager(pmm),
fCurrentBlock(0),
fFreePtr(0),
fFreeBytesRemaining(0),
fHeapAllocSize(kInitialHeapAllocSize)
{}
DOMDocumentImpl::~DOMDocumentImpl(){ this->deleteHeap(); }
void DOMDocumentImpl::deleteHeap(){
while (fCurrentBlock != 0)
{
void *nextBlock = *(void **)fCurrentBlock;
std::cout << "delete memory at " << std::hex << fCurrentBlock << std::endl;
fMemoryManager->deallocate(fCurrentBlock);
fCurrentBlock = nextBlock;
}
}
unsigned int DOMDocumentImpl::getMemoryAllocationBlockSize() const {
return fHeapAllocSize;
}
void DOMDocumentImpl::setMemoryAllocationBlockSize(unsigned int size){
if(size>kMaxSubAllocationSize) fHeapAllocSize=size;
}
void* DOMDocumentImpl::allocate(unsigned int amount, NodeObjectType type){
static std::map<int, std::string> maps = {
{NodeObjectType::ATTR_OBJECT , "ATTR_OBJECT"},
{NodeObjectType::ELEMENT_OBJECT , "ELEMENT_OBJECT"}
};
std::cout<<"New for "<<maps[type]<<" size=0x"<<std::hex<<amount<<std::endl;
return allocate(amount);
}
void* DOMDocumentImpl::allocate(unsigned int amount){
// Align the request size so that suballocated blocks
// beyond this one will be maintained at the same alignment.
amount = alignPointerForNewBlockAllocation(amount);
// If the request is for a largish block, hand it off to the system
// allocator. The block still must be linked into the list of
// allocated blocks so that it will be deleted when the time comes.
if (amount > kMaxSubAllocationSize)
{
// The size of the header we add to our raw blocks
unsigned int sizeOfHeader = alignPointerForNewBlockAllocation(sizeof(void *));
// Try to allocate the block
void* newBlock;
newBlock = fMemoryManager->allocate(sizeOfHeader + amount);
std::cout<<"amount="<<amount<<">kMaxSubAllocationSize="<<kMaxSubAllocationSize<<" newBlock="<<std::hex<<newBlock<<" this="<<std::hex<<this<<std::endl;
// Link it into the list beyond current block, as current block
// is still being subdivided. If there is no current block
// then track that we have no bytes to further divide.
if (fCurrentBlock)
{
*(void **)newBlock = *(void **)fCurrentBlock;
*(void **)fCurrentBlock = newBlock;
}
else
{
*(void **)newBlock = 0;
fCurrentBlock = newBlock;
fFreePtr = 0;
fFreeBytesRemaining = 0;
}
void *retPtr = (char*)newBlock + sizeOfHeader;
return retPtr;
}
// It's a normal (sub-allocatable) request.
// Are we out of room in our current block?
if (amount > fFreeBytesRemaining)
{
// Request doesn't fit in the current block.
// The size of the header we add to our raw blocks
unsigned int sizeOfHeader = alignPointerForNewBlockAllocation(sizeof(void *));
// Get a new block from the system allocator.
void* newBlock;
newBlock = fMemoryManager->allocate(fHeapAllocSize);
std::cout<<"amount="<<std::hex<<amount<<">fFreeBytesRemaining="<<fFreeBytesRemaining<<" fHeapAllocSize="<<fHeapAllocSize<<" newBlock="<<std::hex<<newBlock<<" this="<<std::hex<<this<<std::endl;
*(void **)newBlock = fCurrentBlock;
fCurrentBlock = newBlock;
fFreePtr = (char *)newBlock + sizeOfHeader;
fFreeBytesRemaining = fHeapAllocSize - sizeOfHeader;
if(fHeapAllocSize<kMaxHeapAllocSize)
fHeapAllocSize*=2;
}
// Subdivide the request off current block
std::cout<<"subdivide from old block with amount="<<std::hex<<amount<<" old fFreeBytesRemaining="<<fFreeBytesRemaining<<" this="<<std::hex<<this<<std::endl;
void *retPtr = fFreePtr;
fFreePtr += amount;
fFreeBytesRemaining -= amount;
return retPtr;
}
DOMAttr* DOMDocumentImpl::createAttribute(int i){
return new (this, NodeObjectType::ATTR_OBJECT) DOMAttr(i,i);
}
DOMElement* DOMDocumentImpl::createElement(int i){
return new (this, NodeObjectType::ELEMENT_OBJECT) DOMElement(i,i,i,i);
}
没有合适的资源?快使用搜索试试~ 我知道了~
demo_of_xerces-c++_MemoryManagement
需积分: 0 1 下载量 182 浏览量
2022-09-11
00:03:52
上传
评论
收藏 3KB GZ 举报
温馨提示
共7个文件
cpp:3个
hpp:3个
sh:1个
抽取xerces-c++内存管理部分代码+可作为学习demo使用。
资源详情
资源评论
资源推荐
收起资源包目录
xerces_memorymanager.tar.gz (7个子文件)
xerces_memorymanager
compile.sh 34B
DOMDocumentImpl.hpp 877B
main.cpp 647B
MemoryManager.hpp 650B
MemoryManagerImpl.cpp 352B
MemoryManagerImpl.hpp 645B
DOMDocumentImpl.cpp 6KB
共 7 条
- 1
深山老宅
- 粉丝: 1629
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0