mem.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "attributes.h"
  27. #include "error.h"
  28. #include "avutil.h"
  29. #if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1110 || defined(__SUNPRO_C)
  30. #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
  31. #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v
  32. #elif defined(__TI_COMPILER_VERSION__)
  33. #define DECLARE_ALIGNED(n,t,v) \
  34. AV_PRAGMA(DATA_ALIGN(v,n)) \
  35. t __attribute__((aligned(n))) v
  36. #define DECLARE_ASM_CONST(n,t,v) \
  37. AV_PRAGMA(DATA_ALIGN(v,n)) \
  38. static const t __attribute__((aligned(n))) v
  39. #elif defined(__GNUC__)
  40. #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
  41. #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v
  42. #elif defined(_MSC_VER)
  43. #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
  44. #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v
  45. #else
  46. #define DECLARE_ALIGNED(n,t,v) t v
  47. #define DECLARE_ASM_CONST(n,t,v) static const t v
  48. #endif
  49. #if AV_GCC_VERSION_AT_LEAST(3,1)
  50. #define av_malloc_attrib __attribute__((__malloc__))
  51. #else
  52. #define av_malloc_attrib
  53. #endif
  54. #if AV_GCC_VERSION_AT_LEAST(4,3)
  55. #define av_alloc_size(n) __attribute__((alloc_size(n)))
  56. #else
  57. #define av_alloc_size(n)
  58. #endif
  59. #if LIBAVUTIL_VERSION_MAJOR < 51
  60. # define FF_INTERNAL_MEM_TYPE unsigned int
  61. # define FF_INTERNAL_MEM_TYPE_MAX_VALUE UINT_MAX
  62. #else
  63. # define FF_INTERNAL_MEM_TYPE size_t
  64. # define FF_INTERNAL_MEM_TYPE_MAX_VALUE SIZE_MAX
  65. #endif
  66. /**
  67. * Allocate a block of size bytes with alignment suitable for all
  68. * memory accesses (including vectors if available on the CPU).
  69. * @param size Size in bytes for the memory block to be allocated.
  70. * @return Pointer to the allocated block, NULL if the block cannot
  71. * be allocated.
  72. * @see av_mallocz()
  73. */
  74. void *av_malloc(FF_INTERNAL_MEM_TYPE size) av_malloc_attrib av_alloc_size(1);
  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_malloc(z)() or av_realloc() or NULL.
  81. * @param size Size in bytes for 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. * @see av_fast_realloc()
  86. */
  87. void *av_realloc(void *ptr, FF_INTERNAL_MEM_TYPE size) av_alloc_size(2);
  88. /**
  89. * Allocate or reallocate a block of memory.
  90. * This function does the same thing as av_realloc, except:
  91. * - It takes two arguments and checks the result of the multiplication for
  92. * integer overflow.
  93. * - It frees the input block in case of failure, thus avoiding the memory
  94. * leak with the classic "buf = realloc(buf); if (!buf) return -1;".
  95. */
  96. void *av_realloc_f(void *ptr, size_t nelem, size_t elsize);
  97. /**
  98. * Free a memory block which has been allocated with av_malloc(z)() or
  99. * av_realloc().
  100. * @param ptr Pointer to the memory block which should be freed.
  101. * @note ptr = NULL is explicitly allowed.
  102. * @note It is recommended that you use av_freep() instead.
  103. * @see av_freep()
  104. */
  105. void av_free(void *ptr);
  106. /**
  107. * Allocate a block of size bytes with alignment suitable for all
  108. * memory accesses (including vectors if available on the CPU) and
  109. * zero all the bytes of the block.
  110. * @param size Size in bytes for the memory block to be allocated.
  111. * @return Pointer to the allocated block, NULL if it cannot be allocated.
  112. * @see av_malloc()
  113. */
  114. void *av_mallocz(FF_INTERNAL_MEM_TYPE size) av_malloc_attrib av_alloc_size(1);
  115. /**
  116. * Allocate a block of nmemb * size bytes with alignment suitable for all
  117. * memory accesses (including vectors if available on the CPU) and
  118. * zero all the bytes of the block.
  119. * The allocation will fail if nmemb * size is greater than or equal
  120. * to INT_MAX.
  121. * @param nmemb
  122. * @param size
  123. * @return Pointer to the allocated block, NULL if it cannot be allocated.
  124. */
  125. void *av_calloc(size_t nmemb, size_t size) av_malloc_attrib;
  126. /**
  127. * Duplicate the string s.
  128. * @param s string to be duplicated
  129. * @return Pointer to a newly allocated string containing a
  130. * copy of s or NULL if the string cannot be allocated.
  131. */
  132. char *av_strdup(const char *s) av_malloc_attrib;
  133. /**
  134. * Free a memory block which has been allocated with av_malloc(z)() or
  135. * av_realloc() and set the pointer pointing to it to NULL.
  136. * @param ptr Pointer to the pointer to the memory block which should
  137. * be freed.
  138. * @see av_free()
  139. */
  140. void av_freep(void *ptr);
  141. /**
  142. * Add an element to a dynamic array.
  143. *
  144. * @param tab_ptr Pointer to the array.
  145. * @param nb_ptr Pointer to the number of elements in the array.
  146. * @param elem Element to be added.
  147. */
  148. void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem);
  149. /**
  150. * Multiply two size_t values checking for overflow.
  151. * @return 0 if success, AVERROR(EINVAL) if overflow.
  152. */
  153. static inline int av_size_mult(size_t a, size_t b, size_t *r)
  154. {
  155. size_t t = a * b;
  156. /* Hack inspired from glibc: only try the division if nelem and elsize
  157. * are both greater than sqrt(SIZE_MAX). */
  158. if ((a | b) >= ((size_t)1 << (sizeof(size_t) * 4)) && a && t / a != b)
  159. return AVERROR(EINVAL);
  160. *r = t;
  161. return 0;
  162. }
  163. #endif /* AVUTIL_MEM_H */