1 /*! 2 * \copy 3 * Copyright (c) 2013, Cisco Systems 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 * 31 */ 32 33 #if !defined(WELS_COMMON_MEMORY_ALIGN_H__) 34 #define WELS_COMMON_MEMORY_ALIGN_H__ 35 36 #include "typedefs.h" 37 38 // NOTE: please do not clean below lines even comment, turn on for potential memory leak verify and memory usage monitor etc. 39 //#define MEMORY_CHECK 40 #define MEMORY_MONITOR 41 #ifdef MEMORY_CHECK 42 #ifndef MEMORY_MONITOR 43 #define MEMORY_MONITOR 44 #endif//MEMORY_MONITOR 45 #endif//MEMORY_CHECK 46 47 48 #ifdef MEMORY_CHECK 49 #include <stdio.h> 50 #endif//MEMORY_CHECK 51 52 namespace WelsCommon { 53 54 class CMemoryAlign { 55 public: 56 CMemoryAlign (const uint32_t kuiCacheLineSize); 57 virtual ~CMemoryAlign(); 58 59 void* WelsMallocz (const uint32_t kuiSize, const char* kpTag); 60 void* WelsMalloc (const uint32_t kuiSize, const char* kpTag); 61 void WelsFree (void* pPointer, const char* kpTag); 62 const uint32_t WelsGetCacheLineSize() const; 63 const uint32_t WelsGetMemoryUsage() const; 64 65 private: 66 // private copy & assign constructors adding to fix klocwork scan issues 67 CMemoryAlign (const CMemoryAlign& kcMa); 68 CMemoryAlign& operator= (const CMemoryAlign& kcMa); 69 70 protected: 71 uint32_t m_nCacheLineSize; 72 73 #ifdef MEMORY_MONITOR 74 uint32_t m_nMemoryUsageInBytes; 75 #endif//MEMORY_MONITOR 76 }; 77 78 /*! 79 ************************************************************************************* 80 * \brief malloc with zero filled utilization in Wels 81 * 82 * \param kuiSize size of memory block required 83 * 84 * \return allocated memory pointer exactly, failed in case of NULL return 85 * 86 * \note N/A 87 ************************************************************************************* 88 */ 89 void* WelsMallocz (const uint32_t kuiSize, const char* kpTag); 90 91 /*! 92 ************************************************************************************* 93 * \brief free utilization in Wels 94 * 95 * \param pPtr data pointer to be free. 96 * i.e, uint8_t *pPtr = actual data to be free, argv = &pPtr. 97 * 98 * \return NONE 99 * 100 * \note N/A 101 ************************************************************************************* 102 */ 103 void WelsFree (void* pPtr, const char* kpTag); 104 105 #define WELS_SAFE_FREE(pPtr, pTag) if (pPtr) { WelsFree(pPtr, pTag); pPtr = NULL; } 106 107 #define WELS_NEW_OP(object, type) \ 108 (type*)(new object); 109 110 #define WELS_DELETE_OP(p) \ 111 if(p) delete p; \ 112 p = NULL; 113 114 } 115 116 #endif//WELS_COMMON_MEMORY_ALIGN_H__ 117