mem.h 23 KB

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