sqmem.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. see copyright notice in squirrel.h
  3. */
  4. #include "sqpcheader.h"
  5. #ifndef SQ_EXCLUDE_DEFAULT_MEMFUNCTIONS
  6. #ifdef SQ_DEBUG_MEMORY
  7. int _sq_total_malloc = 0;
  8. int _sq_total_realloc = 0;
  9. int _sq_total_free = 0;
  10. #endif
  11. void *sq_vm_malloc(SQUnsignedInteger size){
  12. #ifdef SQ_DEBUG_MEMORY
  13. ++_sq_total_malloc;
  14. #endif
  15. return malloc(size);
  16. }
  17. void *sq_vm_realloc(void *p, SQUnsignedInteger SQ_UNUSED_ARG(oldsize), SQUnsignedInteger size){
  18. #ifdef SQ_DEBUG_MEMORY
  19. ++_sq_total_realloc;
  20. #endif
  21. return realloc(p, size);
  22. }
  23. void sq_vm_free(void *p, SQUnsignedInteger SQ_UNUSED_ARG(size)){
  24. #ifdef SQ_DEBUG_MEMORY
  25. ++_sq_total_free;
  26. #endif
  27. free(p);
  28. }
  29. #endif
  30. #ifdef CUSTOM_DELETE_OPERATOR
  31. //made public to allow link without libstdc++
  32. void operator delete(void *p, unsigned long len)
  33. {
  34. printf("DELETE SHOULD NOT BE CALLED %p : %d\n", p, (int)len);
  35. assert(0);
  36. }
  37. void operator delete(void *p)
  38. {
  39. printf("DELETE SHOULD NOT BE CALLED %p\n", p);
  40. assert(0);
  41. }
  42. #endif
  43. #ifdef WITH_SQSTDCPP
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. // MSVC uses __cdecl calling convention for new/delete :-O
  47. #ifdef _MSC_VER
  48. # define NEWDEL_CALL __cdecl
  49. #else
  50. # define NEWDEL_CALL
  51. #endif
  52. extern "C" void __cxa_pure_virtual ()
  53. {
  54. puts("__cxa_pure_virtual called\n");
  55. abort ();
  56. }
  57. void * NEWDEL_CALL operator new (size_t size)
  58. {
  59. void *p = malloc (size);
  60. if(!p)
  61. {
  62. puts("not enough memory\n");
  63. abort ();
  64. }
  65. return p;
  66. }
  67. void * NEWDEL_CALL operator new [] (size_t size)
  68. {
  69. return ::operator new(size);
  70. }
  71. void NEWDEL_CALL operator delete (void *p)
  72. {
  73. if (p) free (p);
  74. }
  75. void NEWDEL_CALL operator delete [] (void *p)
  76. {
  77. if (p) free (p);
  78. }
  79. void NEWDEL_CALL operator delete (void *p, size_t)
  80. {
  81. if (p) free (p);
  82. }
  83. #endif // WITH_MYSTDCPP