memory.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/memory.h
  3. // Purpose: Memory operations
  4. // Author: Arthur Seaton, Julian Smart
  5. // Modified by:
  6. // Created: 29/01/98
  7. // Copyright: (c) 1998 Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_MEMORY_H_
  11. #define _WX_MEMORY_H_
  12. #include "wx/defs.h"
  13. #include "wx/string.h"
  14. #include "wx/msgout.h"
  15. #if wxUSE_MEMORY_TRACING || wxUSE_DEBUG_CONTEXT
  16. #include <stddef.h>
  17. WXDLLIMPEXP_BASE void * wxDebugAlloc(size_t size, wxChar * fileName, int lineNum, bool isObject, bool isVect = false);
  18. WXDLLIMPEXP_BASE void wxDebugFree(void * buf, bool isVect = false);
  19. //**********************************************************************************
  20. /*
  21. The global operator new used for everything apart from getting
  22. dynamic storage within this function itself.
  23. */
  24. // We'll only do malloc and free for the moment: leave the interesting
  25. // stuff for the wxObject versions.
  26. #if wxUSE_GLOBAL_MEMORY_OPERATORS
  27. // Undefine temporarily (new is #defined in object.h) because we want to
  28. // declare some new operators.
  29. #ifdef new
  30. #undef new
  31. #endif
  32. #if defined(__SUNCC__)
  33. #define wxUSE_ARRAY_MEMORY_OPERATORS 0
  34. #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) )
  35. #define wxUSE_ARRAY_MEMORY_OPERATORS 1
  36. #elif defined (__SGI_CC_)
  37. // only supported by -n32 compilers
  38. #ifndef __EDG_ABI_COMPATIBILITY_VERSION
  39. #define wxUSE_ARRAY_MEMORY_OPERATORS 0
  40. #endif
  41. #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) )
  42. #define wxUSE_ARRAY_MEMORY_OPERATORS 1
  43. #else
  44. // ::operator new[] is a recent C++ feature, so assume it's not supported
  45. #define wxUSE_ARRAY_MEMORY_OPERATORS 0
  46. #endif
  47. // devik 2000-8-29: All new/delete ops are now inline because they can't
  48. // be marked as dllexport/dllimport. It then leads to weird bugs when
  49. // used on MSW as DLL
  50. #if defined(__WINDOWS__) && (defined(WXUSINGDLL) || defined(WXMAKINGDLL_BASE))
  51. inline void * operator new (size_t size, wxChar * fileName, int lineNum)
  52. {
  53. return wxDebugAlloc(size, fileName, lineNum, false, false);
  54. }
  55. inline void * operator new (size_t size)
  56. {
  57. return wxDebugAlloc(size, NULL, 0, false);
  58. }
  59. inline void operator delete (void * buf)
  60. {
  61. wxDebugFree(buf, false);
  62. }
  63. #if wxUSE_ARRAY_MEMORY_OPERATORS
  64. inline void * operator new[] (size_t size)
  65. {
  66. return wxDebugAlloc(size, NULL, 0, false, true);
  67. }
  68. inline void * operator new[] (size_t size, wxChar * fileName, int lineNum)
  69. {
  70. return wxDebugAlloc(size, fileName, lineNum, false, true);
  71. }
  72. inline void operator delete[] (void * buf)
  73. {
  74. wxDebugFree(buf, true);
  75. }
  76. #endif // wxUSE_ARRAY_MEMORY_OPERATORS
  77. #else
  78. void * operator new (size_t size, wxChar * fileName, int lineNum);
  79. void * operator new (size_t size);
  80. void operator delete (void * buf);
  81. #if wxUSE_ARRAY_MEMORY_OPERATORS
  82. void * operator new[] (size_t size);
  83. void * operator new[] (size_t size, wxChar * fileName, int lineNum);
  84. void operator delete[] (void * buf);
  85. #endif // wxUSE_ARRAY_MEMORY_OPERATORS
  86. #endif // defined(__WINDOWS__) && (defined(WXUSINGDLL) || defined(WXMAKINGDLL_BASE))
  87. // VC++ 6.0
  88. #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) )
  89. inline void operator delete(void* pData, wxChar* /* fileName */, int /* lineNum */)
  90. {
  91. wxDebugFree(pData, false);
  92. }
  93. inline void operator delete[](void* pData, wxChar* /* fileName */, int /* lineNum */)
  94. {
  95. wxDebugFree(pData, true);
  96. }
  97. #endif // __VISUALC__>=1200
  98. #endif // wxUSE_GLOBAL_MEMORY_OPERATORS
  99. //**********************************************************************************
  100. typedef unsigned int wxMarkerType;
  101. /*
  102. Define the struct which will be placed at the start of all dynamically
  103. allocated memory.
  104. */
  105. class WXDLLIMPEXP_BASE wxMemStruct {
  106. friend class WXDLLIMPEXP_FWD_BASE wxDebugContext; // access to the m_next pointer for list traversal.
  107. public:
  108. public:
  109. int AssertList ();
  110. size_t RequestSize () { return m_reqSize; }
  111. wxMarkerType Marker () { return m_firstMarker; }
  112. // When an object is deleted we set the id slot to a specific value.
  113. inline void SetDeleted ();
  114. inline int IsDeleted ();
  115. int Append ();
  116. int Unlink ();
  117. // Used to determine if the object is really a wxMemStruct.
  118. // Not a foolproof test by any means, but better than none I hope!
  119. int AssertIt ();
  120. // Do all validation on a node.
  121. int ValidateNode ();
  122. // Check the integrity of a node and of the list, node by node.
  123. int CheckBlock ();
  124. int CheckAllPrevious ();
  125. // Print a single node.
  126. void PrintNode ();
  127. // Called when the memory linking functions get an error.
  128. void ErrorMsg (const char *);
  129. void ErrorMsg ();
  130. inline void *GetActualData(void) const { return m_actualData; }
  131. void Dump(void);
  132. public:
  133. // Check for underwriting. There are 2 of these checks. This one
  134. // inside the struct and another right after the struct.
  135. wxMarkerType m_firstMarker;
  136. // File name and line number are from cpp.
  137. wxChar* m_fileName;
  138. int m_lineNum;
  139. // The amount of memory requested by the caller.
  140. size_t m_reqSize;
  141. // Used to try to verify that we really are dealing with an object
  142. // of the required class. Can be 1 of 2 values these indicating a valid
  143. // wxMemStruct object, or a deleted wxMemStruct object.
  144. wxMarkerType m_id;
  145. wxMemStruct * m_prev;
  146. wxMemStruct * m_next;
  147. void * m_actualData;
  148. bool m_isObject;
  149. };
  150. typedef void (wxMemStruct::*PmSFV) ();
  151. // Type of the app function that can be installed and called at wxWidgets shutdown
  152. // (after all other registered files with global destructors have been closed down).
  153. typedef void (*wxShutdownNotifyFunction)();
  154. /*
  155. Debugging class. This will only have a single instance, but it's
  156. a reasonable way to keep everything together and to make this
  157. available for change if needed by someone else.
  158. A lot of this stuff would be better off within the wxMemStruct class, but
  159. it's stuff which we need to access at times when there is no wxMemStruct
  160. object so we use this class instead. Think of it as a collection of
  161. globals which have to do with the wxMemStruct class.
  162. */
  163. class WXDLLIMPEXP_BASE wxDebugContext {
  164. protected:
  165. // Used to set alignment for markers.
  166. static size_t CalcAlignment ();
  167. // Returns the amount of padding needed after something of the given
  168. // size. This is so that when we cast pointers backwards and forwards
  169. // the pointer value will be valid for a wxMarkerType.
  170. static size_t GetPadding (const size_t size) ;
  171. // Traverse the list.
  172. static void TraverseList (PmSFV, wxMemStruct *from = NULL);
  173. static int debugLevel;
  174. static bool debugOn;
  175. static int m_balign; // byte alignment
  176. static int m_balignmask; // mask for performing byte alignment
  177. public:
  178. // Set a checkpoint to dump only the memory from
  179. // a given point
  180. static wxMemStruct *checkPoint;
  181. wxDebugContext(void);
  182. ~wxDebugContext(void);
  183. static int GetLevel(void) { return debugLevel; }
  184. static void SetLevel(int level) { debugLevel = level; }
  185. static bool GetDebugMode(void) { return debugOn; }
  186. static void SetDebugMode(bool flag) { debugOn = flag; }
  187. static void SetCheckpoint(bool all = false);
  188. static wxMemStruct *GetCheckpoint(void) { return checkPoint; }
  189. // Calculated from the request size and any padding needed
  190. // before the final marker.
  191. static size_t PaddedSize (const size_t reqSize);
  192. // Calc the total amount of space we need from the system
  193. // to satisfy a caller request. This includes all padding.
  194. static size_t TotSize (const size_t reqSize);
  195. // Return valid pointers to offsets within the allocated memory.
  196. static char * StructPos (const char * buf);
  197. static char * MidMarkerPos (const char * buf);
  198. static char * CallerMemPos (const char * buf);
  199. static char * EndMarkerPos (const char * buf, const size_t size);
  200. // Given a pointer to the start of the caller requested area
  201. // return a pointer to the start of the entire alloc\'d buffer.
  202. static char * StartPos (const char * caller);
  203. // Access to the list.
  204. static wxMemStruct * GetHead () { return m_head; }
  205. static wxMemStruct * GetTail () { return m_tail; }
  206. // Set the list sentinals.
  207. static wxMemStruct * SetHead (wxMemStruct * st) { return (m_head = st); }
  208. static wxMemStruct * SetTail (wxMemStruct * st) { return (m_tail = st); }
  209. // If this is set then every new operation checks the validity
  210. // of the all previous nodes in the list.
  211. static bool GetCheckPrevious () { return m_checkPrevious; }
  212. static void SetCheckPrevious (bool value) { m_checkPrevious = value; }
  213. // Checks all nodes, or all nodes if checkAll is true
  214. static int Check(bool checkAll = false);
  215. // Print out the list of wxMemStruct nodes.
  216. static bool PrintList(void);
  217. // Dump objects
  218. static bool Dump(void);
  219. // Print statistics
  220. static bool PrintStatistics(bool detailed = true);
  221. // Print out the classes in the application.
  222. static bool PrintClasses(void);
  223. // Count the number of non-wxDebugContext-related objects
  224. // that are outstanding
  225. static int CountObjectsLeft(bool sinceCheckpoint = false);
  226. // This function is used to output the dump
  227. static void OutputDumpLine(const wxChar *szFormat, ...);
  228. static void SetShutdownNotifyFunction(wxShutdownNotifyFunction shutdownFn);
  229. private:
  230. // Store these here to allow access to the list without
  231. // needing to have a wxMemStruct object.
  232. static wxMemStruct* m_head;
  233. static wxMemStruct* m_tail;
  234. // Set to false if we're not checking all previous nodes when
  235. // we do a new. Set to true when we are.
  236. static bool m_checkPrevious;
  237. // Holds a pointer to an optional application function to call at shutdown.
  238. static wxShutdownNotifyFunction sm_shutdownFn;
  239. // Have to access our shutdown hook
  240. friend class wxDebugContextDumpDelayCounter;
  241. };
  242. // Final cleanup (e.g. deleting the log object and doing memory leak checking)
  243. // will be delayed until all wxDebugContextDumpDelayCounter objects have been
  244. // destructed. Adding one wxDebugContextDumpDelayCounter per file will delay
  245. // memory leak checking until after destructing all global objects.
  246. class WXDLLIMPEXP_BASE wxDebugContextDumpDelayCounter
  247. {
  248. public:
  249. wxDebugContextDumpDelayCounter();
  250. ~wxDebugContextDumpDelayCounter();
  251. private:
  252. void DoDump();
  253. static int sm_count;
  254. };
  255. // make leak dump after all globals have been destructed
  256. static wxDebugContextDumpDelayCounter wxDebugContextDumpDelayCounter_File;
  257. #define WXDEBUG_DUMPDELAYCOUNTER \
  258. static wxDebugContextDumpDelayCounter wxDebugContextDumpDelayCounter_Extra;
  259. // Output a debug message, in a system dependent fashion.
  260. void WXDLLIMPEXP_BASE wxTrace(const wxChar *fmt ...) WX_ATTRIBUTE_PRINTF_1;
  261. void WXDLLIMPEXP_BASE wxTraceLevel(int level, const wxChar *fmt ...) WX_ATTRIBUTE_PRINTF_2;
  262. #define WXTRACE wxTrace
  263. #define WXTRACELEVEL wxTraceLevel
  264. #else // wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
  265. #define WXDEBUG_DUMPDELAYCOUNTER
  266. // Borland C++ Builder 6 seems to have troubles with inline functions (see bug
  267. // 819700)
  268. #if 0
  269. inline void wxTrace(const wxChar *WXUNUSED(fmt)) {}
  270. inline void wxTraceLevel(int WXUNUSED(level), const wxChar *WXUNUSED(fmt)) {}
  271. #else
  272. #define wxTrace(fmt)
  273. #define wxTraceLevel(l, fmt)
  274. #endif
  275. #define WXTRACE true ? (void)0 : wxTrace
  276. #define WXTRACELEVEL true ? (void)0 : wxTraceLevel
  277. #endif // wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
  278. #endif // _WX_MEMORY_H_