Annotation of sql/odbc/MFCpatches/fixalloc.h, revision 1.1
1.1 ! paf 1: // fixalloc.h - declarations for fixed block allocator
! 2:
! 3: #ifndef __FIXALLOC_H__
! 4: #define __FIXALLOC_H__
! 5:
! 6: #include "afxplex_.h"
! 7:
! 8: /////////////////////////////////////////////////////////////////////////////
! 9: // CFixedAlloc
! 10:
! 11: class CFixedAllocNoSync
! 12: {
! 13: // Constructors
! 14: public:
! 15: CFixedAllocNoSync(UINT nAllocSize, UINT nBlockSize = 64);
! 16:
! 17: // Attributes
! 18: UINT GetAllocSize() { return m_nAllocSize; }
! 19:
! 20: // Operations
! 21: public:
! 22: void* Alloc(); // return a chunk of memory of nAllocSize
! 23: void Free(void* p); // free chunk of memory returned from Alloc
! 24: void FreeAll(); // free everything allocated from this allocator
! 25:
! 26: // Implementation
! 27: public:
! 28: ~CFixedAllocNoSync();
! 29:
! 30: protected:
! 31: struct CNode
! 32: {
! 33: CNode* pNext; // only valid when in free list
! 34: };
! 35:
! 36: UINT m_nAllocSize; // size of each block from Alloc
! 37: UINT m_nBlockSize; // number of blocks to get at a time
! 38: CPlex* m_pBlocks; // linked list of blocks (is nBlocks*nAllocSize)
! 39: CNode* m_pNodeFree; // first free node (NULL if no free nodes)
! 40: };
! 41:
! 42: class CFixedAlloc : public CFixedAllocNoSync
! 43: {
! 44: typedef class CFixedAllocNoSync base;
! 45:
! 46: // Constructors
! 47: public:
! 48: CFixedAlloc(UINT nAllocSize, UINT nBlockSize = 64);
! 49:
! 50: // Operations
! 51: public:
! 52: void* Alloc(); // return a chunk of memory of nAllocSize
! 53: void Free(void* p); // free chunk of memory returned from Alloc
! 54: void FreeAll(); // free everything allocated from this allocator
! 55:
! 56: // Implementation
! 57: public:
! 58: ~CFixedAlloc();
! 59:
! 60: protected:
! 61: CRITICAL_SECTION m_protect;
! 62: };
! 63:
! 64: #ifndef _DEBUG
! 65:
! 66: // DECLARE_FIXED_ALLOC -- used in class definition
! 67: #define DECLARE_FIXED_ALLOC(class_name) \
! 68: public: \
! 69: void* operator new(size_t size) \
! 70: { \
! 71: ASSERT(size == s_alloc.GetAllocSize()); \
! 72: UNUSED(size); \
! 73: return s_alloc.Alloc(); \
! 74: } \
! 75: void* operator new(size_t, void* p) \
! 76: { return p; } \
! 77: void operator delete(void* p) { s_alloc.Free(p); } \
! 78: void* operator new(size_t size, LPCSTR, int) \
! 79: { \
! 80: ASSERT(size == s_alloc.GetAllocSize()); \
! 81: UNUSED(size); \
! 82: return s_alloc.Alloc(); \
! 83: } \
! 84: protected: \
! 85: static CFixedAlloc s_alloc \
! 86:
! 87: // IMPLEMENT_FIXED_ALLOC -- used in class implementation file
! 88: #define IMPLEMENT_FIXED_ALLOC(class_name, block_size) \
! 89: CFixedAlloc class_name::s_alloc(sizeof(class_name), block_size) \
! 90:
! 91: // DECLARE_FIXED_ALLOC -- used in class definition
! 92: #define DECLARE_FIXED_ALLOC_NOSYNC(class_name) \
! 93: public: \
! 94: void* operator new(size_t size) \
! 95: { \
! 96: ASSERT(size == s_alloc.GetAllocSize()); \
! 97: UNUSED(size); \
! 98: return s_alloc.Alloc(); \
! 99: } \
! 100: void* operator new(size_t, void* p) \
! 101: { return p; } \
! 102: void operator delete(void* p) { s_alloc.Free(p); } \
! 103: void* operator new(size_t size, LPCSTR, int) \
! 104: { \
! 105: ASSERT(size == s_alloc.GetAllocSize()); \
! 106: UNUSED(size); \
! 107: return s_alloc.Alloc(); \
! 108: } \
! 109: protected: \
! 110: static CFixedAllocNoSync s_alloc \
! 111:
! 112: // IMPLEMENT_FIXED_ALLOC_NOSYNC -- used in class implementation file
! 113: #define IMPLEMENT_FIXED_ALLOC_NOSYNC(class_nbame, block_size) \
! 114: CFixedAllocNoSync class_name::s_alloc(sizeof(class_name), block_size) \
! 115:
! 116: #else //!_DEBUG
! 117:
! 118: #define DECLARE_FIXED_ALLOC(class_name) // nothing in debug
! 119: #define IMPLEMENT_FIXED_ALLOC(class_name, block_size) // nothing in debug
! 120: #define DECLARE_FIXED_ALLOC_NOSYNC(class_name) // nothing in debug
! 121: #define IMPLEMENT_FIXED_ALLOC_NOSYNC(class_name, block_size) // nothing in debug
! 122:
! 123: #endif //!_DEBUG
! 124:
! 125: #endif
E-mail: