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  * @ingroup lavu_mem
24  * Memory handling functions
25  */
26 
27 module ffmpeg.libavutil.mem;
28 
29 extern (C):
30 import ffmpeg; @nogc nothrow:
31 
32 /**
33  * @addtogroup lavu_mem
34  * Utilities for manipulating memory.
35  *
36  * FFmpeg has several applications of memory that are not required of a typical
37  * program. For example, the computing-heavy components like video decoding and
38  * encoding can be sped up significantly through the use of aligned memory.
39  *
40  * However, for each of FFmpeg's applications of memory, there might not be a
41  * recognized or standardized API for that specific use. Memory alignment, for
42  * instance, varies wildly depending on operating systems, architectures, and
43  * compilers. Hence, this component of @ref libavutil is created to make
44  * dealing with memory consistently possible on all platforms.
45  *
46  * @{
47  *
48  * @defgroup lavu_mem_macros Alignment Macros
49  * Helper macros for declaring aligned variables.
50  * @{
51  */
52 
53 /**
54  * @def DECLARE_ALIGNED(n,t,v)
55  * Declare a variable that is aligned in memory.
56  *
57  * @code{.c}
58  * DECLARE_ALIGNED(16, uint16_t, aligned_int) = 42;
59  * DECLARE_ALIGNED(32, uint8_t, aligned_array)[128];
60  *
61  * // The default-alignment equivalent would be
62  * uint16_t aligned_int = 42;
63  * uint8_t aligned_array[128];
64  * @endcode
65  *
66  * @param n Minimum alignment in bytes
67  * @param t Type of the variable (or array element)
68  * @param v Name of the variable
69  */
70 
71 /**
72  * @def DECLARE_ASM_ALIGNED(n,t,v)
73  * Declare an aligned variable appropriate for use in inline assembly code.
74  *
75  * @code{.c}
76  * DECLARE_ASM_ALIGNED(16, uint64_t, pw_08) = UINT64_C(0x0008000800080008);
77  * @endcode
78  *
79  * @param n Minimum alignment in bytes
80  * @param t Type of the variable (or array element)
81  * @param v Name of the variable
82  */
83 
84 /**
85  * @def DECLARE_ASM_CONST(n,t,v)
86  * Declare a static constant aligned variable appropriate for use in inline
87  * assembly code.
88  *
89  * @code{.c}
90  * DECLARE_ASM_CONST(16, uint64_t, pw_08) = UINT64_C(0x0008000800080008);
91  * @endcode
92  *
93  * @param n Minimum alignment in bytes
94  * @param t Type of the variable (or array element)
95  * @param v Name of the variable
96  */
97 
98 /**
99  * @}
100  */
101 
102 /**
103  * @defgroup lavu_mem_attrs Function Attributes
104  * Function attributes applicable to memory handling functions.
105  *
106  * These function attributes can help compilers emit more useful warnings, or
107  * generate better code.
108  * @{
109  */
110 
111 /**
112  * @def av_malloc_attrib
113  * Function attribute denoting a malloc-like function.
114  *
115  * @see <a href="https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-g_t_0040code_007bmalloc_007d-function-attribute-3251">Function attribute `malloc` in GCC's documentation</a>
116  */
117 
118 /**
119  * @def av_alloc_size(...)
120  * Function attribute used on a function that allocates memory, whose size is
121  * given by the specified parameter(s).
122  *
123  * @code{.c}
124  * void *av_malloc(size_t size) av_alloc_size(1);
125  * void *av_calloc(size_t nmemb, size_t size) av_alloc_size(1, 2);
126  * @endcode
127  *
128  * @param ... One or two parameter indexes, separated by a comma
129  *
130  * @see <a href="https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-g_t_0040code_007balloc_005fsize_007d-function-attribute-3220">Function attribute `alloc_size` in GCC's documentation</a>
131  */
132 
133 /**
134  * @}
135  */
136 
137 /**
138  * @defgroup lavu_mem_funcs Heap Management
139  * Functions responsible for allocating, freeing, and copying memory.
140  *
141  * All memory allocation functions have a built-in upper limit of `INT_MAX`
142  * bytes. This may be changed with av_max_alloc(), although exercise extreme
143  * caution when doing so.
144  *
145  * @{
146  */
147 
148 /**
149  * Allocate a memory block with alignment suitable for all memory accesses
150  * (including vectors if available on the CPU).
151  *
152  * @param size Size in bytes for the memory block to be allocated
153  * @return Pointer to the allocated block, or `NULL` if the block cannot
154  *         be allocated
155  * @see av_mallocz()
156  */
157 void* av_malloc (size_t size);
158 
159 /**
160  * Allocate a memory block with alignment suitable for all memory accesses
161  * (including vectors if available on the CPU) and zero all the bytes of the
162  * block.
163  *
164  * @param size Size in bytes for the memory block to be allocated
165  * @return Pointer to the allocated block, or `NULL` if it cannot be allocated
166  * @see av_malloc()
167  */
168 void* av_mallocz (size_t size);
169 
170 /**
171  * Allocate a memory block for an array with av_malloc().
172  *
173  * The allocated memory will have size `size * nmemb` bytes.
174  *
175  * @param nmemb Number of element
176  * @param size  Size of a single element
177  * @return Pointer to the allocated block, or `NULL` if the block cannot
178  *         be allocated
179  * @see av_malloc()
180  */
181 void* av_malloc_array (size_t nmemb, size_t size);
182 
183 /**
184  * Allocate a memory block for an array with av_mallocz().
185  *
186  * The allocated memory will have size `size * nmemb` bytes.
187  *
188  * @param nmemb Number of elements
189  * @param size  Size of the single element
190  * @return Pointer to the allocated block, or `NULL` if the block cannot
191  *         be allocated
192  *
193  * @see av_mallocz()
194  * @see av_malloc_array()
195  */
196 void* av_mallocz_array (size_t nmemb, size_t size);
197 
198 /**
199  * Non-inlined equivalent of av_mallocz_array().
200  *
201  * Created for symmetry with the calloc() C function.
202  */
203 void* av_calloc (size_t nmemb, size_t size);
204 
205 /**
206  * Allocate, reallocate, or free a block of memory.
207  *
208  * If `ptr` is `NULL` and `size` > 0, allocate a new block. If `size` is
209  * zero, free the memory block pointed to by `ptr`. Otherwise, expand or
210  * shrink that block of memory according to `size`.
211  *
212  * @param ptr  Pointer to a memory block already allocated with
213  *             av_realloc() or `NULL`
214  * @param size Size in bytes of the memory block to be allocated or
215  *             reallocated
216  *
217  * @return Pointer to a newly-reallocated block or `NULL` if the block
218  *         cannot be reallocated or the function is used to free the memory block
219  *
220  * @warning Unlike av_malloc(), the returned pointer is not guaranteed to be
221  *          correctly aligned.
222  * @see av_fast_realloc()
223  * @see av_reallocp()
224  */
225 void* av_realloc (void* ptr, size_t size);
226 
227 /**
228  * Allocate, reallocate, or free a block of memory through a pointer to a
229  * pointer.
230  *
231  * If `*ptr` is `NULL` and `size` > 0, allocate a new block. If `size` is
232  * zero, free the memory block pointed to by `*ptr`. Otherwise, expand or
233  * shrink that block of memory according to `size`.
234  *
235  * @param[in,out] ptr  Pointer to a pointer to a memory block already allocated
236  *                     with av_realloc(), or a pointer to `NULL`. The pointer
237  *                     is updated on success, or freed on failure.
238  * @param[in]     size Size in bytes for the memory block to be allocated or
239  *                     reallocated
240  *
241  * @return Zero on success, an AVERROR error code on failure
242  *
243  * @warning Unlike av_malloc(), the allocated memory is not guaranteed to be
244  *          correctly aligned.
245  */
246 int av_reallocp (void* ptr, size_t size);
247 
248 /**
249  * Allocate, reallocate, or free a block of memory.
250  *
251  * This function does the same thing as av_realloc(), except:
252  * - It takes two size arguments and allocates `nelem * elsize` bytes,
253  *   after checking the result of the multiplication for integer overflow.
254  * - It frees the input block in case of failure, thus avoiding the memory
255  *   leak with the classic
256  *   @code{.c}
257  *   buf = realloc(buf);
258  *   if (!buf)
259  *       return -1;
260  *   @endcode
261  *   pattern.
262  */
263 void* av_realloc_f (void* ptr, size_t nelem, size_t elsize);
264 
265 /**
266  * Allocate, reallocate, or free an array.
267  *
268  * If `ptr` is `NULL` and `nmemb` > 0, allocate a new block. If
269  * `nmemb` is zero, free the memory block pointed to by `ptr`.
270  *
271  * @param ptr   Pointer to a memory block already allocated with
272  *              av_realloc() or `NULL`
273  * @param nmemb Number of elements in the array
274  * @param size  Size of the single element of the array
275  *
276  * @return Pointer to a newly-reallocated block or NULL if the block
277  *         cannot be reallocated or the function is used to free the memory block
278  *
279  * @warning Unlike av_malloc(), the allocated memory is not guaranteed to be
280  *          correctly aligned.
281  * @see av_reallocp_array()
282  */
283 void* av_realloc_array (void* ptr, size_t nmemb, size_t size);
284 
285 /**
286  * Allocate, reallocate, or free an array through a pointer to a pointer.
287  *
288  * If `*ptr` is `NULL` and `nmemb` > 0, allocate a new block. If `nmemb` is
289  * zero, free the memory block pointed to by `*ptr`.
290  *
291  * @param[in,out] ptr   Pointer to a pointer to a memory block already
292  *                      allocated with av_realloc(), or a pointer to `NULL`.
293  *                      The pointer is updated on success, or freed on failure.
294  * @param[in]     nmemb Number of elements
295  * @param[in]     size  Size of the single element
296  *
297  * @return Zero on success, an AVERROR error code on failure
298  *
299  * @warning Unlike av_malloc(), the allocated memory is not guaranteed to be
300  *          correctly aligned.
301  */
302 int av_reallocp_array (void* ptr, size_t nmemb, size_t size);
303 
304 /**
305  * Reallocate the given buffer if it is not large enough, otherwise do nothing.
306  *
307  * If the given buffer is `NULL`, then a new uninitialized buffer is allocated.
308  *
309  * If the given buffer is not large enough, and reallocation fails, `NULL` is
310  * returned and `*size` is set to 0, but the original buffer is not changed or
311  * freed.
312  *
313  * A typical use pattern follows:
314  *
315  * @code{.c}
316  * uint8_t *buf = ...;
317  * uint8_t *new_buf = av_fast_realloc(buf, &current_size, size_needed);
318  * if (!new_buf) {
319  *     // Allocation failed; clean up original buffer
320  *     av_freep(&buf);
321  *     return AVERROR(ENOMEM);
322  * }
323  * @endcode
324  *
325  * @param[in,out] ptr      Already allocated buffer, or `NULL`
326  * @param[in,out] size     Pointer to current size of buffer `ptr`. `*size` is
327  *                         changed to `min_size` in case of success or 0 in
328  *                         case of failure
329  * @param[in]     min_size New size of buffer `ptr`
330  * @return `ptr` if the buffer is large enough, a pointer to newly reallocated
331  *         buffer if the buffer was not large enough, or `NULL` in case of
332  *         error
333  * @see av_realloc()
334  * @see av_fast_malloc()
335  */
336 void* av_fast_realloc (void* ptr, uint* size, size_t min_size);
337 
338 /**
339  * Allocate a buffer, reusing the given one if large enough.
340  *
341  * Contrary to av_fast_realloc(), the current buffer contents might not be
342  * preserved and on error the old buffer is freed, thus no special handling to
343  * avoid memleaks is necessary.
344  *
345  * `*ptr` is allowed to be `NULL`, in which case allocation always happens if
346  * `size_needed` is greater than 0.
347  *
348  * @code{.c}
349  * uint8_t *buf = ...;
350  * av_fast_malloc(&buf, &current_size, size_needed);
351  * if (!buf) {
352  *     // Allocation failed; buf already freed
353  *     return AVERROR(ENOMEM);
354  * }
355  * @endcode
356  *
357  * @param[in,out] ptr      Pointer to pointer to an already allocated buffer.
358  *                         `*ptr` will be overwritten with pointer to new
359  *                         buffer on success or `NULL` on failure
360  * @param[in,out] size     Pointer to current size of buffer `*ptr`. `*size` is
361  *                         changed to `min_size` in case of success or 0 in
362  *                         case of failure
363  * @param[in]     min_size New size of buffer `*ptr`
364  * @see av_realloc()
365  * @see av_fast_mallocz()
366  */
367 void av_fast_malloc (void* ptr, uint* size, size_t min_size);
368 
369 /**
370  * Allocate and clear a buffer, reusing the given one if large enough.
371  *
372  * Like av_fast_malloc(), but all newly allocated space is initially cleared.
373  * Reused buffer is not cleared.
374  *
375  * `*ptr` is allowed to be `NULL`, in which case allocation always happens if
376  * `size_needed` is greater than 0.
377  *
378  * @param[in,out] ptr      Pointer to pointer to an already allocated buffer.
379  *                         `*ptr` will be overwritten with pointer to new
380  *                         buffer on success or `NULL` on failure
381  * @param[in,out] size     Pointer to current size of buffer `*ptr`. `*size` is
382  *                         changed to `min_size` in case of success or 0 in
383  *                         case of failure
384  * @param[in]     min_size New size of buffer `*ptr`
385  * @see av_fast_malloc()
386  */
387 void av_fast_mallocz (void* ptr, uint* size, size_t min_size);
388 
389 /**
390  * Free a memory block which has been allocated with a function of av_malloc()
391  * or av_realloc() family.
392  *
393  * @param ptr Pointer to the memory block which should be freed.
394  *
395  * @note `ptr = NULL` is explicitly allowed.
396  * @note It is recommended that you use av_freep() instead, to prevent leaving
397  *       behind dangling pointers.
398  * @see av_freep()
399  */
400 void av_free (void* ptr);
401 
402 /**
403  * Free a memory block which has been allocated with a function of av_malloc()
404  * or av_realloc() family, and set the pointer pointing to it to `NULL`.
405  *
406  * @code{.c}
407  * uint8_t *buf = av_malloc(16);
408  * av_free(buf);
409  * // buf now contains a dangling pointer to freed memory, and accidental
410  * // dereference of buf will result in a use-after-free, which may be a
411  * // security risk.
412  *
413  * uint8_t *buf = av_malloc(16);
414  * av_freep(&buf);
415  * // buf is now NULL, and accidental dereference will only result in a
416  * // NULL-pointer dereference.
417  * @endcode
418  *
419  * @param ptr Pointer to the pointer to the memory block which should be freed
420  * @note `*ptr = NULL` is safe and leads to no action.
421  * @see av_free()
422  */
423 void av_freep (void* ptr);
424 
425 /**
426  * Duplicate a string.
427  *
428  * @param s String to be duplicated
429  * @return Pointer to a newly-allocated string containing a
430  *         copy of `s` or `NULL` if the string cannot be allocated
431  * @see av_strndup()
432  */
433 char* av_strdup (const(char)* s);
434 
435 /**
436  * Duplicate a substring of a string.
437  *
438  * @param s   String to be duplicated
439  * @param len Maximum length of the resulting string (not counting the
440  *            terminating byte)
441  * @return Pointer to a newly-allocated string containing a
442  *         substring of `s` or `NULL` if the string cannot be allocated
443  */
444 char* av_strndup (const(char)* s, size_t len);
445 
446 /**
447  * Duplicate a buffer with av_malloc().
448  *
449  * @param p    Buffer to be duplicated
450  * @param size Size in bytes of the buffer copied
451  * @return Pointer to a newly allocated buffer containing a
452  *         copy of `p` or `NULL` if the buffer cannot be allocated
453  */
454 void* av_memdup (const(void)* p, size_t size);
455 
456 /**
457  * Overlapping memcpy() implementation.
458  *
459  * @param dst  Destination buffer
460  * @param back Number of bytes back to start copying (i.e. the initial size of
461  *             the overlapping window); must be > 0
462  * @param cnt  Number of bytes to copy; must be >= 0
463  *
464  * @note `cnt > back` is valid, this will copy the bytes we just copied,
465  *       thus creating a repeating pattern with a period length of `back`.
466  */
467 void av_memcpy_backptr (ubyte* dst, int back, int cnt);
468 
469 /**
470  * @}
471  */
472 
473 /**
474  * @defgroup lavu_mem_dynarray Dynamic Array
475  *
476  * Utilities to make an array grow when needed.
477  *
478  * Sometimes, the programmer would want to have an array that can grow when
479  * needed. The libavutil dynamic array utilities fill that need.
480  *
481  * libavutil supports two systems of appending elements onto a dynamically
482  * allocated array, the first one storing the pointer to the value in the
483  * array, and the second storing the value directly. In both systems, the
484  * caller is responsible for maintaining a variable containing the length of
485  * the array, as well as freeing of the array after use.
486  *
487  * The first system stores pointers to values in a block of dynamically
488  * allocated memory. Since only pointers are stored, the function does not need
489  * to know the size of the type. Both av_dynarray_add() and
490  * av_dynarray_add_nofree() implement this system.
491  *
492  * @code
493  * type **array = NULL; //< an array of pointers to values
494  * int    nb    = 0;    //< a variable to keep track of the length of the array
495  *
496  * type to_be_added  = ...;
497  * type to_be_added2 = ...;
498  *
499  * av_dynarray_add(&array, &nb, &to_be_added);
500  * if (nb == 0)
501  *     return AVERROR(ENOMEM);
502  *
503  * av_dynarray_add(&array, &nb, &to_be_added2);
504  * if (nb == 0)
505  *     return AVERROR(ENOMEM);
506  *
507  * // Now:
508  * //  nb           == 2
509  * // &to_be_added  == array[0]
510  * // &to_be_added2 == array[1]
511  *
512  * av_freep(&array);
513  * @endcode
514  *
515  * The second system stores the value directly in a block of memory. As a
516  * result, the function has to know the size of the type. av_dynarray2_add()
517  * implements this mechanism.
518  *
519  * @code
520  * type *array = NULL; //< an array of values
521  * int   nb    = 0;    //< a variable to keep track of the length of the array
522  *
523  * type to_be_added  = ...;
524  * type to_be_added2 = ...;
525  *
526  * type *addr = av_dynarray2_add((void **)&array, &nb, sizeof(*array), NULL);
527  * if (!addr)
528  *     return AVERROR(ENOMEM);
529  * memcpy(addr, &to_be_added, sizeof(to_be_added));
530  *
531  * // Shortcut of the above.
532  * type *addr = av_dynarray2_add((void **)&array, &nb, sizeof(*array),
533  *                               (const void *)&to_be_added2);
534  * if (!addr)
535  *     return AVERROR(ENOMEM);
536  *
537  * // Now:
538  * //  nb           == 2
539  * //  to_be_added  == array[0]
540  * //  to_be_added2 == array[1]
541  *
542  * av_freep(&array);
543  * @endcode
544  *
545  * @{
546  */
547 
548 /**
549  * Add the pointer to an element to a dynamic array.
550  *
551  * The array to grow is supposed to be an array of pointers to
552  * structures, and the element to add must be a pointer to an already
553  * allocated structure.
554  *
555  * The array is reallocated when its size reaches powers of 2.
556  * Therefore, the amortized cost of adding an element is constant.
557  *
558  * In case of success, the pointer to the array is updated in order to
559  * point to the new grown array, and the number pointed to by `nb_ptr`
560  * is incremented.
561  * In case of failure, the array is freed, `*tab_ptr` is set to `NULL` and
562  * `*nb_ptr` is set to 0.
563  *
564  * @param[in,out] tab_ptr Pointer to the array to grow
565  * @param[in,out] nb_ptr  Pointer to the number of elements in the array
566  * @param[in]     elem    Element to add
567  * @see av_dynarray_add_nofree(), av_dynarray2_add()
568  */
569 void av_dynarray_add (void* tab_ptr, int* nb_ptr, void* elem);
570 
571 /**
572  * Add an element to a dynamic array.
573  *
574  * Function has the same functionality as av_dynarray_add(),
575  * but it doesn't free memory on fails. It returns error code
576  * instead and leave current buffer untouched.
577  *
578  * @return >=0 on success, negative otherwise
579  * @see av_dynarray_add(), av_dynarray2_add()
580  */
581 int av_dynarray_add_nofree (void* tab_ptr, int* nb_ptr, void* elem);
582 
583 /**
584  * Add an element of size `elem_size` to a dynamic array.
585  *
586  * The array is reallocated when its number of elements reaches powers of 2.
587  * Therefore, the amortized cost of adding an element is constant.
588  *
589  * In case of success, the pointer to the array is updated in order to
590  * point to the new grown array, and the number pointed to by `nb_ptr`
591  * is incremented.
592  * In case of failure, the array is freed, `*tab_ptr` is set to `NULL` and
593  * `*nb_ptr` is set to 0.
594  *
595  * @param[in,out] tab_ptr   Pointer to the array to grow
596  * @param[in,out] nb_ptr    Pointer to the number of elements in the array
597  * @param[in]     elem_size Size in bytes of an element in the array
598  * @param[in]     elem_data Pointer to the data of the element to add. If
599  *                          `NULL`, the space of the newly added element is
600  *                          allocated but left uninitialized.
601  *
602  * @return Pointer to the data of the element to copy in the newly allocated
603  *         space
604  * @see av_dynarray_add(), av_dynarray_add_nofree()
605  */
606 void* av_dynarray2_add (
607     void** tab_ptr,
608     int* nb_ptr,
609     size_t elem_size,
610     const(ubyte)* elem_data);
611 
612 /**
613  * @}
614  */
615 
616 /**
617  * @defgroup lavu_mem_misc Miscellaneous Functions
618  *
619  * Other functions related to memory allocation.
620  *
621  * @{
622  */
623 
624 /**
625  * Multiply two `size_t` values checking for overflow.
626  *
627  * @param[in]  a,b Operands of multiplication
628  * @param[out] r   Pointer to the result of the operation
629  * @return 0 on success, AVERROR(EINVAL) on overflow
630  */
631 
632 /* Hack inspired from glibc: don't try the division if nelem and elsize
633  * are both less than sqrt(SIZE_MAX). */
634 int av_size_mult (size_t a, size_t b, size_t* r);
635 
636 /**
637  * Set the maximum size that may be allocated in one block.
638  *
639  * The value specified with this function is effective for all libavutil's @ref
640  * lavu_mem_funcs "heap management functions."
641  *
642  * By default, the max value is defined as `INT_MAX`.
643  *
644  * @param max Value to be set as the new maximum size
645  *
646  * @warning Exercise extreme caution when using this function. Don't touch
647  *          this if you do not understand the full consequence of doing so.
648  */
649 void av_max_alloc (size_t max);
650 
651 /**
652  * @}
653  * @}
654  */
655 
656 /* AVUTIL_MEM_H */