convert line ends
[canaan.git] / prj / tech / libsrc / lgalloc / poolimp.h
blob5844cadf89cf3e6651ca58c4ba3f60aa4a860a34
1 ///////////////////////////////////////////////////////////////////////////////
2 // $Source: x:/prj/tech/libsrc/lgalloc/RCS/poolimp.h $
3 // $Author: TOML $
4 // $Date: 1997/08/14 12:22:16 $
5 // $Revision: 1.6 $
6 //
7 // Implementation details of allocation pools. Most clients should only
8 // concern themselves with pool.h
9 //
11 #ifndef __POOLIMP_H
12 #define __POOLIMP_H
14 #include <malloc.h>
15 #include <heaptool.h>
17 #undef Free
19 ///////////////////////////////////////////////////////////////////////////////
21 // Macros to specify how pools should get thier memory.
24 #ifndef _WIN32
25 #define PoolCoreAllocPage() malloc(kPageSize)
26 #define PoolCoreFreePage(p) free(p)
27 #else
28 #define PoolCoreAllocPage() cPoolCore::AllocPage()
29 #define PoolCoreFreePage(p) cPoolCore::FreePage(p)
30 #endif
32 ///////////////////////////////////////////////////////////////////////////////
34 // CLASS: cPoolAllocator
36 // Pooled Allocator to be used by operator new, operator delete overrides and
37 // by the Multi-heap
39 // cPoolAllocator will keep a freelist of items of the same size.
40 // When the freelist is empty it will alloc them in multiple blocks
42 // Instances of cPoolAllocator *must* be staticly allocated,
43 // typically as static instance variables of the client class,
44 // as there is no destructor and thus there would be a memory leak if NOT static.
47 struct sPoolBlock;
49 class cPoolAllocator
51 public:
53 cPoolAllocator();
54 cPoolAllocator(size_t elemSize); // How big will each item be, and howMany per alloc()'d Bucket
56 void Init(size_t elemSize);
58 void * Alloc();
59 void Free(void *);
61 void DumpAllocs();
63 static void DumpPools();
65 #ifdef TRACK_ALLOCS
66 // For debugging/optimization:
67 unsigned long GetBlockNum();
68 unsigned long GetTakes();
69 unsigned long GetBlockSize();
70 unsigned long GetFrees();
71 unsigned long GetMaxTakes();
72 #endif
74 private:
75 void ThreadNewBlock();
77 sPoolBlock * m_pFreeList;
78 size_t m_nElementSize;
79 unsigned m_nBlockingFactor;
81 cPoolAllocator * m_pNextPool;
83 #ifdef TRACK_ALLOCS
84 unsigned long m_nBlocks;
85 unsigned long m_nInUse;
86 unsigned long m_nAllocs;
87 unsigned long m_nFrees;
88 unsigned long m_nMaxTakes;
89 sPoolBlock * m_pAllocList;
90 #endif
92 static cPoolAllocator * m_pPools;
95 ///////////////////////////////////////
97 inline cPoolAllocator::cPoolAllocator()
102 ///////////////////////////////////////
104 inline cPoolAllocator::cPoolAllocator(size_t elemSize)
106 Init(elemSize);
109 ///////////////////////////////////////
111 #ifdef TRACK_ALLOCS
112 inline unsigned long cPoolAllocator::GetBlockNum()
114 return m_nBlocks;
117 ///////////////////////////////////////
119 inline unsigned long cPoolAllocator::GetTakes()
121 return m_nAllocs;
124 ///////////////////////////////////////
126 inline unsigned long cPoolAllocator::GetFrees()
128 return m_nFrees;
131 ///////////////////////////////////////
133 inline unsigned long cPoolAllocator::GetMaxTakes()
135 return m_nMaxTakes;
137 #endif
139 ///////////////////////////////////////////////////////////////////////////////
141 #endif /* !__POOLIMP_H */