Пример #1
0
/** Write bytes to the buffer
 * @param data pointer to the data to be written
 * @param bytes number of bytes to be written
 * @return true if was successful, otherwise false
 */
bool CircularBuffer::Write(const void * data, size_t bytes)
{
	// If buffer B exists, write to it.
	if( m_regionBPointer != NULL )
	{
		if( GetBFreeSpace() < bytes )
			return false;

		memcpy(&m_regionBPointer[m_regionBSize], data, bytes);
		m_regionBSize += bytes;
		return true;
	}

	// Otherwise, write to buffer A, or initialize buffer B depending on which has more space.
	if( GetAFreeSpace() < GetSpaceBeforeA() )
	{
		AllocateB();
		if( GetBFreeSpace() < bytes )
			return false;

		memcpy(&m_regionBPointer[m_regionBSize], data, bytes);
		m_regionBSize += bytes;
		return true;
	}
	else
	{
		if( !m_regionAPointer || GetAFreeSpace() < bytes )
			return false;

		memcpy(&m_regionAPointer[m_regionASize], data, bytes);
		m_regionASize += bytes;
		return true;
	}
}
Пример #2
0
/** Returns the number of available bytes left.
 */
size_t CircularBuffer::GetSpace()
{
	if( m_regionBPointer != NULL )
		return GetBFreeSpace();
	else
	{
		// would allocating buffer B get us more data?
		if( GetAFreeSpace() < GetSpaceBeforeA() )
		{
			AllocateB();
			return GetBFreeSpace();
		}

		// or not?
		return GetAFreeSpace();
	}
}
Пример #3
0
bool CircularBuffer::Write(const char* data, size_t bytes)
{
	assert(mBuffer != nullptr);

	/// Read와 반대로 B가 있다면 B영역에 먼저 쓴다
	if (mBRegionPointer != nullptr)
	{
		if (GetBFreeSpace() < bytes)
			return false;

		memcpy(mBRegionPointer + mBRegionSize, data, bytes);
		mBRegionSize += bytes;

		return true;
	}

	/// A영역보다 다른 영역의 용량이 더 클 경우 그 영역을 B로 설정하고 기록
	if (GetAFreeSpace() < GetSpaceBeforeA())
	{
		AllocateB();

		if (GetBFreeSpace() < bytes)
			return false;

		memcpy(mBRegionPointer + mBRegionSize, data, bytes);
		mBRegionSize += bytes;

		return true;
	}
	/// A영역이 더 크면 당연히 A에 쓰기
	else
	{
		if (GetAFreeSpace() < bytes)
			return false;

		memcpy(mARegionPointer + mARegionSize, data, bytes);
		mARegionSize += bytes;

		return true;
	}
}