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.

266 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) void *av_malloc_array(size_t nmemb, size_t size);
  82. /**
  83. * Allocate or reallocate a block of memory.
  84. * If ptr is NULL and size > 0, allocate a new block. If
  85. * size is zero, free the memory block pointed to by ptr.
  86. * @param ptr Pointer to a memory block already allocated with
  87. * av_realloc() or NULL.
  88. * @param size Size in bytes of the memory block to be allocated or
  89. * reallocated.
  90. * @return Pointer to a newly-reallocated block or NULL if the block
  91. * cannot be reallocated or the function is used to free the memory block.
  92. * @warning Pointers originating from the av_malloc() family of functions must
  93. * not be passed to av_realloc(). The former can be implemented using
  94. * memalign() (or other functions), and there is no guarantee that
  95. * pointers from such functions can be passed to realloc() at all.
  96. * The situation is undefined according to POSIX and may crash with
  97. * some libc implementations.
  98. * @see av_fast_realloc()
  99. */
  100. void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
  101. /**
  102. * Allocate or reallocate a block of memory.
  103. * If *ptr is NULL and size > 0, allocate a new block. If
  104. * size is zero, free the memory block pointed to by ptr.
  105. * @param ptr Pointer to a pointer to a memory block already allocated
  106. * with av_realloc(), or pointer to a pointer to NULL.
  107. * The pointer is updated on success, or freed on failure.
  108. * @param size Size in bytes for the memory block to be allocated or
  109. * reallocated
  110. * @return Zero on success, an AVERROR error code on failure.
  111. * @warning Pointers originating from the av_malloc() family of functions must
  112. * not be passed to av_reallocp(). The former can be implemented using
  113. * memalign() (or other functions), and there is no guarantee that
  114. * pointers from such functions can be passed to realloc() at all.
  115. * The situation is undefined according to POSIX and may crash with
  116. * some libc implementations.
  117. */
  118. int av_reallocp(void *ptr, size_t size);
  119. /**
  120. * Allocate or reallocate an array.
  121. * If ptr is NULL and nmemb > 0, allocate a new block. If
  122. * nmemb is zero, free the memory block pointed to by ptr.
  123. * @param ptr Pointer to a memory block already allocated with
  124. * av_realloc() or NULL.
  125. * @param nmemb Number of elements
  126. * @param size Size of the single element
  127. * @return Pointer to a newly-reallocated block or NULL if the block
  128. * cannot be reallocated or the function is used to free the memory block.
  129. * @warning Pointers originating from the av_malloc() family of functions must
  130. * not be passed to av_realloc(). The former can be implemented using
  131. * memalign() (or other functions), and there is no guarantee that
  132. * pointers from such functions can be passed to realloc() at all.
  133. * The situation is undefined according to POSIX and may crash with
  134. * some libc implementations.
  135. */
  136. av_alloc_size(2, 3) void *av_realloc_array(void *ptr, size_t nmemb, size_t size);
  137. /**
  138. * Allocate or reallocate an array through a pointer to a pointer.
  139. * If *ptr is NULL and nmemb > 0, allocate a new block. If
  140. * nmemb is zero, free the memory block pointed to by ptr.
  141. * @param ptr Pointer to a pointer to a memory block already allocated
  142. * with av_realloc(), or pointer to a pointer to NULL.
  143. * The pointer is updated on success, or freed on failure.
  144. * @param nmemb Number of elements
  145. * @param size Size of the single element
  146. * @return Zero on success, an AVERROR error code on failure.
  147. * @warning Pointers originating from the av_malloc() family of functions must
  148. * not be passed to av_realloc(). The former can be implemented using
  149. * memalign() (or other functions), and there is no guarantee that
  150. * pointers from such functions can be passed to realloc() at all.
  151. * The situation is undefined according to POSIX and may crash with
  152. * some libc implementations.
  153. */
  154. av_alloc_size(2, 3) int av_reallocp_array(void *ptr, size_t nmemb, size_t size);
  155. /**
  156. * Free a memory block which has been allocated with av_malloc(z)() or
  157. * av_realloc().
  158. * @param ptr Pointer to the memory block which should be freed.
  159. * @note ptr = NULL is explicitly allowed.
  160. * @note It is recommended that you use av_freep() instead.
  161. * @see av_freep()
  162. */
  163. void av_free(void *ptr);
  164. /**
  165. * Allocate a block of size bytes with alignment suitable for all
  166. * memory accesses (including vectors if available on the CPU) and
  167. * zero all the bytes of the block.
  168. * @param size Size in bytes for the memory block to be allocated.
  169. * @return Pointer to the allocated block, NULL if it cannot be allocated.
  170. * @see av_malloc()
  171. */
  172. void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
  173. /**
  174. * Allocate a block of size * nmemb bytes with av_mallocz().
  175. * @param nmemb Number of elements
  176. * @param size Size of the single element
  177. * @return Pointer to the allocated block, NULL if the block cannot
  178. * be allocated.
  179. * @see av_mallocz()
  180. * @see av_malloc_array()
  181. */
  182. av_alloc_size(1, 2) void *av_mallocz_array(size_t nmemb, size_t size);
  183. /**
  184. * Duplicate the string s.
  185. * @param s string to be duplicated
  186. * @return Pointer to a newly-allocated string containing a
  187. * copy of s or NULL if the string cannot be allocated.
  188. */
  189. char *av_strdup(const char *s) av_malloc_attrib;
  190. /**
  191. * Duplicate a substring of the string s.
  192. * @param s string to be duplicated
  193. * @param len the maximum length of the resulting string (not counting the
  194. * terminating byte).
  195. * @return Pointer to a newly-allocated string containing a
  196. * copy of s or NULL if the string cannot be allocated.
  197. */
  198. char *av_strndup(const char *s, size_t len) av_malloc_attrib;
  199. /**
  200. * Free a memory block which has been allocated with av_malloc(z)() or
  201. * av_realloc() and set the pointer pointing to it to NULL.
  202. * @param ptr Pointer to the pointer to the memory block which should
  203. * be freed.
  204. * @see av_free()
  205. */
  206. void av_freep(void *ptr);
  207. /**
  208. * deliberately overlapping memcpy implementation
  209. * @param dst destination buffer
  210. * @param back how many bytes back we start (the initial size of the overlapping window)
  211. * @param cnt number of bytes to copy, must be >= 0
  212. *
  213. * cnt > back is valid, this will copy the bytes we just copied,
  214. * thus creating a repeating pattern with a period length of back.
  215. */
  216. void av_memcpy_backptr(uint8_t *dst, int back, int cnt);
  217. /**
  218. * Reallocate the given block if it is not large enough, otherwise do nothing.
  219. *
  220. * @see av_realloc
  221. */
  222. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size);
  223. /**
  224. * Allocate a buffer, reusing the given one if large enough.
  225. *
  226. * Contrary to av_fast_realloc the current buffer contents might not be
  227. * preserved and on error the old buffer is freed, thus no special
  228. * handling to avoid memleaks is necessary.
  229. *
  230. * @param ptr pointer to pointer to already allocated buffer, overwritten with pointer to new buffer
  231. * @param size size of the buffer *ptr points to
  232. * @param min_size minimum size of *ptr buffer after returning, *ptr will be NULL and
  233. * *size 0 if an error occurred.
  234. */
  235. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size);
  236. /**
  237. * @}
  238. */
  239. #endif /* AVUTIL_MEM_H */