]> git.sesse.net Git - ffmpeg/blob - libavutil/mem.h
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavutil / mem.h
1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file
23  * memory handling functions
24  */
25
26 #ifndef AVUTIL_MEM_H
27 #define AVUTIL_MEM_H
28
29 #include "attributes.h"
30 #include "error.h"
31 #include "avutil.h"
32
33 /**
34  * @addtogroup lavu_mem
35  * @{
36  */
37
38
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
55 #else
56     #define DECLARE_ALIGNED(n,t,v)      t v
57     #define DECLARE_ASM_CONST(n,t,v)    static const t v
58 #endif
59
60 #if AV_GCC_VERSION_AT_LEAST(3,1)
61     #define av_malloc_attrib __attribute__((__malloc__))
62 #else
63     #define av_malloc_attrib
64 #endif
65
66 #if AV_GCC_VERSION_AT_LEAST(4,3)
67     #define av_alloc_size(n) __attribute__((alloc_size(n)))
68 #else
69     #define av_alloc_size(n)
70 #endif
71
72 /**
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
77  * be allocated.
78  * @see av_mallocz()
79  */
80 void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);
81
82 /**
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
89  * reallocated.
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()
93  */
94 void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
95
96 /**
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
100  *   integer overflow.
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;".
103  */
104 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize);
105
106 /**
107  * Free a memory block which has been allocated with av_malloc(z)() or
108  * av_realloc().
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.
112  * @see av_freep()
113  */
114 void av_free(void *ptr);
115
116 /**
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.
122  * @see av_malloc()
123  */
124 void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
125
126 /**
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
131  * to INT_MAX.
132  * @param nmemb
133  * @param size
134  * @return Pointer to the allocated block, NULL if it cannot be allocated.
135  */
136 void *av_calloc(size_t nmemb, size_t size) av_malloc_attrib;
137
138 /**
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.
143  */
144 char *av_strdup(const char *s) av_malloc_attrib;
145
146 /**
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
150  * be freed.
151  * @see av_free()
152  */
153 void av_freep(void *ptr);
154
155 /**
156  * Add an element to a dynamic array.
157  *
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.
161  */
162 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem);
163
164 /**
165  * Multiply two size_t values checking for overflow.
166  * @return  0 if success, AVERROR(EINVAL) if overflow.
167  */
168 static inline int av_size_mult(size_t a, size_t b, size_t *r)
169 {
170     size_t t = a * b;
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);
175     *r = t;
176     return 0;
177 }
178
179 /**
180  * Set the maximum size that may me allocated in one block.
181  */
182 void av_max_alloc(size_t max);
183
184 /**
185  * @}
186  */
187
188 #endif /* AVUTIL_MEM_H */