yading@11: /* yading@11: * copyright (c) 2006 Michael Niedermayer yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: /** yading@11: * @file yading@11: * memory handling functions yading@11: */ yading@11: yading@11: #ifndef AVUTIL_MEM_H yading@11: #define AVUTIL_MEM_H yading@11: yading@11: #include yading@11: #include yading@11: yading@11: #include "attributes.h" yading@11: #include "error.h" yading@11: #include "avutil.h" yading@11: yading@11: /** yading@11: * @addtogroup lavu_mem yading@11: * @{ yading@11: */ yading@11: yading@11: yading@11: #if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1110 || defined(__SUNPRO_C) yading@11: #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v yading@11: #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v yading@11: #elif defined(__TI_COMPILER_VERSION__) yading@11: #define DECLARE_ALIGNED(n,t,v) \ yading@11: AV_PRAGMA(DATA_ALIGN(v,n)) \ yading@11: t __attribute__((aligned(n))) v yading@11: #define DECLARE_ASM_CONST(n,t,v) \ yading@11: AV_PRAGMA(DATA_ALIGN(v,n)) \ yading@11: static const t __attribute__((aligned(n))) v yading@11: #elif defined(__GNUC__) yading@11: #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v yading@11: #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v yading@11: #elif defined(_MSC_VER) yading@11: #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v yading@11: #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v yading@11: #else yading@11: #define DECLARE_ALIGNED(n,t,v) t v yading@11: #define DECLARE_ASM_CONST(n,t,v) static const t v yading@11: #endif yading@11: yading@11: #if AV_GCC_VERSION_AT_LEAST(3,1) yading@11: #define av_malloc_attrib __attribute__((__malloc__)) yading@11: #else yading@11: #define av_malloc_attrib yading@11: #endif yading@11: yading@11: #if AV_GCC_VERSION_AT_LEAST(4,3) yading@11: #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__))) yading@11: #else yading@11: #define av_alloc_size(...) yading@11: #endif yading@11: yading@11: /** yading@11: * Allocate a block of size bytes with alignment suitable for all yading@11: * memory accesses (including vectors if available on the CPU). yading@11: * @param size Size in bytes for the memory block to be allocated. yading@11: * @return Pointer to the allocated block, NULL if the block cannot yading@11: * be allocated. yading@11: * @see av_mallocz() yading@11: */ yading@11: void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1); yading@11: yading@11: /** yading@11: * Helper function to allocate a block of size * nmemb bytes with yading@11: * using av_malloc() yading@11: * @param nmemb Number of elements yading@11: * @param size Size of the single element yading@11: * @return Pointer to the allocated block, NULL if the block cannot yading@11: * be allocated. yading@11: * @see av_malloc() yading@11: */ yading@11: av_alloc_size(1, 2) static inline void *av_malloc_array(size_t nmemb, size_t size) yading@11: { yading@11: if (size <= 0 || nmemb >= INT_MAX / size) yading@11: return NULL; yading@11: return av_malloc(nmemb * size); yading@11: } yading@11: yading@11: /** yading@11: * Allocate or reallocate a block of memory. yading@11: * If ptr is NULL and size > 0, allocate a new block. If yading@11: * size is zero, free the memory block pointed to by ptr. yading@11: * @param ptr Pointer to a memory block already allocated with yading@11: * av_malloc(z)() or av_realloc() or NULL. yading@11: * @param size Size in bytes for the memory block to be allocated or yading@11: * reallocated. yading@11: * @return Pointer to a newly reallocated block or NULL if the block yading@11: * cannot be reallocated or the function is used to free the memory block. yading@11: * @see av_fast_realloc() yading@11: */ yading@11: void *av_realloc(void *ptr, size_t size) av_alloc_size(2); yading@11: yading@11: /** yading@11: * Allocate or reallocate a block of memory. yading@11: * This function does the same thing as av_realloc, except: yading@11: * - It takes two arguments and checks the result of the multiplication for yading@11: * integer overflow. yading@11: * - It frees the input block in case of failure, thus avoiding the memory yading@11: * leak with the classic "buf = realloc(buf); if (!buf) return -1;". yading@11: */ yading@11: void *av_realloc_f(void *ptr, size_t nelem, size_t elsize); yading@11: yading@11: /** yading@11: * Free a memory block which has been allocated with av_malloc(z)() or yading@11: * av_realloc(). yading@11: * @param ptr Pointer to the memory block which should be freed. yading@11: * @note ptr = NULL is explicitly allowed. yading@11: * @note It is recommended that you use av_freep() instead. yading@11: * @see av_freep() yading@11: */ yading@11: void av_free(void *ptr); yading@11: yading@11: /** yading@11: * Allocate a block of size bytes with alignment suitable for all yading@11: * memory accesses (including vectors if available on the CPU) and yading@11: * zero all the bytes of the block. yading@11: * @param size Size in bytes for the memory block to be allocated. yading@11: * @return Pointer to the allocated block, NULL if it cannot be allocated. yading@11: * @see av_malloc() yading@11: */ yading@11: void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1); yading@11: yading@11: /** yading@11: * Allocate a block of nmemb * size bytes with alignment suitable for all yading@11: * memory accesses (including vectors if available on the CPU) and yading@11: * zero all the bytes of the block. yading@11: * The allocation will fail if nmemb * size is greater than or equal yading@11: * to INT_MAX. yading@11: * @param nmemb yading@11: * @param size yading@11: * @return Pointer to the allocated block, NULL if it cannot be allocated. yading@11: */ yading@11: void *av_calloc(size_t nmemb, size_t size) av_malloc_attrib; yading@11: yading@11: /** yading@11: * Helper function to allocate a block of size * nmemb bytes with yading@11: * using av_mallocz() yading@11: * @param nmemb Number of elements yading@11: * @param size Size of the single element yading@11: * @return Pointer to the allocated block, NULL if the block cannot yading@11: * be allocated. yading@11: * @see av_mallocz() yading@11: * @see av_malloc_array() yading@11: */ yading@11: av_alloc_size(1, 2) static inline void *av_mallocz_array(size_t nmemb, size_t size) yading@11: { yading@11: if (size <= 0 || nmemb >= INT_MAX / size) yading@11: return NULL; yading@11: return av_mallocz(nmemb * size); yading@11: } yading@11: yading@11: /** yading@11: * Duplicate the string s. yading@11: * @param s string to be duplicated yading@11: * @return Pointer to a newly allocated string containing a yading@11: * copy of s or NULL if the string cannot be allocated. yading@11: */ yading@11: char *av_strdup(const char *s) av_malloc_attrib; yading@11: yading@11: /** yading@11: * Free a memory block which has been allocated with av_malloc(z)() or yading@11: * av_realloc() and set the pointer pointing to it to NULL. yading@11: * @param ptr Pointer to the pointer to the memory block which should yading@11: * be freed. yading@11: * @see av_free() yading@11: */ yading@11: void av_freep(void *ptr); yading@11: yading@11: /** yading@11: * Add an element to a dynamic array. yading@11: * yading@11: * @param tab_ptr Pointer to the array. yading@11: * @param nb_ptr Pointer to the number of elements in the array. yading@11: * @param elem Element to be added. yading@11: */ yading@11: void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem); yading@11: yading@11: /** yading@11: * Multiply two size_t values checking for overflow. yading@11: * @return 0 if success, AVERROR(EINVAL) if overflow. yading@11: */ yading@11: static inline int av_size_mult(size_t a, size_t b, size_t *r) yading@11: { yading@11: size_t t = a * b; yading@11: /* Hack inspired from glibc: only try the division if nelem and elsize yading@11: * are both greater than sqrt(SIZE_MAX). */ yading@11: if ((a | b) >= ((size_t)1 << (sizeof(size_t) * 4)) && a && t / a != b) yading@11: return AVERROR(EINVAL); yading@11: *r = t; yading@11: return 0; yading@11: } yading@11: yading@11: /** yading@11: * Set the maximum size that may me allocated in one block. yading@11: */ yading@11: void av_max_alloc(size_t max); yading@11: yading@11: /** yading@11: * @brief deliberately overlapping memcpy implementation yading@11: * @param dst destination buffer yading@11: * @param back how many bytes back we start (the initial size of the overlapping window), must be > 0 yading@11: * @param cnt number of bytes to copy, must be >= 0 yading@11: * yading@11: * cnt > back is valid, this will copy the bytes we just copied, yading@11: * thus creating a repeating pattern with a period length of back. yading@11: */ yading@11: void av_memcpy_backptr(uint8_t *dst, int back, int cnt); yading@11: yading@11: /** yading@11: * @} yading@11: */ yading@11: yading@11: #endif /* AVUTIL_MEM_H */