bmem.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2013 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "base.h"
  19. #include "bmem.h"
  20. #include "platform.h"
  21. #include "threading.h"
  22. /*
  23. * NOTE: totally jacked the mem alignment trick from ffmpeg, credit to them:
  24. * http://www.ffmpeg.org/
  25. */
  26. #define ALIGNMENT 32
  27. /* TODO: use memalign for non-windows systems */
  28. #if defined(_WIN32)
  29. #define ALIGNED_MALLOC 1
  30. #else
  31. #define ALIGNMENT_HACK 1
  32. #endif
  33. static void *a_malloc(size_t size)
  34. {
  35. #ifdef ALIGNED_MALLOC
  36. return _aligned_malloc(size, ALIGNMENT);
  37. #elif ALIGNMENT_HACK
  38. void *ptr = NULL;
  39. long diff;
  40. ptr = malloc(size + ALIGNMENT);
  41. if (ptr) {
  42. diff = ((~(long)ptr) & (ALIGNMENT - 1)) + 1;
  43. ptr = (char *)ptr + diff;
  44. ((char *)ptr)[-1] = (char)diff;
  45. }
  46. return ptr;
  47. #else
  48. return malloc(size);
  49. #endif
  50. }
  51. static void *a_realloc(void *ptr, size_t size)
  52. {
  53. #ifdef ALIGNED_MALLOC
  54. return _aligned_realloc(ptr, size, ALIGNMENT);
  55. #elif ALIGNMENT_HACK
  56. long diff;
  57. if (!ptr)
  58. return a_malloc(size);
  59. diff = ((char *)ptr)[-1];
  60. ptr = realloc((char *)ptr - diff, size + diff);
  61. if (ptr)
  62. ptr = (char *)ptr + diff;
  63. return ptr;
  64. #else
  65. return realloc(ptr, size);
  66. #endif
  67. }
  68. static void a_free(void *ptr)
  69. {
  70. #ifdef ALIGNED_MALLOC
  71. _aligned_free(ptr);
  72. #elif ALIGNMENT_HACK
  73. if (ptr)
  74. free((char *)ptr - ((char *)ptr)[-1]);
  75. #else
  76. free(ptr);
  77. #endif
  78. }
  79. static long num_allocs = 0;
  80. void *bmalloc(size_t size)
  81. {
  82. if (!size) {
  83. blog(LOG_ERROR,
  84. "bmalloc: Allocating 0 bytes is broken behavior, please "
  85. "fix your code! This will crash in future versions of "
  86. "OBS.");
  87. size = 1;
  88. }
  89. void *ptr = a_malloc(size);
  90. if (!ptr) {
  91. os_breakpoint();
  92. bcrash("Out of memory while trying to allocate %lu bytes",
  93. (unsigned long)size);
  94. }
  95. os_atomic_inc_long(&num_allocs);
  96. return ptr;
  97. }
  98. void *brealloc(void *ptr, size_t size)
  99. {
  100. if (!ptr)
  101. os_atomic_inc_long(&num_allocs);
  102. if (!size) {
  103. blog(LOG_ERROR,
  104. "brealloc: Allocating 0 bytes is broken behavior, please "
  105. "fix your code! This will crash in future versions of "
  106. "OBS.");
  107. size = 1;
  108. }
  109. ptr = a_realloc(ptr, size);
  110. if (!ptr) {
  111. os_breakpoint();
  112. bcrash("Out of memory while trying to allocate %lu bytes",
  113. (unsigned long)size);
  114. }
  115. return ptr;
  116. }
  117. void bfree(void *ptr)
  118. {
  119. if (ptr) {
  120. os_atomic_dec_long(&num_allocs);
  121. a_free(ptr);
  122. }
  123. }
  124. long bnum_allocs(void)
  125. {
  126. return num_allocs;
  127. }
  128. int base_get_alignment(void)
  129. {
  130. return ALIGNMENT;
  131. }
  132. void *bmemdup(const void *ptr, size_t size)
  133. {
  134. void *out = bmalloc(size);
  135. if (size)
  136. memcpy(out, ptr, size);
  137. return out;
  138. }
  139. OBS_DEPRECATED void base_set_allocator(struct base_allocator *defs)
  140. {
  141. UNUSED_PARAMETER(defs);
  142. }