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.

407 lines
15KB

  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. * memory handling functions
  23. */
  24. #ifndef AVUTIL_MEM_H
  25. #define AVUTIL_MEM_H
  26. #include <limits.h>
  27. #include <stdint.h>
  28. #include "attributes.h"
  29. #include "error.h"
  30. #include "avutil.h"
  31. /**
  32. * @addtogroup lavu_mem
  33. * @{
  34. */
  35. #if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1110 || defined(__SUNPRO_C)
  36. #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
  37. #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v
  38. #elif defined(__TI_COMPILER_VERSION__)
  39. #define DECLARE_ALIGNED(n,t,v) \
  40. AV_PRAGMA(DATA_ALIGN(v,n)) \
  41. t __attribute__((aligned(n))) v
  42. #define DECLARE_ASM_CONST(n,t,v) \
  43. AV_PRAGMA(DATA_ALIGN(v,n)) \
  44. static const t __attribute__((aligned(n))) v
  45. #elif defined(__GNUC__)
  46. #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
  47. #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v
  48. #elif defined(_MSC_VER)
  49. #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
  50. #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v
  51. #else
  52. #define DECLARE_ALIGNED(n,t,v) t v
  53. #define DECLARE_ASM_CONST(n,t,v) static const t v
  54. #endif
  55. #if AV_GCC_VERSION_AT_LEAST(3,1)
  56. #define av_malloc_attrib __attribute__((__malloc__))
  57. #else
  58. #define av_malloc_attrib
  59. #endif
  60. #if AV_GCC_VERSION_AT_LEAST(4,3)
  61. #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__)))
  62. #else
  63. #define av_alloc_size(...)
  64. #endif
  65. /**
  66. * Allocate a block of size bytes with alignment suitable for all
  67. * memory accesses (including vectors if available on the CPU).
  68. * @param size Size in bytes for the memory block to be allocated.
  69. * @return Pointer to the allocated block, NULL if the block cannot
  70. * be allocated.
  71. * @see av_mallocz()
  72. */
  73. void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);
  74. /**
  75. * Allocate a block of size bytes with alignment suitable for all
  76. * memory accesses (including vectors if available on the CPU) and
  77. * zero all the bytes of the block.
  78. * @param size Size in bytes for the memory block to be allocated.
  79. * @return Pointer to the allocated block, NULL if it cannot be allocated.
  80. * @see av_malloc()
  81. */
  82. void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
  83. /**
  84. * Allocate a block of size * nmemb bytes with av_malloc().
  85. * @param nmemb Number of elements
  86. * @param size Size of the single element
  87. * @return Pointer to the allocated block, NULL if the block cannot
  88. * be allocated.
  89. * @see av_malloc()
  90. */
  91. av_alloc_size(1, 2) static inline void *av_malloc_array(size_t nmemb, size_t size)
  92. {
  93. if (!size || nmemb >= INT_MAX / size)
  94. return NULL;
  95. return av_malloc(nmemb * size);
  96. }
  97. /**
  98. * Allocate a block of size * nmemb bytes with av_mallocz().
  99. * @param nmemb Number of elements
  100. * @param size Size of the single element
  101. * @return Pointer to the allocated block, NULL if the block cannot
  102. * be allocated.
  103. * @see av_mallocz()
  104. * @see av_malloc_array()
  105. */
  106. av_alloc_size(1, 2) static inline void *av_mallocz_array(size_t nmemb, size_t size)
  107. {
  108. if (!size || nmemb >= INT_MAX / size)
  109. return NULL;
  110. return av_mallocz(nmemb * size);
  111. }
  112. /**
  113. * Allocate a block of nmemb * size bytes with alignment suitable for all
  114. * memory accesses (including vectors if available on the CPU) and
  115. * zero all the bytes of the block.
  116. * The allocation will fail if nmemb * size is greater than or equal
  117. * to INT_MAX.
  118. * @param nmemb
  119. * @param size
  120. * @return Pointer to the allocated block, NULL if it cannot be allocated.
  121. */
  122. void *av_calloc(size_t nmemb, size_t size) av_malloc_attrib;
  123. /**
  124. * Allocate or reallocate a block of memory.
  125. * If ptr is NULL and size > 0, allocate a new block. If
  126. * size is zero, free the memory block pointed to by ptr.
  127. * @param ptr Pointer to a memory block already allocated with
  128. * av_realloc() or NULL.
  129. * @param size Size in bytes of the memory block to be allocated or
  130. * reallocated.
  131. * @return Pointer to a newly-reallocated block or NULL if the block
  132. * cannot be reallocated or the function is used to free the memory block.
  133. * @warning Pointers originating from the av_malloc() family of functions must
  134. * not be passed to av_realloc(). The former can be implemented using
  135. * memalign() (or other functions), and there is no guarantee that
  136. * pointers from such functions can be passed to realloc() at all.
  137. * The situation is undefined according to POSIX and may crash with
  138. * some libc implementations.
  139. * @see av_fast_realloc()
  140. */
  141. void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
  142. /**
  143. * Allocate or reallocate a block of memory.
  144. * If *ptr is NULL and size > 0, allocate a new block. If
  145. * size is zero, free the memory block pointed to by ptr.
  146. * @param ptr Pointer to a pointer to a memory block already allocated
  147. * with av_realloc(), or pointer to a pointer to NULL.
  148. * The pointer is updated on success, or freed on failure.
  149. * @param size Size in bytes for the memory block to be allocated or
  150. * reallocated
  151. * @return Zero on success, an AVERROR error code on failure.
  152. * @warning Pointers originating from the av_malloc() family of functions must
  153. * not be passed to av_reallocp(). The former can be implemented using
  154. * memalign() (or other functions), and there is no guarantee that
  155. * pointers from such functions can be passed to realloc() at all.
  156. * The situation is undefined according to POSIX and may crash with
  157. * some libc implementations.
  158. */
  159. av_warn_unused_result
  160. int av_reallocp(void *ptr, size_t size);
  161. /**
  162. * Allocate or reallocate a block of memory.
  163. * This function does the same thing as av_realloc, except:
  164. * - It takes two arguments and checks the result of the multiplication for
  165. * integer overflow.
  166. * - It frees the input block in case of failure, thus avoiding the memory
  167. * leak with the classic "buf = realloc(buf); if (!buf) return -1;".
  168. */
  169. void *av_realloc_f(void *ptr, size_t nelem, size_t elsize);
  170. /**
  171. * Allocate or reallocate an array.
  172. * If ptr is NULL and nmemb > 0, allocate a new block. If
  173. * nmemb is zero, free the memory block pointed to by ptr.
  174. * @param ptr Pointer to a memory block already allocated with
  175. * av_realloc() or NULL.
  176. * @param nmemb Number of elements
  177. * @param size Size of the single element
  178. * @return Pointer to a newly-reallocated block or NULL if the block
  179. * cannot be reallocated or the function is used to free the memory block.
  180. * @warning Pointers originating from the av_malloc() family of functions must
  181. * not be passed to av_realloc(). The former can be implemented using
  182. * memalign() (or other functions), and there is no guarantee that
  183. * pointers from such functions can be passed to realloc() at all.
  184. * The situation is undefined according to POSIX and may crash with
  185. * some libc implementations.
  186. */
  187. av_alloc_size(2, 3) void *av_realloc_array(void *ptr, size_t nmemb, size_t size);
  188. /**
  189. * Allocate or reallocate an array through a pointer to a pointer.
  190. * If *ptr is NULL and nmemb > 0, allocate a new block. If
  191. * nmemb is zero, free the memory block pointed to by ptr.
  192. * @param ptr Pointer to a pointer to a memory block already allocated
  193. * with av_realloc(), or pointer to a pointer to NULL.
  194. * The pointer is updated on success, or freed on failure.
  195. * @param nmemb Number of elements
  196. * @param size Size of the single element
  197. * @return Zero on success, an AVERROR error code on failure.
  198. * @warning Pointers originating from the av_malloc() family of functions must
  199. * not be passed to av_realloc(). The former can be implemented using
  200. * memalign() (or other functions), and there is no guarantee that
  201. * pointers from such functions can be passed to realloc() at all.
  202. * The situation is undefined according to POSIX and may crash with
  203. * some libc implementations.
  204. */
  205. av_alloc_size(2, 3) int av_reallocp_array(void *ptr, size_t nmemb, size_t size);
  206. /**
  207. * Reallocate the given block if it is not large enough, otherwise do nothing.
  208. *
  209. * @see av_realloc
  210. */
  211. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size);
  212. /**
  213. * Allocate a buffer, reusing the given one if large enough.
  214. *
  215. * Contrary to av_fast_realloc the current buffer contents might not be
  216. * preserved and on error the old buffer is freed, thus no special
  217. * handling to avoid memleaks is necessary.
  218. *
  219. * @param ptr pointer to pointer to already allocated buffer, overwritten with pointer to new buffer
  220. * @param size size of the buffer *ptr points to
  221. * @param min_size minimum size of *ptr buffer after returning, *ptr will be NULL and
  222. * *size 0 if an error occurred.
  223. */
  224. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size);
  225. /**
  226. * Allocate a buffer, reusing the given one if large enough.
  227. *
  228. * All newly allocated space is initially cleared
  229. * Contrary to av_fast_realloc the current buffer contents might not be
  230. * preserved and on error the old buffer is freed, thus no special
  231. * handling to avoid memleaks is necessary.
  232. *
  233. * @param ptr pointer to pointer to already allocated buffer, overwritten with pointer to new buffer
  234. * @param size size of the buffer *ptr points to
  235. * @param min_size minimum size of *ptr buffer after returning, *ptr will be NULL and
  236. * *size 0 if an error occurred.
  237. */
  238. void av_fast_mallocz(void *ptr, unsigned int *size, size_t min_size);
  239. /**
  240. * Free a memory block which has been allocated with av_malloc(z)() or
  241. * av_realloc().
  242. * @param ptr Pointer to the memory block which should be freed.
  243. * @note ptr = NULL is explicitly allowed.
  244. * @note It is recommended that you use av_freep() instead.
  245. * @see av_freep()
  246. */
  247. void av_free(void *ptr);
  248. /**
  249. * Free a memory block which has been allocated with av_malloc(z)() or
  250. * av_realloc() and set the pointer pointing to it to NULL.
  251. * @param ptr Pointer to the pointer to the memory block which should
  252. * be freed.
  253. * @note passing a pointer to a NULL pointer is safe and leads to no action.
  254. * @see av_free()
  255. */
  256. void av_freep(void *ptr);
  257. /**
  258. * Duplicate the string s.
  259. * @param s string to be duplicated
  260. * @return Pointer to a newly-allocated string containing a
  261. * copy of s or NULL if the string cannot be allocated.
  262. */
  263. char *av_strdup(const char *s) av_malloc_attrib;
  264. /**
  265. * Duplicate a substring of the string s.
  266. * @param s string to be duplicated
  267. * @param len the maximum length of the resulting string (not counting the
  268. * terminating byte).
  269. * @return Pointer to a newly-allocated string containing a
  270. * copy of s or NULL if the string cannot be allocated.
  271. */
  272. char *av_strndup(const char *s, size_t len) av_malloc_attrib;
  273. /**
  274. * Duplicate the buffer p.
  275. * @param p buffer to be duplicated
  276. * @return Pointer to a newly allocated buffer containing a
  277. * copy of p or NULL if the buffer cannot be allocated.
  278. */
  279. void *av_memdup(const void *p, size_t size);
  280. /**
  281. * deliberately overlapping memcpy implementation
  282. * @param dst destination buffer
  283. * @param back how many bytes back we start (the initial size of the overlapping window), must be > 0
  284. * @param cnt number of bytes to copy, must be >= 0
  285. *
  286. * cnt > back is valid, this will copy the bytes we just copied,
  287. * thus creating a repeating pattern with a period length of back.
  288. */
  289. void av_memcpy_backptr(uint8_t *dst, int back, int cnt);
  290. /**
  291. * Add an element to a dynamic array.
  292. *
  293. * The array to grow is supposed to be an array of pointers to
  294. * structures, and the element to add must be a pointer to an already
  295. * allocated structure.
  296. *
  297. * The array is reallocated when its size reaches powers of 2.
  298. * Therefore, the amortized cost of adding an element is constant.
  299. *
  300. * In case of success, the pointer to the array is updated in order to
  301. * point to the new grown array, and the number pointed to by nb_ptr
  302. * is incremented.
  303. * In case of failure, the array is freed, *tab_ptr is set to NULL and
  304. * *nb_ptr is set to 0.
  305. *
  306. * @param tab_ptr pointer to the array to grow
  307. * @param nb_ptr pointer to the number of elements in the array
  308. * @param elem element to add
  309. * @see av_dynarray_add_nofree(), av_dynarray2_add()
  310. */
  311. void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem);
  312. /**
  313. * Add an element to a dynamic array.
  314. *
  315. * Function has the same functionality as av_dynarray_add(),
  316. * but it doesn't free memory on fails. It returns error code
  317. * instead and leave current buffer untouched.
  318. *
  319. * @param tab_ptr pointer to the array to grow
  320. * @param nb_ptr pointer to the number of elements in the array
  321. * @param elem element to add
  322. * @return >=0 on success, negative otherwise.
  323. * @see av_dynarray_add(), av_dynarray2_add()
  324. */
  325. av_warn_unused_result
  326. int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem);
  327. /**
  328. * Add an element of size elem_size to a dynamic array.
  329. *
  330. * The array is reallocated when its number of elements reaches powers of 2.
  331. * Therefore, the amortized cost of adding an element is constant.
  332. *
  333. * In case of success, the pointer to the array is updated in order to
  334. * point to the new grown array, and the number pointed to by nb_ptr
  335. * is incremented.
  336. * In case of failure, the array is freed, *tab_ptr is set to NULL and
  337. * *nb_ptr is set to 0.
  338. *
  339. * @param tab_ptr pointer to the array to grow
  340. * @param nb_ptr pointer to the number of elements in the array
  341. * @param elem_size size in bytes of the elements in the array
  342. * @param elem_data pointer to the data of the element to add. If NULL, the space of
  343. * the new added element is not filled.
  344. * @return pointer to the data of the element to copy in the new allocated space.
  345. * If NULL, the new allocated space is left uninitialized."
  346. * @see av_dynarray_add(), av_dynarray_add_nofree()
  347. */
  348. void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size,
  349. const uint8_t *elem_data);
  350. /**
  351. * Multiply two size_t values checking for overflow.
  352. * @return 0 if success, AVERROR(EINVAL) if overflow.
  353. */
  354. static inline int av_size_mult(size_t a, size_t b, size_t *r)
  355. {
  356. size_t t = a * b;
  357. /* Hack inspired from glibc: don't try the division if nelem and elsize
  358. * are both less than sqrt(SIZE_MAX). */
  359. if ((a | b) >= ((size_t)1 << (sizeof(size_t) * 4)) && a && t / a != b)
  360. return AVERROR(EINVAL);
  361. *r = t;
  362. return 0;
  363. }
  364. /**
  365. * Set the maximum size that may me allocated in one block.
  366. */
  367. void av_max_alloc(size_t max);
  368. /**
  369. * @}
  370. */
  371. #endif /* AVUTIL_MEM_H */