bmem.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "threading.h"
  21. /*
  22. * NOTE: totally jacked the mem alignment trick from ffmpeg, credit to them:
  23. * http://www.ffmpeg.org/
  24. */
  25. #define ALIGNMENT 32
  26. /* TODO: use memalign for non-windows systems */
  27. #if defined(_WIN32)
  28. #define ALIGNED_MALLOC 1
  29. #else
  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 long 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. os_atomic_inc_long(&num_allocs);
  91. return ptr;
  92. }
  93. void *brealloc(void *ptr, size_t size)
  94. {
  95. if (!ptr)
  96. os_atomic_inc_long(&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. os_atomic_dec_long(&num_allocs);
  109. alloc.free(ptr);
  110. }
  111. long bnum_allocs(void)
  112. {
  113. return num_allocs;
  114. }
  115. int base_get_alignment(void)
  116. {
  117. return ALIGNMENT;
  118. }
  119. void *bmemdup(const void *ptr, size_t size)
  120. {
  121. void *out = bmalloc(size);
  122. if (size)
  123. memcpy(out, ptr, size);
  124. return out;
  125. }