mem.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 libavutil/mem.h
  22. * memory handling functions
  23. */
  24. #ifndef AVUTIL_MEM_H
  25. #define AVUTIL_MEM_H
  26. #include "common.h"
  27. #if AV_GCC_VERSION_AT_LEAST(3,1)
  28. #define av_malloc_attrib __attribute__((__malloc__))
  29. #else
  30. #define av_malloc_attrib
  31. #endif
  32. #if (!defined(__ICC) || __ICC > 1100) && AV_GCC_VERSION_AT_LEAST(4,3)
  33. #define av_alloc_size(n) __attribute__((alloc_size(n)))
  34. #else
  35. #define av_alloc_size(n)
  36. #endif
  37. /**
  38. * Allocates a block of \p size bytes with alignment suitable for all
  39. * memory accesses (including vectors if available on the CPU).
  40. * @param size Size in bytes for the memory block to be allocated.
  41. * @return Pointer to the allocated block, NULL if the block cannot
  42. * be allocated.
  43. * @see av_mallocz()
  44. */
  45. void *av_malloc(unsigned int size) av_malloc_attrib av_alloc_size(1);
  46. /**
  47. * Allocates or reallocates a block of memory.
  48. * If \p ptr is NULL and \p size > 0, allocates a new block. If \p
  49. * size is zero, frees the memory block pointed to by \p ptr.
  50. * @param size Size in bytes for the memory block to be allocated or
  51. * reallocated.
  52. * @param ptr Pointer to a memory block already allocated with
  53. * av_malloc(z)() or av_realloc() or NULL.
  54. * @return Pointer to a newly reallocated block or NULL if the block
  55. * cannot be reallocated or the function is used to free the memory block.
  56. * @see av_fast_realloc()
  57. */
  58. void *av_realloc(void *ptr, unsigned int size) av_alloc_size(2);
  59. /**
  60. * Frees a memory block which has been allocated with av_malloc(z)() or
  61. * av_realloc().
  62. * @param ptr Pointer to the memory block which should be freed.
  63. * @note ptr = NULL is explicitly allowed.
  64. * @note It is recommended that you use av_freep() instead.
  65. * @see av_freep()
  66. */
  67. void av_free(void *ptr);
  68. /**
  69. * Allocates a block of \p size bytes with alignment suitable for all
  70. * memory accesses (including vectors if available on the CPU) and
  71. * zeroes all the bytes of the block.
  72. * @param size Size in bytes for the memory block to be allocated.
  73. * @return Pointer to the allocated block, NULL if it cannot be allocated.
  74. * @see av_malloc()
  75. */
  76. void *av_mallocz(unsigned int size) av_malloc_attrib av_alloc_size(1);
  77. /**
  78. * Duplicates the string \p s.
  79. * @param s string to be duplicated
  80. * @return Pointer to a newly allocated string containing a
  81. * copy of \p s or NULL if the string cannot be allocated.
  82. */
  83. char *av_strdup(const char *s) av_malloc_attrib;
  84. /**
  85. * Frees a memory block which has been allocated with av_malloc(z)() or
  86. * av_realloc() and set the pointer pointing to it to NULL.
  87. * @param ptr Pointer to the pointer to the memory block which should
  88. * be freed.
  89. * @see av_free()
  90. */
  91. void av_freep(void *ptr);
  92. #endif /* AVUTIL_MEM_H */