LCOV - code coverage report
Current view: top level - src/libavutil - mem.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 11 14 78.6 %
Date: 2017-01-21 09:32:20 Functions: 3 3 100.0 %

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

Generated by: LCOV version 1.12