mem.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "avutil.h"
  28. #if defined(__ICC) && _ICC < 1200 || defined(__SUNPRO_C)
  29. #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
  30. #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v
  31. #elif defined(__TI_COMPILER_VERSION__)
  32. #define DECLARE_ALIGNED(n,t,v) \
  33. AV_PRAGMA(DATA_ALIGN(v,n)) \
  34. t __attribute__((aligned(n))) v
  35. #define DECLARE_ASM_CONST(n,t,v) \
  36. AV_PRAGMA(DATA_ALIGN(v,n)) \
  37. static const t __attribute__((aligned(n))) v
  38. #elif defined(__GNUC__)
  39. #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
  40. #define DECLARE_ASM_CONST(n,t,v) static const t attribute_used __attribute__ ((aligned (n))) v
  41. #elif defined(_MSC_VER)
  42. #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
  43. #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v
  44. #else
  45. #define DECLARE_ALIGNED(n,t,v) t v
  46. #define DECLARE_ASM_CONST(n,t,v) static const t v
  47. #endif
  48. #if AV_GCC_VERSION_AT_LEAST(3,1)
  49. #define av_malloc_attrib __attribute__((__malloc__))
  50. #else
  51. #define av_malloc_attrib
  52. #endif
  53. #if (!defined(__ICC) || __ICC > 1200) && AV_GCC_VERSION_AT_LEAST(4,3)
  54. #define av_alloc_size(n) __attribute__((alloc_size(n)))
  55. #else
  56. #define av_alloc_size(n)
  57. #endif
  58. #if LIBAVUTIL_VERSION_MAJOR < 51
  59. # define FF_INTERNAL_MEM_TYPE unsigned int
  60. # define FF_INTERNAL_MEM_TYPE_MAX_VALUE UINT_MAX
  61. #else
  62. # define FF_INTERNAL_MEM_TYPE size_t
  63. # define FF_INTERNAL_MEM_TYPE_MAX_VALUE SIZE_MAX
  64. #endif
  65. /**
  66. * Allocate a block of size bytes with alignment suitable for all
  67. * memory accesses (including vectors if available on the CPU).
  68. * @param size Size in bytes for the memory block to be allocated.
  69. * @return Pointer to the allocated block, NULL if the block cannot
  70. * be allocated.
  71. * @see av_mallocz()
  72. */
  73. void *av_malloc(FF_INTERNAL_MEM_TYPE size) av_malloc_attrib av_alloc_size(1);
  74. /**
  75. * Allocate or reallocate a block of memory.
  76. * If ptr is NULL and size > 0, allocate a new block. If
  77. * size is zero, free the memory block pointed to by ptr.
  78. * @param size Size in bytes for the memory block to be allocated or
  79. * reallocated.
  80. * @param ptr Pointer to a memory block already allocated with
  81. * av_malloc(z)() or av_realloc() or NULL.
  82. * @return Pointer to a newly reallocated block or NULL if the block
  83. * cannot be reallocated or the function is used to free the memory block.
  84. * @see av_fast_realloc()
  85. */
  86. void *av_realloc(void *ptr, FF_INTERNAL_MEM_TYPE size) av_alloc_size(2);
  87. /**
  88. * Free a memory block which has been allocated with av_malloc(z)() or
  89. * av_realloc().
  90. * @param ptr Pointer to the memory block which should be freed.
  91. * @note ptr = NULL is explicitly allowed.
  92. * @note It is recommended that you use av_freep() instead.
  93. * @see av_freep()
  94. */
  95. void av_free(void *ptr);
  96. /**
  97. * Allocate a block of size bytes with alignment suitable for all
  98. * memory accesses (including vectors if available on the CPU) and
  99. * zero all the bytes of the block.
  100. * @param size Size in bytes for the memory block to be allocated.
  101. * @return Pointer to the allocated block, NULL if it cannot be allocated.
  102. * @see av_malloc()
  103. */
  104. void *av_mallocz(FF_INTERNAL_MEM_TYPE size) av_malloc_attrib av_alloc_size(1);
  105. /**
  106. * Duplicate the string s.
  107. * @param s string to be duplicated
  108. * @return Pointer to a newly allocated string containing a
  109. * copy of s or NULL if the string cannot be allocated.
  110. */
  111. char *av_strdup(const char *s) av_malloc_attrib;
  112. /**
  113. * Free a memory block which has been allocated with av_malloc(z)() or
  114. * av_realloc() and set the pointer pointing to it to NULL.
  115. * @param ptr Pointer to the pointer to the memory block which should
  116. * be freed.
  117. * @see av_free()
  118. */
  119. void av_freep(void *ptr);
  120. #endif /* AVUTIL_MEM_H */