在C++开发的过程中难免经常使用的一些new 和 delete 对内存操作,这样其实会整个系统运行 增加开销和内存碎片,可以在初始化的时候 对这些内存进行申请,在整个程序运行结束的时候再进行释放。
例如:
#define MAX_CAMERA_BUFFER_SIZE 1000
class PktPool {
public:
AVPacket *pkt = NULL;
int serial=0;
int index = 0;
static int curIndex;
PktPool() {
pkt = av_packet_alloc();
};
~PktPool(){
av_packet_free(&pkt);
}
PktPool &operator=(AVPacket *data) {
if(pkt != NULL){
av_packet_move_ref(this->pkt, data);
}
return *this;
}
PktPool &operator=(PktPool &data) {
av_packet_move_ref(this->pkt, data.pkt);
return *this;
}
void free(){
av_packet_unref(this->pkt);
}
static PktPool *PktPoolPtr;
static PktPool *GetPktPool() {
curIndex = (curIndex + 1) % MAX_CAMERA_BUFFER_SIZE;
PktPoolPtr[curIndex].index = curIndex;
return &PktPoolPtr[curIndex];
}
};
//对 PktPoolPtr 申请MAX_CAMERA_BUFFER_SIZE大小的内存
PktPool* PktPool::PktPoolPtr = new PktPool[MAX_CAMERA_BUFFER_SIZE];
int PktPool::curIndex = 0;
对 PktPoolPtr 申请MAX_CAMERA_BUFFER_SIZE个PktPool的内存,通过PktPool ::GetPktPool获取到最新的偏移内存,index为内存偏移下标,只要通过这个index就能访问到对应的内存,就能一直循环使用这个类的数组内存。
PktNote作用:记录对应的index节点。
class PktNode{
public:
int index = 0;
int64_t pts = 0;
int type = 0;
PktNode(){
};
PktNode(int Index ,int64_t Pts,int Type ){
this->index=Index;
this->pts=Pts;
this->type=Type;
};
PktNode &operator=(PktNode &data) {
this->index=data.index;
this->pts=data.pts;
this->type=data.type;
return *this;
}
};
StreamDataPool 类似于队列先进先出的原理,用于存放PktNode
template<class T>
class StreamDataPool {
public:
T *buf = NULL;
int mSize;
int mWriteOps = 0;
int mReadOps = 0;
bool isVaild = false;
int nb_packets;
int size;
int64_t duration;
int abort_request;
int serial;
SDL_mutex *mutex;
SDL_cond *cond;
string mName;
bool writeData(T *data) {
if (buf == NULL) return false;
if (((mWriteOps + 1) % mSize) == mReadOps) {
printf("writeOps false %d %d \n", mWriteOps, mReadOps);
return false;
}
buf[mWriteOps] = *data;
mWriteOps = (mWriteOps + 1) % mSize;
return true;
}
T *readData() {
if (buf == NULL) return NULL;
if (mReadOps == mWriteOps) {
printf("readData false %d %d \n", mWriteOps, mReadOps);
return NULL;
}
int readOps = mReadOps;
mReadOps = (mReadOps + 1) % mSize;
return &buf[readOps];
}
list<void *> ReadBufferList(bool isOnlyRead) {
list<void *> dataList;
if (buf == NULL) return dataList;
int readOps = mReadOps;
while (readOps != mWriteOps) {
dataList.push_back(&buf[readOps]);
readOps = (readOps + 1) % mSize;
}
if (!isOnlyRead) {
mReadOps = readOps;
}
return dataList;
}
void pop() {
if (mReadOps == mWriteOps) return;
mReadOps = (mReadOps + 1) % mSize;
}
void setSize(int size) {
mSize = size;
mWriteOps = 0;
mReadOps = 0;
buf = new T[size];
}
void setSize(int size, string name) {
mSize = size;
mWriteOps = 0;
mReadOps = 0;
serial = 0;
buf = new T[size];
mName = name;
}
void destroy() {
isVaild = false;
if (buf != NULL) {
delete buf;
buf = NULL;
}
}
bool isFull(){
if(mWriteOps - mReadOps == mSize) return true;
}
bool isEnty(){
if(mReadOps - mWriteOps) return true;
return false;
}
void clean(){
mWriteOps = 0;
mReadOps = 0;
}
};
下一篇实战:
https://blog.csdn.net/yinsui1839/article/details/127644090?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22127644090%22%2C%22source%22%3A%22yinsui1839%22%7D