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.

214 lines
7.3KB

  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__)
  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 <= 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_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. * @note av_realloc() is not guaranteed to maintain the alignment of
  98. * pointers originating from the av_malloc() family of functions.
  99. * @see av_fast_realloc()
  100. */
  101. void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
  102. /**
  103. * Allocate or reallocate an array.
  104. * If ptr is NULL and nmemb > 0, allocate a new block. If
  105. * nmemb is zero, free the memory block pointed to by ptr.
  106. * @param ptr Pointer to a memory block already allocated with
  107. * av_realloc() or NULL.
  108. * @param nmemb Number of elements
  109. * @param size Size of the single element
  110. * @return Pointer to a newly-reallocated block or NULL if the block
  111. * cannot be reallocated or the function is used to free the memory block.
  112. * @note av_realloc_array() is not guaranteed to maintain the alignment of
  113. * pointers originating from the av_malloc() family of functions.
  114. */
  115. av_alloc_size(2, 3) void *av_realloc_array(void *ptr, size_t nmemb, size_t size);
  116. /**
  117. * Allocate or reallocate an array through a pointer to a pointer.
  118. * If *ptr is NULL and nmemb > 0, allocate a new block. If
  119. * nmemb is zero, free the memory block pointed to by ptr.
  120. * @param ptr Pointer to a pointer to a memory block already allocated
  121. * with av_realloc(), or pointer to a pointer to NULL.
  122. * The pointer is updated on success, or freed on failure.
  123. * @param nmemb Number of elements
  124. * @param size Size of the single element
  125. * @return Zero on success, an AVERROR error code on failure.
  126. * @note av_reallocp_array() is not guaranteed to maintain the alignment of
  127. * pointers originating from the av_malloc() family of functions.
  128. */
  129. av_alloc_size(2, 3) int av_reallocp_array(void *ptr, size_t nmemb, size_t size);
  130. /**
  131. * Free a memory block which has been allocated with av_malloc(z)() or
  132. * av_realloc().
  133. * @param ptr Pointer to the memory block which should be freed.
  134. * @note ptr = NULL is explicitly allowed.
  135. * @note It is recommended that you use av_freep() instead.
  136. * @see av_freep()
  137. */
  138. void av_free(void *ptr);
  139. /**
  140. * Allocate a block of size bytes with alignment suitable for all
  141. * memory accesses (including vectors if available on the CPU) and
  142. * zero all the bytes of the block.
  143. * @param size Size in bytes for the memory block to be allocated.
  144. * @return Pointer to the allocated block, NULL if it cannot be allocated.
  145. * @see av_malloc()
  146. */
  147. void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
  148. /**
  149. * Allocate a block of size * nmemb bytes with av_mallocz().
  150. * @param nmemb Number of elements
  151. * @param size Size of the single element
  152. * @return Pointer to the allocated block, NULL if the block cannot
  153. * be allocated.
  154. * @see av_mallocz()
  155. * @see av_malloc_array()
  156. */
  157. av_alloc_size(1, 2) static inline void *av_mallocz_array(size_t nmemb, size_t size)
  158. {
  159. if (size <= 0 || nmemb >= INT_MAX / size)
  160. return NULL;
  161. return av_mallocz(nmemb * size);
  162. }
  163. /**
  164. * Duplicate the string s.
  165. * @param s string to be duplicated
  166. * @return Pointer to a newly-allocated string containing a
  167. * copy of s or NULL if the string cannot be allocated.
  168. */
  169. char *av_strdup(const char *s) av_malloc_attrib;
  170. /**
  171. * Free a memory block which has been allocated with av_malloc(z)() or
  172. * av_realloc() and set the pointer pointing to it to NULL.
  173. * @param ptr Pointer to the pointer to the memory block which should
  174. * be freed.
  175. * @see av_free()
  176. */
  177. void av_freep(void *ptr);
  178. /**
  179. * deliberately overlapping memcpy implementation
  180. * @param dst destination buffer
  181. * @param back how many bytes back we start (the initial size of the overlapping window)
  182. * @param cnt number of bytes to copy, must be >= 0
  183. *
  184. * cnt > back is valid, this will copy the bytes we just copied,
  185. * thus creating a repeating pattern with a period length of back.
  186. */
  187. void av_memcpy_backptr(uint8_t *dst, int back, int cnt);
  188. /**
  189. * @}
  190. */
  191. #endif /* AVUTIL_MEM_H */