gim_memory.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of GIMPACT Library.
  4. For the latest info, see http://gimpact.sourceforge.net/
  5. Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
  6. email: projectileman@yahoo.com
  7. This library is free software; you can redistribute it and/or
  8. modify it under the terms of EITHER:
  9. (1) The GNU Lesser General Public License as published by the Free
  10. Software Foundation; either version 2.1 of the License, or (at
  11. your option) any later version. The text of the GNU Lesser
  12. General Public License is included with this library in the
  13. file GIMPACT-LICENSE-LGPL.TXT.
  14. (2) The BSD-style license that is included with this library in
  15. the file GIMPACT-LICENSE-BSD.TXT.
  16. (3) The zlib/libpng license that is included with this library in
  17. the file GIMPACT-LICENSE-ZLIB.TXT.
  18. This library is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
  21. GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "gim_memory.h"
  25. #include "stdlib.h"
  26. #ifdef GIM_SIMD_MEMORY
  27. #include "LinearMath/btAlignedAllocator.h"
  28. #endif
  29. static gim_alloc_function *g_allocfn = 0;
  30. static gim_alloca_function *g_allocafn = 0;
  31. static gim_realloc_function *g_reallocfn = 0;
  32. static gim_free_function *g_freefn = 0;
  33. void gim_set_alloc_handler (gim_alloc_function *fn)
  34. {
  35. g_allocfn = fn;
  36. }
  37. void gim_set_alloca_handler (gim_alloca_function *fn)
  38. {
  39. g_allocafn = fn;
  40. }
  41. void gim_set_realloc_handler (gim_realloc_function *fn)
  42. {
  43. g_reallocfn = fn;
  44. }
  45. void gim_set_free_handler (gim_free_function *fn)
  46. {
  47. g_freefn = fn;
  48. }
  49. gim_alloc_function *gim_get_alloc_handler()
  50. {
  51. return g_allocfn;
  52. }
  53. gim_alloca_function *gim_get_alloca_handler()
  54. {
  55. return g_allocafn;
  56. }
  57. gim_realloc_function *gim_get_realloc_handler ()
  58. {
  59. return g_reallocfn;
  60. }
  61. gim_free_function *gim_get_free_handler ()
  62. {
  63. return g_freefn;
  64. }
  65. void * gim_alloc(size_t size)
  66. {
  67. void * ptr;
  68. if (g_allocfn)
  69. {
  70. ptr = g_allocfn(size);
  71. }
  72. else
  73. {
  74. #ifdef GIM_SIMD_MEMORY
  75. ptr = btAlignedAlloc(size,16);
  76. #else
  77. ptr = malloc(size);
  78. #endif
  79. }
  80. return ptr;
  81. }
  82. void * gim_alloca(size_t size)
  83. {
  84. if (g_allocafn) return g_allocafn(size); else return gim_alloc(size);
  85. }
  86. void * gim_realloc(void *ptr, size_t oldsize, size_t newsize)
  87. {
  88. void * newptr = gim_alloc(newsize);
  89. size_t copysize = oldsize<newsize?oldsize:newsize;
  90. gim_simd_memcpy(newptr,ptr,copysize);
  91. gim_free(ptr);
  92. return newptr;
  93. }
  94. void gim_free(void *ptr)
  95. {
  96. if (!ptr) return;
  97. if (g_freefn)
  98. {
  99. g_freefn(ptr);
  100. }
  101. else
  102. {
  103. #ifdef GIM_SIMD_MEMORY
  104. btAlignedFree(ptr);
  105. #else
  106. free(ptr);
  107. #endif
  108. }
  109. }