memory.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
  4. * All rights reserved. Email: [email protected] Web: www.q12.org *
  5. * *
  6. * This library is free software; you can redistribute it and/or *
  7. * modify it under the terms of EITHER: *
  8. * (1) The GNU Lesser General Public License as published by the Free *
  9. * Software Foundation; either version 2.1 of the License, or (at *
  10. * your option) any later version. The text of the GNU Lesser *
  11. * General Public License is included with this library in the *
  12. * file LICENSE.TXT. *
  13. * (2) The BSD-style license that is included with this library in *
  14. * the file LICENSE-BSD.TXT. *
  15. * *
  16. * This library is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  19. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  20. * *
  21. *************************************************************************/
  22. #include <ode/odeconfig.h>
  23. #include <ode/memory.h>
  24. #include <ode/error.h>
  25. #include "config.h"
  26. static dAllocFunction *allocfn = 0;
  27. static dReallocFunction *reallocfn = 0;
  28. static dFreeFunction *freefn = 0;
  29. #ifdef __MINGW32__
  30. /*
  31. this is a guard against AC_FUNC_MALLOC and AC_FUNC_REALLOC
  32. which break cross compilation, no issues in native MSYS.
  33. */
  34. #undef malloc
  35. #undef realloc
  36. #endif
  37. void dSetAllocHandler (dAllocFunction *fn)
  38. {
  39. allocfn = fn;
  40. }
  41. void dSetReallocHandler (dReallocFunction *fn)
  42. {
  43. reallocfn = fn;
  44. }
  45. void dSetFreeHandler (dFreeFunction *fn)
  46. {
  47. freefn = fn;
  48. }
  49. dAllocFunction *dGetAllocHandler()
  50. {
  51. return allocfn;
  52. }
  53. dReallocFunction *dGetReallocHandler()
  54. {
  55. return reallocfn;
  56. }
  57. dFreeFunction *dGetFreeHandler()
  58. {
  59. return freefn;
  60. }
  61. void * dAlloc (sizeint size)
  62. {
  63. if (allocfn) return allocfn (size); else return malloc (size);
  64. }
  65. void * dRealloc (void *ptr, sizeint oldsize, sizeint newsize)
  66. {
  67. if (reallocfn) return reallocfn (ptr,oldsize,newsize);
  68. else return realloc (ptr,newsize);
  69. }
  70. void dFree (void *ptr, sizeint size)
  71. {
  72. if (!ptr) return;
  73. if (freefn) freefn (ptr,size); else free (ptr);
  74. }