bmem.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /*
  21. * NOTE: totally jacked the mem alignment trick from ffmpeg, credit to them:
  22. * http://www.ffmpeg.org/
  23. */
  24. #define ALIGNMENT 32
  25. /* TODO: use memalign for non-windows systems */
  26. #if defined(_WIN32)
  27. #define ALIGNED_MALLOC 1
  28. #else
  29. #define ALIGNMENT_HACK 1
  30. #endif
  31. static void *a_malloc(size_t size)
  32. {
  33. #ifdef ALIGNED_MALLOC
  34. return _aligned_malloc(size, ALIGNMENT);
  35. #elif ALIGNMENT_HACK
  36. void *ptr = NULL;
  37. long diff;
  38. ptr = malloc(size + ALIGNMENT);
  39. diff = ((~(long)ptr) & (ALIGNMENT - 1)) + 1;
  40. ptr = (char *)ptr + diff;
  41. ((char *)ptr)[-1] = (char)diff;
  42. return ptr;
  43. #else
  44. return malloc(size);
  45. #endif
  46. }
  47. static void *a_realloc(void *ptr, size_t size)
  48. {
  49. #ifdef ALIGNED_MALLOC
  50. return _aligned_realloc(ptr, size, ALIGNMENT);
  51. #elif ALIGNMENT_HACK
  52. long diff;
  53. if (!ptr)
  54. return a_malloc(size);
  55. diff = ((char *)ptr)[-1];
  56. ptr = realloc((char*)ptr - diff, size + diff);
  57. if (ptr)
  58. ptr = (char *)ptr + diff;
  59. return ptr;
  60. #else
  61. return realloc(ptr, size);
  62. #endif
  63. }
  64. static void a_free(void *ptr)
  65. {
  66. #ifdef ALIGNED_MALLOC
  67. _aligned_free(ptr);
  68. #elif ALIGNMENT_HACK
  69. if (ptr)
  70. free((char *)ptr - ((char*)ptr)[-1]);
  71. #else
  72. free(ptr);
  73. #endif
  74. }
  75. static struct base_allocator alloc = {a_malloc, a_realloc, a_free};
  76. static uint64_t num_allocs = 0;
  77. void base_set_allocator(struct base_allocator *defs)
  78. {
  79. memcpy(&alloc, defs, sizeof(struct base_allocator));
  80. }
  81. void *bmalloc(size_t size)
  82. {
  83. void *ptr = alloc.malloc(size);
  84. if (!ptr && !size)
  85. ptr = alloc.malloc(1);
  86. if (!ptr)
  87. bcrash("Out of memory while trying to allocate %lu bytes",
  88. (unsigned long)size);
  89. num_allocs++;
  90. return ptr;
  91. }
  92. void *brealloc(void *ptr, size_t size)
  93. {
  94. if (!ptr)
  95. num_allocs++;
  96. ptr = alloc.realloc(ptr, size);
  97. if (!ptr && !size)
  98. ptr = alloc.realloc(ptr, 1);
  99. if (!ptr)
  100. bcrash("Out of memory while trying to allocate %lu bytes",
  101. (unsigned long)size);
  102. return ptr;
  103. }
  104. void bfree(void *ptr)
  105. {
  106. if (ptr)
  107. num_allocs--;
  108. alloc.free(ptr);
  109. }
  110. uint64_t bnum_allocs(void)
  111. {
  112. return num_allocs;
  113. }
  114. int base_get_alignment(void)
  115. {
  116. return ALIGNMENT;
  117. }
  118. void *bmemdup(const void *ptr, size_t size)
  119. {
  120. void *out = bmalloc(size);
  121. if (size)
  122. memcpy(out, ptr, size);
  123. return out;
  124. }