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.

184 lines
5.9KB

  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. * Helper function to allocate a block of size * nmemb bytes with
  75. * using av_malloc()
  76. * @param nmemb Number of elements
  77. * @param size Size of the single element
  78. * @return Pointer to the allocated block, NULL if the block cannot
  79. * be allocated.
  80. * @see av_malloc()
  81. */
  82. av_alloc_size(1, 2) static inline void *av_malloc_array(size_t nmemb, size_t size)
  83. {
  84. if (size <= 0 || nmemb >= INT_MAX / size)
  85. return NULL;
  86. return av_malloc(nmemb * size);
  87. }
  88. /**
  89. * Allocate or reallocate a block of memory.
  90. * If ptr is NULL and size > 0, allocate a new block. If
  91. * size is zero, free the memory block pointed to by ptr.
  92. * @param ptr Pointer to a memory block already allocated with
  93. * av_malloc(z)() or av_realloc() or NULL.
  94. * @param size Size in bytes for the memory block to be allocated or
  95. * reallocated.
  96. * @return Pointer to a newly reallocated block or NULL if the block
  97. * cannot be reallocated or the function is used to free the memory block.
  98. * @see av_fast_realloc()
  99. */
  100. void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
  101. /**
  102. * Free a memory block which has been allocated with av_malloc(z)() or
  103. * av_realloc().
  104. * @param ptr Pointer to the memory block which should be freed.
  105. * @note ptr = NULL is explicitly allowed.
  106. * @note It is recommended that you use av_freep() instead.
  107. * @see av_freep()
  108. */
  109. void av_free(void *ptr);
  110. /**
  111. * Allocate a block of size bytes with alignment suitable for all
  112. * memory accesses (including vectors if available on the CPU) and
  113. * zero all the bytes of the block.
  114. * @param size Size in bytes for the memory block to be allocated.
  115. * @return Pointer to the allocated block, NULL if it cannot be allocated.
  116. * @see av_malloc()
  117. */
  118. void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
  119. /**
  120. * Helper function to allocate a block of size * nmemb bytes with
  121. * using av_mallocz()
  122. * @param nmemb Number of elements
  123. * @param size Size of the single element
  124. * @return Pointer to the allocated block, NULL if the block cannot
  125. * be allocated.
  126. * @see av_mallocz()
  127. * @see av_malloc_array()
  128. */
  129. av_alloc_size(1, 2) static inline void *av_mallocz_array(size_t nmemb, size_t size)
  130. {
  131. if (size <= 0 || nmemb >= INT_MAX / size)
  132. return NULL;
  133. return av_mallocz(nmemb * size);
  134. }
  135. /**
  136. * Duplicate the string s.
  137. * @param s string to be duplicated
  138. * @return Pointer to a newly allocated string containing a
  139. * copy of s or NULL if the string cannot be allocated.
  140. */
  141. char *av_strdup(const char *s) av_malloc_attrib;
  142. /**
  143. * Free a memory block which has been allocated with av_malloc(z)() or
  144. * av_realloc() and set the pointer pointing to it to NULL.
  145. * @param ptr Pointer to the pointer to the memory block which should
  146. * be freed.
  147. * @see av_free()
  148. */
  149. void av_freep(void *ptr);
  150. /**
  151. * @brief deliberately overlapping memcpy implementation
  152. * @param dst destination buffer
  153. * @param back how many bytes back we start (the initial size of the overlapping window)
  154. * @param cnt number of bytes to copy, must be >= 0
  155. *
  156. * cnt > back is valid, this will copy the bytes we just copied,
  157. * thus creating a repeating pattern with a period length of back.
  158. */
  159. void av_memcpy_backptr(uint8_t *dst, int back, int cnt);
  160. /**
  161. * @}
  162. */
  163. #endif /* AVUTIL_MEM_H */