Polygon Crucher SDK - Documentation
Documentation
Loading...
Searching...
No Matches
XMemory.h
Go to the documentation of this file.
1//! @file XMemory.h
2//! @brief Set memory callbacks, memory leaks and low level allocation functions
3//!
4//////////////////////////////////////////////////////////////////////
5
6#if !defined(CXMEMORY_INCLUDE_H)
7#define CXMEMORY_INCLUDE_H
8
9#ifdef _MSC_VER
10#pragma once
11#endif // _MSC_VER
12
13#include "XMemoryMacros.h"
14
15BEGIN_MOOTOOLS_NAMESPACE
16
17// if MOOTOOLS_USE_NEW_AND_DELETE is not defined, we use a specific callbackable malloc / realloc function
18// Otherwise we use the standard c standard malloc / realloc...
19#ifndef MOOTOOLS_USE_NEW_AND_DELETE
20
21 //! @enum XAllocType
22 //! @brief Defines the required allocation type provided through XMemoryCallback callback
23 typedef enum XAllocType
24 {
25 XALLOC_UNDEFINED = 0, // Never sent
26
27 // Information about the memory being allocated of freed
28 XALLOC_MALLOC, //!< malloc C method
29 XALLOC_CALLOC, //!< calloc C method. Do not forget to call something equivalent to calloc that set memory to 0
30 XALLOC_FREE, //!< free C method
31 XALLOC_NEW, //!< new C++
32 XALLOC_DELETE, //!< delete C++
33 XALLOC_MALLOC_ALIGNED, //!< Not used
34 XALLOC_CALLOC_ALIGNED, //!< Not used
35 XALLOC_NEW_ALIGNED, //!< Not used
36 } XAllocType;
37
38 // Memory callback types
39#ifdef _DEBUG
40 typedef void * (*mallocCallbackDbg)(size_t newSize, XAllocType type, LPCSTR file, int line); //!< Called by malloc / calloc / new operation
41 typedef void * (*reallocCallbackDbg)(void *pointer, size_t newSize, LPCSTR file, int line); //!< Called by realloc operation
42 typedef void (*freeCallbackDbg)(void *pointer, XAllocType type, LPCSTR file, int line); //!< Called by free / delete operation
43#else
44 typedef void * (*mallocCallback)(size_t newSize, XAllocType type); //!< Called by malloc / calloc / new operation
45 typedef void * (*reallocCallback)(void *pointer, size_t newSize); //!< Called by realloc operation
46 typedef void (*freeCallback)(void *pointer, XAllocType type); //!< Called by free / delete operation
47#endif
48
49 //! @struct XMemoryCallback
50 //! @brief Defines a set of memory callback handlers that can be modified.
51 //! @details memoryCallbackNotify is a global XMemoryCallback. If you modify it, you can replace default callbacks by your own one.
52 typedef struct XMemoryCallback
53 {
54#ifdef _DEBUG
55 mallocCallbackDbg malloc;
56 reallocCallbackDbg realloc;
57 freeCallbackDbg free;
58#else
59 mallocCallback malloc;
60 reallocCallback realloc;
61 freeCallback free;
62#endif
64 {
65 malloc = NULL;
66 realloc = NULL;
67 free = NULL;
68 }
70
71 //! Use this memory callback object to receive notification on new, malloc, realloc, free, delete
72 //! @note memoryCallbackNotify is initialized at startup.\n
73 //! As the static order initialization is unknown, do not use these memory function to allocate static objects.\n
74 //! Such objet might be allocated before XMemoryCallback constructors calls resulting in invalid callback pointer and potential crash.
75 DLL_TOOLSFUNCTION extern XMemoryCallback memoryCallbackNotify;
76
77 //! @name Main library allocators (superseed C standard allocators)
78 //! @{
79 DLL_TOOLSFUNCTION void *xmalloc(size_t nSize);
80 DLL_TOOLSFUNCTION void xfree(void *p);
81 DLL_TOOLSFUNCTION void *xrealloc(void *p, size_t newSize); // Can be called only if xmalloc has been used
82 DLL_TOOLSFUNCTION void *xcalloc(size_t numElement, size_t elementSize);
83
84 DLL_TOOLSFUNCTION void *xnew(size_t nSize); //!< Only used by macros xNew
85 DLL_TOOLSFUNCTION void xdelete(void *p); //!< Only used by macros xDelete
86
87 //! @}
88#ifdef _DEBUG
90 void EnableMemoryLeaksChecking(bool enable); //!< Enable memory leak checking which can requires a lot of memory for big 3d scene (disabled by default)
91
92 // Replace std c memory function by equivalent debug function
93 #define xmalloc(nSize) xmalloc_dbg(nSize, __FILE__, __DATE__, __LINE__) // Can also use xAllocate
94 #define xfree(nSize) xfree_dbg(nSize, __FILE__, __DATE__, __LINE__) // Can also use xDeallocate
95 #define xrealloc(p, nSize) xrealloc_dbg(p, nSize, __FILE__, __DATE__, __LINE__) //
96 #define xcalloc(num, nSize) xcalloc_dbg(num, nSize, __FILE__, __DATE__, __LINE__)
97 #define xmalloc_array(nSize, nCount) xmalloc_dbg((nSize)*(nCount), __FILE__, __DATE__, __LINE__) // Can also use xAllocateArray
98 #define xfree_array(p, nCount) xfree_dbg(p, __FILE__, __DATE__, __LINE__) // Can also use xDeallocateArray
99
100 #define xnew(nSize) xnew_dbg(nSize, __FILE__, __DATE__, __LINE__) // use xNew. This one only handles the memory allocation.
101 #define xdelete(p) xdelete_dbg(p, __FILE__, __DATE__, __LINE__) // use xDelete. This one only handles the memory deletion.
102 #define xnew_array(nSize, nCount) xnew_array_dbg(nSize, nCount, __FILE__, __DATE__, __LINE__) // use xNewArray. This one only handles the memory allocation.
103 #define xdelete_array(p, nCount) xdelete_array_dbg(p, nCount, __FILE__, __DATE__, __LINE__) // use xDeleteArray. This one only handles the memory deletion.
104
105 // Declare debug memory functions
106 DLL_TOOLSFUNCTION void *xmalloc_dbg(size_t nSize, LPCSTR lpszFileName, LPCSTR lpszFileDate, int nLine);
107 DLL_TOOLSFUNCTION void xfree_dbg(void *p, LPCSTR lpszFileName, LPCSTR lpszFileDate, int nLine);
108 DLL_TOOLSFUNCTION void *xrealloc_dbg(void *p, size_t nSize, LPCSTR lpszFileName, LPCSTR lpszFileDate, int nLine);
109 DLL_TOOLSFUNCTION void *xcalloc_dbg(size_t num, size_t nSize, LPCSTR lpszFileName, LPCSTR lpszFileDate, int nLine);
110
111 DLL_TOOLSFUNCTION void *xnew_dbg(size_t nSize, LPCSTR lpszFileName, LPCSTR lpszFileDate, int line);
112 DLL_TOOLSFUNCTION void xdelete_dbg(void* p, LPCSTR lpszFileName, LPCSTR lpszFileDate, int line);
113 DLL_TOOLSFUNCTION void *xnew_array_dbg(size_t nSize, size_t nCount, LPCSTR lpszFileName, LPCSTR lpszFileDate, int line); // This allow to verify if we destroy the correct number of elements
114 DLL_TOOLSFUNCTION void xdelete_array_dbg(void* p, size_t nCount, LPCSTR lpszFileName, LPCSTR lpszFileDate, int line);
115#else
116 #define xmalloc_array(nSize, nCount) xmalloc((nSize)*(nCount))
117 #define xfree_array(p, nCount) xfree(p)
118 #define xnew_array(nSize, nCount) xnew((nSize)*(nCount))
119 #define xdelete_array(p, nCount) xdelete(p)
120#endif
121#else // MOOTOOLS_USE_NEW_AND_DELETE
122 #define DisableMemoryLeaksChecking() {}
123 #define xmalloc malloc
124 #define xmalloc_array malloc
125 #define xfree free
126 #define xrealloc realloc
127 #define xcalloc calloc
128
129 // xnew and xdelete should only be used by xNew and xDelete which don't use them when MOOTOOLS_USE_NEW_AND_DELETE defined
130#endif
131
132END_MOOTOOLS_NAMESPACE
133
134#endif // !defined(CXMEMORY_INCLUDE_H)
DLL_TOOLSFUNCTION XMemoryCallback memoryCallbackNotify
void(* freeCallback)(void *pointer, XAllocType type)
Called by free / delete operation.
Definition XMemory.h:46
void *(* mallocCallback)(size_t newSize, XAllocType type)
Called by malloc / calloc / new operation.
Definition XMemory.h:44
DLL_TOOLSFUNCTION void * xnew(size_t nSize)
Only used by macros xNew.
void *(* reallocCallback)(void *pointer, size_t newSize)
Called by realloc operation.
Definition XMemory.h:45
DLL_TOOLSFUNCTION void xdelete(void *p)
Only used by macros xDelete.
XAllocType
Defines the required allocation type provided through XMemoryCallback callback.
Definition XMemory.h:24
@ XALLOC_CALLOC_ALIGNED
Not used.
Definition XMemory.h:34
@ XALLOC_NEW
new C++
Definition XMemory.h:31
@ XALLOC_DELETE
delete C++
Definition XMemory.h:32
@ XALLOC_MALLOC_ALIGNED
Not used.
Definition XMemory.h:33
@ XALLOC_FREE
free C method
Definition XMemory.h:30
@ XALLOC_CALLOC
calloc C method. Do not forget to call something equivalent to calloc that set memory to 0
Definition XMemory.h:29
@ XALLOC_MALLOC
malloc C method
Definition XMemory.h:28
@ XALLOC_NEW_ALIGNED
Not used.
Definition XMemory.h:35
definitions of macros for SDK new / delete / allocation operators.
The class defines an x, y, z 3D point which can use int, float or double.
Definition 3DPoint.h:27
Defines a set of memory callback handlers that can be modified.
Definition XMemory.h:53