Showing the changes to codec/decoder/core/src/mem_align.cpp (1 of 308 files). Show all
Unified · Split
| @@ -0,0 +1,115 @@ | ||
| 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 | |