From 5fe99562a51f105160eb4443bba3c20ce8ead8e5 Mon Sep 17 00:00:00 2001 From: falkTX Date: Sun, 5 Nov 2017 16:55:03 +0100 Subject: [PATCH] water: don't throw exceptions on memory failture, only return error --- source/modules/water/files/File.cpp | 13 +++-- .../modules/water/files/FileOutputStream.cpp | 8 ++-- source/modules/water/memory/HeapBlock.h | 47 ------------------- source/modules/water/midi/MidiMessage.cpp | 3 +- source/modules/water/streams/InputStream.cpp | 13 ----- source/modules/water/streams/InputStream.h | 9 ---- source/modules/water/text/String.cpp | 10 +++- source/modules/water/water.h | 7 --- 8 files changed, 23 insertions(+), 87 deletions(-) diff --git a/source/modules/water/files/File.cpp b/source/modules/water/files/File.cpp index e2f86d200..1a240893c 100644 --- a/source/modules/water/files/File.cpp +++ b/source/modules/water/files/File.cpp @@ -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 buffer1 (bufferSize), buffer2 (bufferSize); + HeapBlock 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 buffer (8194); + HeapBlock 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)); } diff --git a/source/modules/water/files/FileOutputStream.cpp b/source/modules/water/files/FileOutputStream.cpp index ceebb7cd8..38f936e52 100644 --- a/source/modules/water/files/FileOutputStream.cpp +++ b/source/modules/water/files/FileOutputStream.cpp @@ -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() diff --git a/source/modules/water/memory/HeapBlock.h b/source/modules/water/memory/HeapBlock.h index 59b6b556e..309ab77a9 100644 --- a/source/modules/water/memory/HeapBlock.h +++ b/source/modules/water/memory/HeapBlock.h @@ -31,17 +31,6 @@ namespace water { -#if ! JUCE_EXCEPTIONS_DISABLED -namespace HeapBlockHelper -{ - template - struct ThrowOnFail { static void checkPointer (void*) {} }; - - template<> - struct ThrowOnFail { 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 (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 (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::checkPointer (data); - #endif - } }; } diff --git a/source/modules/water/midi/MidiMessage.cpp b/source/modules/water/midi/MidiMessage.cpp index 8b673f3b5..701a498bf 100644 --- a/source/modules/water/midi/MidiMessage.cpp +++ b/source/modules/water/midi/MidiMessage.cpp @@ -648,7 +648,8 @@ bool MidiMessage::isSysEx() const noexcept MidiMessage MidiMessage::createSysExMessage (const void* sysexData, const int dataSize) { - HeapBlock m ((size_t) dataSize + 2); + HeapBlock m; + CARLA_SAFE_ASSERT_RETURN(m.malloc((size_t) dataSize + 2U), MidiMessage()); m[0] = 0xf0; memcpy (m + 1, sysexData, (size_t) dataSize); diff --git a/source/modules/water/streams/InputStream.cpp b/source/modules/water/streams/InputStream.cpp index 2da435587..503cefe79 100644 --- a/source/modules/water/streams/InputStream.cpp +++ b/source/modules/water/streams/InputStream.cpp @@ -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 temp ((size_t) skipBufferSize); - - while (numBytesToSkip > 0 && ! isExhausted()) - numBytesToSkip -= read (temp, (int) jmin (numBytesToSkip, (int64) skipBufferSize)); - } -} - } diff --git a/source/modules/water/streams/InputStream.h b/source/modules/water/streams/InputStream.h index c236b95a0..e764c1bc7 100644 --- a/source/modules/water/streams/InputStream.h +++ b/source/modules/water/streams/InputStream.h @@ -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 {} diff --git a/source/modules/water/text/String.cpp b/source/modules/water/text/String.cpp index b0f17f5ca..b38ec68a6 100644 --- a/source/modules/water/text/String.cpp +++ b/source/modules/water/text/String.cpp @@ -1706,7 +1706,8 @@ bool String::containsNonWhitespaceChars() const noexcept //===================================================================================================================== static String getStringFromWindows1252Codepage (const char* data, size_t num) { - HeapBlock unicode (num + 1); + HeapBlock 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 temp; + CARLA_SAFE_ASSERT_RETURN(temp.malloc(bufferSize), String()); + for (;;) { va_list args; va_start (args, pf); - HeapBlock temp (bufferSize); + // FIXME - needed? + temp.clear (bufferSize); + const int num = vsnprintf (temp.getData(), bufferSize - 1, pf.toRawUTF8(), args); va_end (args); diff --git a/source/modules/water/water.h b/source/modules/water/water.h index 0403d3151..f99c8194f 100644 --- a/source/modules/water/water.h +++ b/source/modules/water/water.h @@ -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 //==============================================================================