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"
33 #if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1110 || defined(__SUNPRO_C)
34 #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
35 #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v
36 #elif defined(__TI_COMPILER_VERSION__)
37 #define DECLARE_ALIGNED(n,t,v) \
38 AV_PRAGMA(DATA_ALIGN(v,n)) \
39 t __attribute__((aligned(n))) v
40 #define DECLARE_ASM_CONST(n,t,v) \
41 AV_PRAGMA(DATA_ALIGN(v,n)) \
42 static const t __attribute__((aligned(n))) v
43 #elif defined(__GNUC__)
44 #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
45 #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v
46 #elif defined(_MSC_VER)
47 #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
48 #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v
50 #define DECLARE_ALIGNED(n,t,v) t v
51 #define DECLARE_ASM_CONST(n,t,v) static const t v
54 #if AV_GCC_VERSION_AT_LEAST(3,1)
55 #define av_malloc_attrib __attribute__((__malloc__))
57 #define av_malloc_attrib
60 #if AV_GCC_VERSION_AT_LEAST(4,3)
61 #define av_alloc_size(n) __attribute__((alloc_size(n)))
63 #define av_alloc_size(n)
67 * Allocate a block of size bytes with alignment suitable for all
68 * memory accesses (including vectors if available on the CPU).
69 * @param size Size in bytes for the memory block to be allocated.
70 * @return Pointer to the allocated block, NULL if the block cannot
74 void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);
77 * Allocate or reallocate a block of memory.
78 * If ptr is NULL and size > 0, allocate a new block. If
79 * size is zero, free the memory block pointed to by ptr.
80 * @param ptr Pointer to a memory block already allocated with
81 * av_malloc(z)() or av_realloc() or NULL.
82 * @param size Size in bytes for the memory block to be allocated or
84 * @return Pointer to a newly reallocated block or NULL if the block
85 * cannot be reallocated or the function is used to free the memory block.
86 * @see av_fast_realloc()
88 void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
91 * Allocate or reallocate a block of memory.
92 * This function does the same thing as av_realloc, except:
93 * - It takes two arguments and checks the result of the multiplication for
95 * - It frees the input block in case of failure, thus avoiding the memory
96 * leak with the classic "buf = realloc(buf); if (!buf) return -1;".
98 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize);
101 * Free a memory block which has been allocated with av_malloc(z)() or
103 * @param ptr Pointer to the memory block which should be freed.
104 * @note ptr = NULL is explicitly allowed.
105 * @note It is recommended that you use av_freep() instead.
108 void av_free(void *ptr);
111 * Allocate a block of size bytes with alignment suitable for all
112 * memory accesses (including vectors if available on the CPU) and
113 * zero all the bytes of the block.
114 * @param size Size in bytes for the memory block to be allocated.
115 * @return Pointer to the allocated block, NULL if it cannot be allocated.
118 void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
121 * Allocate a block of nmemb * size bytes with alignment suitable for all
122 * memory accesses (including vectors if available on the CPU) and
123 * zero all the bytes of the block.
124 * The allocation will fail if nmemb * size is greater than or equal
128 * @return Pointer to the allocated block, NULL if it cannot be allocated.
130 void *av_calloc(size_t nmemb, size_t size) av_malloc_attrib;
133 * Duplicate the string s.
134 * @param s string to be duplicated
135 * @return Pointer to a newly allocated string containing a
136 * copy of s or NULL if the string cannot be allocated.
138 char *av_strdup(const char *s) av_malloc_attrib;
141 * Free a memory block which has been allocated with av_malloc(z)() or
142 * av_realloc() and set the pointer pointing to it to NULL.
143 * @param ptr Pointer to the pointer to the memory block which should
147 void av_freep(void *ptr);
150 * Add an element to a dynamic array.
152 * @param tab_ptr Pointer to the array.
153 * @param nb_ptr Pointer to the number of elements in the array.
154 * @param elem Element to be added.
156 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem);
159 * Multiply two size_t values checking for overflow.
160 * @return 0 if success, AVERROR(EINVAL) if overflow.
162 static inline int av_size_mult(size_t a, size_t b, size_t *r)
165 /* Hack inspired from glibc: only try the division if nelem and elsize
166 * are both greater than sqrt(SIZE_MAX). */
167 if ((a | b) >= ((size_t)1 << (sizeof(size_t) * 4)) && a && t / a != b)
168 return AVERROR(EINVAL);
173 #endif /* AVUTIL_MEM_H */