Click to See Complete Forum and Search --> : Cyclic Buffer


Kohinoor24
June 21st, 2002, 05:07 AM
I have a cyclic Buffer,of size say 10000.
The following is the class which Iam putting into the buffer.

template <class B>class NgDocData
{
Public:

/* Constructor */
NgDocData(CycleBuffer& b);

protected:

list<NgSubDocData<B>> m_ngSubDocDataList;// Iam putting elements into the list.

};
Iam first allocating memory for the buffer for this object by calling new on it like:

The Cycle Buffer class with its functions are below:

*m_ngDocumentData = new NgDocData< SimpleBuffer>( CycleBuffer );
[1]so what does "new"actually do.Does it allocates memory for the private & protected members of a class.
[2]What does sizeof(NgDocData< SimpleBuffer>) means.
Does it mean the size of all private & protected members of the class
NgDocData< SimpleBuffer>.
[3]so If iam allocating memory for the class like this & When I push something into the list,does the memory allocated,will be taken over by
the object pushed into the list.

It calls the allocatebytes() function of The cyclic buffer& allocate memory for that many bytes .

Then I have a "ReassignBuffer()" function which increments the read ptr.
I increment the read ptr,by calculating the bytes of the object written into the buffer & incrementing the read ptr by that many bytes.

Now the Question comes,as it is a cyclic buffer,sometimes some bytes of the object will be written to the end of the buffer & some will be written to the beginning of the buffer.

so my question here is, as we are creating the object like this

*m_ngDocumentData = new NgDocData< SimpleBuffer>( CycleBuffer );
Memory is allocated for the size of the object,Which I guess is for the list & the objects it holds.only the bytes of the object here are distributed at the end & beginning of the buffer.Is that a problem.Will the corr: object for whom the bytes are allocated like this,be put into the list.

please answer questions marked [].
///////////////////////////////////////////////////////////////////////////////////////////

class CycleBuffer:public Buffer
{


public:



CycleBuffer(size_type bufferSize):Buffer(bufferSize)
{
m_pFree = m_pBuffer;// Decalared in base class Buffer as protected member
m_pRead = m_pBuffer;
m_pMutex = NULL;
m_Counter = 0;
m_BytesInBuffer = 0;
m_Size = 0;

}
/*Here num is the size of object put*/
char* allocateBytes(size_type num)
throw (DocGenEx)
{
AcquireLock();
size_type BytesToWrite = ( m_bufferSize - m_BytesInBuffer) > num ? num : ( m_bufferSize - m_BytesInBuffer);

if ( BytesToWrite )
{ /*Incrementing my Writer ptr,m_pFree*/
if(BytesToWrite <= max_size())
{
char* pAllocated = m_pFree;
m_pFree += BytesToWrite;
/*Calculating size of a single object which is put into the list*/
m_Size += BytesToWrite;
return pAllocated;
}
else
{
unsigned int ulBytesInWrap = (BytesToWrite - max_size());
m_pFree = m_pBuffer + ulBytesInWrap;
m_Size += BytesToWrite;
}
}
m_BytesInBuffer += BytesToWrite;

ReleaseLock();

throw DocGenEx(EDG_EX_OUT_OF_MEMORY, NULL, __FILE__, __LINE__);
}
void deallocate(void* p, size_type num)
{
return;
}
/*This Function Increments the read ptr as data is read from the buffer*/
/* */
void ReassignBuffer()
{
AcquireLock();
list<unsigned int>::iterator it = m_DocSize.begin();
unsigned int BytesToRead = *it;
if (BytesToRead)
{
unsigned int ulLengthToEnd = m_bufferSize - (m_pRead - m_pBuffer);

if ( ulLengthToEnd >= BytesToRead)
{

m_pRead = m_pBuffer + (((m_pRead + BytesToRead) - m_pBuffer) % m_bufferSize);
}
else
{
unsigned int ulBytesInWrap = BytesToRead - ulLengthToEnd;
m_pRead =m_pBuffer + ulBytesInWrap;
}
}
m_BytesInBuffer -= BytesToRead;
m_DocSize.pop_front();
ReleaseLock();
}

size_type max_size() const
{
return m_pBuffer + m_bufferSize - m_pFree;
}
void clear()
{
m_pFree = m_pBuffer;
}
}
void ResetDocBytes()
{
m_Size = 0;
}
void SetDocBytes()
{
m_DocSize.push_back(m_Size);
//return m_Size;

}
inline void SetCounter(int temp){ m_Counter = temp;}
inline int GetCounter(){ return m_Counter;}


protected:

char* m_pFree; // Write Ptr.
char* m_pRead; // Read Ptr
unsigned int m_BytesInBuffer; // Bytes in The Buffer
int m_Counter;// Count of objects in the buffer.
unsigned int m_Size; // Incremented till a subdocument is written to the list & then when a new subdocument starts,assigning it 0;
list<unsigned int> m_DocSize;

};
///////////////////////////////////////////////////////////////////////////////////////////




Thanks in advance