WarSMemory.h
资源名称:warftpd.zip [点击查看]
上传用户:surprise9
上传日期:2007-01-04
资源大小:426k
文件大小:8k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // See the "War Software Series License Agreement" for details concerning
- // use and distribution.
- // ---
- // This source code, executables and programs containing source code or
- // binaries or proprietetary technology from the War Software Series are
- // NOT alloed used, viewed or tested by any governmental agencies in
- // any countries. This includes the government, departments, police,
- // military etc.
- // ---
- // This file is intended for use with Tab space = 2
- // Created and maintained in MSVC Developer Studio
- // ---
- // NAME : WarSMemory.h
- // PURPOSE : Shared memory management
- // PROGRAM :
- // DATE : March 9 1997
- // AUTHOR : Jarle Aase
- // ---
- //
- // REVISION HISTORY
- //
- #define LNAME_SHARED_MEM_SEGMENT "warlsms" // Semaphore, exclusive lock
- #define SHMEM_BLKS_MUTEX_NAME "warlsmm" // CSharedBlksMem mutex name
- #define MAX_SHARED_SEGMENT_NAME_LEN 24 // Name length of a memory segment
- #define SHARED_MEM_MAX_BLKS 128 // Max 128 blocks
- #define SMEM_NUM_BLK_SIZES 12 // 12 segment sizes
- #define SMEM_NUM_BLK_SIZES_BASE 16 // Smallest segment
- #define SHMEM_NUM_USER_PTRS 16
- ///////////////////////////////////////////////////////////////
- // Simple locking syncronization
- class DLL_WAR_SOFTWARE_ CGlobalLock
- {
- public:
- CGlobalLock(LPCSTR Name);
- ~CGlobalLock();
- CSemaphore *m_pSemaphore;
- CSingleLock *m_pSligleLock;
- };
- ///////////////////////////////////////////////////////////////////////////
- // Exceptions
- class DLL_WAR_SOFTWARE_ CSharedMemoryException : public CObject
- {
- public:
- void Trhrow(LPCSTR Name)
- {
- m_Name = Name;
- throw this;
- }
- LPCSTR m_Name;
- };
- ///////////////////////////////////////////////////////////////////////////
- // Primary MMF management
- struct SHARED_MEM_SEG_HDR
- {
- DWORD Size; // Size of the segment, incl. header
- DWORD AccessCnt; // Number of open handles
- char Name[MAX_SHARED_SEGMENT_NAME_LEN]; // Name of shared segment
- LPVOID pUserPtr[SHMEM_NUM_USER_PTRS];
- // Pointers that can be used by clients
- // Not used by the Smem framework
- };
- class DLL_WAR_SOFTWARE_ CSharedMemSegment : public CObject
- {
- public:
- CSharedMemSegment();
- ~CSharedMemSegment();
- LPVOID Create(LPCSTR Name, DWORD Size, BOOL KeepInMemory = FALSE);
- LPVOID GetDataPtr()
- {return ((char *)m_pSMH) + sizeof(SHARED_MEM_SEG_HDR);}
- HANDLE m_hMMFile; // Handle to memory mapped file
- CString m_Name;
- DWORD m_Size;
- SHARED_MEM_SEG_HDR *m_pSMH;
- };
- //////////////////////////////////////////////////////////////
- // Dynamic memory management
- // Shared memory pointer. Block no (offset into Blk)
- // and offset into actual memory location.
- // A user pointer to the memory will be:
- // Local Ptr + Block no + offset + header lenght of node header
- struct SMEM
- {
- unsigned blk:8; // Block
- unsigned ofs:24; // Offset
- SMEM& operator = (SMEM& sm)
- {
- *(DWORD *)this = *(DWORD *)&sm;
- return *this;
- }
- SMEM& operator = (int ival)
- {
- ASSERT(ival == 0);
- *(DWORD *)this = ival;
- return *this;
- }
- BOOL operator != (SMEM& sm)
- {return (this->ofs != sm.ofs) || (this->blk != sm.blk);}
- BOOL operator == (SMEM& sm)
- {return (this->ofs == sm.ofs) && (this->blk == sm.blk);}
- BOOL operator != (int iVal)
- {return ((int)*(DWORD *)this) != iVal;}
- BOOL operator == (int iVal)
- {return ((int)*(DWORD *)this) == iVal;}
- int operator ! ()
- {return *this == 0;}
- operator int ()
- {
- return (int)*(DWORD *)this;
- }
- operator bool ()
- {
- return ((int)*(DWORD *)this) != 0;
- }
- };
- #define SMISNULL(sm) (sm.ofs == 0)
- // Header block for dynamic memory management
- struct SUPER_MEM_BLKS_HDR
- {
- BOOL IsInitialized;
- int Flags;
- SMEM FreeTable[SMEM_NUM_BLK_SIZES]; // Free segments, only used by the first segment
- // <= 16 bytes, < = 32 bytes, <= 64 bytes, <= 128, <= 256
- // <= 512, <= 1024 <= 2048, <= 4096, <= 8192, <= 16384, >+
- DWORD UserMemory; // Allocayed memory by vm_malloc
- DWORD PhysMemory; // Physical memory used, including heders
- DWORD NumNodes; // Number of nodes
- DWORD UsedNodes; // Nodes used
- DWORD SegmentSize; // Size of segment in bytes
- DWORD NumUsedSegments; // Super only. Number of allocated segments
- };
- // Allocated memory buffer header
- // These can be 'free' or used.
- struct VIRTUAL_MEM_NODE_HDR
- {
- unsigned isfree:1; // Is free for use
- unsigned hasnext:1; // Has a next node.
- unsigned size:24; // Physical size, including header
- SMEM prev; // Linked list, prev node (next node can be calculated from size)
- #ifdef _DEBUG
- unsigned short watermark[4]; // Watermark to verify free/used memory. Unused is 0xfeee, used is 0
- #endif
- };
- struct VIRTUAL_FREE_MEM_NODE_HDR
- {
- VIRTUAL_MEM_NODE_HDR node_hdr;
- SMEM nextfree; // Next free node
- SMEM prevfree; // Prev free node
- int index;
- };
- class DLL_WAR_SOFTWARE_ CSharedBlksMem : public CObject
- {
- public:
- static const unsigned short tag_FreeWatermark[4];
- static const unsigned short tag_UsedWatermark[4];
- CSharedBlksMem();
- ~CSharedBlksMem();
- BOOL Create(LPCSTR Name, DWORD BlkSize);
- #ifdef _DEBUG
- LPVOID __dbg__Ptr(SMEM sm);
- #endif
- LPVOID __Ptr(SMEM sm)
- {
- #ifdef _DEBUG
- return (LPVOID)(((char *)__dbg__Ptr(sm)));
- #else
- if (!m_Blks[sm.blk]) Rescan();
- return sm.ofs ? (LPVOID *)(((char *)m_Blks[sm.blk]) + sm.ofs + sizeof(VIRTUAL_MEM_NODE_HDR)) : NULL;
- #endif
- }
- VIRTUAL_MEM_NODE_HDR *__hdr__Ptr(SMEM sm)
- {
- #ifdef _DEBUG
- if (!m_Blks[sm.blk]) Rescan();
- VIRTUAL_MEM_NODE_HDR *p = sm.ofs ? (VIRTUAL_MEM_NODE_HDR *)(((char *)m_Blks[sm.blk]) + sm.ofs) : NULL;
- if (p)
- {
- ASSERT(AfxIsValidAddress(p, sizeof(VIRTUAL_MEM_NODE_HDR)));
- }
- return p;
- #else
- if (!m_Blks[sm.blk]) Rescan();
- return sm.ofs ? (VIRTUAL_MEM_NODE_HDR *)(((char *)m_Blks[sm.blk]) + sm.ofs) : NULL;
- #endif
- }
- SMEM Smem(LPVOID ptr, int blk_num)
- {
- SMEM sm;
- if (!m_Blks[sm.blk]) Rescan();
- sm.ofs = (unsigned)(((char *)ptr) - (int)m_Blks[blk_num]);
- sm.blk = blk_num;
- return sm;
- }
- BOOL sh_malloc(SMEM& me, DWORD nBytes);
- BOOL sh_realloc(SMEM& shm, DWORD nBytes);
- void sh_free(SMEM shm);
- // Low level
- BOOL Initialize();
- void Rescan(); // Rescan block list
- BOOL AllocSegment(int Index); // Allocate a segment
- int GetLenIndex(VIRTUAL_MEM_NODE_HDR *pNode);
- int GetLenIndex(int MinSize);
- DWORD GetNodeDataLen(VIRTUAL_MEM_NODE_HDR *pNode)
- { return pNode->size - sizeof(VIRTUAL_MEM_NODE_HDR);}
- void __LinkFree(int Segment, VIRTUAL_FREE_MEM_NODE_HDR *pNode);
- BOOL __FindFreeNode(SMEM& me, DWORD nBytes, int& ListIndex);
- void __UnlinkFree(int ListIndex, SMEM me);
- BOOL __AllocNewNode(SMEM& me, DWORD nBytes, int& ListIndex);
- BOOL __AllocNode(VIRTUAL_FREE_MEM_NODE_HDR *pNode, SMEM& me, DWORD nBytes, int ListIndex);
- void __CreateNewNode(
- DWORD Segment,
- VIRTUAL_FREE_MEM_NODE_HDR *pPrevNode,
- VIRTUAL_FREE_MEM_NODE_HDR *pNode,
- DWORD nBytes,
- BOOL HasNext);
- // Data members
- LPVOID m_Blks[SHARED_MEM_MAX_BLKS]; // Local process segment list
- CSharedMemSegment m_Segments[SHARED_MEM_MAX_BLKS]; // Local segment list
- HANDLE m_Mutex; // Mutex for access to malloc/free
- CString m_Name;
- DWORD m_SegmentSize;
- BOOL m_IsInitialized;
- // Diagnostics
- #if SMEM_DEBUG
- void AssertValidFreeTables();
- #else
- #define AssertValidFreeTables()
- #endif
- };
- // Thread syncronization
- struct CWRLOCK_SMHDR
- {
- BOOL IsInitialized;
- LONG Counter;
- };
- class DLL_WAR_SOFTWARE_ CRWLock
- {
- private:
- void Claim(int i);
- void Release(int i);
- HANDLE OpenEvent(LPCSTR Name, BOOL bManualReset, BOOL bInitialState);
- HANDLE OpenMutex(LPCSTR Name);
- CString CatStr(LPCSTR Name, LPCSTR ext);
- HANDLE hMutex;
- HANDLE hWriterMutex;
- HANDLE hReaderEvent;
- LPLONG pCounter;
- CSharedMemSegment SMemSeg;
- public:
- enum
- {
- WRITER = 0,
- READER = 1
- };
- CRWLock();
- ~CRWLock();
- BOOL Create(LPCSTR Name);
- void ReadLock() {Claim(READER);}
- void WriteLock() {Claim(WRITER);}
- void UnlockRead() {Release(READER);}
- void UnlockWrite() {Release(WRITER);}
- };