关闭

模拟Windows句柄的实现于管理

标签: windowstablenullstructclass
938人阅读 评论(1) 收藏 举报
分类:
#ifndef _HANDLE_MGR_H_
#define _HANDLE_MGR_H_

#undef MALLOC
#undef FREE

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))


#define TABLE_SIZE 1024*1024

#pragma pack(push,1)

//句柄信息
typedef struct tagHandleItem
{
    unsigned int   ID;        //句柄ID
    void         * Pointer;   //句柄指针
    unsigned char  bValid;    //指针是否有效
}HandleItem;

typedef struct tagHandleTable
{
    HandleItem       hands[TABLE_SIZE];
    tagHandleTable  *next;
}HandleTable;

#pragma pack(pop)

class CHandleMgr
{
public:

    CHandleMgr()
    {
        m_pHandTable=NULL;
    }

    virtual ~CHandleMgr()
    {
        tagHandleTable  *next;
        while(m_pHandTable)
        {
            next=m_pHandTable->next;
            FREE(m_pHandTable);
            m_pHandTable=next;
        }
    }

private:

    HandleTable *m_pHandTable;

    HANDLE CreateHandle(HandleTable *pHandTable, void *p, UINT iTable)
    {
        if (pHandTable)
        {
            //检测当前表
            for (size_t i=0;i<TABLE_SIZE;i++)
            {
                if (!pHandTable->hands[i].bValid || pHandTable->hands[i].Pointer==NULL)
                {
                    pHandTable->hands[i].ID=iTable*TABLE_SIZE+i+1;
                    pHandTable->hands[i].bValid=TRUE;
                    pHandTable->hands[i].Pointer=p;
                    return HANDLE((DWORD)pHandTable+(i*sizeof(HandleItem)));
                }
            }

            //检测下一个表
            if (pHandTable->next)
            {
                HANDLE handle=CreateHandle(pHandTable->next,p,iTable+1);
                if (handle) return handle;
            }

            //未找到可用的,则在最后增加一个表
            HandleTable *ptable=(HandleTable *)MALLOC(sizeof(HandleTable));

            if (ptable)
            {
                memset(ptable,0,sizeof(HandleTable));
                pHandTable->next=ptable;

                ptable->hands[0].ID=iTable*TABLE_SIZE+1;
                ptable->hands[0].bValid=TRUE;
                ptable->hands[0].Pointer=p;

                return HANDLE(ptable);
            }

        }
        return NULL;
    }


    void *IndexToPointer(HandleTable *pHandTable, UINT iTable, UINT index)
    {
        if (NULL==pHandTable) return NULL;

        UINT iStart=iTable*TABLE_SIZE;
        if (index>=iStart && index<iStart+TABLE_SIZE)
        {
            index-=iTable*TABLE_SIZE;

            if ( pHandTable->hands[index].bValid && pHandTable->hands[index].Pointer )
            {
                return pHandTable->hands[index].Pointer;
            }
            
        }else{
            return IndexToPointer(pHandTable->next,iTable+1,index);
        }
        
        return 0;
    }

    HANDLE IndexToHandle(HandleTable *pHandTable, UINT iTable, UINT index)
    {
        if (NULL==pHandTable) return NULL;

        UINT iStart=iTable*TABLE_SIZE;
        if (index>=iStart && index<iStart+TABLE_SIZE)
        {
            index-=iTable*TABLE_SIZE;

            if ( pHandTable->hands[index].bValid && pHandTable->hands[index].Pointer )
            {
                return HANDLE(&pHandTable->hands[index]);
            }
            
        }else{
            return IndexToHandle(pHandTable->next,iTable+1,index);
        }
        
        return 0;
    }

public:

    HANDLE CreateHandle(void *p)
    {
        if (m_pHandTable==NULL)
        {
            //未找到可用的,则在最后增加一个表
            m_pHandTable=(HandleTable *)MALLOC(sizeof(HandleTable));
            if (m_pHandTable)
            {
                memset(m_pHandTable,0,sizeof(HandleTable));
                m_pHandTable->hands[0].ID=1;
                m_pHandTable->hands[0].bValid=TRUE;
                m_pHandTable->hands[0].Pointer=p;
                return HANDLE(m_pHandTable);
            }
        }

        return CreateHandle(m_pHandTable,p,0);
    }

    BOOL CloseHandle(HANDLE handle)
    {
        if (handle && !IsBadWritePtr(handle,sizeof(HandleItem)))
        {
            if ( ((HandleItem *)handle)->bValid && ((HandleItem *)handle)->Pointer )
            {
                ((HandleItem *)handle)->bValid=FALSE;
                ((HandleItem *)handle)->Pointer=NULL;
            }
        }
        return TRUE;
    }

    void *HandleToPointer(HANDLE handle)
    {
        if (handle && !IsBadWritePtr(handle,sizeof(HandleItem)))
        {
            if ( ((HandleItem *)handle)->bValid && ((HandleItem *)handle)->Pointer )
            {
                //TRACE("%08X %08X", handle, ((HandleItem *)handle)->Pointer);
                return ((HandleItem *)handle)->Pointer;
            }
        }
        return NULL;
    }

    UINT HandleToID(HANDLE handle)
    {
        if (handle && !IsBadWritePtr(handle,sizeof(HandleItem)))
        {
            if ( ((HandleItem *)handle)->bValid && ((HandleItem *)handle)->Pointer )
            {
                return ((HandleItem *)handle)->ID;
            }
        }
        return 0;
    }

    HANDLE IDToHandle(UINT ID)
    {
        if (ID==0) return NULL;

        UINT index=ID-1;
        return IndexToHandle(m_pHandTable,0,index);
    }

    void *IDToPointer(UINT ID)
    {
        if (ID==0) return NULL;

        UINT index=ID-1;
        return IndexToPointer(m_pHandTable,0,index);
    }

};

#endif //(_HANDLE_MGR_H_)


 

2
2

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:128835次
    • 积分:2059
    • 等级:
    • 排名:第18638名
    • 原创:73篇
    • 转载:3篇
    • 译文:1篇
    • 评论:61条
    最新评论