// ============================================================================ // Copyright (C) 2001-2012 by Jozef Starosczyk // ixen@copyhandler.com // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU Library General Public License // (version 2) as published by the Free Software Foundation; // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU Library General Public // License along with this program; if not, write to the // Free Software Foundation, Inc., // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // ============================================================================ /// @file TDataBuffer.h /// @date 2012/03/04 /// @brief Contains class representing buffer for data. // ============================================================================ #ifndef __TDATABUFFER_H__ #define __TDATABUFFER_H__ #include "libchcore.h" #include #include BEGIN_CHCORE_NAMESPACE class TDataBufferManager; class TSimpleDataBuffer { public: TSimpleDataBuffer(); ~TSimpleDataBuffer(); LPVOID GetBufferPtr(); void ReleaseBuffer(); private: TSimpleDataBuffer(const TSimpleDataBuffer&); TSimpleDataBuffer& operator=(const TSimpleDataBuffer&); void Initialize(TDataBufferManager& rBufferManager, LPVOID pBuffer, size_t stBufferSize); private: LPVOID m_pBuffer; TDataBufferManager* m_pBufferManager; size_t m_stBufferSize; friend class TDataBufferManager; }; class TDataBufferManager { public: TDataBufferManager(); ~TDataBufferManager(); static bool CheckBufferConfig(size_t& stMaxMemory, size_t& stPageSize, size_t& stBufferSize); static bool CheckBufferConfig(size_t& stMaxMemory); // initialization void Initialize(size_t stMaxMemory); void Initialize(size_t stMaxMemory, size_t stPageSize, size_t stBufferSize); bool IsInitialized() const; // current settings size_t GetMaxMemorySize() const { return m_stMaxMemory; } size_t GetPageSize() const { return m_stPageSize; } size_t GetBufferSize() const { return m_stBufferSize; } // buffer retrieval bool HasFreeBuffer() const; // checks if a buffer is available without allocating any new memory bool GetFreeBuffer(TSimpleDataBuffer& rSimpleBuffer); void ReleaseBuffer(TSimpleDataBuffer& rSimpleBuffer); private: void FreeBuffers(); bool AllocNewPage(); bool CanAllocPage() const; // checks if a buffer can be returned after allocating new page of memory private: std::vector m_vVirtualAllocBlocks; std::list m_listUnusedBuffers; size_t m_stMaxMemory; // maximum amount of memory to use size_t m_stPageSize; // size of a single page of real memory to be allocated (allocation granularity) size_t m_stBufferSize; // size of a single chunk of memory retrievable by caller }; END_CHCORE_NAMESPACE #endif