OpenMoHAA 0.82.0
Loading...
Searching...
No Matches
gsMemory.h
1
3#ifndef __GSIMEMORY_H__
4#define __GSIMEMORY_H__
5
6
9#include "gsCommon.h"
10
11// GameSpy allocation wrappers. Used for quickly adding pre-post allocation functionality such as
12// - routing to a specific mempool
13// - collecting mem usage stats
14// (x) is a enumerated type for the specific module
15#if(1)
16 #define GSI_PRE_ALLOC(x)
17 #define GSI_POST_ALLOC()
18#elif(0)
19 // - collecting mem usage stats
20 #define GSI_PRE_ALLOC(x) gsMemMgrTagPush (x);
21 #define GSI_POST_ALLOC() gsMemMgrTagPop ();
22#elif(0)
23 // - routing to a specific mempool
24 #define GSI_PRE_ALLOC(x) gsMemMgrContextPush (x);
25 #define GSI_POST_ALLOC() gsMemMgrContextPop ();
26#endif
27
28#if defined (__cplusplus)
29extern "C"
30{
31#endif
32
33
34typedef enum
35{
36 MEMTAG_DEFAULT,
37 MEMTAG_SERVER_BROWSER,
38 MEMTAG_PEER,
39 MEMTAG_GP,
40 MEMTAG_QR2,
41 MEMTAG_NN,
42 MEMTAG_GT2,
43 MEMTAG_COUNT
44} MEMTAG_SDK;
45
46//--------------------------------------------------------------------------
47// GameSpy specific memory functions. By default these will route to system malloc
48// calls. Use gsiMemoryCallbacksSet or gsiMemoryCallbacksGameSpySet to change this.
49void* gsimalloc (size_t size);
50void* gsirealloc (void* ptr, size_t size);
51void gsifree (void* ptr);
52void* gsimemalign (size_t boundary, size_t size); // TODO
53
54//--------------------------------------------------------------------------
55// Customer supplied memory manager customization interface
56// call this to replace the Gamespy specific memory functions with your own.
57#ifdef WIN32
58typedef void *(__cdecl *gsMallocCB) (size_t size);
59typedef void (__cdecl *gsFreeCB) (void* ptr);
60typedef void *(__cdecl *gsReallocCB) (void* ptr, size_t size);
61typedef void *(__cdecl *gsMemalignCB)(size_t boundary, size_t size);
62#else
63typedef void *(*gsMallocCB) (size_t size);
64typedef void (*gsFreeCB) (void* ptr);
65typedef void *(*gsReallocCB) (void* ptr, size_t size);
66typedef void *(*gsMemalignCB)(size_t boundary, size_t size);
67#endif
68// call this to override above gsi.... calls with your own.
69void gsiMemoryCallbacksSet(gsMallocCB p_malloc, gsFreeCB p_free, gsReallocCB p_realloc, gsMemalignCB p_memalign);
70
71//--------------------------------------------------------------------------
72// GameSpy Built in Memory Manager
73// call this to override above gsi.... calls with GameSpy's built in memory manager
74// *** You must have GSI_MEM_MANAGED defined, otherwise, you will have a link error ***/
75
76// This is a list of memory pools used. API specific values determined at run time.
77// the gsi mem manager uses the concept of multiple memory pools or contexts.
78// use pop and push commands to pop and push the current context off of the stack
79
80typedef enum
81{
82 gsMemMgrContext_Invalid= -1,
83 gsMemMgrContext_Default= 0,
84 gsMemMgrContext_Count = 16 // max number of mempools
85}gsMemMgrContext;
86
87
88// call this to enable GameSpy's provided memory manager
89// Create a mempool for the given context. If that context is in use, it will return the next available
90// if none are avaible it will return gsMemMgrContext_Invalid
91// exx use: gQR2MemContext = gsMemMgrCreate (0,0,16 * 1024);
92// will find the first avaiable spot, create a mempool of 16k, and return the context handle.
93// then later in your API
94// enter an API function
95// gsMemMgrContextPush(gQR2MemContext);
96// do some allocs
97// gQR2MemContextPop()
98// return from function.
99// PoolName is purely for debugging and stats feedback purposes only.
100// If you want your api to use the current, or the default pool, then don't bother creating one
101// just always set the context to 0, and make sure int your app init, the default (0) pool is created.
102/*
103 Recommended usage:
104 Call gsMemMgrCreate once at app start with a static buffer. Make all calls to this.
105 Alternatively, call it once per API to sue a seperate pool per API.
106*/
107gsMemMgrContext gsMemMgrCreate (gsMemMgrContext context, const char *PoolName,void* thePoolBuffer, size_t thePoolSize);
108
109// Use this to determine which pool and subsequent allocations will be taken from.
110//exx use
111/*
112 fn()
113 {
114 gsMemMgrContextPush(thisAPIContext);
115
116 make allocations.
117
118 //restore settings
119 gsMemMgrContextPop(thisAPIContext);
120
121 }
122*/
123// note, this is not neccessary for "free".
124void gsMemMgrContextPush (gsMemMgrContext context);
125gsMemMgrContext gsMemMgrContextPop ();
126
127
128// clear contents, original mempool ptr must still be freed by app.
129void gsMemMgrDestroy(gsMemMgrContext context);
130
131// -------------Diagnostics------------------------
132// These functions all run on the current mempool context.
133void gsMemMgrDumpStats();
134void gsMemMgrDumpAllocations();
135void gsMemMgrValidateMemoryPool(); // walk heap and check integrity
136
137// -------------Tool use ------------------------
138// find which mempool context this ptr is part of, if any.
139// returns gsMemMgrContext_Invalid otherwise.
140gsMemMgrContext gsMemMgrContextFind (void *ptr);
141const char *MemMgrBufferGetName(gsMemMgrContext context);
142
143
144// -------------Memory Use Profiling ------------------------
145// this tag is added to each concurrent alloc. Use this to reference allocations.
146// For example, you can find out the mem used by all ptr with a given tag
147// in order to find out how much mem a module or set of allocs use.
148void gsMemMgrTagPush (gsi_u8 tag);
149void gsMemMgrTagPop ();
150gsi_u8 gsMemMgrTagGet (void *ptr);
151gsi_u32 gsMemMgrMemUsedByTagGet(gsi_u8 tag);
152
153// return total available memory for the given memory pool context
154gsi_u32 gsMemMgrMemAvailGet (gsMemMgrContext context);
155// return total used memory for the given memory pool context
156gsi_u32 gsMemMgrMemUsedGet (gsMemMgrContext context);
157// return largest allocatable chunk within the given memory pool context. This
158// will be the same or probably smaller then the value returned by gsMemMgrMemAvailGet
159// depending on degree of memory fragmentation.
160gsi_u32 gsMemMgrMemLargestAvailGet (gsMemMgrContext context);
161
162// The Highwater mark for memory used is the highest memory usage ever gets to for this
163// given heap. It is the most important stat, as your mempool must be at least this big.
164// Exactly how big your pool needs to be depends on fragmentation. So it may need to be slightly
165// bigger then this amount.
166gsi_u32 gsMemMgrMemHighwaterMarkGet (gsMemMgrContext context);
167
168
169// -------------Self Test, not for production use ------------------------
170void gsMemMgrSelfText();
171
172#if defined (__cplusplus)
173}
174#endif
175
176#endif // __GSIMEMORY_H__
177