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.

172 lines
5.4KB

  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 "attributes.h"
  28. #include "avutil.h"
  29. /**
  30. * @addtogroup lavu_mem
  31. * @{
  32. */
  33. #if defined(__ICC) && __ICC < 1200 || defined(__SUNPRO_C)
  34. #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
  35. #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v
  36. #elif defined(__TI_COMPILER_VERSION__)
  37. #define DECLARE_ALIGNED(n,t,v) \
  38. AV_PRAGMA(DATA_ALIGN(v,n)) \
  39. t __attribute__((aligned(n))) v
  40. #define DECLARE_ASM_CONST(n,t,v) \
  41. AV_PRAGMA(DATA_ALIGN(v,n)) \
  42. static const t __attribute__((aligned(n))) v
  43. #elif defined(__GNUC__)
  44. #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
  45. #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v
  46. #elif defined(_MSC_VER)
  47. #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
  48. #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v
  49. #else
  50. #define DECLARE_ALIGNED(n,t,v) t v
  51. #define DECLARE_ASM_CONST(n,t,v) static const t v
  52. #endif
  53. #if AV_GCC_VERSION_AT_LEAST(3,1)
  54. #define av_malloc_attrib __attribute__((__malloc__))
  55. #else
  56. #define av_malloc_attrib
  57. #endif
  58. #if AV_GCC_VERSION_AT_LEAST(4,3)
  59. #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__)))
  60. #else
  61. #define av_alloc_size(...)
  62. #endif
  63. /**
  64. * Allocate a block of size bytes with alignment suitable for all
  65. * memory accesses (including vectors if available on the CPU).
  66. * @param size Size in bytes for the memory block to be allocated.
  67. * @return Pointer to the allocated block, NULL if the block cannot
  68. * be allocated.
  69. * @see av_mallocz()
  70. */
  71. void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);
  72. /**
  73. * Helper function to allocate a block of size * nmemb bytes with
  74. * using 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_malloc(z)() or av_realloc() or NULL.
  93. * @param size Size in bytes for 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. * @see av_fast_realloc()
  98. */
  99. void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
  100. /**
  101. * Free a memory block which has been allocated with av_malloc(z)() or
  102. * av_realloc().
  103. * @param ptr Pointer to the memory block which should be freed.
  104. * @note ptr = NULL is explicitly allowed.
  105. * @note It is recommended that you use av_freep() instead.
  106. * @see av_freep()
  107. */
  108. void av_free(void *ptr);
  109. /**
  110. * Allocate a block of size bytes with alignment suitable for all
  111. * memory accesses (including vectors if available on the CPU) and
  112. * zero all the bytes of the block.
  113. * @param size Size in bytes for the memory block to be allocated.
  114. * @return Pointer to the allocated block, NULL if it cannot be allocated.
  115. * @see av_malloc()
  116. */
  117. void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
  118. /**
  119. * Helper function to allocate a block of size * nmemb bytes with
  120. * using av_mallocz()
  121. * @param nmemb Number of elements
  122. * @param size Size of the single element
  123. * @return Pointer to the allocated block, NULL if the block cannot
  124. * be allocated.
  125. * @see av_mallocz()
  126. * @see av_malloc_array()
  127. */
  128. av_alloc_size(1, 2) static inline void *av_mallocz_array(size_t nmemb, size_t size)
  129. {
  130. if (size <= 0 || nmemb >= INT_MAX / size)
  131. return NULL;
  132. return av_mallocz(nmemb * size);
  133. }
  134. /**
  135. * Duplicate the string s.
  136. * @param s string to be duplicated
  137. * @return Pointer to a newly allocated string containing a
  138. * copy of s or NULL if the string cannot be allocated.
  139. */
  140. char *av_strdup(const char *s) av_malloc_attrib;
  141. /**
  142. * Free a memory block which has been allocated with av_malloc(z)() or
  143. * av_realloc() and set the pointer pointing to it to NULL.
  144. * @param ptr Pointer to the pointer to the memory block which should
  145. * be freed.
  146. * @see av_free()
  147. */
  148. void av_freep(void *ptr);
  149. /**
  150. * @}
  151. */
  152. #endif /* AVUTIL_MEM_H */