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.

222 lines
7.1KB

  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 "attributes.h"
  27. #include "error.h"
  28. #include "avutil.h"
  29. /**
  30. * @addtogroup lavu_mem
  31. * @{
  32. */
  33. #if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1110 || defined(__SUNPRO_C)
  34. #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
  35. #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v
  36. #elif defined(__TI_COMPILER_VERSION__)
  37. #define DECLARE_ALIGNED(n,t,v) \
  38. AV_PRAGMA(DATA_ALIGN(v,n)) \
  39. t __attribute__((aligned(n))) v
  40. #define DECLARE_ASM_CONST(n,t,v) \
  41. AV_PRAGMA(DATA_ALIGN(v,n)) \
  42. static const t __attribute__((aligned(n))) v
  43. #elif defined(__GNUC__)
  44. #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
  45. #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v
  46. #elif defined(_MSC_VER)
  47. #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
  48. #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v
  49. #else
  50. #define DECLARE_ALIGNED(n,t,v) t v
  51. #define DECLARE_ASM_CONST(n,t,v) static const t v
  52. #endif
  53. #if AV_GCC_VERSION_AT_LEAST(3,1)
  54. #define av_malloc_attrib __attribute__((__malloc__))
  55. #else
  56. #define av_malloc_attrib
  57. #endif
  58. #if AV_GCC_VERSION_AT_LEAST(4,3)
  59. #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__)))
  60. #else
  61. #define av_alloc_size(...)
  62. #endif
  63. /**
  64. * Allocate a block of size bytes with alignment suitable for all
  65. * memory accesses (including vectors if available on the CPU).
  66. * @param size Size in bytes for the memory block to be allocated.
  67. * @return Pointer to the allocated block, NULL if the block cannot
  68. * be allocated.
  69. * @see av_mallocz()
  70. */
  71. void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);
  72. /**
  73. * Helper function to allocate a block of size * nmemb bytes with
  74. * using av_malloc()
  75. * @param nmemb Number of elements
  76. * @param size Size of the single element
  77. * @return Pointer to the allocated block, NULL if the block cannot
  78. * be allocated.
  79. * @see av_malloc()
  80. */
  81. av_alloc_size(1,2) static inline void *av_malloc_array(size_t nmemb, size_t size)
  82. {
  83. if (size <= 0 || nmemb >= INT_MAX / size)
  84. return NULL;
  85. return av_malloc(nmemb * size);
  86. }
  87. /**
  88. * Allocate or reallocate a block of memory.
  89. * If ptr is NULL and size > 0, allocate a new block. If
  90. * size is zero, free the memory block pointed to by ptr.
  91. * @param ptr Pointer to a memory block already allocated with
  92. * av_malloc(z)() or av_realloc() or NULL.
  93. * @param size Size in bytes for the memory block to be allocated or
  94. * reallocated.
  95. * @return Pointer to a newly reallocated block or NULL if the block
  96. * cannot be reallocated or the function is used to free the memory block.
  97. * @see av_fast_realloc()
  98. */
  99. void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
  100. /**
  101. * Allocate or reallocate a block of memory.
  102. * This function does the same thing as av_realloc, except:
  103. * - It takes two arguments and checks the result of the multiplication for
  104. * integer overflow.
  105. * - It frees the input block in case of failure, thus avoiding the memory
  106. * leak with the classic "buf = realloc(buf); if (!buf) return -1;".
  107. */
  108. void *av_realloc_f(void *ptr, size_t nelem, size_t elsize);
  109. /**
  110. * Free a memory block which has been allocated with av_malloc(z)() or
  111. * av_realloc().
  112. * @param ptr Pointer to the memory block which should be freed.
  113. * @note ptr = NULL is explicitly allowed.
  114. * @note It is recommended that you use av_freep() instead.
  115. * @see av_freep()
  116. */
  117. void av_free(void *ptr);
  118. /**
  119. * Allocate a block of size bytes with alignment suitable for all
  120. * memory accesses (including vectors if available on the CPU) and
  121. * zero all the bytes of the block.
  122. * @param size Size in bytes for the memory block to be allocated.
  123. * @return Pointer to the allocated block, NULL if it cannot be allocated.
  124. * @see av_malloc()
  125. */
  126. void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
  127. /**
  128. * Allocate a block of nmemb * size bytes with alignment suitable for all
  129. * memory accesses (including vectors if available on the CPU) and
  130. * zero all the bytes of the block.
  131. * The allocation will fail if nmemb * size is greater than or equal
  132. * to INT_MAX.
  133. * @param nmemb
  134. * @param size
  135. * @return Pointer to the allocated block, NULL if it cannot be allocated.
  136. */
  137. void *av_calloc(size_t nmemb, size_t size) av_malloc_attrib;
  138. /**
  139. * Helper function to allocate a block of size * nmemb bytes with
  140. * using av_mallocz()
  141. * @param nmemb Number of elements
  142. * @param size Size of the single element
  143. * @return Pointer to the allocated block, NULL if the block cannot
  144. * be allocated.
  145. * @see av_mallocz()
  146. * @see av_malloc_array()
  147. */
  148. av_alloc_size(1,2) static inline void *av_mallocz_array(size_t nmemb, size_t size)
  149. {
  150. if (size <= 0 || nmemb >= INT_MAX / size)
  151. return NULL;
  152. return av_mallocz(nmemb * size);
  153. }
  154. /**
  155. * Duplicate the string s.
  156. * @param s string to be duplicated
  157. * @return Pointer to a newly allocated string containing a
  158. * copy of s or NULL if the string cannot be allocated.
  159. */
  160. char *av_strdup(const char *s) av_malloc_attrib;
  161. /**
  162. * Free a memory block which has been allocated with av_malloc(z)() or
  163. * av_realloc() and set the pointer pointing to it to NULL.
  164. * @param ptr Pointer to the pointer to the memory block which should
  165. * be freed.
  166. * @see av_free()
  167. */
  168. void av_freep(void *ptr);
  169. /**
  170. * Add an element to a dynamic array.
  171. *
  172. * @param tab_ptr Pointer to the array.
  173. * @param nb_ptr Pointer to the number of elements in the array.
  174. * @param elem Element to be added.
  175. */
  176. void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem);
  177. /**
  178. * Multiply two size_t values checking for overflow.
  179. * @return 0 if success, AVERROR(EINVAL) if overflow.
  180. */
  181. static inline int av_size_mult(size_t a, size_t b, size_t *r)
  182. {
  183. size_t t = a * b;
  184. /* Hack inspired from glibc: only try the division if nelem and elsize
  185. * are both greater than sqrt(SIZE_MAX). */
  186. if ((a | b) >= ((size_t)1 << (sizeof(size_t) * 4)) && a && t / a != b)
  187. return AVERROR(EINVAL);
  188. *r = t;
  189. return 0;
  190. }
  191. /**
  192. * Set the maximum size that may me allocated in one block.
  193. */
  194. void av_max_alloc(size_t max);
  195. /**
  196. * @}
  197. */
  198. #endif /* AVUTIL_MEM_H */