mem.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * default memory allocator for libavutil
  3. * Copyright (c) 2002 Fabrice Bellard.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file mem.c
  23. * default memory allocator for libavutil.
  24. */
  25. #include "common.h"
  26. /* here we can use OS dependent allocation functions */
  27. #undef malloc
  28. #undef free
  29. #undef realloc
  30. #ifdef HAVE_MALLOC_H
  31. #include <malloc.h>
  32. #endif
  33. /* you can redefine av_malloc and av_free in your project to use your
  34. memory allocator. You do not need to suppress this file because the
  35. linker will do it automatically */
  36. void *av_malloc(unsigned int size)
  37. {
  38. void *ptr;
  39. #ifdef CONFIG_MEMALIGN_HACK
  40. long diff;
  41. #endif
  42. /* let's disallow possible ambiguous cases */
  43. if(size > (INT_MAX-16) )
  44. return NULL;
  45. #ifdef CONFIG_MEMALIGN_HACK
  46. ptr = malloc(size+16);
  47. if(!ptr)
  48. return ptr;
  49. diff= ((-(long)ptr - 1)&15) + 1;
  50. ptr = (char*)ptr + diff;
  51. ((char*)ptr)[-1]= diff;
  52. #elif defined (HAVE_MEMALIGN)
  53. ptr = memalign(16,size);
  54. /* Why 64?
  55. Indeed, we should align it:
  56. on 4 for 386
  57. on 16 for 486
  58. on 32 for 586, PPro - k6-III
  59. on 64 for K7 (maybe for P3 too).
  60. Because L1 and L2 caches are aligned on those values.
  61. But I don't want to code such logic here!
  62. */
  63. /* Why 16?
  64. Because some CPUs need alignment, for example SSE2 on P4, & most RISC CPUs
  65. it will just trigger an exception and the unaligned load will be done in the
  66. exception handler or it will just segfault (SSE2 on P4)
  67. Why not larger? Because I did not see a difference in benchmarks ...
  68. */
  69. /* benchmarks with p3
  70. memalign(64)+1 3071,3051,3032
  71. memalign(64)+2 3051,3032,3041
  72. memalign(64)+4 2911,2896,2915
  73. memalign(64)+8 2545,2554,2550
  74. memalign(64)+16 2543,2572,2563
  75. memalign(64)+32 2546,2545,2571
  76. memalign(64)+64 2570,2533,2558
  77. btw, malloc seems to do 8 byte alignment by default here
  78. */
  79. #else
  80. ptr = malloc(size);
  81. #endif
  82. return ptr;
  83. }
  84. void *av_realloc(void *ptr, unsigned int size)
  85. {
  86. #ifdef CONFIG_MEMALIGN_HACK
  87. int diff;
  88. #endif
  89. /* let's disallow possible ambiguous cases */
  90. if(size > (INT_MAX-16) )
  91. return NULL;
  92. #ifdef CONFIG_MEMALIGN_HACK
  93. //FIXME this isn't aligned correctly, though it probably isn't needed
  94. if(!ptr) return av_malloc(size);
  95. diff= ((char*)ptr)[-1];
  96. return (char*)realloc((char*)ptr - diff, size + diff) + diff;
  97. #else
  98. return realloc(ptr, size);
  99. #endif
  100. }
  101. void av_free(void *ptr)
  102. {
  103. /* XXX: this test should not be needed on most libcs */
  104. if (ptr)
  105. #ifdef CONFIG_MEMALIGN_HACK
  106. free((char*)ptr - ((char*)ptr)[-1]);
  107. #else
  108. free(ptr);
  109. #endif
  110. }
  111. void av_freep(void *arg)
  112. {
  113. void **ptr= (void**)arg;
  114. av_free(*ptr);
  115. *ptr = NULL;
  116. }
  117. void *av_mallocz(unsigned int size)
  118. {
  119. void *ptr = av_malloc(size);
  120. if (ptr)
  121. memset(ptr, 0, size);
  122. return ptr;
  123. }
  124. char *av_strdup(const char *s)
  125. {
  126. char *ptr;
  127. int len = strlen(s) + 1;
  128. ptr = av_malloc(len);
  129. if (ptr)
  130. memcpy(ptr, s, len);
  131. return ptr;
  132. }