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.

690 lines
23KB

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