checkpoint debugging of stream buffer.
This commit is contained in:
parent
eb2a202834
commit
76269a48c1
|
@ -38,64 +38,84 @@ const void* CStreamBuffer::peek(UInt32 n)
|
||||||
|
|
||||||
void CStreamBuffer::pop(UInt32 n)
|
void CStreamBuffer::pop(UInt32 n)
|
||||||
{
|
{
|
||||||
|
// discard all chunks if n is greater than or equal to m_size
|
||||||
|
if (n >= m_size) {
|
||||||
|
m_size = 0;
|
||||||
|
m_chunks.clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// update size
|
||||||
m_size -= n;
|
m_size -= n;
|
||||||
|
|
||||||
// discard chunks until more than n bytes would've been discarded
|
// discard chunks until more than n bytes would've been discarded
|
||||||
ChunkList::iterator scan = m_chunks.begin();
|
ChunkList::iterator scan = m_chunks.begin();
|
||||||
while (scan->size() <= n && scan != m_chunks.end()) {
|
assert(scan != m_chunks.end());
|
||||||
|
while (scan->size() <= n) {
|
||||||
n -= scan->size();
|
n -= scan->size();
|
||||||
scan = m_chunks.erase(scan);
|
scan = m_chunks.erase(scan);
|
||||||
|
assert(scan != m_chunks.end());
|
||||||
}
|
}
|
||||||
|
assert(n > 0);
|
||||||
|
|
||||||
// if there's anything left over then remove it from the head chunk.
|
// remove left over bytes from the head chunk
|
||||||
// if there's no head chunk then we're already empty.
|
|
||||||
if (scan == m_chunks.end()) {
|
|
||||||
m_size = 0;
|
|
||||||
}
|
|
||||||
else if (n > 0) {
|
|
||||||
scan->erase(scan->begin(), scan->begin() + n);
|
scan->erase(scan->begin(), scan->begin() + n);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "CLog.h"
|
||||||
void CStreamBuffer::write(
|
void CStreamBuffer::write(
|
||||||
const void* vdata, UInt32 n)
|
const void* vdata, UInt32 n)
|
||||||
{
|
{
|
||||||
assert(vdata != NULL);
|
assert(vdata != NULL);
|
||||||
|
|
||||||
|
// ignore if no data, otherwise update size
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
log((CLOG_DEBUG "### %p buffering %d bytes onto %d bytes", this, n, m_size));
|
||||||
m_size += n;
|
m_size += n;
|
||||||
|
|
||||||
// cast data to bytes
|
// cast data to bytes
|
||||||
const UInt8* data = reinterpret_cast<const UInt8*>(vdata);
|
const UInt8* data = reinterpret_cast<const UInt8*>(vdata);
|
||||||
|
if (n > 1000)
|
||||||
|
log((CLOG_DEBUG "### %p src: %u %u %u %u %u %u %u %u", this,
|
||||||
|
data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]));
|
||||||
|
|
||||||
// point to last chunk if it has space, otherwise append an empty chunk
|
// point to last chunk if it has space, otherwise append an empty chunk
|
||||||
ChunkList::iterator scan = m_chunks.end();
|
ChunkList::iterator scan = m_chunks.end();
|
||||||
if (scan != m_chunks.begin()) {
|
if (scan != m_chunks.begin()) {
|
||||||
|
log((CLOG_DEBUG "### %p at least one chunk", this));
|
||||||
--scan;
|
--scan;
|
||||||
if (scan->size() >= kChunkSize)
|
if (scan->size() >= kChunkSize)
|
||||||
|
{
|
||||||
|
log((CLOG_DEBUG "### %p last chunk full", this));
|
||||||
++scan;
|
++scan;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (scan == m_chunks.end()) {
|
if (scan == m_chunks.end()) {
|
||||||
scan = m_chunks.insert(scan);
|
log((CLOG_DEBUG "### %p append chunk", this));
|
||||||
|
scan = m_chunks.insert(scan, Chunk());
|
||||||
}
|
}
|
||||||
|
|
||||||
// append data in chunks
|
// append data in chunks
|
||||||
while (n > 0) {
|
while (n > 0) {
|
||||||
// choose number of bytes for next chunk
|
// choose number of bytes for next chunk
|
||||||
|
assert(scan->size() <= kChunkSize);
|
||||||
UInt32 count = kChunkSize - scan->size();
|
UInt32 count = kChunkSize - scan->size();
|
||||||
if (count > n)
|
if (count > n)
|
||||||
count = n;
|
count = n;
|
||||||
|
|
||||||
// transfer data
|
// transfer data
|
||||||
|
log((CLOG_DEBUG "### %p append %d bytes onto last chunk", this, count));
|
||||||
scan->insert(scan->end(), data, data + count);
|
scan->insert(scan->end(), data, data + count);
|
||||||
n -= count;
|
n -= count;
|
||||||
data += count;
|
data += count;
|
||||||
|
|
||||||
// append another empty chunk if we're not done yet
|
// append another empty chunk if we're not done yet
|
||||||
if (n > 0) {
|
if (n > 0) {
|
||||||
scan = m_chunks.insert(scan);
|
++scan;
|
||||||
|
scan = m_chunks.insert(scan, Chunk());
|
||||||
|
log((CLOG_DEBUG "### %p append chunk2", this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue