bmem.c 2.9 KB

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