2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * memory handling functions
29 #include "attributes.h"
34 * @addtogroup lavu_mem
39 #if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1110 || defined(__SUNPRO_C)
40 #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
41 #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v
42 #elif defined(__TI_COMPILER_VERSION__)
43 #define DECLARE_ALIGNED(n,t,v) \
44 AV_PRAGMA(DATA_ALIGN(v,n)) \
45 t __attribute__((aligned(n))) v
46 #define DECLARE_ASM_CONST(n,t,v) \
47 AV_PRAGMA(DATA_ALIGN(v,n)) \
48 static const t __attribute__((aligned(n))) v
49 #elif defined(__GNUC__)
50 #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
51 #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v
52 #elif defined(_MSC_VER)
53 #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
54 #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v
56 #define DECLARE_ALIGNED(n,t,v) t v
57 #define DECLARE_ASM_CONST(n,t,v) static const t v
60 #if AV_GCC_VERSION_AT_LEAST(3,1)
61 #define av_malloc_attrib __attribute__((__malloc__))
63 #define av_malloc_attrib
66 #if AV_GCC_VERSION_AT_LEAST(4,3)
67 #define av_alloc_size(n) __attribute__((alloc_size(n)))
69 #define av_alloc_size(n)
73 * Allocate a block of size bytes with alignment suitable for all
74 * memory accesses (including vectors if available on the CPU).
75 * @param size Size in bytes for the memory block to be allocated.
76 * @return Pointer to the allocated block, NULL if the block cannot
80 void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);
83 * Allocate or reallocate a block of memory.
84 * If ptr is NULL and size > 0, allocate a new block. If
85 * size is zero, free the memory block pointed to by ptr.
86 * @param ptr Pointer to a memory block already allocated with
87 * av_malloc(z)() or av_realloc() or NULL.
88 * @param size Size in bytes for the memory block to be allocated or
90 * @return Pointer to a newly reallocated block or NULL if the block
91 * cannot be reallocated or the function is used to free the memory block.
92 * @see av_fast_realloc()
94 void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
97 * Allocate or reallocate a block of memory.
98 * This function does the same thing as av_realloc, except:
99 * - It takes two arguments and checks the result of the multiplication for
101 * - It frees the input block in case of failure, thus avoiding the memory
102 * leak with the classic "buf = realloc(buf); if (!buf) return -1;".
104 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize);
107 * Free a memory block which has been allocated with av_malloc(z)() or
109 * @param ptr Pointer to the memory block which should be freed.
110 * @note ptr = NULL is explicitly allowed.
111 * @note It is recommended that you use av_freep() instead.
114 void av_free(void *ptr);
117 * Allocate a block of size bytes with alignment suitable for all
118 * memory accesses (including vectors if available on the CPU) and
119 * zero all the bytes of the block.
120 * @param size Size in bytes for the memory block to be allocated.
121 * @return Pointer to the allocated block, NULL if it cannot be allocated.
124 void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
127 * Allocate a block of nmemb * size bytes with alignment suitable for all
128 * memory accesses (including vectors if available on the CPU) and
129 * zero all the bytes of the block.
130 * The allocation will fail if nmemb * size is greater than or equal
134 * @return Pointer to the allocated block, NULL if it cannot be allocated.
136 void *av_calloc(size_t nmemb, size_t size) av_malloc_attrib;
139 * Duplicate the string s.
140 * @param s string to be duplicated
141 * @return Pointer to a newly allocated string containing a
142 * copy of s or NULL if the string cannot be allocated.
144 char *av_strdup(const char *s) av_malloc_attrib;
147 * Free a memory block which has been allocated with av_malloc(z)() or
148 * av_realloc() and set the pointer pointing to it to NULL.
149 * @param ptr Pointer to the pointer to the memory block which should
153 void av_freep(void *ptr);
156 * Add an element to a dynamic array.
158 * @param tab_ptr Pointer to the array.
159 * @param nb_ptr Pointer to the number of elements in the array.
160 * @param elem Element to be added.
162 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem);
165 * Multiply two size_t values checking for overflow.
166 * @return 0 if success, AVERROR(EINVAL) if overflow.
168 static inline int av_size_mult(size_t a, size_t b, size_t *r)
171 /* Hack inspired from glibc: only try the division if nelem and elsize
172 * are both greater than sqrt(SIZE_MAX). */
173 if ((a | b) >= ((size_t)1 << (sizeof(size_t) * 4)) && a && t / a != b)
174 return AVERROR(EINVAL);
180 * Set the maximum size that may me allocated in one block.
182 void av_max_alloc(size_t max);
188 #endif /* AVUTIL_MEM_H */