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