mem.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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(__INTEL_COMPILER) && __INTEL_COMPILER < 1110 || 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 av_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 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. /**
  59. * Allocate a block of size bytes with alignment suitable for all
  60. * memory accesses (including vectors if available on the CPU).
  61. * @param size Size in bytes for the memory block to be allocated.
  62. * @return Pointer to the allocated block, NULL if the block cannot
  63. * be allocated.
  64. * @see av_mallocz()
  65. */
  66. void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);
  67. /**
  68. * Allocate or reallocate a block of memory.
  69. * If ptr is NULL and size > 0, allocate a new block. If
  70. * size is zero, free the memory block pointed to by ptr.
  71. * @param size Size in bytes for the memory block to be allocated or
  72. * reallocated.
  73. * @param ptr Pointer to a memory block already allocated with
  74. * av_malloc(z)() or av_realloc() or NULL.
  75. * @return Pointer to a newly reallocated block or NULL if the block
  76. * cannot be reallocated or the function is used to free the memory block.
  77. * @see av_fast_realloc()
  78. */
  79. void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
  80. /**
  81. * Free a memory block which has been allocated with av_malloc(z)() or
  82. * av_realloc().
  83. * @param ptr Pointer to the memory block which should be freed.
  84. * @note ptr = NULL is explicitly allowed.
  85. * @note It is recommended that you use av_freep() instead.
  86. * @see av_freep()
  87. */
  88. void av_free(void *ptr);
  89. /**
  90. * Allocate a block of size bytes with alignment suitable for all
  91. * memory accesses (including vectors if available on the CPU) and
  92. * zero all the bytes of the block.
  93. * @param size Size in bytes for the memory block to be allocated.
  94. * @return Pointer to the allocated block, NULL if it cannot be allocated.
  95. * @see av_malloc()
  96. */
  97. void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
  98. /**
  99. * Duplicate the string s.
  100. * @param s string to be duplicated
  101. * @return Pointer to a newly allocated string containing a
  102. * copy of s or NULL if the string cannot be allocated.
  103. */
  104. char *av_strdup(const char *s) av_malloc_attrib;
  105. /**
  106. * Free a memory block which has been allocated with av_malloc(z)() or
  107. * av_realloc() and set the pointer pointing to it to NULL.
  108. * @param ptr Pointer to the pointer to the memory block which should
  109. * be freed.
  110. * @see av_free()
  111. */
  112. void av_freep(void *ptr);
  113. /**
  114. * Add an element to a dynamic array.
  115. *
  116. * @param tab_ptr Pointer to the array.
  117. * @param nb_ptr Pointer to the number of elements in the array.
  118. * @param elem Element to be added.
  119. */
  120. void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem);
  121. #endif /* AVUTIL_MEM_H */