]> git.sesse.net Git - casparcg/blob - ffmpeg 0.8/include/libavutil/mem.h
(no commit message)
[casparcg] / ffmpeg 0.8 / include / 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 #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
49 #else
50     #define DECLARE_ALIGNED(n,t,v)      t v
51     #define DECLARE_ASM_CONST(n,t,v)    static const t v
52 #endif
53
54 #if AV_GCC_VERSION_AT_LEAST(3,1)
55     #define av_malloc_attrib __attribute__((__malloc__))
56 #else
57     #define av_malloc_attrib
58 #endif
59
60 #if AV_GCC_VERSION_AT_LEAST(4,3)
61     #define av_alloc_size(n) __attribute__((alloc_size(n)))
62 #else
63     #define av_alloc_size(n)
64 #endif
65
66 /**
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
71  * be allocated.
72  * @see av_mallocz()
73  */
74 void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);
75
76 /**
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
83  * reallocated.
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()
87  */
88 void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
89
90 /**
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
94  *   integer overflow.
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;".
97  */
98 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize);
99
100 /**
101  * Free a memory block which has been allocated with av_malloc(z)() or
102  * av_realloc().
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.
106  * @see av_freep()
107  */
108 void av_free(void *ptr);
109
110 /**
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.
116  * @see av_malloc()
117  */
118 void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
119
120 /**
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
125  * to INT_MAX.
126  * @param nmemb
127  * @param size
128  * @return Pointer to the allocated block, NULL if it cannot be allocated.
129  */
130 void *av_calloc(size_t nmemb, size_t size) av_malloc_attrib;
131
132 /**
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.
137  */
138 char *av_strdup(const char *s) av_malloc_attrib;
139
140 /**
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
144  * be freed.
145  * @see av_free()
146  */
147 void av_freep(void *ptr);
148
149 /**
150  * Add an element to a dynamic array.
151  *
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.
155  */
156 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem);
157
158 /**
159  * Multiply two size_t values checking for overflow.
160  * @return  0 if success, AVERROR(EINVAL) if overflow.
161  */
162 static inline int av_size_mult(size_t a, size_t b, size_t *r)
163 {
164     size_t t = a * b;
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);
169     *r = t;
170     return 0;
171 }
172
173 #endif /* AVUTIL_MEM_H */