?? pos
void CBuffer::StreamSet(int pos)
{
readpos = 0;
writepos = 0;
}
missing
if(data == NULL) return at every function that uses the buffer
CBuffer::CBuffer()
{
BuffSize = 30;
data = (char*)malloc(BuffSize);
count = 0;
readpos = 0;
writepos = 0;
}
void CBuffer::clear()
{
if(BuffSize > 30)
{
free(data);
BuffSize = 30;
data =(char*) malloc(BuffSize);
}
count = 0;
readpos = 0;
writepos = 0;
}
void CBuffer::StreamWrite(void *in, int size)
{
if(writepos+size >= BuffSize)
{
BuffSize = writepos+size + 30;
if((data = (char*)realloc(data, BuffSize)) == NULL)return;
}
memcpy(data+writepos, in, size);
writepos += size;
if(writepos > count)count = writepos;
}
frankly the buffer size of 30 is a little prone to fragmentation ans slowness. you should allocate a decent size from the start, at least in the KB range and in factor of 2^n , say 8K, 8192
as you write, you reallocate by another 8192 bytes or a few 8192 bytes according to size (if size is larger than chunk).
if(writepos+size >= BuffSize)
{
Buffsize += ceil(size/8192) * 8192
re-alloc
}
when you clear, you could just reset the memory index
count = 0;
readpos = 0;
writepos = 0;
and memset to 0 the size of BuffSize (probably you can keep the garbage in the buffer and no need to memset)
you would gradually have adjusted to the size of memory the application needs, you would allocate in a memory friendlily way, you would have just a few reallocs
instead of malloc and realloc and free, you can use the windows method, include <windowsx.h> (I think) and use GlobalAllocPtr() , GlobalReallocPtr and GLobalFreePtr