bmem.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /******************************************************************************
  2. Copyright (c) 2013 by Hugh Bailey <[email protected]>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not
  10. claim that you wrote the original software. If you use this software
  11. in a product, an acknowledgment in the product documentation would be
  12. appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source
  16. distribution.
  17. ******************************************************************************/
  18. #include <malloc.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "base.h"
  22. #include "bmem.h"
  23. /*
  24. * NOTE: totally jacked the mem alignment trick from ffmpeg, credit to them:
  25. * http://www.ffmpeg.org/
  26. */
  27. #define ALIGNMENT 16
  28. #if defined(_WIN32) && !defined(_WIN64)
  29. #define ALIGNED_MALLOC 1
  30. #elif !defined(__LP64__)
  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. diff = ((~(long)ptr) & (ALIGNMENT - 1)) + 1;
  42. ptr = (char *)ptr + diff;
  43. ((char *)ptr)[-1] = (char)diff;
  44. return ptr;
  45. #else
  46. return malloc(size);
  47. #endif
  48. }
  49. static void *a_realloc(void *ptr, size_t size)
  50. {
  51. #ifdef ALIGNED_MALLOC
  52. return _aligned_realloc(ptr, size, ALIGNMENT);
  53. #elif ALIGNMENT_HACK
  54. long diff;
  55. if (!ptr)
  56. return a_malloc(size);
  57. diff = ((char *)ptr)[-1];
  58. ptr = realloc((char*)ptr - diff, size + diff);
  59. if (ptr)
  60. ptr = (char *)ptr + diff;
  61. return ptr;
  62. #else
  63. return realloc(ptr, size);
  64. #endif
  65. }
  66. static void a_free(void *ptr)
  67. {
  68. #ifdef ALIGNED_MALLOC
  69. _aligned_free(ptr);
  70. #elif ALIGNMENT_HACK
  71. if (ptr)
  72. free((char *)ptr - ((char*)ptr)[-1]);
  73. #else
  74. free(ptr);
  75. #endif
  76. }
  77. static struct base_allocator alloc = {a_malloc, a_realloc, a_free};
  78. static size_t num_allocs = 0;
  79. void base_set_allocator(struct base_allocator *defs)
  80. {
  81. memcpy(&alloc, defs, sizeof(struct base_allocator));
  82. }
  83. void *bmalloc(size_t size)
  84. {
  85. void *ptr = alloc.malloc(size);
  86. if (!ptr && !size)
  87. ptr = alloc.malloc(1);
  88. if (!ptr)
  89. bcrash("Out of memory while trying to allocate %lu bytes",
  90. (unsigned long)size);
  91. num_allocs++;
  92. return ptr;
  93. }
  94. void *brealloc(void *ptr, size_t size)
  95. {
  96. if (!ptr)
  97. num_allocs++;
  98. ptr = alloc.realloc(ptr, size);
  99. if (!ptr && !size)
  100. ptr = alloc.realloc(ptr, 1);
  101. if (!ptr)
  102. bcrash("Out of memory while trying to allocate %lu bytes",
  103. (unsigned long)size);
  104. return ptr;
  105. }
  106. void bfree(void *ptr)
  107. {
  108. if (ptr)
  109. num_allocs--;
  110. alloc.free(ptr);
  111. }
  112. size_t bnum_allocs(void)
  113. {
  114. return num_allocs;
  115. }
  116. void *bmemdup(const void *ptr, size_t size)
  117. {
  118. void *out = bmalloc(size);
  119. if (size)
  120. memcpy(out, ptr, size);
  121. return out;
  122. }