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