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 module ffmpeg.libavutil.mem;
21 /**
22  * @file
23  * memory handling functions
24  */
25 
26 /*#ifndef AVUTIL_MEM_H
27 #define AVUTIL_MEM_H
28 
29 #include <limits.h>*/
30 import std.stdint;
31 import core.stdc.errno;
32 import ffmpeg.libavutil.error;
33 /*#include "attributes.h"
34 #include "error.h"
35 #include "avutil.h"*/
36 
37 @nogc nothrow extern(C):
38 
39 /**
40  * @addtogroup lavu_mem
41  * @{
42  */
43 
44 
45 /*#if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1110 || defined(__SUNPRO_C)
46     #define DECLARE_ALIGNED(n,t,v)      t __attribute__ ((aligned (n))) v
47     #define DECLARE_ASM_CONST(n,t,v)    const t __attribute__ ((aligned (n))) v
48 #elif defined(__TI_COMPILER_VERSION__)
49     #define DECLARE_ALIGNED(n,t,v)                      \
50         AV_PRAGMA(DATA_ALIGN(v,n))                      \
51         t __attribute__((aligned(n))) v
52     #define DECLARE_ASM_CONST(n,t,v)                    \
53         AV_PRAGMA(DATA_ALIGN(v,n))                      \
54         static const t __attribute__((aligned(n))) v
55 #elif defined(__GNUC__)
56     #define DECLARE_ALIGNED(n,t,v)      t __attribute__ ((aligned (n))) v
57     #define DECLARE_ASM_CONST(n,t,v)    static const t av_used __attribute__ ((aligned (n))) v
58 #elif defined(_MSC_VER)
59     #define DECLARE_ALIGNED(n,t,v)      __declspec(align(n)) t v
60     #define DECLARE_ASM_CONST(n,t,v)    __declspec(align(n)) static const t v
61 #else
62     #define DECLARE_ALIGNED(n,t,v)      t v
63     #define DECLARE_ASM_CONST(n,t,v)    static const t v
64 #endif
65 
66 #if AV_GCC_VERSION_AT_LEAST(3,1)
67     #define av_malloc_attrib __attribute__((__malloc__))
68 #else
69     #define av_malloc_attrib
70 #endif
71 
72 #if AV_GCC_VERSION_AT_LEAST(4,3)
73     #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__)))
74 #else
75     #define av_alloc_size(...)
76 #endif*/
77 
78 /**
79  * Allocate a block of size bytes with alignment suitable for all
80  * memory accesses (including vectors if available on the CPU).
81  * @param size Size in bytes for the memory block to be allocated.
82  * @return Pointer to the allocated block, NULL if the block cannot
83  * be allocated.
84  * @see av_mallocz()
85  */
86 void *av_malloc(size_t size);
87 
88 /**
89  * Helper function to allocate a block of size * nmemb bytes with
90  * using av_malloc()
91  * @param nmemb Number of elements
92  * @param size Size of the single element
93  * @return Pointer to the allocated block, NULL if the block cannot
94  * be allocated.
95  * @see av_malloc()
96  */
97 static void *av_malloc_array(size_t nmemb, size_t size)
98 {
99     if (size <= 0 || nmemb >= int.max / size)
100         return null;
101     return av_malloc(nmemb * size);
102 }
103 
104 /**
105  * Allocate or reallocate a block of memory.
106  * If ptr is NULL and size > 0, allocate a new block. If
107  * size is zero, free the memory block pointed to by ptr.
108  * @param ptr Pointer to a memory block already allocated with
109  * av_malloc(z)() or av_realloc() or NULL.
110  * @param size Size in bytes for the memory block to be allocated or
111  * reallocated.
112  * @return Pointer to a newly reallocated block or NULL if the block
113  * cannot be reallocated or the function is used to free the memory block.
114  * @see av_fast_realloc()
115  */
116 void *av_realloc(void *ptr, size_t size);
117 
118 /**
119  * Allocate or reallocate a block of memory.
120  * This function does the same thing as av_realloc, except:
121  * - It takes two arguments and checks the result of the multiplication for
122  *   integer overflow.
123  * - It frees the input block in case of failure, thus avoiding the memory
124  *   leak with the classic "buf = realloc(buf); if (!buf) return -1;".
125  */
126 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize);
127 
128 /**
129  * Free a memory block which has been allocated with av_malloc(z)() or
130  * av_realloc().
131  * @param ptr Pointer to the memory block which should be freed.
132  * @note ptr = NULL is explicitly allowed.
133  * @note It is recommended that you use av_freep() instead.
134  * @see av_freep()
135  */
136 void av_free(void *ptr);
137 
138 /**
139  * Allocate a block of size bytes with alignment suitable for all
140  * memory accesses (including vectors if available on the CPU) and
141  * zero all the bytes of the block.
142  * @param size Size in bytes for the memory block to be allocated.
143  * @return Pointer to the allocated block, NULL if it cannot be allocated.
144  * @see av_malloc()
145  */
146 void *av_mallocz(size_t size);
147 
148 /**
149  * Allocate a block of nmemb * size bytes with alignment suitable for all
150  * memory accesses (including vectors if available on the CPU) and
151  * zero all the bytes of the block.
152  * The allocation will fail if nmemb * size is greater than or equal
153  * to INT_MAX.
154  * @param nmemb
155  * @param size
156  * @return Pointer to the allocated block, NULL if it cannot be allocated.
157  */
158 void *av_calloc(size_t nmemb, size_t size);
159 
160 /**
161  * Helper function to allocate a block of size * nmemb bytes with
162  * using av_mallocz()
163  * @param nmemb Number of elements
164  * @param size Size of the single element
165  * @return Pointer to the allocated block, NULL if the block cannot
166  * be allocated.
167  * @see av_mallocz()
168  * @see av_malloc_array()
169  */
170 static void *av_mallocz_array(size_t nmemb, size_t size)
171 {
172     if (size <= 0 || nmemb >= int.max / size)
173         return null;
174     return av_mallocz(nmemb * size);
175 }
176 
177 /**
178  * Duplicate the string s.
179  * @param s string to be duplicated
180  * @return Pointer to a newly allocated string containing a
181  * copy of s or NULL if the string cannot be allocated.
182  */
183 char *av_strdup(const char *s);
184 
185 /**
186  * Free a memory block which has been allocated with av_malloc(z)() or
187  * av_realloc() and set the pointer pointing to it to NULL.
188  * @param ptr Pointer to the pointer to the memory block which should
189  * be freed.
190  * @see av_free()
191  */
192 void av_freep(void *ptr);
193 
194 /**
195  * Add an element to a dynamic array.
196  *
197  * @param tab_ptr Pointer to the array.
198  * @param nb_ptr  Pointer to the number of elements in the array.
199  * @param elem    Element to be added.
200  */
201 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem);
202 
203 /**
204  * Multiply two size_t values checking for overflow.
205  * @return  0 if success, AVERROR(EINVAL) if overflow.
206  */
207 static int av_size_mult(size_t a, size_t b, size_t *r)
208 {
209     size_t t = a * b;
210     /* Hack inspired from glibc: only try the division if nelem and elsize
211      * are both greater than sqrt(SIZE_MAX). */
212     if ((a | b) >= (cast(size_t)1 << size_t.sizeof * 4) && a && t / a != b)
213         return AVERROR!(EINVAL);
214     *r = t;
215     return 0;
216 }
217 
218 /**
219  * Set the maximum size that may me allocated in one block.
220  */
221 void av_max_alloc(size_t max);
222 
223 /**
224  * @brief deliberately overlapping memcpy implementation
225  * @param dst destination buffer
226  * @param back how many bytes back we start (the initial size of the overlapping window), must be > 0
227  * @param cnt number of bytes to copy, must be >= 0
228  *
229  * cnt > back is valid, this will copy the bytes we just copied,
230  * thus creating a repeating pattern with a period length of back.
231  */
232 void av_memcpy_backptr(uint8_t *dst, int back, int cnt);
233 
234 /**
235  * @}
236  */
237 
238 //#endif /* AVUTIL_MEM_H */