gcsx_mem.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* GCSx
  2. ** MEM.H
  3. **
  4. ** Memory functions that perform full error reporting and garbage collection
  5. */
  6. /*****************************************************************************
  7. ** Copyright (C) 2003-2006 Janson
  8. **
  9. ** This program is free software; you can redistribute it and/or modify
  10. ** it under the terms of the GNU General Public License as published by
  11. ** the Free Software Foundation; either version 2 of the License, or
  12. ** (at your option) any later version.
  13. **
  14. ** This program is distributed in the hope that it will be useful,
  15. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU General Public License
  20. ** along with this program; if not, write to the Free Software
  21. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  22. *****************************************************************************/
  23. #ifndef __GCSx_MEM_H_
  24. #define __GCSx_MEM_H_
  25. #ifndef NDEBUG
  26. #define MEMDEBUG
  27. #endif
  28. // Handles "graceful" exit upon crash- does NOT return
  29. // MemoryError indicates whether this is a memory error (can't use new)
  30. void fatalCrash(int memoryError, const char* title, ...);
  31. #ifndef MEMDEBUG
  32. // Our memory management with exceptions and garbage collection and debugging
  33. // The regular versions "crash" if memory runs out
  34. // The nothrow versions simply return NULL
  35. void* operator new(std::size_t size);
  36. void* operator new[](std::size_t size);
  37. void operator delete(void* ptr);
  38. void operator delete[](void* ptr);
  39. void* operator new(std::size_t size, const std::nothrow_t&);
  40. void* operator new[](std::size_t size, const std::nothrow_t&);
  41. void operator delete(void* ptr, const std::nothrow_t&);
  42. void operator delete[](void* ptr, const std::nothrow_t&);
  43. // Placeholder if nodebugging
  44. #define start_func
  45. #else
  46. // Debugging memory routines
  47. void* new_debug(std::size_t size, int array, int noThrow);
  48. void new_delete(void* ptr, int array);
  49. inline void* operator new(std::size_t size) { return new_debug(size, 0, 0); }
  50. inline void* operator new[](std::size_t size) { return new_debug(size, 1, 0); }
  51. inline void operator delete(void* ptr) { return new_delete(ptr, 0); }
  52. inline void operator delete[](void* ptr) { return new_delete(ptr, 1); }
  53. inline void* operator new(std::size_t size, const std::nothrow_t&) { return new_debug(size, 0, 1); }
  54. inline void* operator new[](std::size_t size, const std::nothrow_t&) { return new_debug(size, 1, 1); }
  55. inline void operator delete(void* ptr, const std::nothrow_t&) { return new_delete(ptr, 0); }
  56. inline void operator delete[](void* ptr, const std::nothrow_t&) { return new_delete(ptr, 1); }
  57. // Debugging memory AND trace
  58. class function_begin_end {
  59. public:
  60. static int level;
  61. const char* name;
  62. const char* previous;
  63. function_begin_end(const char* n);
  64. ~function_begin_end();
  65. };
  66. #ifdef __GNUC__
  67. #define start_func function_begin_end my_function_begin_end(__PRETTY_FUNCTION__);
  68. #else
  69. #define start_func function_begin_end my_function_begin_end(__func__);
  70. #endif
  71. // Set a point at which we track all further mem allocations
  72. void enableMemListTrack();
  73. // Call this at the same point during exit, to check for leaks
  74. void memLeakCheck();
  75. // Returns true if ANY memory errors/leaks have been caught yet
  76. // (doesn't count out-of-memory exceptions as one)
  77. int memDebugAnyErrors();
  78. // If debugging is enabled, we also have a way
  79. // to throw exceptions at a threshold
  80. void setMemThreshold(std::size_t thresh);
  81. #endif
  82. #endif