mem.h 20 KB

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