1 /*========================== begin_copyright_notice ============================ 2 3 Copyright (C) 2019-2021 Intel Corporation 4 5 SPDX-License-Identifier: MIT 6 7 ============================= end_copyright_notice ===========================*/ 8 9 #pragma once 10 11 #include "LinearAllocator.h" 12 #include "MemCopy.h" 13 14 namespace iSTD 15 { 16 17 /*****************************************************************************\ 18 19 Class: 20 CBuffer 21 22 Description: 23 Allocates and manages a system memory buffer 24 25 \*****************************************************************************/ 26 template<class CAllocatorType> 27 class CBuffer : public CLinearAllocator<CAllocatorType> 28 { 29 public: 30 31 CBuffer( void ); 32 virtual ~CBuffer( void ); 33 34 bool Allocate( const DWORD allocSize, const DWORD alignSize ); 35 void Deallocate( void ); 36 37 DWORD GetBlockSize( void ) const; 38 void* GetLinearAddress( void ) const; 39 40 protected: 41 42 BYTE* m_pAllocAddress; // Address of allocation pointer 43 }; 44 45 /*****************************************************************************\ 46 47 Function: 48 CBuffer Constructor 49 50 Description: 51 Initializes internal data 52 53 Input: 54 none 55 56 Output: 57 none 58 59 \*****************************************************************************/ 60 template<class CAllocatorType> CBuffer(void)61inline CBuffer<CAllocatorType>::CBuffer( void ) 62 : CLinearAllocator<CAllocatorType>( NULL, 0 ) 63 { 64 m_pAllocAddress = NULL; 65 } 66 67 /*****************************************************************************\ 68 69 Function: 70 CBuffer Destructor 71 72 Description: 73 Deletes internal data 74 75 Input: 76 none 77 78 Output: 79 none 80 81 \*****************************************************************************/ 82 template<class CAllocatorType> ~CBuffer(void)83inline CBuffer<CAllocatorType>::~CBuffer( void ) 84 { 85 Deallocate(); 86 } 87 88 /*****************************************************************************\ 89 90 Function: 91 CBuffer::Allocate 92 93 Description: 94 Allocates memory for the buffer 95 96 Input: 97 const DWORD allocSize - size in bytes 98 const DWORD alignSize - alignment in bytes 99 100 Output: 101 bool - success or fail 102 103 \*****************************************************************************/ 104 template<class CAllocatorType> Allocate(const DWORD allocSize,const DWORD alignSize)105inline bool CBuffer<CAllocatorType>::Allocate( 106 const DWORD allocSize, 107 const DWORD alignSize ) 108 { 109 Deallocate(); 110 111 const DWORD alignedAllocSize = allocSize + alignSize; 112 113 if( alignedAllocSize ) 114 { 115 m_pAllocAddress = (BYTE*)CAllocatorType::Allocate( alignedAllocSize ); 116 117 if( m_pAllocAddress ) 118 { 119 const DWORD offset = ( alignSize ) 120 ? GetAlignmentOffset( m_pAllocAddress, alignSize ) 121 : 0; 122 123 this->m_pBaseAddress = this->m_pAllocAddress + offset; 124 this->m_Size = allocSize; 125 126 SafeMemSet( this->m_pBaseAddress, 0, this->m_Size ); 127 } 128 else 129 { 130 ASSERT(0); 131 this->m_Size = 0; 132 } 133 } 134 else 135 { 136 ASSERT(0); 137 this->m_Size = 0; 138 } 139 140 this->m_SizeUsed = 0; 141 this->m_SizeReserved = 0; 142 143 return ( this->m_Size ) ? true : false; 144 } 145 146 /*****************************************************************************\ 147 148 Function: 149 CBuffer::Deallocate 150 151 Description: 152 Deallocates memory for the buffer 153 154 Input: 155 none 156 157 Output: 158 none 159 160 \*****************************************************************************/ 161 template<class CAllocatorType> Deallocate(void)162inline void CBuffer<CAllocatorType>::Deallocate( void ) 163 { 164 CAllocatorType::Deallocate( m_pAllocAddress ); 165 m_pAllocAddress = NULL; 166 167 this->m_pBaseAddress = NULL; 168 this->m_Size = 0; 169 this->m_SizeUsed = 0; 170 this->m_SizeReserved = 0; 171 } 172 173 /*****************************************************************************\ 174 175 Function: 176 CBuffer::GetBlockSize 177 178 Description: 179 Gets the total size of the buffer 180 181 Input: 182 none 183 184 Output: 185 DWORD - size in bytes 186 187 \*****************************************************************************/ 188 template<class CAllocatorType> GetBlockSize(void)189inline DWORD CBuffer<CAllocatorType>::GetBlockSize( void ) const 190 { 191 return this->m_Size; 192 } 193 194 /*****************************************************************************\ 195 196 Function: 197 CBuffer::GetLinearAddress 198 199 Description: 200 Gets the base address of the buffer 201 202 Input: 203 void 204 205 Output: 206 void* - linear address 207 208 \*****************************************************************************/ 209 template<class CAllocatorType> GetLinearAddress(void)210inline void* CBuffer<CAllocatorType>::GetLinearAddress( void ) const 211 { 212 return (void*)(this->m_pBaseAddress); 213 } 214 215 } // iSTD 216