You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

721 lines
24KB

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