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.

172 lines
4.4KB

  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 dependant 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. /**
  37. * Memory allocation of size byte with alignment suitable for all
  38. * memory accesses (including vectors if available on the
  39. * CPU). av_malloc(0) must return a non NULL pointer.
  40. */
  41. void *av_malloc(unsigned int size)
  42. {
  43. void *ptr;
  44. #ifdef CONFIG_MEMALIGN_HACK
  45. long diff;
  46. #endif
  47. /* let's disallow possible ambiguous cases */
  48. if(size > (INT_MAX-16) )
  49. return NULL;
  50. #ifdef CONFIG_MEMALIGN_HACK
  51. ptr = malloc(size+16);
  52. if(!ptr)
  53. return ptr;
  54. diff= ((-(long)ptr - 1)&15) + 1;
  55. ptr += diff;
  56. ((char*)ptr)[-1]= diff;
  57. #elif defined (HAVE_MEMALIGN)
  58. ptr = memalign(16,size);
  59. /* Why 64?
  60. Indeed, we should align it:
  61. on 4 for 386
  62. on 16 for 486
  63. on 32 for 586, PPro - k6-III
  64. on 64 for K7 (maybe for P3 too).
  65. Because L1 and L2 caches are aligned on those values.
  66. But I don't want to code such logic here!
  67. */
  68. /* Why 16?
  69. because some cpus need alignment, for example SSE2 on P4, & most RISC cpus
  70. it will just trigger an exception and the unaligned load will be done in the
  71. exception handler or it will just segfault (SSE2 on P4)
  72. Why not larger? because i didnt see a difference in benchmarks ...
  73. */
  74. /* benchmarks with p3
  75. memalign(64)+1 3071,3051,3032
  76. memalign(64)+2 3051,3032,3041
  77. memalign(64)+4 2911,2896,2915
  78. memalign(64)+8 2545,2554,2550
  79. memalign(64)+16 2543,2572,2563
  80. memalign(64)+32 2546,2545,2571
  81. memalign(64)+64 2570,2533,2558
  82. btw, malloc seems to do 8 byte alignment by default here
  83. */
  84. #else
  85. ptr = malloc(size);
  86. #endif
  87. return ptr;
  88. }
  89. /**
  90. * av_realloc semantics (same as glibc): if ptr is NULL and size > 0,
  91. * identical to malloc(size). If size is zero, it is identical to
  92. * free(ptr) and NULL is returned.
  93. */
  94. void *av_realloc(void *ptr, unsigned int size)
  95. {
  96. #ifdef CONFIG_MEMALIGN_HACK
  97. int diff;
  98. #endif
  99. /* let's disallow possible ambiguous cases */
  100. if(size > (INT_MAX-16) )
  101. return NULL;
  102. #ifdef CONFIG_MEMALIGN_HACK
  103. //FIXME this isn't aligned correctly, though it probably isn't needed
  104. if(!ptr) return av_malloc(size);
  105. diff= ((char*)ptr)[-1];
  106. return realloc(ptr - diff, size + diff) + diff;
  107. #else
  108. return realloc(ptr, size);
  109. #endif
  110. }
  111. /**
  112. * Free memory which has been allocated with av_malloc(z)() or av_realloc().
  113. * NOTE: ptr = NULL is explicetly allowed
  114. * Note2: it is recommended that you use av_freep() instead
  115. */
  116. void av_free(void *ptr)
  117. {
  118. /* XXX: this test should not be needed on most libcs */
  119. if (ptr)
  120. #ifdef CONFIG_MEMALIGN_HACK
  121. free(ptr - ((char*)ptr)[-1]);
  122. #else
  123. free(ptr);
  124. #endif
  125. }
  126. /**
  127. * Frees memory and sets the pointer to NULL.
  128. * @param arg pointer to the pointer which should be freed
  129. */
  130. void av_freep(void *arg)
  131. {
  132. void **ptr= (void**)arg;
  133. av_free(*ptr);
  134. *ptr = NULL;
  135. }
  136. void *av_mallocz(unsigned int size)
  137. {
  138. void *ptr;
  139. ptr = av_malloc(size);
  140. if (ptr)
  141. memset(ptr, 0, size);
  142. return ptr;
  143. }
  144. char *av_strdup(const char *s)
  145. {
  146. char *ptr;
  147. int len;
  148. len = strlen(s) + 1;
  149. ptr = av_malloc(len);
  150. if (ptr)
  151. memcpy(ptr, s, len);
  152. return ptr;
  153. }