#ifndef _MEM_POOL_H_
#define _MEM_POOL_H_
#include "myXPU.h"
#include "myType.h"
#include "linkCache.h"
#include "myMtux.h"

class MemoryPool {
    public:
        MemoryPool();
        MemoryPool(int devID, MemType inType, float factor);
        virtual ~MemoryPool();
        void InitPoolMem(int devID, MemType inType, float factor);
        MyVoid *GetCacheMemFromPool(int fType, long long needLen, int iHD);
        int TakeBackCMemToPool(MyVoid *iCMem);
        MemType GetMemType();
        long long GetCacheLen();
        int GetMemBlankUserNum(MyVoid *iCMem);
        void PrintUseage();
        
    private:
        void ResetUseInfo();
        MyVoid *GetCacheMemFromPoolMutux(int fType, long long needLen, int iHD);
        int TakeBackCMemToPoolMutux(MyVoid *iCMem);
        char *mCacheMem;
        long long mCacheLen;
        MemType mMemType;
        LinkCache mFreeSpaceLk;
        LinkCache mUseSpaceLk;
        MySignal mMySignal;
        int mDevID;
};

class GlobalMemPoolFloat {
    public:
        GlobalMemPoolFloat();
        GlobalMemPoolFloat(int devID, MemType inType, float factor, int num, const char *info);
        void SetConfig(int devID, MemType inType, float factor, int num, const char *info);
        virtual ~GlobalMemPoolFloat();
        MyVoid *GetCacheMemFromPool(int fType, long long needLen, int iHD);
        int TakeBackCMemToPool(MyVoid *iCMem);
        MemType GetMemType();
        void PrintMemInfo();
        int GetMemBlankUserNum(MyVoid *iCMem);
        int GetDevID();
        void PrintSubMemUsage();     
        char mMemInfo[128];

    private:
        MyVoid *GetCacheMemFromPoolOther(int fType, long long needLen, int iHD);
        int TakeBackCMemToPoolOther(MyVoid *iCMem);
        #ifdef _USE_OPENCL_
        MyVoid *GetCacheMemFromPoolCL(int fType, long long needLen, int iHD);
        int TakeBackCMemToPoolCL(MyVoid *iCMem);
        #endif
        void RealeaseCLLinkCacheMem();
        MemoryPool *mMemoryPool;
        int mPoolNum;
        MemType mMemType;
        //char mMemInfo[128];
        LinkCache mCLDisSVMCache;
        long long mCanAllocLen, mCurrentAllocLen;
        int mDevID;
};

#endif
