Showing the changes to codec/encoder/core/src/memory_align.cpp (1 of 308 files). Show all
Unified · Split
| @@ -0,0 +1,161 @@ | |||
| 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 | #include <stdlib.h> | ||
| 34 | #include <string.h> | ||
| 35 | #include <assert.h> | ||
| 36 | #include "memory_align.h" | ||
| 37 | #include "macros.h" | ||
| 38 | |||
| 39 | namespace WelsSVCEnc { | ||
| 40 | |||
| 41 | CMemoryAlign::CMemoryAlign( const uint32_t kuiCacheLineSize ) | ||
| 42 | #ifdef MEMORY_MONITOR | ||
| 43 | : m_nMemoryUsageInBytes( 0 ) | ||
| 44 | #endif//MEMORY_MONITOR | ||
| 45 | { | ||
| 46 | if ( (kuiCacheLineSize == 0) || (kuiCacheLineSize & 0x0f) ) | ||
| 47 | m_nCacheLineSize = 0x10; | ||
| 48 | else | ||
| 49 | m_nCacheLineSize = kuiCacheLineSize; | ||
| 50 | |||
| 51 | #ifdef MEMORY_CHECK | ||
| 52 | m_fpMemChkPoint = fopen("./enc_mem_check_point.txt", "wt+"); | ||
| 53 | m_nCountRequestNum = 0; | ||
| 54 | #endif//MEMORY_CHECK | ||
| 55 | } | ||
| 56 | |||
| 57 | CMemoryAlign::~CMemoryAlign() | ||
| 58 | { | ||
| 59 | #ifdef MEMORY_MONITOR | ||
| 60 | assert( m_nMemoryUsageInBytes == 0 ); | ||
| 61 | #endif//MEMORY_MONITOR | ||
| 62 | |||
| 63 | #ifdef MEMORY_CHECK | ||
| 64 | fclose(m_fpMemChkPoint); | ||
| 65 | m_fpMemChkPoint = NULL; | ||
| 66 | |||
| 67 | m_nCountRequestNum = 0; | ||
| 68 | #endif//MEMORY_CHECK | ||
| 69 | } | ||
| 70 | |||
| 71 | void* CMemoryAlign::WelsMallocz( const uint32_t kuiSize, const str_t *kpTag ) | ||
| 72 | { | ||
| 73 | void *pPointer = WelsMalloc( kuiSize, kpTag ); | ||
| 74 | if ( NULL == pPointer ) | ||
| 75 | { | ||
| 76 | return NULL; | ||
| 77 | } | ||
| 78 | // zero memory | ||
| 79 | memset( pPointer, 0, kuiSize ); | ||
| 80 | |||
| 81 | return pPointer; | ||
| 82 | } | ||
| 83 | |||
| 84 | void* CMemoryAlign::WelsMalloc( const uint32_t kuiSize, const str_t *kpTag ) | ||
| 85 | { | ||
| 86 | const int32_t kiSizeOfVoidPointer = sizeof( void ** ); | ||
| 87 | const int32_t kiSizeOfInt = sizeof( int32_t ); | ||
| 88 | const int32_t kiAlignedBytes = m_nCacheLineSize - 1; | ||
| 89 | const int32_t kiTrialRequestedSize = kuiSize + kiAlignedBytes + kiSizeOfVoidPointer + kiSizeOfInt; | ||
| 90 | #if MEMORY_REQUEST_ALIGN_BYTES | ||
| 91 | // ensure 4 bytes boundary aligned memory request, unused extra bytes padding in pData payload | ||
| 92 | const int32_t kiActualRequestedSize = WELS_ALIGN(kiTrialRequestedSize, MEMORY_REQUEST_ALIGN_BYTES); | ||
| 93 | const uint32_t kiPayloadSize = kuiSize + MEMORY_REQUEST_ALIGN_BYTES - (kiTrialRequestedSize & (MEMORY_REQUEST_ALIGN_BYTES-1)); | ||
| 94 | #else | ||
| 95 | const int32_t kiActualRequestedSize = kiTrialRequestedSize; | ||
| 96 | const uint32_t kiPayloadSize = kuiSize; | ||
| 97 | #endif//MEMORY_REQUEST_ALIGN_BYTES | ||
| 98 | |||
| 99 | uint8_t* pBuf = (uint8_t *) malloc( kiActualRequestedSize ); | ||
| 100 | #ifdef MEMORY_CHECK | ||
| 101 | if (m_fpMemChkPoint != NULL) | ||
| 102 | { | ||
| 103 | if ( kpTag != NULL ) | ||
| 104 | fprintf( m_fpMemChkPoint, "WelsMalloc(), 0x%x : actual uiSize:\t%d\tbytes, input uiSize: %d bytes, %d - %s\n", (void *)pBuf, kiActualRequestedSize, kuiSize, m_nCountRequestNum++, kpTag ); | ||
| 105 | else | ||
| 106 | fprintf( m_fpMemChkPoint, "WelsMalloc(), 0x%x : actual uiSize:\t%d\tbytes, input uiSize: %d bytes, %d \n", (void *)pBuf, kiActualRequestedSize, kuiSize, m_nCountRequestNum++ ); | ||
| 107 | fflush( m_fpMemChkPoint); | ||
| 108 | } | ||
| 109 | #endif | ||
| 110 | uint8_t* pAlignedBuffer; | ||
| 111 | |||
| 112 | if ( NULL == pBuf ) | ||
| 113 | return NULL; | ||
| 114 | |||
| 115 | pAlignedBuffer = pBuf + kiAlignedBytes + kiSizeOfVoidPointer + kiSizeOfInt; | ||
| 116 | pAlignedBuffer -= ((int32_t) pAlignedBuffer & kiAlignedBytes); | ||
| 117 | *( (void **) ( pAlignedBuffer - kiSizeOfVoidPointer ) ) = pBuf; | ||
| 118 | *( (int32_t *) ( pAlignedBuffer - (kiSizeOfVoidPointer + kiSizeOfInt) ) ) = kiPayloadSize; | ||
| 119 | |||
| 120 | #ifdef MEMORY_MONITOR | ||
| 121 | m_nMemoryUsageInBytes += kiActualRequestedSize; | ||
| 122 | #endif//MEMORY_MONITOR | ||
| 123 | |||
| 124 | return pAlignedBuffer; | ||
| 125 | } | ||
| 126 | |||
| 127 | void CMemoryAlign::WelsFree( void* pPointer, const str_t *kpTag ) | ||
| 128 | { | ||
| 129 | if( pPointer ) | ||
| 130 | { | ||
| 131 | #ifdef MEMORY_MONITOR | ||
| 132 | const int32_t kiMemoryLength = *((int32_t *)((uint8_t *)pPointer- sizeof(void **) - sizeof(int32_t))) + m_nCacheLineSize - 1 + sizeof(void **) + sizeof(int32_t); | ||
| 133 | m_nMemoryUsageInBytes -= kiMemoryLength; | ||
| 134 | #endif//MEMORY_MONITOR | ||
| 135 | #ifdef MEMORY_CHECK | ||
| 136 | if (m_fpMemChkPoint != NULL) | ||
| 137 | { | ||
| 138 | if ( kpTag != NULL ) | ||
| 139 | fprintf( m_fpMemChkPoint, "WelsFree(), 0x%x - %s: \t%d\t bytes \n", (void *)(*( ( ( void **) pPointer ) - 1 )), kpTag, kiMemoryLength ); | ||
| 140 | else | ||
| 141 | fprintf( m_fpMemChkPoint, "WelsFree(), 0x%x \n", (void *)(*( ( ( void **) pPointer ) - 1 )) ); | ||
| 142 | fflush( m_fpMemChkPoint); | ||
| 143 | } | ||
| 144 | #endif | ||
| 145 | free( *( ( ( void **) pPointer ) - 1 ) ); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | const uint32_t CMemoryAlign::WelsGetCacheLineSize() const | ||
| 150 | { | ||
| 151 | return m_nCacheLineSize; | ||
| 152 | } | ||
| 153 | |||
| 154 | #if defined(MEMORY_MONITOR) | ||
| 155 | const uint32_t CMemoryAlign::WelsGetMemoryUsage() const | ||
| 156 | { | ||
| 157 | return m_nMemoryUsageInBytes; | ||
| 158 | } | ||
| 159 | #endif//MEMORY_MONITOR | ||
| 160 | |||
| 161 | } // end of namespace WelsSVCEnc | ||