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.

276 lines
10KB

  1. /*
  2. * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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 "avutil.h"
  30. /**
  31. * @addtogroup lavu_mem
  32. * @{
  33. */
  34. #if defined(__ICC) && __ICC < 1200 || defined(__SUNPRO_C)
  35. #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
  36. #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v
  37. #elif defined(__TI_COMPILER_VERSION__)
  38. #define DECLARE_ALIGNED(n,t,v) \
  39. AV_PRAGMA(DATA_ALIGN(v,n)) \
  40. t __attribute__((aligned(n))) v
  41. #define DECLARE_ASM_CONST(n,t,v) \
  42. AV_PRAGMA(DATA_ALIGN(v,n)) \
  43. static const t __attribute__((aligned(n))) v
  44. #elif defined(__GNUC__) || defined(__clang__)
  45. #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
  46. #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v
  47. #elif defined(_MSC_VER)
  48. #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
  49. #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v
  50. #else
  51. #define DECLARE_ALIGNED(n,t,v) t v
  52. #define DECLARE_ASM_CONST(n,t,v) static const t v
  53. #endif
  54. #if AV_GCC_VERSION_AT_LEAST(3,1)
  55. #define av_malloc_attrib __attribute__((__malloc__))
  56. #else
  57. #define av_malloc_attrib
  58. #endif
  59. #if AV_GCC_VERSION_AT_LEAST(4,3)
  60. #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__)))
  61. #else
  62. #define av_alloc_size(...)
  63. #endif
  64. /**
  65. * Allocate a block of size bytes with alignment suitable for all
  66. * memory accesses (including vectors if available on the CPU).
  67. * @param size Size in bytes for the memory block to be allocated.
  68. * @return Pointer to the allocated block, NULL if the block cannot
  69. * be allocated.
  70. * @see av_mallocz()
  71. */
  72. void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);
  73. /**
  74. * Allocate a block of size * nmemb bytes with 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 || 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_realloc() or NULL.
  93. * @param size Size in bytes of 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. * @warning Pointers originating from the av_malloc() family of functions must
  98. * not be passed to av_realloc(). The former can be implemented using
  99. * memalign() (or other functions), and there is no guarantee that
  100. * pointers from such functions can be passed to realloc() at all.
  101. * The situation is undefined according to POSIX and may crash with
  102. * some libc implementations.
  103. * @see av_fast_realloc()
  104. */
  105. void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
  106. /**
  107. * Allocate or reallocate a block of memory.
  108. * If *ptr is NULL and size > 0, allocate a new block. If
  109. * size is zero, free the memory block pointed to by ptr.
  110. * @param ptr Pointer to a pointer to a memory block already allocated
  111. * with av_realloc(), or pointer to a pointer to NULL.
  112. * The pointer is updated on success, or freed on failure.
  113. * @param size Size in bytes for the memory block to be allocated or
  114. * reallocated
  115. * @return Zero on success, an AVERROR error code on failure.
  116. * @warning Pointers originating from the av_malloc() family of functions must
  117. * not be passed to av_reallocp(). The former can be implemented using
  118. * memalign() (or other functions), and there is no guarantee that
  119. * pointers from such functions can be passed to realloc() at all.
  120. * The situation is undefined according to POSIX and may crash with
  121. * some libc implementations.
  122. */
  123. int av_reallocp(void *ptr, size_t size);
  124. /**
  125. * Allocate or reallocate an array.
  126. * If ptr is NULL and nmemb > 0, allocate a new block. If
  127. * nmemb is zero, free the memory block pointed to by ptr.
  128. * @param ptr Pointer to a memory block already allocated with
  129. * av_realloc() or NULL.
  130. * @param nmemb Number of elements
  131. * @param size Size of the single element
  132. * @return Pointer to a newly-reallocated block or NULL if the block
  133. * cannot be reallocated or the function is used to free the memory block.
  134. * @warning Pointers originating from the av_malloc() family of functions must
  135. * not be passed to av_realloc(). The former can be implemented using
  136. * memalign() (or other functions), and there is no guarantee that
  137. * pointers from such functions can be passed to realloc() at all.
  138. * The situation is undefined according to POSIX and may crash with
  139. * some libc implementations.
  140. */
  141. av_alloc_size(2, 3) void *av_realloc_array(void *ptr, size_t nmemb, size_t size);
  142. /**
  143. * Allocate or reallocate an array through a pointer to a pointer.
  144. * If *ptr is NULL and nmemb > 0, allocate a new block. If
  145. * nmemb 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 nmemb Number of elements
  150. * @param size Size of the single element
  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_realloc(). 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_alloc_size(2, 3) int av_reallocp_array(void *ptr, size_t nmemb, size_t size);
  160. /**
  161. * Free a memory block which has been allocated with av_malloc(z)() or
  162. * av_realloc().
  163. * @param ptr Pointer to the memory block which should be freed.
  164. * @note ptr = NULL is explicitly allowed.
  165. * @note It is recommended that you use av_freep() instead.
  166. * @see av_freep()
  167. */
  168. void av_free(void *ptr);
  169. /**
  170. * Allocate a block of size bytes with alignment suitable for all
  171. * memory accesses (including vectors if available on the CPU) and
  172. * zero all the bytes of the block.
  173. * @param size Size in bytes for the memory block to be allocated.
  174. * @return Pointer to the allocated block, NULL if it cannot be allocated.
  175. * @see av_malloc()
  176. */
  177. void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
  178. /**
  179. * Allocate a block of size * nmemb bytes with av_mallocz().
  180. * @param nmemb Number of elements
  181. * @param size Size of the single element
  182. * @return Pointer to the allocated block, NULL if the block cannot
  183. * be allocated.
  184. * @see av_mallocz()
  185. * @see av_malloc_array()
  186. */
  187. av_alloc_size(1, 2) static inline void *av_mallocz_array(size_t nmemb, size_t size)
  188. {
  189. if (!size || nmemb >= INT_MAX / size)
  190. return NULL;
  191. return av_mallocz(nmemb * size);
  192. }
  193. /**
  194. * Duplicate the string s.
  195. * @param s string to be duplicated
  196. * @return Pointer to a newly-allocated string containing a
  197. * copy of s or NULL if the string cannot be allocated.
  198. */
  199. char *av_strdup(const char *s) av_malloc_attrib;
  200. /**
  201. * Duplicate a substring of the string s.
  202. * @param s string to be duplicated
  203. * @param len the maximum length of the resulting string (not counting the
  204. * terminating byte).
  205. * @return Pointer to a newly-allocated string containing a
  206. * copy of s or NULL if the string cannot be allocated.
  207. */
  208. char *av_strndup(const char *s, size_t len) av_malloc_attrib;
  209. /**
  210. * Free a memory block which has been allocated with av_malloc(z)() or
  211. * av_realloc() and set the pointer pointing to it to NULL.
  212. * @param ptr Pointer to the pointer to the memory block which should
  213. * be freed.
  214. * @see av_free()
  215. */
  216. void av_freep(void *ptr);
  217. /**
  218. * deliberately overlapping memcpy implementation
  219. * @param dst destination buffer
  220. * @param back how many bytes back we start (the initial size of the overlapping window)
  221. * @param cnt number of bytes to copy, must be >= 0
  222. *
  223. * cnt > back is valid, this will copy the bytes we just copied,
  224. * thus creating a repeating pattern with a period length of back.
  225. */
  226. void av_memcpy_backptr(uint8_t *dst, int back, int cnt);
  227. /**
  228. * Reallocate the given block if it is not large enough, otherwise do nothing.
  229. *
  230. * @see av_realloc
  231. */
  232. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size);
  233. /**
  234. * Allocate a buffer, reusing the given one if large enough.
  235. *
  236. * Contrary to av_fast_realloc the current buffer contents might not be
  237. * preserved and on error the old buffer is freed, thus no special
  238. * handling to avoid memleaks is necessary.
  239. *
  240. * @param ptr pointer to pointer to already allocated buffer, overwritten with pointer to new buffer
  241. * @param size size of the buffer *ptr points to
  242. * @param min_size minimum size of *ptr buffer after returning, *ptr will be NULL and
  243. * *size 0 if an error occurred.
  244. */
  245. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size);
  246. /**
  247. * @}
  248. */
  249. #endif /* AVUTIL_MEM_H */