nengel@2: /* nengel@2: * copyright (c) 2006 Michael Niedermayer nengel@2: * nengel@2: * This file is part of FFmpeg. nengel@2: * nengel@2: * FFmpeg is free software; you can redistribute it and/or nengel@2: * modify it under the terms of the GNU Lesser General Public nengel@2: * License as published by the Free Software Foundation; either nengel@2: * version 2.1 of the License, or (at your option) any later version. nengel@2: * nengel@2: * FFmpeg is distributed in the hope that it will be useful, nengel@2: * but WITHOUT ANY WARRANTY; without even the implied warranty of nengel@2: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU nengel@2: * Lesser General Public License for more details. nengel@2: * nengel@2: * You should have received a copy of the GNU Lesser General Public nengel@2: * License along with FFmpeg; if not, write to the Free Software nengel@2: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA nengel@2: */ nengel@2: nengel@2: /** nengel@2: * @file nengel@2: * memory handling functions nengel@2: */ nengel@2: nengel@2: #ifndef AVUTIL_MEM_H nengel@2: #define AVUTIL_MEM_H nengel@2: nengel@2: #include "attributes.h" nengel@2: #include "config.h" nengel@2: nengel@2: #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v nengel@2: #define DECLARE_ALIGNED_16(t,v) t __attribute__ ((aligned (16))) v nengel@2: #define DECLARE_ASM_CONST(n,t,v) static const t __attribute__((used)) __attribute__ ((aligned (n))) v nengel@2: nengel@2: #if AV_GCC_VERSION_AT_LEAST(3,1) nengel@2: #define av_malloc_attrib __attribute__((__malloc__)) nengel@2: #else nengel@2: #define av_malloc_attrib nengel@2: #endif nengel@2: nengel@2: /** nengel@2: * Allocates a block of size bytes with alignment suitable for all nengel@2: * memory accesses (including vectors if available on the CPU). nengel@2: * @param size Size in bytes for the memory block to be allocated. nengel@2: * @return Pointer to the allocated block, NULL if the block cannot nengel@2: * be allocated. nengel@2: * @see av_mallocz() nengel@2: */ nengel@2: void *av_malloc(unsigned int size) av_malloc_attrib; nengel@2: nengel@2: /** nengel@2: * Allocates or reallocates a block of memory. nengel@2: * If ptr is NULL and size > 0, allocates a new block. If nengel@2: * size is zero, frees the memory block pointed to by ptr. nengel@2: * @param size Size in bytes for the memory block to be allocated or nengel@2: * reallocated. nengel@2: * @param ptr Pointer to a memory block already allocated with nengel@2: * av_malloc(z)() or av_realloc() or NULL. nengel@2: * @return Pointer to a newly reallocated block or NULL if the block nengel@2: * cannot be reallocated or the function is used to free the memory block. nengel@2: * @see av_fast_realloc() nengel@2: */ nengel@2: void *av_realloc(void *ptr, unsigned int size); nengel@2: nengel@2: /** nengel@2: * Reallocates the given block if it is not large enough, otherwise it nengel@2: * does nothing. nengel@2: * nengel@2: * @see av_realloc nengel@2: */ nengel@2: void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size); nengel@2: nengel@2: /** nengel@2: * Allocates a buffer, reusing the given one if large enough. nengel@2: * nengel@2: * Contrary to av_fast_realloc the current buffer contents might not be nengel@2: * preserved and on error the old buffer is freed, thus no special nengel@2: * handling to avoid memleaks is necessary. nengel@2: * nengel@2: * @param ptr pointer to pointer to already allocated buffer, overwritten with pointer to new buffer nengel@2: * @param size size of the buffer *ptr points to nengel@2: * @param min_size minimum size of *ptr buffer after returning, *ptr will be NULL and nengel@2: * *size 0 if an error occurred. nengel@2: */ nengel@2: void av_fast_malloc(void *ptr, unsigned int *size, unsigned int min_size); nengel@2: nengel@2: /** nengel@2: * Frees a memory block which has been allocated with av_malloc(z)() or nengel@2: * av_realloc(). nengel@2: * @param ptr Pointer to the memory block which should be freed. nengel@2: * @note ptr = NULL is explicitly allowed. nengel@2: * @note It is recommended that you use av_freep() instead. nengel@2: * @see av_freep() nengel@2: */ nengel@2: nengel@2: void av_free(void *ptr); nengel@2: nengel@2: /** nengel@2: * Allocates a block of size bytes with alignment suitable for all nengel@2: * memory accesses (including vectors if available on the CPU) and nengel@2: * zeroes all the bytes of the block. nengel@2: * @param size Size in bytes for the memory block to be allocated. nengel@2: * @return Pointer to the allocated block, NULL if it cannot be allocated. nengel@2: * @see av_malloc() nengel@2: */ nengel@2: void *av_mallocz(unsigned int size) av_malloc_attrib; nengel@2: nengel@2: /** nengel@2: * Duplicates the string s. nengel@2: * @param s string to be duplicated nengel@2: * @return Pointer to a newly allocated string containing a nengel@2: * copy of s or NULL if the string cannot be allocated. nengel@2: */ nengel@2: char *av_strdup(const char *s) av_malloc_attrib; nengel@2: nengel@2: /** nengel@2: * Frees a memory block which has been allocated with av_malloc(z)() or nengel@2: * av_realloc() and set the pointer pointing to it to NULL. nengel@2: * @param ptr Pointer to the pointer to the memory block which should nengel@2: * be freed. nengel@2: * @see av_free() nengel@2: */ nengel@2: void av_freep(void *ptr); nengel@2: nengel@2: nengel@2: static av_always_inline uint32_t pack16to32(int a, int b){ nengel@2: #if HAVE_BIGENDIAN nengel@2: return (b&0xFFFF) + (a<<16); nengel@2: #else nengel@2: return (a&0xFFFF) + (b<<16); nengel@2: #endif nengel@2: } nengel@2: nengel@2: static av_always_inline uint16_t pack8to16(int a, int b){ nengel@2: #if HAVE_BIGENDIAN nengel@2: return (b&0xFF) + (a<<8); nengel@2: #else nengel@2: return (a&0xFF) + (b<<8); nengel@2: #endif nengel@2: } nengel@2: nengel@2: #endif /* AVUTIL_MEM_H */