Audacity 3.2.0
Public Member Functions | Protected Attributes | List of all members
Steinberg::MemoryStream Class Reference

#include <memorystream.h>

Inheritance diagram for Steinberg::MemoryStream:
[legend]
Collaboration diagram for Steinberg::MemoryStream:
[legend]

Public Member Functions

 MemoryStream ()
 
 MemoryStream (void *memory, TSize memorySize)
 reuse a given memory without getting ownership More...
 
virtual ~MemoryStream ()
 
tresult PLUGIN_API read (void *buffer, int32 numBytes, int32 *numBytesRead) SMTG_OVERRIDE
 
tresult PLUGIN_API write (void *buffer, int32 numBytes, int32 *numBytesWritten) SMTG_OVERRIDE
 
tresult PLUGIN_API seek (int64 pos, int32 mode, int64 *result) SMTG_OVERRIDE
 
tresult PLUGIN_API tell (int64 *pos) SMTG_OVERRIDE
 
TSize getSize () const
 returns the current memory size More...
 
void setSize (TSize size)
 set the memory size, a realloc will occur if memory already used More...
 
char * getData () const
 returns the memory pointer More...
 
char * detachData ()
 returns the memory pointer and give up ownership More...
 
bool truncate ()
 realloc to the current use memory size if needed More...
 
bool truncateToCursor ()
 truncate memory at current cursor position More...
 

Protected Attributes

char * memory
 
TSize memorySize
 
TSize size
 
int64 cursor
 
bool ownMemory
 
bool allocationError
 

Detailed Description

Memory based Stream for IBStream implementation (using malloc).

Definition at line 47 of file lib-vst3/memorystream.h.

Constructor & Destructor Documentation

◆ MemoryStream() [1/2]

MemoryStream::MemoryStream ( )

◆ MemoryStream() [2/2]

MemoryStream::MemoryStream ( void *  memory,
TSize  memorySize 
)

reuse a given memory without getting ownership

Definition at line 48 of file lib-vst3/memorystream.cpp.

49: memory ((char*)data)
50, memorySize (length)
51, size (length)
52, cursor (0)
53, ownMemory (false)
54, allocationError (false)
55{
56 FUNKNOWN_CTOR
57}

◆ ~MemoryStream()

MemoryStream::~MemoryStream ( )
virtual

Definition at line 72 of file lib-vst3/memorystream.cpp.

73{
74 if (ownMemory && memory)
75 ::free (memory);
76
77 FUNKNOWN_DTOR
78}
void free(void *ptr)
Definition: VectorOps.h:34

References staffpad::vo::free(), memory, and ownMemory.

Here is the call graph for this function:

Member Function Documentation

◆ detachData()

char * MemoryStream::detachData ( )

returns the memory pointer and give up ownership

Definition at line 267 of file lib-vst3/memorystream.cpp.

268{
269 if (ownMemory)
270 {
271 char* result = memory;
272 memory = nullptr;
273 memorySize = 0;
274 size = 0;
275 cursor = 0;
276 return result;
277 }
278 return nullptr;
279}

References cursor, memory, memorySize, ownMemory, and size.

◆ getData()

char * MemoryStream::getData ( ) const

returns the memory pointer

Definition at line 261 of file lib-vst3/memorystream.cpp.

262{
263 return memory;
264}

References memory.

◆ getSize()

TSize MemoryStream::getSize ( ) const

returns the current memory size

Definition at line 190 of file lib-vst3/memorystream.cpp.

191{
192 return size;
193}

References size.

◆ read()

tresult PLUGIN_API MemoryStream::read ( void *  buffer,
int32  numBytes,
int32 *  numBytesRead 
)

Definition at line 81 of file lib-vst3/memorystream.cpp.

82{
83 if (memory == nullptr)
84 {
86 return kOutOfMemory;
87 numBytes = 0;
88 }
89 else
90 {
91 // Does read exceed size ?
92 if (cursor + numBytes > size)
93 {
94 int32 maxBytes = int32 (size - cursor);
95
96 // Has length become zero or negative ?
97 if (maxBytes <= 0)
98 {
99 cursor = size;
100 numBytes = 0;
101 }
102 else
103 numBytes = maxBytes;
104 }
105
106 if (numBytes)
107 {
108 memcpy (data, &memory[cursor], static_cast<size_t> (numBytes));
109 cursor += numBytes;
110 }
111 }
112
113 if (numBytesRead)
114 *numBytesRead = numBytes;
115
116 return kResultTrue;
117}

References allocationError, cursor, memory, and size.

◆ seek()

tresult PLUGIN_API MemoryStream::seek ( int64  pos,
int32  mode,
int64 *  result 
)

Definition at line 154 of file lib-vst3/memorystream.cpp.

155{
156 switch (mode)
157 {
158 case kIBSeekSet:
159 cursor = pos;
160 break;
161 case kIBSeekCur:
162 cursor = cursor + pos;
163 break;
164 case kIBSeekEnd:
165 cursor = size + pos;
166 break;
167 }
168
169 if (ownMemory == false)
170 if (cursor > memorySize)
172
173 if (result)
174 *result = cursor;
175
176 return kResultTrue;
177}

References cursor, memorySize, ownMemory, and size.

Referenced by VST3Wrapper::InitializeComponents().

Here is the caller graph for this function:

◆ setSize()

void MemoryStream::setSize ( TSize  size)

set the memory size, a realloc will occur if memory already used

Definition at line 196 of file lib-vst3/memorystream.cpp.

197{
198 if (s <= 0)
199 {
200 if (ownMemory && memory)
201 free (memory);
202
203 memory = nullptr;
204 memorySize = 0;
205 size = 0;
206 cursor = 0;
207 return;
208 }
209
210 TSize newMemorySize = (((Max (memorySize, s) - 1) / kMemGrowAmount) + 1) * kMemGrowAmount;
211 if (newMemorySize == memorySize)
212 {
213 size = s;
214 return;
215 }
216
217 if (memory && ownMemory == false)
218 {
219 allocationError = true;
220 return;
221 }
222
223 ownMemory = true;
224 char* newMemory = nullptr;
225
226 if (memory)
227 {
228 newMemory = (char*)realloc (memory, (size_t)newMemorySize);
229 if (newMemory == nullptr && newMemorySize > 0)
230 {
231 newMemory = (char*)malloc ((size_t)newMemorySize);
232 if (newMemory)
233 {
234 memcpy (newMemory, memory, (size_t)Min (newMemorySize, memorySize));
235 free (memory);
236 }
237 }
238 }
239 else
240 newMemory = (char*)malloc ((size_t)newMemorySize);
241
242 if (newMemory == nullptr)
243 {
244 if (newMemorySize > 0)
245 allocationError = true;
246
247 memory = nullptr;
248 memorySize = 0;
249 size = 0;
250 cursor = 0;
251 }
252 else
253 {
254 memory = newMemory;
255 memorySize = newMemorySize;
256 size = s;
257 }
258}
static const TSize kMemGrowAmount

References allocationError, cursor, staffpad::vo::free(), Steinberg::kMemGrowAmount, memory, memorySize, ownMemory, and size.

Referenced by write().

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

◆ tell()

tresult PLUGIN_API MemoryStream::tell ( int64 *  pos)

Definition at line 180 of file lib-vst3/memorystream.cpp.

181{
182 if (!pos)
183 return kInvalidArgument;
184
185 *pos = cursor;
186 return kResultTrue;
187}

References cursor.

◆ truncate()

bool MemoryStream::truncate ( )

realloc to the current use memory size if needed

Definition at line 282 of file lib-vst3/memorystream.cpp.

283{
284 if (ownMemory == false)
285 return false;
286
287 if (memorySize == size)
288 return true;
289
291
292 if (memorySize == 0)
293 {
294 if (memory)
295 {
296 free (memory);
297 memory = nullptr;
298 }
299 }
300 else
301 {
302 if (memory)
303 {
304 char* newMemory = (char*)realloc (memory, (size_t)memorySize);
305 if (newMemory)
306 memory = newMemory;
307 }
308 }
309 return true;
310}

References staffpad::vo::free(), memory, memorySize, ownMemory, and size.

Referenced by truncateToCursor().

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

◆ truncateToCursor()

bool MemoryStream::truncateToCursor ( )

truncate memory at current cursor position

Definition at line 313 of file lib-vst3/memorystream.cpp.

314{
315 size = cursor;
316 return truncate ();
317}
bool truncate()
realloc to the current use memory size if needed

References cursor, size, and truncate().

Here is the call graph for this function:

◆ write()

tresult PLUGIN_API MemoryStream::write ( void *  buffer,
int32  numBytes,
int32 *  numBytesWritten 
)

Definition at line 120 of file lib-vst3/memorystream.cpp.

121{
122 if (allocationError)
123 return kOutOfMemory;
124 if (buffer == nullptr)
125 return kInvalidArgument;
126
127 // Does write exceed size ?
128 TSize requiredSize = cursor + numBytes;
129 if (requiredSize > size)
130 {
131 if (requiredSize > memorySize)
132 setSize (requiredSize);
133 else
134 size = requiredSize;
135 }
136
137 // Copy data
138 if (memory && cursor >= 0 && numBytes > 0)
139 {
140 memcpy (&memory[cursor], buffer, static_cast<size_t> (numBytes));
141 // Update cursor
142 cursor += numBytes;
143 }
144 else
145 numBytes = 0;
146
147 if (numBytesWritten)
148 *numBytesWritten = numBytes;
149
150 return kResultTrue;
151}
void setSize(TSize size)
set the memory size, a realloc will occur if memory already used

References allocationError, cursor, memory, memorySize, setSize(), and size.

Here is the call graph for this function:

Member Data Documentation

◆ allocationError

bool Steinberg::MemoryStream::allocationError
protected

Definition at line 76 of file lib-vst3/memorystream.h.

Referenced by read(), setSize(), and write().

◆ cursor

int64 Steinberg::MemoryStream::cursor
protected

Definition at line 74 of file lib-vst3/memorystream.h.

Referenced by detachData(), read(), seek(), setSize(), tell(), truncateToCursor(), and write().

◆ memory

char* Steinberg::MemoryStream::memory
protected

Definition at line 71 of file lib-vst3/memorystream.h.

Referenced by detachData(), getData(), read(), setSize(), truncate(), write(), and ~MemoryStream().

◆ memorySize

TSize Steinberg::MemoryStream::memorySize
protected

Definition at line 72 of file lib-vst3/memorystream.h.

Referenced by detachData(), seek(), setSize(), truncate(), and write().

◆ ownMemory

bool Steinberg::MemoryStream::ownMemory
protected

Definition at line 75 of file lib-vst3/memorystream.h.

Referenced by detachData(), seek(), setSize(), truncate(), and ~MemoryStream().

◆ size

TSize Steinberg::MemoryStream::size
protected

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