153 lines
4.3 KiB
C++
153 lines
4.3 KiB
C++
|
|
#ifndef _IMEMORY_POOL_H_
|
|
#define _IMEMORY_POOL_H_
|
|
|
|
#include <queue>
|
|
#include <map>
|
|
#include <list>
|
|
#include <string>
|
|
#include <iterator>
|
|
#include <mutex>
|
|
#include <condition_variable>
|
|
#include <thread>
|
|
#include <cstring>
|
|
|
|
#include "Additional.hpp"
|
|
#include "PlatformDefine.h"
|
|
|
|
namespace pool{
|
|
|
|
typedef enum {
|
|
QUEUE_OWNER = 0,
|
|
QUEUE_USER
|
|
}QUEUE_TYPE_e;
|
|
#define MAKE_ALIGNX(size, align) ((size+align-1)&(~(align-1)))
|
|
|
|
/*****************************************************************************/
|
|
|
|
//禁止拷贝基类
|
|
class noncopyable {
|
|
protected:
|
|
noncopyable() {}
|
|
~noncopyable() {}
|
|
private:
|
|
//禁止拷贝
|
|
noncopyable(const noncopyable &that) = delete;
|
|
noncopyable(noncopyable &&that) = delete;
|
|
noncopyable &operator=(const noncopyable &that) = delete;
|
|
noncopyable &operator=(noncopyable &&that) = delete;
|
|
};
|
|
|
|
class IMemoryBlock : public noncopyable
|
|
{
|
|
public:
|
|
~IMemoryBlock();
|
|
using IMemoryBlockUsedList = std::list<std::string >;
|
|
IMemoryBlock(AdditionalImp & imp) noexcept;
|
|
|
|
AdditionalImp* data() noexcept;
|
|
void useradd(const std::string &owner);
|
|
void useradd();
|
|
void usermlus(const std::string &owner);
|
|
void usermlus();
|
|
int used() const;
|
|
std::string owner() const;
|
|
void setOwner(const std::string &owner);
|
|
void reset();
|
|
void dump();
|
|
int release(const std::string &queuName);
|
|
void dumpHistory(const std::string str, const std::string &owner);
|
|
|
|
inline GD_VIDEO_FRAME_S* yuv(){
|
|
return _imp->data<GD_VIDEO_FRAME_S*>("YUV");
|
|
}
|
|
inline GD_VIDEO_FRAME_S* rgb(){
|
|
return _imp->data<GD_VIDEO_FRAME_S*>("RGB");
|
|
}
|
|
inline GD_VIDEO_FRAME_S* original(){
|
|
return _imp->data<GD_VIDEO_FRAME_S*>("original");
|
|
}
|
|
/* data */
|
|
int use_cnt = 0; //user count
|
|
std::string _owner;
|
|
unsigned long long _using_timestamp = 0;
|
|
AdditionalImp* _imp;
|
|
|
|
private:
|
|
IMemoryBlockUsedList _used_list;
|
|
IMemoryBlockUsedList _history_used_list;
|
|
std::mutex _mutex;
|
|
};
|
|
|
|
class IMemoryPool
|
|
{
|
|
public:
|
|
using IMemoryPoolPtr = std::queue<IMemoryBlock *>;
|
|
using IMemoryMap = std::map<std::string, void *>;
|
|
using IMemoryPoolIterator = std::map<std::string, void *>::iterator;
|
|
|
|
static IMemoryPool* getInstance();
|
|
|
|
//
|
|
int addBuffer(AdditionalImp &imp);
|
|
int size();
|
|
|
|
IMemoryBlock* dequeue(std::string &owner);
|
|
int queue(IMemoryBlock* data);
|
|
void* getQueue(const std::string &name);
|
|
int restoreQueue(const std::string &name, void *queue);
|
|
|
|
void queuePush(IMemoryBlock* data, const std::string &queuName);
|
|
int release(IMemoryBlock* data, const std::string &queuName);
|
|
void dump();
|
|
|
|
static int CalcInfo(GD_VIDEO_FRAME_S *frame);
|
|
static int blockMalloc(GD_VIDEO_FRAME_S *frame);
|
|
private:
|
|
IMemoryPool();
|
|
~IMemoryPool() {}
|
|
void recycleThread();
|
|
|
|
int _block_cnt;
|
|
IMemoryPoolPtr _queue_ptr;
|
|
IMemoryPoolPtr _queue_ptr_dump;
|
|
IMemoryMap _queue_map;
|
|
std::mutex _mutex;
|
|
|
|
};
|
|
|
|
class IMemoryQueue{
|
|
public:
|
|
using IMemoryQueuePtr = std::queue<IMemoryBlock *>;
|
|
|
|
void queueInit(const std::string &name, const int cnt, QUEUE_TYPE_e type = QUEUE_USER);
|
|
QUEUE_TYPE_e fifoType() const ;
|
|
int readyRead(IMemoryQueue *queue);
|
|
|
|
IMemoryBlock* getFrame(IMemoryQueue *queue, const int timeout = 0);
|
|
IMemoryBlock* getFrame(const int timeout = 0);
|
|
|
|
int sendFrame(IMemoryBlock *buffer);
|
|
int sendFrame(IMemoryQueue *queue, IMemoryBlock *buffer);
|
|
int sendFrame(const std::string &name, IMemoryBlock *buffer);
|
|
|
|
int releaseFrame(IMemoryBlock *buffer);
|
|
std::string queueName() const ;
|
|
unsigned int size();
|
|
void clearQueue();
|
|
int push(IMemoryBlock *buffer);
|
|
IMemoryBlock* pull();
|
|
void dump();
|
|
|
|
private:
|
|
IMemoryQueuePtr _buffer_queue;
|
|
unsigned int _max_cnt;
|
|
std::string _queue_name;
|
|
std::mutex _mutex;
|
|
QUEUE_TYPE_e _type;
|
|
std::condition_variable _cond;
|
|
};
|
|
}
|
|
|
|
#endif
|