⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buffer.cpp

📁 用于仿真系统中多个仿真对象间的内存管理
💻 CPP
字号:
// Buffer.cpp: implementation of the Buffer class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Buffer.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Buffer::Buffer(long size,long thresh,MemoryPool* pool):
refCount(1)
{
	datapoint = 0;
	datahead = 0;
	bufsize = size;
	if(size > thresh)
	{
		strategy = new DataStrategy(pool);
	}else
	{
		strategy = new MsgStrategy(pool);
	}
}

Buffer::~Buffer()
{
	delete strategy;
}

Buffer::Buffer(const Buffer& rhs)
{

}

float* Buffer::GetDataBuf()
{
	if(datahead != 0)//已分配内存
		return datahead;
	
	datapoint = strategy->GetMemFromPool();
	datahead = datapoint;
	return datapoint;
}

void Buffer::AddReference()
{
	++refCount;
}

void Buffer::ReleaseReference()
{
	if(--refCount == 0)
	{
		strategy->ReturnMemToPool(datahead);	
		delete this; 
	}
}

float Buffer::ReadSample()
{
	float sample =  *datapoint;
	datapoint++;
	return sample;
}

void Buffer::WriteSample(float sample)
{
	*datapoint = sample;
	datapoint++;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -