Showing the changes to codec/encoder/core/src/memory_align.cpp (1 of 225 files). Show all
Unified · Split
| @@ -1,161 +1,156 @@ | |||
| 1 | /*! | 1 | /*! |
| 2 | * \copy | 2 | * \copy |
| 3 | * Copyright (c) 2013, Cisco Systems | 3 | * Copyright (c) 2013, Cisco Systems |
| 4 | * All rights reserved. | 4 | * All rights reserved. |
| 5 | * | 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without | 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions | 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: | 8 | * are met: |
| 9 | * | 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright | 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. | 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * | 12 | * |
| 13 | * * Redistributions in binary form must reproduce the above copyright | 13 | * * Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in | 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the | 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. | 16 | * distribution. |
| 17 | * | 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 24 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 24 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | 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 | 28 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | * POSSIBILITY OF SUCH DAMAGE. | 29 | * POSSIBILITY OF SUCH DAMAGE. |
| 30 | * | 30 | * |
| 31 | */ | 31 | */ |
| 32 | 32 | ||
| 33 | #include <stdlib.h> | 33 | #include <stdlib.h> |
| 34 | #include <string.h> | 34 | #include <string.h> |
| 35 | #include <assert.h> | 35 | #include <assert.h> |
| 36 | #include "memory_align.h" | 36 | #include "memory_align.h" |
| 37 | #include "macros.h" | 37 | #include "macros.h" |
| 38 | 38 | ||
| 39 | namespace WelsSVCEnc { | 39 | namespace WelsSVCEnc { |
| 40 | 40 | ||
| 41 | CMemoryAlign::CMemoryAlign( const uint32_t kuiCacheLineSize ) | 41 | CMemoryAlign::CMemoryAlign (const uint32_t kuiCacheLineSize) |
| 42 | #ifdef MEMORY_MONITOR | 42 | #ifdef MEMORY_MONITOR |
| 43 | : m_nMemoryUsageInBytes( 0 ) | 43 | : m_nMemoryUsageInBytes (0) |
| 44 | #endif//MEMORY_MONITOR | 44 | #endif//MEMORY_MONITOR |
| 45 | { | 45 | { |
| 46 | if ( (kuiCacheLineSize == 0) || (kuiCacheLineSize & 0x0f) ) | 46 | if ((kuiCacheLineSize == 0) || (kuiCacheLineSize & 0x0f)) |
| 47 | m_nCacheLineSize = 0x10; | 47 | m_nCacheLineSize = 0x10; |
| 48 | else | 48 | else |
| 49 | m_nCacheLineSize = kuiCacheLineSize; | 49 | m_nCacheLineSize = kuiCacheLineSize; |
| 50 | 50 | ||
| 51 | #ifdef MEMORY_CHECK | 51 | #ifdef MEMORY_CHECK |
| 52 | m_fpMemChkPoint = fopen("./enc_mem_check_point.txt", "wt+"); | 52 | m_fpMemChkPoint = fopen ("./enc_mem_check_point.txt", "wt+"); |
| 53 | m_nCountRequestNum = 0; | 53 | m_nCountRequestNum = 0; |
| 54 | #endif//MEMORY_CHECK | 54 | #endif//MEMORY_CHECK |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | CMemoryAlign::~CMemoryAlign() | 57 | CMemoryAlign::~CMemoryAlign() { |
| 58 | { | 58 | #ifdef MEMORY_MONITOR |
| 59 | #ifdef MEMORY_MONITOR | 59 | assert (m_nMemoryUsageInBytes == 0); |
| 60 | assert( m_nMemoryUsageInBytes == 0 ); | 60 | #endif//MEMORY_MONITOR |
| 61 | #endif//MEMORY_MONITOR | 61 | |
| 62 | 62 | #ifdef MEMORY_CHECK | |
| 63 | #ifdef MEMORY_CHECK | 63 | fclose (m_fpMemChkPoint); |
| 64 | fclose(m_fpMemChkPoint); | 64 | m_fpMemChkPoint = NULL; |
| 65 | m_fpMemChkPoint = NULL; | 65 | |
| 66 | 66 | m_nCountRequestNum = 0; | |
| 67 | m_nCountRequestNum = 0; | 67 | #endif//MEMORY_CHECK |
| 68 | #endif//MEMORY_CHECK | 68 | } |
| 69 | } | 69 | |
| 70 | 70 | void* CMemoryAlign::WelsMallocz (const uint32_t kuiSize, const str_t* kpTag) { | |
| 71 | void* CMemoryAlign::WelsMallocz( const uint32_t kuiSize, const str_t *kpTag ) | 71 | void* pPointer = WelsMalloc (kuiSize, kpTag); |
| 72 | { | 72 | if (NULL == pPointer) { |
| 73 | void *pPointer = WelsMalloc( kuiSize, kpTag ); | 73 | return NULL; |
| 74 | if ( NULL == pPointer ) | 74 | } |
| 75 | { | 75 | // zero memory |
| 76 | return NULL; | 76 | memset (pPointer, 0, kuiSize); |
| 77 | } | 77 | |
| 78 | // zero memory | 78 | return pPointer; |
| 79 | memset( pPointer, 0, kuiSize ); | 79 | } |
| 80 | 80 | ||
| 81 | return pPointer; | 81 | void* CMemoryAlign::WelsMalloc (const uint32_t kuiSize, const str_t* kpTag) { |
| 82 | } | 82 | const int32_t kiSizeOfVoidPointer = sizeof (void**); |
| 83 | 83 | const int32_t kiSizeOfInt = sizeof (int32_t); | |
| 84 | void* CMemoryAlign::WelsMalloc( const uint32_t kuiSize, const str_t *kpTag ) | 84 | const int32_t kiAlignedBytes = m_nCacheLineSize - 1; |
| 85 | { | 85 | const int32_t kiTrialRequestedSize = kuiSize + kiAlignedBytes + kiSizeOfVoidPointer + kiSizeOfInt; |
| 86 | const int32_t kiSizeOfVoidPointer = sizeof( void ** ); | 86 | #if MEMORY_REQUEST_ALIGN_BYTES |
| 87 | const int32_t kiSizeOfInt = sizeof( int32_t ); | 87 | // ensure 4 bytes boundary aligned memory request, unused extra bytes padding in pData payload |
| 88 | const int32_t kiAlignedBytes = m_nCacheLineSize - 1; | 88 | const int32_t kiActualRequestedSize = WELS_ALIGN (kiTrialRequestedSize, MEMORY_REQUEST_ALIGN_BYTES); |
| 89 | const int32_t kiTrialRequestedSize = kuiSize + kiAlignedBytes + kiSizeOfVoidPointer + kiSizeOfInt; | 89 | const uint32_t kiPayloadSize = kuiSize + MEMORY_REQUEST_ALIGN_BYTES - (kiTrialRequestedSize & |
| 90 | #if MEMORY_REQUEST_ALIGN_BYTES | 90 | (MEMORY_REQUEST_ALIGN_BYTES - 1)); |
| 91 | // ensure 4 bytes boundary aligned memory request, unused extra bytes padding in pData payload | 91 | #else |
| 92 | const int32_t kiActualRequestedSize = WELS_ALIGN(kiTrialRequestedSize, MEMORY_REQUEST_ALIGN_BYTES); | 92 | const int32_t kiActualRequestedSize = kiTrialRequestedSize; |
| 93 | const uint32_t kiPayloadSize = kuiSize + MEMORY_REQUEST_ALIGN_BYTES - (kiTrialRequestedSize & (MEMORY_REQUEST_ALIGN_BYTES-1)); | 93 | const uint32_t kiPayloadSize = kuiSize; |
| 94 | #else | 94 | #endif//MEMORY_REQUEST_ALIGN_BYTES |
| 95 | const int32_t kiActualRequestedSize = kiTrialRequestedSize; | 95 | |
| 96 | const uint32_t kiPayloadSize = kuiSize; | 96 | uint8_t* pBuf = (uint8_t*) malloc (kiActualRequestedSize); |
| 97 | #endif//MEMORY_REQUEST_ALIGN_BYTES | 97 | #ifdef MEMORY_CHECK |
| 98 | 98 | if (m_fpMemChkPoint != NULL) { | |
| 99 | uint8_t* pBuf = (uint8_t *) malloc( kiActualRequestedSize ); | 99 | if (kpTag != NULL) |
| 100 | #ifdef MEMORY_CHECK | 100 | fprintf (m_fpMemChkPoint, "WelsMalloc(), 0x%x : actual uiSize:\t%d\tbytes, input uiSize: %d bytes, %d - %s\n", |
| 101 | if (m_fpMemChkPoint != NULL) | 101 | (void*)pBuf, kiActualRequestedSize, kuiSize, m_nCountRequestNum++, kpTag); |
| 102 | { | 102 | else |
| 103 | if ( kpTag != NULL ) | 103 | fprintf (m_fpMemChkPoint, "WelsMalloc(), 0x%x : actual uiSize:\t%d\tbytes, input uiSize: %d bytes, %d \n", (void*)pBuf, |
| 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 ); | 104 | kiActualRequestedSize, kuiSize, m_nCountRequestNum++); |
| 105 | else | 105 | fflush (m_fpMemChkPoint); |
| 106 | fprintf( m_fpMemChkPoint, "WelsMalloc(), 0x%x : actual uiSize:\t%d\tbytes, input uiSize: %d bytes, %d \n", (void *)pBuf, kiActualRequestedSize, kuiSize, m_nCountRequestNum++ ); | 106 | } |
| 107 | fflush( m_fpMemChkPoint); | 107 | #endif |
| 108 | } | 108 | uint8_t* pAlignedBuffer; |
| 109 | #endif | 109 | |
| 110 | uint8_t* pAlignedBuffer; | 110 | if (NULL == pBuf) |
| 111 | 111 | return NULL; | |
| 112 | if ( NULL == pBuf ) | 112 | |
| 113 | return NULL; | 113 | pAlignedBuffer = pBuf + kiAlignedBytes + kiSizeOfVoidPointer + kiSizeOfInt; |
| 114 | 114 | pAlignedBuffer -= ((int32_t) pAlignedBuffer & kiAlignedBytes); | |
| 115 | pAlignedBuffer = pBuf + kiAlignedBytes + kiSizeOfVoidPointer + kiSizeOfInt; | 115 | * ((void**) (pAlignedBuffer - kiSizeOfVoidPointer)) = pBuf; |
| 116 | pAlignedBuffer -= ((int32_t) pAlignedBuffer & kiAlignedBytes); | 116 | * ((int32_t*) (pAlignedBuffer - (kiSizeOfVoidPointer + kiSizeOfInt))) = kiPayloadSize; |
| 117 | *( (void **) ( pAlignedBuffer - kiSizeOfVoidPointer ) ) = pBuf; | 117 | |
| 118 | *( (int32_t *) ( pAlignedBuffer - (kiSizeOfVoidPointer + kiSizeOfInt) ) ) = kiPayloadSize; | 118 | #ifdef MEMORY_MONITOR |
| 119 | 119 | m_nMemoryUsageInBytes += kiActualRequestedSize; | |
| 120 | #ifdef MEMORY_MONITOR | 120 | #endif//MEMORY_MONITOR |
| 121 | m_nMemoryUsageInBytes += kiActualRequestedSize; | 121 | |
| 122 | #endif//MEMORY_MONITOR | 122 | return pAlignedBuffer; |
| 123 | 123 | } | |
| 124 | return pAlignedBuffer; | 124 | |
| 125 | } | 125 | void CMemoryAlign::WelsFree (void* pPointer, const str_t* kpTag) { |
| 126 | 126 | if (pPointer) { | |
| 127 | void CMemoryAlign::WelsFree( void* pPointer, const str_t *kpTag ) | 127 | #ifdef MEMORY_MONITOR |
| 128 | { | 128 | const int32_t kiMemoryLength = * ((int32_t*) ((uint8_t*)pPointer - sizeof (void**) - sizeof ( |
| 129 | if( pPointer ) | 129 | int32_t))) + m_nCacheLineSize - 1 + sizeof (void**) + sizeof (int32_t); |
| 130 | { | 130 | m_nMemoryUsageInBytes -= kiMemoryLength; |
| 131 | #ifdef MEMORY_MONITOR | 131 | #endif//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); | 132 | #ifdef MEMORY_CHECK |
| 133 | m_nMemoryUsageInBytes -= kiMemoryLength; | 133 | if (m_fpMemChkPoint != NULL) { |
| 134 | #endif//MEMORY_MONITOR | 134 | if (kpTag != NULL) |
| 135 | #ifdef MEMORY_CHECK | 135 | fprintf (m_fpMemChkPoint, "WelsFree(), 0x%x - %s: \t%d\t bytes \n", (void*) (* (((void**) pPointer) - 1)), kpTag, |
| 136 | if (m_fpMemChkPoint != NULL) | 136 | kiMemoryLength); |
| 137 | { | 137 | else |
| 138 | if ( kpTag != NULL ) | 138 | fprintf (m_fpMemChkPoint, "WelsFree(), 0x%x \n", (void*) (* (((void**) pPointer) - 1))); |
| 139 | fprintf( m_fpMemChkPoint, "WelsFree(), 0x%x - %s: \t%d\t bytes \n", (void *)(*( ( ( void **) pPointer ) - 1 )), kpTag, kiMemoryLength ); | 139 | fflush (m_fpMemChkPoint); |
| 140 | else | 140 | } |
| 141 | fprintf( m_fpMemChkPoint, "WelsFree(), 0x%x \n", (void *)(*( ( ( void **) pPointer ) - 1 )) ); | 141 | #endif |
| 142 | fflush( m_fpMemChkPoint); | 142 | free (* (((void**) pPointer) - 1)); |
| 143 | } | 143 | } |
| 144 | #endif | 144 | } |
| 145 | free( *( ( ( void **) pPointer ) - 1 ) ); | 145 | |
| 146 | } | 146 | const uint32_t CMemoryAlign::WelsGetCacheLineSize() const { |
| 147 | } | 147 | return m_nCacheLineSize; |
| 148 | 148 | } | |
| 149 | const uint32_t CMemoryAlign::WelsGetCacheLineSize() const | 149 | |
| 150 | { | 150 | #if defined(MEMORY_MONITOR) |
| 151 | return m_nCacheLineSize; | 151 | const uint32_t CMemoryAlign::WelsGetMemoryUsage() const { |
| 152 | } | 152 | return m_nMemoryUsageInBytes; |
| 153 | 153 | } | |
| 154 | #if defined(MEMORY_MONITOR) | 154 | #endif//MEMORY_MONITOR |
| 155 | const uint32_t CMemoryAlign::WelsGetMemoryUsage() const | 155 | |
| 156 | { | 156 | } // end of namespace WelsSVCEnc |
| 157 | return m_nMemoryUsageInBytes; | ||
| 158 | } | ||
| 159 | #endif//MEMORY_MONITOR | ||
| 160 | |||
| 161 | } // end of namespace WelsSVCEnc | ||