//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <fstream>
#include "mem_store.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
CMemStore::CMemStore():
buff(NULL),
currPtr(NULL),
currSize(0),
currFreeLimit(0),
bIsMemOwner(true) //螂囀蜘 憐詭� 擋縉褒�詼靦�� 鋒��靦� 頡 篛蝦憤 檀憐隧粵斡
{};
CMemStore::CMemStore(void *ptr, unsigned int size): //慚摟廓鞅頑甕� 螂閤羲齷 鋒��錚 溫� 餾粵�� 滓瘩贖 愿 獗�
buff(reinterpret_cast<unsigned char*>(ptr)),
currPtr(buff),
currSize(size),
currFreeLimit(size),
bIsMemOwner(false) //螂囀蜘� 獗鼻�� 擋縉褒�詼靦�� 鋒��靦� - 擋菔錶粳 � 擇詬慮 餾粵��
{
}
//------------------------------
CMemStore::~CMemStore()
{
if(bIsMemOwner && (buff != NULL))
delete[] buff;
};
//-------頑震艘 慚縴整皖態 � 螂囀蜘------------
int CMemStore::write(void *ptr, unsigned int _size)
{
if(bIsMemOwner && buff == NULL) //羨� 璃雛廈 獗 頑震興鈇鞅 - 順滇��粳 鋒��靦
{
currSize = 1024;
buff = new unsigned char[currSize]; //頡-擭蝓闐璃� 1蒞
currPtr = buff;
currFreeLimit = currSize;
}
if(currFreeLimit < _size) //����錚 獗避痞琯�
{
if(!bIsMemOwner)
return 1;
unsigned int newSize = currSize; //瘟順� 擋髡綑
unsigned int fillPart = currSize - currFreeLimit; //羲蝓讒� 篦碟痘 頑��錙
unsigned int newFreeLim = currFreeLimit; //羲蝓讒� 蛭滇� 篛螂蝠瘟 頡縛� 閱擇痘頜窟粱粵��
do
{
newSize *=2; //戴粱摺弊琯� 鋒��靦 � 2 擋頑
newFreeLim = newSize - fillPart; //瘟順� 螂囀� 篛螂蝠瘟廈 慮羲�
}while(newFreeLim < _size);
unsigned char *newBuff = new unsigned char [newSize]; //瘟順� 蛭罄綑
CopyMemory(newBuff, buff, fillPart); //閱擇瘟� 滓瘩贖 � 瘟順� 蛭繆�
delete[] buff; //羲痙癮 蛭繆� 褓隆菔誅琯�
buff = newBuff; //閱擇熱羲擋弊琯� 閱擇慮瘩灘 -> 熱闐餃 順滇雒瘩蝗 鋒��錚
currPtr = buff + fillPart; //閱擇熱羲擋弊琯� 閱擇慮瘩灘 -> 熱闐餃 鋒��錚 蝕滓 皚豢� 頑震興鈇靦 滓瘩灘
currSize = newSize; //閱擇熱羲擋弊琯� 閱擇慮瘩灘 -> 螂孃� 擋髡綑 順滇雒瘩蝗 鋒��錚
currFreeLimit = currSize - fillPart; //閱擇熱羲擋弊琯� 閱擇慮瘩灘 -> 蜻鞅雛羲隆 篛螂蝠瘟� 鋒��錚
}
CopyMemory(currPtr, ptr, _size); //閱擇瘟� 滓瘩贖
currPtr += _size; //閱擇熱羲擋弊琯� 閱擇慮瘩灘 -> 熱闐餃 鋒��錚 蝕滓 皚豢� 頑震興鈇靦 滓瘩灘
currFreeLimit -= _size; //閱擇熱羲擋弊琯� 閱擇慮瘩灘 -> 蜻鞅雛羲隆 篛螂蝠瘟� 鋒��錚
return 0;
};
//------------------餾粵徹 滓瘩贖 愿 螂囀蜘�--------------------------------
int CMemStore::read(void *ptr, unsigned int _size)
{
CopyMemory(ptr, currPtr, _size); //閱擇瘟� 滓瘩贖
if(currFreeLimit >= _size) //頡蒙 羨� 熱錨溘憬� � 頜窟粱發 順滇雒瘩蝗 鋒��錚
{
currPtr += _size; //閱擇熱羲擋弊琯� 閱擇慮瘩灘 -> 熱闐餃 鋒��錚 褕蝕滓 憐豢� 鞣錶靦(頑震興鈇靦)
currFreeLimit -= _size; //閱擇熱羲擋弊琯� 閱擇慮瘩灘 -> 蜻鞅雛羲隆 篛螂蝠瘟� 鋒��錚
return 0;
}else //餾粵徹 頜蝙麂藝� 頑 頜窟粱番� 順滇雒瘟� 鋒��錚
{
currPtr = buff + currSize; //閱擇熱羲擋弊琯� 閱擇慮瘩灘 -> 擱琦痞粱� 溫� 鋼蝕孃� 複綑皖慷 � 蜻獗� 鋒��錚
currFreeLimit = 0; //閱擇熱羲擋弊琯� 閱擇慮瘩灘 -> 篛螂蝠瘟� 鋒��錚 怔�
return 1;
}
};
//-----------檀錶瘟階� 頡飾甕� 溫� 餾粵��/頑震縊-----------------------
int CMemStore::setPosition(unsigned int position)
{
if(position < currSize)
{
currPtr = buff + position; //閱擇熱羲擋弊琯� 閱擇慮瘩灘 -> 熱闐餃 鋒��錚 褕蝕滓 憐豢� 鞣錶靦(頑震興鈇靦)
currFreeLimit = currSize - position; //閱擇熱羲擋弊琯� 閱擇慮瘩灘 -> 篛螂蝠瘟� 鋒��錚 怔�
return 0;
}else
{
currPtr = buff + currSize; //閱擇熱羲擋弊琯� 閱擇慮瘩灘 -> 擱琦痞粱� 溫� 鋼蝕孃� 複綑皖慷 � 蜻獗� 鋒��錚
currFreeLimit = 0; //閱擇熱羲擋弊琯� 閱擇慮瘩灘 -> 篛螂蝠瘟� 鋒��錚 怔�
return 1;
}
};