Browse Source

water: don't throw exceptions on memory failture, only return error

tags/1.9.8
falkTX 7 years ago
parent
commit
5fe99562a5
8 changed files with 23 additions and 87 deletions
  1. +8
    -5
      source/modules/water/files/File.cpp
  2. +5
    -3
      source/modules/water/files/FileOutputStream.cpp
  3. +0
    -47
      source/modules/water/memory/HeapBlock.h
  4. +2
    -1
      source/modules/water/midi/MidiMessage.cpp
  5. +0
    -13
      source/modules/water/streams/InputStream.cpp
  6. +0
    -9
      source/modules/water/streams/InputStream.h
  7. +8
    -2
      source/modules/water/text/String.cpp
  8. +0
    -7
      source/modules/water/water.h

+ 8
- 5
source/modules/water/files/File.cpp View File

@@ -737,9 +737,7 @@ bool File::appendText (const String& text,
const bool writeUnicodeHeaderBytes) const
{
FileOutputStream out (*this);
if (out.failedToOpen())
return false;
CARLA_SAFE_ASSERT_RETURN(out.failedToOpen(), false);
out.writeText (text, asUnicode, writeUnicodeHeaderBytes);
return true;
@@ -766,7 +764,10 @@ bool File::hasIdenticalContentTo (const File& other) const
if (in1.openedOk() && in2.openedOk())
{
const int bufferSize = 4096;
HeapBlock<char> buffer1 (bufferSize), buffer2 (bufferSize);
HeapBlock<char> buffer1, buffer2;
CARLA_SAFE_ASSERT_RETURN(buffer1.malloc (bufferSize), false);
CARLA_SAFE_ASSERT_RETURN(buffer2.malloc (bufferSize), false);
for (;;)
{
@@ -1545,7 +1546,9 @@ private:
#else
static String getLinkedFile (const String& file)
{
HeapBlock<char> buffer (8194);
HeapBlock<char> buffer;
CARLA_SAFE_ASSERT_RETURN(buffer.malloc(8194), String());
const int numBytes = (int) readlink (file.toRawUTF8(), buffer, 8192);
return String::fromUTF8 (buffer, jmax (0, numBytes));
}


+ 5
- 3
source/modules/water/files/FileOutputStream.cpp View File

@@ -36,10 +36,12 @@ FileOutputStream::FileOutputStream (const File& f, const size_t bufferSizeToUse)
status (Result::ok()),
currentPosition (0),
bufferSize (bufferSizeToUse),
bytesInBuffer (0),
buffer (jmax (bufferSizeToUse, (size_t) 16))
bytesInBuffer (0)
{
openHandle();
if (buffer.malloc(jmax (bufferSizeToUse, (size_t) 16)))
openHandle();
else
status = Result::fail ("Allocation failure");
}
FileOutputStream::~FileOutputStream()


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

@@ -31,17 +31,6 @@
namespace water {
#if ! JUCE_EXCEPTIONS_DISABLED
namespace HeapBlockHelper
{
template <bool shouldThrow>
struct ThrowOnFail { static void checkPointer (void*) {} };
template<>
struct ThrowOnFail<true> { static void checkPointer (void* data) { if (data == nullptr) throw std::bad_alloc(); } };
}
#endif
//==============================================================================
/**
Very simple container class to hold a pointer to some data on the heap.
@@ -102,33 +91,6 @@ public:
{
}
/** Creates a HeapBlock containing a number of elements.
The contents of the block are undefined, as it will have been created by a
malloc call.
If you want an array of zero values, you can use the calloc() method or the
other constructor that takes an InitialisationState parameter.
*/
explicit HeapBlock (const size_t numElements)
: data (static_cast<ElementType*> (std::malloc (numElements * sizeof (ElementType))))
{
throwOnAllocationFailure();
}
/** Creates a HeapBlock containing a number of elements.
The initialiseToZero parameter determines whether the new memory should be cleared,
or left uninitialised.
*/
HeapBlock (const size_t numElements, const bool initialiseToZero)
: data (static_cast<ElementType*> (initialiseToZero
? std::calloc (numElements, sizeof (ElementType))
: std::malloc (numElements * sizeof (ElementType))))
{
throwOnAllocationFailure();
}
/** Destructor.
This will free the data, if any has been allocated.
*/
@@ -297,15 +259,6 @@ public:
private:
//==============================================================================
ElementType* data;
void throwOnAllocationFailure() const
{
#if JUCE_EXCEPTIONS_DISABLED
jassert (data != nullptr); // without exceptions, you'll need to find a better way to handle this failure case.
#else
HeapBlockHelper::ThrowOnFail<throwOnFailure>::checkPointer (data);
#endif
}
};
}


+ 2
- 1
source/modules/water/midi/MidiMessage.cpp View File

@@ -648,7 +648,8 @@ bool MidiMessage::isSysEx() const noexcept
MidiMessage MidiMessage::createSysExMessage (const void* sysexData, const int dataSize)
{
HeapBlock<uint8> m ((size_t) dataSize + 2);
HeapBlock<uint8> m;
CARLA_SAFE_ASSERT_RETURN(m.malloc((size_t) dataSize + 2U), MidiMessage());
m[0] = 0xf0;
memcpy (m + 1, sysexData, (size_t) dataSize);


+ 0
- 13
source/modules/water/streams/InputStream.cpp View File

@@ -226,17 +226,4 @@ String InputStream::readEntireStreamAsString()
return mo.toString();
}
//==============================================================================
void InputStream::skipNextBytes (int64 numBytesToSkip)
{
if (numBytesToSkip > 0)
{
const int skipBufferSize = (int) jmin (numBytesToSkip, (int64) 16384);
HeapBlock<char> temp ((size_t) skipBufferSize);
while (numBytesToSkip > 0 && ! isExhausted())
numBytesToSkip -= read (temp, (int) jmin (numBytesToSkip, (int64) skipBufferSize));
}
}
}

+ 0
- 9
source/modules/water/streams/InputStream.h View File

@@ -246,15 +246,6 @@ public:
*/
virtual bool setPosition (int64 newPosition) = 0;
/** Reads and discards a number of bytes from the stream.
Some input streams might implement this efficiently, but the base
class will just keep reading data until the requisite number of bytes
have been done.
*/
virtual void skipNextBytes (int64 numBytesToSkip);
protected:
//==============================================================================
InputStream() noexcept {}


+ 8
- 2
source/modules/water/text/String.cpp View File

@@ -1706,7 +1706,8 @@ bool String::containsNonWhitespaceChars() const noexcept
//=====================================================================================================================
static String getStringFromWindows1252Codepage (const char* data, size_t num)
{
HeapBlock<char> unicode (num + 1);
HeapBlock<char> unicode;
CARLA_SAFE_ASSERT_RETURN(unicode.malloc(num + 1), String());
for (size_t i = 0; i < num; ++i)
unicode[i] = CharacterFunctions::getUnicodeCharFromWindows1252Codepage ((uint8) data[i]);
@@ -1745,12 +1746,17 @@ String String::formatted (const String pf, ... )
{
size_t bufferSize = 256;
HeapBlock<char> temp;
CARLA_SAFE_ASSERT_RETURN(temp.malloc(bufferSize), String());
for (;;)
{
va_list args;
va_start (args, pf);
HeapBlock<char> temp (bufferSize);
// FIXME - needed?
temp.clear (bufferSize);
const int num = vsnprintf (temp.getData(), bufferSize - 1, pf.toRawUTF8(), args);
va_end (args);


+ 0
- 7
source/modules/water/water.h View File

@@ -53,13 +53,6 @@
#if __has_feature (cxx_deleted_functions)
#define JUCE_DELETED_FUNCTION = delete
#endif
#ifndef JUCE_EXCEPTIONS_DISABLED
#if ! __has_feature (cxx_exceptions)
#define JUCE_EXCEPTIONS_DISABLED 1
#endif
#endif
#endif
//==============================================================================


Loading…
Cancel
Save