Browse Source

Tweaks for Ildaeil, MemoryOutputStream::getDataAndRelease()

Signed-off-by: falkTX <falktx@falktx.com>
tags/v2.4.1
parent
commit
183fc4e236
7 changed files with 55 additions and 52 deletions
  1. +2
    -0
      source/backend/CarlaEngine.hpp
  2. +8
    -0
      source/modules/water/memory/HeapBlock.h
  3. +7
    -0
      source/modules/water/memory/MemoryBlock.cpp
  4. +4
    -0
      source/modules/water/memory/MemoryBlock.h
  5. +26
    -42
      source/modules/water/streams/MemoryOutputStream.cpp
  6. +7
    -10
      source/modules/water/streams/MemoryOutputStream.h
  7. +1
    -0
      source/modules/water/streams/OutputStream.h

+ 2
- 0
source/backend/CarlaEngine.hpp View File

@@ -1341,6 +1341,7 @@ protected:
*/
void setPluginPeaksRT(uint pluginId, float const inPeaks[2], float const outPeaks[2]) noexcept;

public:
/*!
* Common save project function for main engine and plugin.
*/
@@ -1351,6 +1352,7 @@ protected:
*/
bool loadProjectInternal(water::XmlDocument& xmlDoc, bool alwaysLoadConnections);

protected:
// -------------------------------------------------------------------
// Helper functions



+ 8
- 0
source/modules/water/memory/HeapBlock.h View File

@@ -253,6 +253,14 @@ public:
zeromem (data, sizeof (ElementType) * numElements);
}
/** Release this object's data ownership, returning the data pointer. */
ElementType* release() noexcept
{
ElementType* const r = data;
data = nullptr;
return r;
}
/** This typedef can be used to get the type of the heapblock's elements. */
typedef ElementType Type;


+ 7
- 0
source/modules/water/memory/MemoryBlock.cpp View File

@@ -167,6 +167,13 @@ void MemoryBlock::swapWith (MemoryBlock& other) noexcept
data.swapWith (other.data);
}
//==============================================================================
void* MemoryBlock::release () noexcept
{
size = 0;
return data.release();
}
//==============================================================================
void MemoryBlock::fillWith (const uint8 value) noexcept
{


+ 4
- 0
source/modules/water/memory/MemoryBlock.h View File

@@ -197,6 +197,10 @@ public:
*/
void swapWith (MemoryBlock& other) noexcept;
//==============================================================================
/** Release this object's data ownership, returning the data pointer. */
void* release () noexcept;
//==============================================================================
/** Attempts to parse the contents of the block as a zero-terminated UTF8 string. */
String toString() const;


+ 26
- 42
source/modules/water/streams/MemoryOutputStream.cpp View File

@@ -29,28 +29,21 @@
namespace water {
MemoryOutputStream::MemoryOutputStream (const size_t initialSize)
: blockToUse (&internalBlock), externalData (nullptr),
position (0), size (0), availableSize (0)
: internalBlock(), blockToUse (internalBlock),
position (0), size (0)
{
internalBlock.setSize (initialSize, false);
}
MemoryOutputStream::MemoryOutputStream (MemoryBlock& memoryBlockToWriteTo,
const bool appendToExistingBlockContent)
: blockToUse (&memoryBlockToWriteTo), externalData (nullptr),
position (0), size (0), availableSize (0)
: internalBlock(), blockToUse (memoryBlockToWriteTo),
position (0), size (0)
{
if (appendToExistingBlockContent)
position = size = memoryBlockToWriteTo.getSize();
}
MemoryOutputStream::MemoryOutputStream (void* destBuffer, size_t destBufferSize)
: blockToUse (nullptr), externalData (destBuffer),
position (0), size (0), availableSize (destBufferSize)
{
wassert (externalData != nullptr); // This must be a valid pointer.
}
MemoryOutputStream::~MemoryOutputStream()
{
trimExternalBlockSize();
@@ -63,14 +56,13 @@ void MemoryOutputStream::flush()
void MemoryOutputStream::trimExternalBlockSize()
{
if (blockToUse != &internalBlock && blockToUse != nullptr)
blockToUse->setSize (size, false);
if (blockToUse != internalBlock)
blockToUse.setSize (size, false);
}
void MemoryOutputStream::preallocate (const size_t bytesToPreallocate)
{
if (blockToUse != nullptr)
blockToUse->ensureSize (bytesToPreallocate + 1);
blockToUse.ensureSize (bytesToPreallocate + 1);
}
void MemoryOutputStream::reset() noexcept
@@ -81,26 +73,14 @@ void MemoryOutputStream::reset() noexcept
char* MemoryOutputStream::prepareToWrite (size_t numBytes)
{
wassert ((ssize_t) numBytes >= 0);
size_t storageNeeded = position + numBytes;
CARLA_SAFE_ASSERT_RETURN ((ssize_t) numBytes >= 0, nullptr);
char* data;
const size_t storageNeeded = position + numBytes;
if (blockToUse != nullptr)
{
if (storageNeeded >= blockToUse->getSize())
blockToUse->ensureSize ((storageNeeded + jmin (storageNeeded / 2, (size_t) (1024 * 1024)) + 32) & ~31u);
data = static_cast<char*> (blockToUse->getData());
}
else
{
if (storageNeeded > availableSize)
return nullptr;
data = static_cast<char*> (externalData);
}
if (storageNeeded >= blockToUse.getSize())
blockToUse.ensureSize ((storageNeeded + jmin (storageNeeded / 2, (size_t) (1024 * 1024)) + 32) & ~31u);
char* const data = static_cast<char*> (blockToUse.getData());
char* const writePointer = data + position;
position += numBytes;
size = jmax (size, position);
@@ -109,14 +89,14 @@ char* MemoryOutputStream::prepareToWrite (size_t numBytes)
bool MemoryOutputStream::write (const void* const buffer, size_t howMany)
{
wassert (buffer != nullptr);
CARLA_SAFE_ASSERT_RETURN (buffer != nullptr, false);
if (howMany == 0)
return true;
if (char* dest = prepareToWrite (howMany))
if (char* const dest = prepareToWrite (howMany))
{
memcpy (dest, buffer, howMany);
std::memcpy (dest, buffer, howMany);
return true;
}
@@ -155,13 +135,18 @@ MemoryBlock MemoryOutputStream::getMemoryBlock() const
const void* MemoryOutputStream::getData() const noexcept
{
if (blockToUse == nullptr)
return externalData;
if (blockToUse.getSize() > size)
static_cast<char*> (blockToUse.getData()) [size] = 0;
if (blockToUse->getSize() > size)
static_cast<char*> (blockToUse->getData()) [size] = 0;
return blockToUse.getData();
}
void* MemoryOutputStream::getDataAndRelease() noexcept
{
if (blockToUse.getSize() > size)
static_cast<char*> (blockToUse.getData()) [size] = 0;
return blockToUse->getData();
return blockToUse.release();
}
bool MemoryOutputStream::setPosition (int64 newPosition)
@@ -187,8 +172,7 @@ int64 MemoryOutputStream::writeFromInputStream (InputStream& source, int64 maxNu
if (maxNumBytesToWrite > availableData || maxNumBytesToWrite < 0)
maxNumBytesToWrite = availableData;
if (blockToUse != nullptr)
preallocate (blockToUse->getSize() + (size_t) maxNumBytesToWrite);
preallocate (blockToUse.getSize() + (size_t) maxNumBytesToWrite);
}
return OutputStream::writeFromInputStream (source, maxNumBytesToWrite);


+ 7
- 10
source/modules/water/streams/MemoryOutputStream.h View File

@@ -62,13 +62,6 @@ public:
MemoryOutputStream (MemoryBlock& memoryBlockToWriteTo,
bool appendToExistingBlockContent);
/** Creates a MemoryOutputStream that will write into a user-supplied, fixed-size
block of memory.
When using this mode, the stream will write directly into this memory area until
it's full, at which point write operations will fail.
*/
MemoryOutputStream (void* destBuffer, size_t destBufferSize);
/** Destructor.
This will free any data that was written to it.
*/
@@ -80,6 +73,11 @@ public:
*/
const void* getData() const noexcept;
/** Returns a pointer to the data that has been written to the stream and releases the buffer pointer.
@see getDataSize
*/
void* getDataAndRelease() noexcept;
/** Returns the number of bytes of data that have been written to the stream.
@see getData
*/
@@ -122,10 +120,9 @@ public:
private:
//==============================================================================
MemoryBlock* const blockToUse;
MemoryBlock internalBlock;
void* externalData;
size_t position, size, availableSize;
MemoryBlock& blockToUse;
size_t position, size;
void trimExternalBlockSize();
char* prepareToWrite (size_t);


+ 1
- 0
source/modules/water/streams/OutputStream.h View File

@@ -27,6 +27,7 @@
#define WATER_OUTPUTSTREAM_H_INCLUDED
#include "../water.h"
#include "../text/String.h"
namespace water {


Loading…
Cancel
Save