Audacity 3.2.0
Classes | Public Types | Public Member Functions | Private Types | Private Attributes | Static Private Attributes | List of all members
MemoryStream Class Referencefinal

A low overhead memory stream with O(1) append, low heap fragmentation and a linear memory view. More...

#include <MemoryStream.h>

Collaboration diagram for MemoryStream:
[legend]

Classes

struct  Chunk
 
struct  Iterator
 

Public Types

using StreamData = std::vector< uint8_t >
 
using StreamChunk = std::pair< const void *, size_t >
 

Public Member Functions

 MemoryStream ()=default
 
 MemoryStream (MemoryStream &&)=default
 
void Clear ()
 
void AppendByte (char data)
 
void AppendData (const void *data, const size_t length)
 
const void * GetData () const
 
const size_t GetSize () const noexcept
 
bool IsEmpty () const noexcept
 
Iterator begin () const
 
Iterator end () const
 

Private Types

using ChunksList = std::list< Chunk >
 

Private Attributes

ChunksList mChunks
 
StreamData mLinearData
 
size_t mDataSize { 0 }
 

Static Private Attributes

static constexpr size_t ChunkSize
 

Detailed Description

A low overhead memory stream with O(1) append, low heap fragmentation and a linear memory view.

wxMemoryBuffer always appends 1Kb to the end of the buffer, causing severe performance issues and significant heap fragmentation. There is no possibility to control the increment value.

std::vector doubles its memory size which can be problematic for large projects as well. Not as bad as wxMemoryBuffer though.

Definition at line 30 of file lib-utility/memorystream.h.

Member Typedef Documentation

◆ ChunksList

using MemoryStream::ChunksList = std::list<Chunk>
private

Definition at line 51 of file lib-utility/memorystream.h.

◆ StreamChunk

using MemoryStream::StreamChunk = std::pair<const void*, size_t>

Definition at line 34 of file lib-utility/memorystream.h.

◆ StreamData

using MemoryStream::StreamData = std::vector<uint8_t>

Definition at line 33 of file lib-utility/memorystream.h.

Constructor & Destructor Documentation

◆ MemoryStream() [1/2]

MemoryStream::MemoryStream ( )
default

◆ MemoryStream() [2/2]

MemoryStream::MemoryStream ( MemoryStream &&  )
default

Member Function Documentation

◆ AppendByte()

void MemoryStream::AppendByte ( char  data)

Definition at line 22 of file lib-utility/memorystream.cpp.

23{
24 AppendData(&data, 1);
25}
void AppendData(const void *data, const size_t length)

References AppendData().

Referenced by ProjectSerializer::EndTag(), ProjectSerializer::ProjectSerializer(), ProjectSerializer::StartTag(), ProjectSerializer::Write(), ProjectSerializer::WriteAttr(), ProjectSerializer::WriteData(), XMLUtf8BufferWriter::WriteEscaped(), and ProjectSerializer::WriteName().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AppendData()

void MemoryStream::AppendData ( const void *  data,
const size_t  length 
)

Definition at line 27 of file lib-utility/memorystream.cpp.

28{
29 if (mChunks.empty())
30 mChunks.emplace_back();
31
32 StreamChunk dataView = { data, length };
33
34 while (mChunks.back().Append(dataView) > 0)
35 mChunks.emplace_back();
36
37 mDataSize += length;
38}
std::pair< const void *, size_t > StreamChunk

References mChunks, and mDataSize.

Referenced by AppendByte(), ProjectSerializer::ProjectSerializer(), XMLUtf8BufferWriter::Write(), ProjectSerializer::Write(), ProjectSerializer::WriteAttr(), anonymous_namespace{ProjectSerializer.cpp}::WriteBigEndian(), ProjectSerializer::WriteData(), anonymous_namespace{ProjectSerializer.cpp}::WriteLittleEndian(), and ProjectSerializer::WriteName().

Here is the caller graph for this function:

◆ begin()

MemoryStream::Iterator MemoryStream::begin ( ) const

Definition at line 105 of file lib-utility/memorystream.cpp.

106{
107 return Iterator(this, true);
108}

Referenced by GetData().

Here is the caller graph for this function:

◆ Clear()

void MemoryStream::Clear ( )

Definition at line 15 of file lib-utility/memorystream.cpp.

16{
17 mChunks = {};
18 mLinearData = {};
19 mDataSize = 0;
20}

References mChunks, mDataSize, and mLinearData.

◆ end()

MemoryStream::Iterator MemoryStream::end ( ) const

Definition at line 110 of file lib-utility/memorystream.cpp.

111{
112 return Iterator(this, false);
113}

Referenced by GetData().

Here is the caller graph for this function:

◆ GetData()

const void * MemoryStream::GetData ( ) const

Definition at line 40 of file lib-utility/memorystream.cpp.

41{
42 if (!mChunks.empty())
43 {
44 const size_t desiredSize = GetSize();
45
46 mLinearData.reserve(desiredSize);
47
48 for (const Chunk& chunk : mChunks)
49 {
50 auto begin = chunk.Data.begin();
51 auto end = begin + chunk.BytesUsed;
52
53 mLinearData.insert(mLinearData.end(), begin, end);
54 }
55
56 mChunks = {};
57 }
58
59 return mLinearData.data();
60}
const size_t GetSize() const noexcept
Iterator begin() const

References begin(), end(), GetSize(), mChunks, and mLinearData.

Here is the call graph for this function:

◆ GetSize()

const size_t MemoryStream::GetSize ( ) const
noexcept

Definition at line 62 of file lib-utility/memorystream.cpp.

63{
64 return mDataSize;
65}

References mDataSize.

Referenced by GetData(), ProjectSerializer::IsEmpty(), and ProjectFileIO::WriteDoc().

Here is the caller graph for this function:

◆ IsEmpty()

bool MemoryStream::IsEmpty ( ) const
noexcept

Definition at line 100 of file lib-utility/memorystream.cpp.

101{
102 return mDataSize == 0;
103}

References mDataSize.

Member Data Documentation

◆ ChunkSize

constexpr size_t MemoryStream::ChunkSize
staticconstexprprivate
Initial value:
=
1024 * 1024 -
2 * sizeof(void*) -
sizeof(size_t)

Definition at line 37 of file lib-utility/memorystream.h.

Referenced by MemoryStream::Chunk::Append().

◆ mChunks

ChunksList MemoryStream::mChunks
mutableprivate

Definition at line 99 of file lib-utility/memorystream.h.

Referenced by AppendData(), Clear(), and GetData().

◆ mDataSize

size_t MemoryStream::mDataSize { 0 }
private

Definition at line 102 of file lib-utility/memorystream.h.

Referenced by AppendData(), Clear(), GetSize(), and IsEmpty().

◆ mLinearData

StreamData MemoryStream::mLinearData
mutableprivate

Definition at line 100 of file lib-utility/memorystream.h.

Referenced by Clear(), and GetData().


The documentation for this class was generated from the following files: