bmem.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 struct base_allocator alloc = {a_malloc, a_realloc, a_free};
  80. static long num_allocs = 0;
  81. void base_set_allocator(struct base_allocator *defs)
  82. {
  83. memcpy(&alloc, defs, sizeof(struct base_allocator));
  84. }
  85. void *bmalloc(size_t size)
  86. {
  87. void *ptr = alloc.malloc(size);
  88. if (!ptr && !size)
  89. ptr = alloc.malloc(1);
  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. ptr = alloc.realloc(ptr, size);
  103. if (!ptr && !size)
  104. ptr = alloc.realloc(ptr, 1);
  105. if (!ptr) {
  106. os_breakpoint();
  107. bcrash("Out of memory while trying to allocate %lu bytes",
  108. (unsigned long)size);
  109. }
  110. return ptr;
  111. }
  112. void bfree(void *ptr)
  113. {
  114. if (ptr) {
  115. os_atomic_dec_long(&num_allocs);
  116. alloc.free(ptr);
  117. }
  118. }
  119. long bnum_allocs(void)
  120. {
  121. return num_allocs;
  122. }
  123. int base_get_alignment(void)
  124. {
  125. return ALIGNMENT;
  126. }
  127. void *bmemdup(const void *ptr, size_t size)
  128. {
  129. void *out = bmalloc(size);
  130. if (size)
  131. memcpy(out, ptr, size);
  132. return out;
  133. }