mem.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. * Helper function to allocate a block of size * nmemb bytes with
  76. * using av_malloc()
  77. * @param nmemb Number of elements
  78. * @param size Size of the single element
  79. * @return Pointer to the allocated block, NULL if the block cannot
  80. * be allocated.
  81. * @see av_malloc()
  82. */
  83. av_alloc_size(1, 2) static inline void *av_malloc_array(size_t nmemb, size_t size)
  84. {
  85. if (size <= 0 || nmemb >= INT_MAX / size) {
  86. return NULL;
  87. }
  88. return av_malloc(nmemb * size);
  89. }
  90. /**
  91. * Allocate or reallocate a block of memory.
  92. * If ptr is NULL and size > 0, allocate a new block. If
  93. * size is zero, free the memory block pointed to by ptr.
  94. * @param ptr Pointer to a memory block already allocated with
  95. * av_malloc(z)() or av_realloc() or NULL.
  96. * @param size Size in bytes for the memory block to be allocated or
  97. * reallocated.
  98. * @return Pointer to a newly reallocated block or NULL if the block
  99. * cannot be reallocated or the function is used to free the memory block.
  100. * @see av_fast_realloc()
  101. */
  102. void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
  103. /**
  104. * Allocate or reallocate a block of memory.
  105. * This function does the same thing as av_realloc, except:
  106. * - It takes two arguments and checks the result of the multiplication for
  107. * integer overflow.
  108. * - It frees the input block in case of failure, thus avoiding the memory
  109. * leak with the classic "buf = realloc(buf); if (!buf) return -1;".
  110. */
  111. void *av_realloc_f(void *ptr, size_t nelem, size_t elsize);
  112. /**
  113. * Free a memory block which has been allocated with av_malloc(z)() or
  114. * av_realloc().
  115. * @param ptr Pointer to the memory block which should be freed.
  116. * @note ptr = NULL is explicitly allowed.
  117. * @note It is recommended that you use av_freep() instead.
  118. * @see av_freep()
  119. */
  120. void av_free(void *ptr);
  121. /**
  122. * Allocate a block of size bytes with alignment suitable for all
  123. * memory accesses (including vectors if available on the CPU) and
  124. * zero all the bytes of the block.
  125. * @param size Size in bytes for the memory block to be allocated.
  126. * @return Pointer to the allocated block, NULL if it cannot be allocated.
  127. * @see av_malloc()
  128. */
  129. void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
  130. /**
  131. * Allocate a block of nmemb * size bytes with alignment suitable for all
  132. * memory accesses (including vectors if available on the CPU) and
  133. * zero all the bytes of the block.
  134. * The allocation will fail if nmemb * size is greater than or equal
  135. * to INT_MAX.
  136. * @param nmemb
  137. * @param size
  138. * @return Pointer to the allocated block, NULL if it cannot be allocated.
  139. */
  140. void *av_calloc(size_t nmemb, size_t size) av_malloc_attrib;
  141. /**
  142. * Helper function to allocate a block of size * nmemb bytes with
  143. * using av_mallocz()
  144. * @param nmemb Number of elements
  145. * @param size Size of the single element
  146. * @return Pointer to the allocated block, NULL if the block cannot
  147. * be allocated.
  148. * @see av_mallocz()
  149. * @see av_malloc_array()
  150. */
  151. av_alloc_size(1, 2) static inline void *av_mallocz_array(size_t nmemb, size_t size)
  152. {
  153. if (size <= 0 || nmemb >= INT_MAX / size) {
  154. return NULL;
  155. }
  156. return av_mallocz(nmemb * size);
  157. }
  158. /**
  159. * Duplicate the string s.
  160. * @param s string to be duplicated
  161. * @return Pointer to a newly allocated string containing a
  162. * copy of s or NULL if the string cannot be allocated.
  163. */
  164. char *av_strdup(const char *s) av_malloc_attrib;
  165. /**
  166. * Free a memory block which has been allocated with av_malloc(z)() or
  167. * av_realloc() and set the pointer pointing to it to NULL.
  168. * @param ptr Pointer to the pointer to the memory block which should
  169. * be freed.
  170. * @see av_free()
  171. */
  172. void av_freep(void *ptr);
  173. /**
  174. * Add an element to a dynamic array.
  175. *
  176. * @param tab_ptr Pointer to the array.
  177. * @param nb_ptr Pointer to the number of elements in the array.
  178. * @param elem Element to be added.
  179. */
  180. void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem);
  181. /**
  182. * Multiply two size_t values checking for overflow.
  183. * @return 0 if success, AVERROR(EINVAL) if overflow.
  184. */
  185. static inline int av_size_mult(size_t a, size_t b, size_t *r)
  186. {
  187. size_t t = a * b;
  188. /* Hack inspired from glibc: only try the division if nelem and elsize
  189. * are both greater than sqrt(SIZE_MAX). */
  190. if ((a | b) >= ((size_t)1 << (sizeof(size_t) * 4)) && a && t / a != b) {
  191. return AVERROR(EINVAL);
  192. }
  193. *r = t;
  194. return 0;
  195. }
  196. /**
  197. * Set the maximum size that may me allocated in one block.
  198. */
  199. void av_max_alloc(size_t max);
  200. /**
  201. * @brief deliberately overlapping memcpy implementation
  202. * @param dst destination buffer
  203. * @param back how many bytes back we start (the initial size of the overlapping window), must be > 0
  204. * @param cnt number of bytes to copy, must be >= 0
  205. *
  206. * cnt > back is valid, this will copy the bytes we just copied,
  207. * thus creating a repeating pattern with a period length of back.
  208. */
  209. void av_memcpy_backptr(uint8_t *dst, int back, int cnt);
  210. /**
  211. * @}
  212. */
  213. #endif /* AVUTIL_MEM_H */