#ifndef _MEMORY_HANDLE_
#define _MEMORY_HANDLE_
#include "myXPU.h"
#include "myType.h"
#include "myHead.h"
#include "globalMemPoolFloat.h"

class MemoryHandle {

    public:
        MemoryHandle();
        MemoryHandle(int ifType, int needLen, int devID, GlobalMemPoolFloat *iGMemMain, GlobalMemPoolFloat *iGMemBack, int hID);
        virtual ~MemoryHandle();
        MyVoid *GetMemBuff();
        MyVoid *GetXPUMemBuff();
        int GetMemBlankUserNum();
        int GetMemSize();
        MemType GetMemType();
        void TakeBackCMem();
        void EraseData();
        void ApplyMemory();
        int IfEmptyCMem();

        GlobalMemPoolFloat *GetUseMemPool();
        GlobalMemPoolFloat *GetMainMemPool();
        GlobalMemPoolFloat *GetBackMemPool();
        
        void CopyFromHandle(MemoryHandle *srcHandle);
        void CatInFromHandle(int offset, MemoryHandle *srcHandle);
        void CatInFromHandleOffset(int dOffset, int sOffset, int srcUseLen, MemoryHandle *srcHandle);

        static void MemoryHandleAddin(MemoryHandle *dH, MemoryHandle *sH);
        static void MemoryHandleDotin(MemoryHandle *dH, MemoryHandle *sH);
        static void CopyCMemToHandleMem(int sfType, int sID, MemoryHandle *mHandle, int offset, MyVoid *comMem, int len);

        MyVoid *mXPUMemBuff;
        void CLMapReadBegin();
        void CLMapWriteBegin();
        void CLMapReadWriteBegin();
        void CLMapReadEnd();
        void CLMapWriteEnd();
        void CLMapReadWriteEnd();
        
        int GetFType();
        int GetDevID();

        int mMemSize;
        MyVoid *mMemBuff;
        MemType mMemType;

        int flag;  // 1: create new;  2: add new user
        GlobalMemPoolFloat *mUseMemPool, *mMainMemPool, *mBackMemPool;
        int mDevID;
        int mInitDevID;
        int mHID;
        int mfType;

    private:
        static void MemoryHandleAddinFromBuff(MemoryHandle *dH, int dOffset, int sfType, int sID, MyVoid *srcBuff, int srcOffset, int needLen, MemType sMemType);
        static void MemoryHandleAddinFromBuff(MemoryHandle *dH, int sfType, int sID, MyVoid *srcBuff, MemType sMemType);
        void InitStatus();
        void InitHandle(int ifType, int needLen, int devID, GlobalMemPoolFloat *iGMemMain, GlobalMemPoolFloat *iGMemBack, int hID);
};

#endif
