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.

259 lines
9.7KB

  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(__GNUC__) || defined(__clang__)
  38. #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
  39. #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v
  40. #elif defined(_MSC_VER)
  41. #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
  42. #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v
  43. #else
  44. #define DECLARE_ALIGNED(n,t,v) t v
  45. #define DECLARE_ASM_CONST(n,t,v) static const t v
  46. #endif
  47. #if AV_GCC_VERSION_AT_LEAST(3,1)
  48. #define av_malloc_attrib __attribute__((__malloc__))
  49. #else
  50. #define av_malloc_attrib
  51. #endif
  52. #if AV_GCC_VERSION_AT_LEAST(4,3)
  53. #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__)))
  54. #else
  55. #define av_alloc_size(...)
  56. #endif
  57. /**
  58. * Allocate a block of size bytes with alignment suitable for all
  59. * memory accesses (including vectors if available on the CPU).
  60. * @param size Size in bytes for the memory block to be allocated.
  61. * @return Pointer to the allocated block, NULL if the block cannot
  62. * be allocated.
  63. * @see av_mallocz()
  64. */
  65. void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);
  66. /**
  67. * Allocate a block of size * nmemb bytes with av_malloc().
  68. * @param nmemb Number of elements
  69. * @param size Size of the single element
  70. * @return Pointer to the allocated block, NULL if the block cannot
  71. * be allocated.
  72. * @see av_malloc()
  73. */
  74. av_alloc_size(1, 2) void *av_malloc_array(size_t nmemb, size_t size);
  75. /**
  76. * Allocate or reallocate a block of memory.
  77. * If ptr is NULL and size > 0, allocate a new block. If
  78. * size is zero, free the memory block pointed to by ptr.
  79. * @param ptr Pointer to a memory block already allocated with
  80. * av_realloc() or NULL.
  81. * @param size Size in bytes of the memory block to be allocated or
  82. * reallocated.
  83. * @return Pointer to a newly-reallocated block or NULL if the block
  84. * cannot be reallocated or the function is used to free the memory block.
  85. * @warning Pointers originating from the av_malloc() family of functions must
  86. * not be passed to av_realloc(). The former can be implemented using
  87. * memalign() (or other functions), and there is no guarantee that
  88. * pointers from such functions can be passed to realloc() at all.
  89. * The situation is undefined according to POSIX and may crash with
  90. * some libc implementations.
  91. * @see av_fast_realloc()
  92. */
  93. void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
  94. /**
  95. * Allocate or reallocate a block of memory.
  96. * If *ptr is NULL and size > 0, allocate a new block. If
  97. * size is zero, free the memory block pointed to by ptr.
  98. * @param ptr Pointer to a pointer to a memory block already allocated
  99. * with av_realloc(), or pointer to a pointer to NULL.
  100. * The pointer is updated on success, or freed on failure.
  101. * @param size Size in bytes for the memory block to be allocated or
  102. * reallocated
  103. * @return Zero on success, an AVERROR error code on failure.
  104. * @warning Pointers originating from the av_malloc() family of functions must
  105. * not be passed to av_reallocp(). The former can be implemented using
  106. * memalign() (or other functions), and there is no guarantee that
  107. * pointers from such functions can be passed to realloc() at all.
  108. * The situation is undefined according to POSIX and may crash with
  109. * some libc implementations.
  110. */
  111. int av_reallocp(void *ptr, size_t size);
  112. /**
  113. * Allocate or reallocate an array.
  114. * If ptr is NULL and nmemb > 0, allocate a new block. If
  115. * nmemb is zero, free the memory block pointed to by ptr.
  116. * @param ptr Pointer to a memory block already allocated with
  117. * av_realloc() or NULL.
  118. * @param nmemb Number of elements
  119. * @param size Size of the single element
  120. * @return Pointer to a newly-reallocated block or NULL if the block
  121. * cannot be reallocated or the function is used to free the memory block.
  122. * @warning Pointers originating from the av_malloc() family of functions must
  123. * not be passed to av_realloc(). The former can be implemented using
  124. * memalign() (or other functions), and there is no guarantee that
  125. * pointers from such functions can be passed to realloc() at all.
  126. * The situation is undefined according to POSIX and may crash with
  127. * some libc implementations.
  128. */
  129. av_alloc_size(2, 3) void *av_realloc_array(void *ptr, size_t nmemb, size_t size);
  130. /**
  131. * Allocate or reallocate an array through a pointer to a pointer.
  132. * If *ptr is NULL and nmemb > 0, allocate a new block. If
  133. * nmemb is zero, free the memory block pointed to by ptr.
  134. * @param ptr Pointer to a pointer to a memory block already allocated
  135. * with av_realloc(), or pointer to a pointer to NULL.
  136. * The pointer is updated on success, or freed on failure.
  137. * @param nmemb Number of elements
  138. * @param size Size of the single element
  139. * @return Zero on success, an AVERROR error code on failure.
  140. * @warning Pointers originating from the av_malloc() family of functions must
  141. * not be passed to av_realloc(). The former can be implemented using
  142. * memalign() (or other functions), and there is no guarantee that
  143. * pointers from such functions can be passed to realloc() at all.
  144. * The situation is undefined according to POSIX and may crash with
  145. * some libc implementations.
  146. */
  147. av_alloc_size(2, 3) int av_reallocp_array(void *ptr, size_t nmemb, size_t size);
  148. /**
  149. * Free a memory block which has been allocated with av_malloc(z)() or
  150. * av_realloc().
  151. * @param ptr Pointer to the memory block which should be freed.
  152. * @note ptr = NULL is explicitly allowed.
  153. * @note It is recommended that you use av_freep() instead.
  154. * @see av_freep()
  155. */
  156. void av_free(void *ptr);
  157. /**
  158. * Allocate a block of size bytes with alignment suitable for all
  159. * memory accesses (including vectors if available on the CPU) and
  160. * zero all the bytes of the block.
  161. * @param size Size in bytes for the memory block to be allocated.
  162. * @return Pointer to the allocated block, NULL if it cannot be allocated.
  163. * @see av_malloc()
  164. */
  165. void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
  166. /**
  167. * Allocate a block of size * nmemb bytes with av_mallocz().
  168. * @param nmemb Number of elements
  169. * @param size Size of the single element
  170. * @return Pointer to the allocated block, NULL if the block cannot
  171. * be allocated.
  172. * @see av_mallocz()
  173. * @see av_malloc_array()
  174. */
  175. av_alloc_size(1, 2) void *av_mallocz_array(size_t nmemb, size_t size);
  176. /**
  177. * Duplicate the string s.
  178. * @param s string to be duplicated
  179. * @return Pointer to a newly-allocated string containing a
  180. * copy of s or NULL if the string cannot be allocated.
  181. */
  182. char *av_strdup(const char *s) av_malloc_attrib;
  183. /**
  184. * Duplicate a substring of the string s.
  185. * @param s string to be duplicated
  186. * @param len the maximum length of the resulting string (not counting the
  187. * terminating byte).
  188. * @return Pointer to a newly-allocated string containing a
  189. * copy of s or NULL if the string cannot be allocated.
  190. */
  191. char *av_strndup(const char *s, size_t len) av_malloc_attrib;
  192. /**
  193. * Free a memory block which has been allocated with av_malloc(z)() or
  194. * av_realloc() and set the pointer pointing to it to NULL.
  195. * @param ptr Pointer to the pointer to the memory block which should
  196. * be freed.
  197. * @see av_free()
  198. */
  199. void av_freep(void *ptr);
  200. /**
  201. * 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)
  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. * Reallocate the given block if it is not large enough, otherwise do nothing.
  212. *
  213. * @see av_realloc
  214. */
  215. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size);
  216. /**
  217. * Allocate a buffer, reusing the given one if large enough.
  218. *
  219. * Contrary to av_fast_realloc the current buffer contents might not be
  220. * preserved and on error the old buffer is freed, thus no special
  221. * handling to avoid memleaks is necessary.
  222. *
  223. * @param ptr pointer to pointer to already allocated buffer, overwritten with pointer to new buffer
  224. * @param size size of the buffer *ptr points to
  225. * @param min_size minimum size of *ptr buffer after returning, *ptr will be NULL and
  226. * *size 0 if an error occurred.
  227. */
  228. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size);
  229. /**
  230. * @}
  231. */
  232. #endif /* AVUTIL_MEM_H */