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.

123 lines
4.2KB

  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 mem.h
  22. * Memory handling functions.
  23. */
  24. #ifndef AVUTIL_MEM_H
  25. #define AVUTIL_MEM_H
  26. #include "config.h"
  27. #include "common.h"
  28. #if defined(__ICC) || defined(__SUNPRO_C)
  29. #define DECLARE_ALIGNED(n,t,v) t v __attribute__ ((aligned (n)))
  30. #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v
  31. #elif defined(__GNUC__)
  32. #define DECLARE_ALIGNED(n,t,v) t v __attribute__ ((aligned (n)))
  33. #define DECLARE_ASM_CONST(n,t,v) static const t v attribute_used __attribute__ ((aligned (n)))
  34. #elif defined(_MSC_VER)
  35. #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
  36. #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v
  37. #elif HAVE_INLINE_ASM
  38. #error The asm code needs alignment, but we do not know how to do it for this compiler.
  39. #else
  40. #define DECLARE_ALIGNED(n,t,v) t v
  41. #define DECLARE_ASM_CONST(n,t,v) static const t v
  42. #endif
  43. #if AV_GCC_VERSION_AT_LEAST(3,1)
  44. #define av_malloc_attrib __attribute__((__malloc__))
  45. #else
  46. #define av_malloc_attrib
  47. #endif
  48. #if (!defined(__ICC) || __ICC > 1100) && AV_GCC_VERSION_AT_LEAST(4,3)
  49. #define av_alloc_size(n) __attribute__((alloc_size(n)))
  50. #else
  51. #define av_alloc_size(n)
  52. #endif
  53. /**
  54. * Allocate a block of \p size bytes with alignment suitable for all
  55. * memory accesses (including vectors if available on the CPU).
  56. * @param size Size in bytes for the memory block to be allocated.
  57. * @return Pointer to the allocated block, NULL if it cannot allocate
  58. * it.
  59. * @see av_mallocz()
  60. */
  61. void *av_malloc(unsigned int size) av_malloc_attrib av_alloc_size(1);
  62. /**
  63. * Allocate or reallocate a block of memory.
  64. * If \p ptr is NULL and \p size > 0, allocate a new block. If \p
  65. * size is zero, free the memory block pointed by \p ptr.
  66. * @param size Size in bytes for the memory block to be allocated or
  67. * reallocated.
  68. * @param ptr Pointer to a memory block already allocated with
  69. * av_malloc(z)() or av_realloc() or NULL.
  70. * @return Pointer to a newly reallocated block or NULL if it cannot
  71. * reallocate or the function is used to free the memory block.
  72. * @see av_fast_realloc()
  73. */
  74. void *av_realloc(void *ptr, unsigned int size) av_alloc_size(2);
  75. /**
  76. * Free a memory block which has been allocated with av_malloc(z)() or
  77. * av_realloc().
  78. * @param ptr Pointer to the memory block which should be freed.
  79. * @note ptr = NULL is explicitly allowed.
  80. * @note It is recommended that you use av_freep() instead.
  81. * @see av_freep()
  82. */
  83. void av_free(void *ptr);
  84. /**
  85. * Allocate a block of \p size bytes with alignment suitable for all
  86. * memory accesses (including vectors if available on the CPU) and
  87. * set to zeroes all the bytes of the block.
  88. * @param size Size in bytes for the memory block to be allocated.
  89. * @return Pointer to the allocated block, NULL if it cannot allocate
  90. * it.
  91. * @see av_malloc()
  92. */
  93. void *av_mallocz(unsigned int size) av_malloc_attrib av_alloc_size(1);
  94. /**
  95. * Duplicate the string \p s.
  96. * @param s String to be duplicated.
  97. * @return Pointer to a newly allocated string containing a
  98. * copy of \p s or NULL if it cannot be allocated.
  99. */
  100. char *av_strdup(const char *s) av_malloc_attrib;
  101. /**
  102. * Free a memory block which has been allocated with av_malloc(z)() or
  103. * av_realloc() and set to NULL the pointer to it.
  104. * @param ptr Pointer to the pointer to the memory block which should
  105. * be freed.
  106. * @see av_free()
  107. */
  108. void av_freep(void *ptr);
  109. #endif /* AVUTIL_MEM_H */