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.

88 lines
2.7KB

  1. /*
  2. * Copyright (c) 2002 Fabrice Bellard
  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. #ifndef AVUTIL_MEM_INTERNAL_H
  21. #define AVUTIL_MEM_INTERNAL_H
  22. #include "config.h"
  23. #include <stdint.h>
  24. #include "avassert.h"
  25. #include "mem.h"
  26. // Some broken preprocessors need a second expansion
  27. // to be forced to tokenize __VA_ARGS__
  28. #define E1(x) x
  29. #define LOCAL_ALIGNED_A(a, t, v, s, o, ...) \
  30. uint8_t la_##v[sizeof(t s o) + (a)]; \
  31. t (*v) o = (void *)FFALIGN((uintptr_t)la_##v, a)
  32. #define LOCAL_ALIGNED_D(a, t, v, s, o, ...) \
  33. DECLARE_ALIGNED(a, t, la_##v) s o; \
  34. t (*v) o = la_##v
  35. #define LOCAL_ALIGNED(a, t, v, ...) LOCAL_ALIGNED_##a(t, v, __VA_ARGS__)
  36. #if HAVE_LOCAL_ALIGNED
  37. # define LOCAL_ALIGNED_4(t, v, ...) E1(LOCAL_ALIGNED_D(4, t, v, __VA_ARGS__,,))
  38. #else
  39. # define LOCAL_ALIGNED_4(t, v, ...) E1(LOCAL_ALIGNED_A(4, t, v, __VA_ARGS__,,))
  40. #endif
  41. #if HAVE_LOCAL_ALIGNED
  42. # define LOCAL_ALIGNED_8(t, v, ...) E1(LOCAL_ALIGNED_D(8, t, v, __VA_ARGS__,,))
  43. #else
  44. # define LOCAL_ALIGNED_8(t, v, ...) E1(LOCAL_ALIGNED_A(8, t, v, __VA_ARGS__,,))
  45. #endif
  46. #if HAVE_LOCAL_ALIGNED
  47. # define LOCAL_ALIGNED_16(t, v, ...) E1(LOCAL_ALIGNED_D(16, t, v, __VA_ARGS__,,))
  48. #else
  49. # define LOCAL_ALIGNED_16(t, v, ...) E1(LOCAL_ALIGNED_A(16, t, v, __VA_ARGS__,,))
  50. #endif
  51. #if HAVE_LOCAL_ALIGNED
  52. # define LOCAL_ALIGNED_32(t, v, ...) E1(LOCAL_ALIGNED_D(32, t, v, __VA_ARGS__,,))
  53. #else
  54. # define LOCAL_ALIGNED_32(t, v, ...) E1(LOCAL_ALIGNED_A(32, t, v, __VA_ARGS__,,))
  55. #endif
  56. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  57. {
  58. void *val;
  59. memcpy(&val, ptr, sizeof(val));
  60. if (min_size <= *size) {
  61. av_assert0(val || !min_size);
  62. return 0;
  63. }
  64. min_size = FFMAX(min_size + min_size / 16 + 32, min_size);
  65. av_freep(ptr);
  66. val = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  67. memcpy(ptr, &val, sizeof(val));
  68. if (!val)
  69. min_size = 0;
  70. *size = min_size;
  71. return 1;
  72. }
  73. #endif /* AVUTIL_MEM_INTERNAL_H */