Showing the changes to codec/decoder/core/src/mem_align.cpp (1 of 225 files). Show all
Unified · Split
| @@ -1,115 +1,109 @@ | |||
| 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 "mem_align.h" | 33 | #include "mem_align.h" |
| 34 | #include "crt_util_safe_x.h" | 34 | #include "crt_util_safe_x.h" |
| 35 | 35 | ||
| 36 | namespace WelsDec { | 36 | namespace WelsDec { |
| 37 | 37 | ||
| 38 | //#define MEMORY_CHECK | 38 | //#define MEMORY_CHECK |
| 39 | #ifdef MEMORY_CHECK | 39 | #ifdef MEMORY_CHECK |
| 40 | 40 | ||
| 41 | WelsFileHandle * pMemCheckMalloc = NULL; | 41 | WelsFileHandle* pMemCheckMalloc = NULL; |
| 42 | WelsFileHandle * pMemCheckFree = NULL; | 42 | WelsFileHandle* pMemCheckFree = NULL; |
| 43 | 43 | ||
| 44 | int32_t iCountMalloc = 0; | 44 | int32_t iCountMalloc = 0; |
| 45 | #endif | 45 | #endif |
| 46 | // | 46 | // |
| 47 | 47 | ||
| 48 | ///////////////////////////////////////////////////////////////////////////////// | 48 | ///////////////////////////////////////////////////////////////////////////////// |
| 49 | #define ALIGNBYTES (16) | 49 | #define ALIGNBYTES (16) |
| 50 | ///////////////////////////////////////////////////////////////////////////////// | 50 | ///////////////////////////////////////////////////////////////////////////////// |
| 51 | 51 | ||
| 52 | void_t * WelsMalloc( const uint32_t kuiSize, const str_t *kpTag ) | 52 | void_t* WelsMalloc (const uint32_t kuiSize, const str_t* kpTag) { |
| 53 | { | 53 | const int32_t kiSizeVoidPtr = sizeof (void_t**); |
| 54 | const int32_t kiSizeVoidPtr = sizeof( void_t ** ); | 54 | const int32_t kiSizeInt = sizeof (int32_t); |
| 55 | const int32_t kiSizeInt = sizeof( int32_t ); | 55 | #ifdef HAVE_CACHE_LINE_ALIGN |
| 56 | #ifdef HAVE_CACHE_LINE_ALIGN | 56 | const int32_t kiAlignBytes = ALIGNBYTES - 1; |
| 57 | const int32_t kiAlignBytes = ALIGNBYTES - 1; | 57 | #else |
| 58 | #else | 58 | const int32_t kiAlignBytes = 15; |
| 59 | const int32_t kiAlignBytes = 15; | 59 | #endif// HAVE_CACHE_LINE_ALIGN |
| 60 | #endif// HAVE_CACHE_LINE_ALIGN | 60 | uint8_t* pBuf = (uint8_t*) malloc (kuiSize + kiAlignBytes + kiSizeVoidPtr + kiSizeInt); |
| 61 | uint8_t* pBuf = (uint8_t *) malloc( kuiSize + kiAlignBytes + kiSizeVoidPtr + kiSizeInt ); | 61 | uint8_t* pAlignBuf; |
| 62 | uint8_t* pAlignBuf; | 62 | |
| 63 | 63 | #ifdef MEMORY_CHECK | |
| 64 | #ifdef MEMORY_CHECK | 64 | if (pMemCheckMalloc == NULL) { |
| 65 | if( pMemCheckMalloc == NULL ){ | 65 | pMemCheckMalloc = WelsFopen (".\\mem_check_malloc.txt", "at+"); |
| 66 | pMemCheckMalloc = WelsFopen(".\\mem_check_malloc.txt", "at+"); | 66 | pMemCheckFree = WelsFopen (".\\mem_check_free.txt", "at+"); |
| 67 | pMemCheckFree = WelsFopen(".\\mem_check_free.txt", "at+"); | 67 | } |
| 68 | } | 68 | |
| 69 | 69 | if (kpTag != NULL) { | |
| 70 | if ( kpTag != NULL ) | 70 | if (pMemCheckMalloc != NULL) { |
| 71 | { | 71 | fprintf (pMemCheckMalloc, "0x%x, size: %d , malloc %s\n", (void_t*)pBuf, |
| 72 | if ( pMemCheckMalloc != NULL ) | 72 | (kuiSize + kiAlignBytes + kiSizeVoidPtr + kiSizeInt), kpTag); |
| 73 | { | 73 | } |
| 74 | fprintf( pMemCheckMalloc, "0x%x, size: %d , malloc %s\n", (void_t *)pBuf, (kuiSize + kiAlignBytes + kiSizeVoidPtr + kiSizeInt), kpTag ); | 74 | if (pMemCheckMalloc != NULL) { |
| 75 | } | 75 | fflush (pMemCheckMalloc); |
| 76 | if ( pMemCheckMalloc != NULL ) | 76 | } |
| 77 | { | 77 | } |
| 78 | fflush( pMemCheckMalloc ); | 78 | #endif |
| 79 | } | 79 | |
| 80 | } | 80 | if (NULL == pBuf) |
| 81 | #endif | 81 | return NULL; |
| 82 | 82 | ||
| 83 | if ( NULL == pBuf ) | 83 | // to fill zero values |
| 84 | return NULL; | 84 | memset (pBuf, 0, kuiSize + kiAlignBytes + kiSizeVoidPtr + kiSizeInt); |
| 85 | 85 | ||
| 86 | // to fill zero values | 86 | pAlignBuf = pBuf + kiAlignBytes + kiSizeVoidPtr + kiSizeInt; |
| 87 | memset( pBuf, 0, kuiSize + kiAlignBytes + kiSizeVoidPtr + kiSizeInt ); | 87 | pAlignBuf -= (int32_t) pAlignBuf & kiAlignBytes; |
| 88 | 88 | * ((void_t**) (pAlignBuf - kiSizeVoidPtr)) = pBuf; | |
| 89 | pAlignBuf = pBuf + kiAlignBytes + kiSizeVoidPtr + kiSizeInt; | 89 | * ((int32_t*) (pAlignBuf - (kiSizeVoidPtr + kiSizeInt))) = kuiSize; |
| 90 | pAlignBuf -= (int32_t) pAlignBuf & kiAlignBytes; | 90 | |
| 91 | *( (void_t **) ( pAlignBuf - kiSizeVoidPtr ) ) = pBuf; | 91 | return (pAlignBuf); |
| 92 | *( (int32_t *) ( pAlignBuf - (kiSizeVoidPtr + kiSizeInt) ) ) = kuiSize; | 92 | } |
| 93 | 93 | ||
| 94 | return (pAlignBuf); | 94 | ///////////////////////////////////////////////////////////////////////////// |
| 95 | } | 95 | |
| 96 | 96 | void_t WelsFree (void_t* pPtr, const str_t* kpTag) { | |
| 97 | ///////////////////////////////////////////////////////////////////////////// | 97 | if (pPtr) { |
| 98 | 98 | #ifdef MEMORY_CHECK | |
| 99 | void_t WelsFree( void_t* pPtr, const str_t *kpTag ) | 99 | if (NULL != pMemCheckFree && kpTag != NULL) { |
| 100 | { | 100 | fprintf (pMemCheckFree, "0x%x, free %s\n", (void_t*) (* (((void_t**) pPtr) - 1)), kpTag); |
| 101 | if( pPtr ) | 101 | fflush (pMemCheckFree); |
| 102 | { | 102 | } |
| 103 | #ifdef MEMORY_CHECK | 103 | #endif |
| 104 | if ( NULL != pMemCheckFree && kpTag != NULL ) | 104 | free (* (((void_t**) pPtr) - 1)); |
| 105 | { | 105 | } |
| 106 | fprintf( pMemCheckFree, "0x%x, free %s\n", (void_t *)(*( ( ( void_t **) pPtr ) - 1 )), kpTag ); | 106 | } |
| 107 | fflush( pMemCheckFree ); | 107 | |
| 108 | } | 108 | ///////////////////////////////////////////////////////////////////////////// |
| 109 | #endif | 109 | } // namespace WelsDec |
| 110 | free( *( ( ( void_t **) pPtr ) - 1 ) ); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | ///////////////////////////////////////////////////////////////////////////// | ||
| 115 | } // namespace WelsDec | ||