1
0

bmem.c 3.2 KB

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