⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 warmemorybuffer.cpp

📁 ftpserver very good sample
💻 CPP
字号:
#include "StdAfx.h"#include "WarMemoryBuffer.h"   // class implemented/////////////////////////////// PUBLIC ///////////////////////////////////////WAR_CPPT_IMPL WarMemoryBuffer<char>;//============================= LIFECYCLE ====================================template <class T>WarMemoryBuffer<T>::WarMemoryBuffer<T>(size_t AllocSize): mEndOfBlock(NULL),mpNextPtr(NULL),mAllocSize(AllocSize),mTotalAllocatedSize(0){}    template <class T>WarMemoryBuffer<T>::~WarMemoryBuffer<T>(){    Reset();}//============================= OPERATORS ====================================//============================= OPERATIONS ===================================template <class T>void WarMemoryBuffer<T>::Reset(){    while(!mBlocks.empty())    {        delete mBlocks.front();        mBlocks.pop_front();    }    mpNextPtr = NULL;    mAllocSize = 0;    mTotalAllocatedSize = 0;    mEndOfBlock = NULL;}template <class T>T *WarMemoryBuffer<T>::Malloc(size_t Size){    if ((GetFree() * sizeof(T)) < Size)        AllocBlock();        T *p = mpNextPtr;    mpNextPtr += Size;    mTotalAllocatedSize += Size * sizeof(T);    ByteAlign();    return p;}template <class T>T *WarMemoryBuffer<T>::StrDup(std::basic_string<T>& from){    T *p = Malloc(from.size() +1);    memcpy(p, from.c_str(), from.size() * sizeof(T));    p[from.size()] = 0;    return p;}template <class T>char *WarMemoryBuffer<T>::MallocBytes(size_t bytes){    if (GetFree() < bytes)        AllocBlock();        char *p = (char *)mpNextPtr;    mpNextPtr = (T *)(char *)(p + bytes);    mTotalAllocatedSize += bytes;    ByteAlign();    return p;}//============================= CALLBACK   ===================================//============================= ACCESS     ===================================//============================= INQUIRY    ===================================/////////////////////////////// PROTECTED  ////////////////////////////////////////////////////////////////// PRIVATE    ///////////////////////////////////template <class T>void WarMemoryBuffer<T>::ByteAlign(){    if ((int)mpNextPtr % sizeof(int))    {        int AddBytes = sizeof(int) - ((int)mpNextPtr % sizeof(int));        mpNextPtr = (T *)(char *)((char *)mpNextPtr) + AddBytes;        mTotalAllocatedSize += AddBytes;    }}template <class T>void WarMemoryBuffer<T>::AllocBlock(){    mBlocks.push_back(mpNextPtr = new T[mAllocSize]);    mEndOfBlock = (war_ccstr_t)mpNextPtr + mAllocSize;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -