use modern construct 'nullptr' instead of 'NULL' or '0'
[platform/core/uifw/dali-core.git] / dali / internal / common / message-buffer.cpp
index 52f6743..5893f91 100644 (file)
@@ -1,18 +1,19 @@
-//
-// Copyright (c) 2014 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Flora License, Version 1.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://floralicense.org/license/
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an AS IS BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
 
 // CLASS HEADER
 #include <dali/internal/common/message-buffer.h>
@@ -28,15 +29,16 @@ namespace // unnamed namespace
 {
 
 // Increase capacity by 1.5 when buffer limit reached
-const unsigned int INCREMENT_NUMERATOR   = 3u;
-const unsigned int INCREMENT_DENOMINATOR = 2u;
+const uint32_t INCREMENT_NUMERATOR   = 3u;
+const uint32_t INCREMENT_DENOMINATOR = 2u;
 
-const unsigned int MESSAGE_SIZE_FIELD = 1u; // Size required to mark the message size
-const unsigned int MESSAGE_END_FIELD  = 1u; // Size required to mark the end of messages
+const uint32_t MESSAGE_SIZE_FIELD = 1u; // Size required to mark the message size
+const uint32_t MESSAGE_END_FIELD  = 1u; // Size required to mark the end of messages
 
-const unsigned int MESSAGE_SIZE_PLUS_END_FIELD = MESSAGE_SIZE_FIELD + MESSAGE_END_FIELD;
+const uint32_t MESSAGE_SIZE_PLUS_END_FIELD = MESSAGE_SIZE_FIELD + MESSAGE_END_FIELD;
 
-const unsigned int MAX_DIVISION_BY_WORD_REMAINDER = sizeof(unsigned int) - 1u; // For word alignment on ARM
+const std::size_t MAX_DIVISION_BY_WORD_REMAINDER = sizeof(Dali::Internal::MessageBuffer::WordType) - 1u; // For word alignment on ARM
+const std::size_t WORD_SIZE = sizeof(Dali::Internal::MessageBuffer::WordType);
 
 } // unnamed namespace
 
@@ -47,9 +49,9 @@ namespace Internal
 {
 
 MessageBuffer::MessageBuffer( std::size_t initialCapacity )
-: mInitialCapacity( initialCapacity / sizeof(unsigned int) ),
-  mData( NULL ),
-  mNextSlot( NULL ),
+: mInitialCapacity( initialCapacity / WORD_SIZE ),
+  mData( nullptr ),
+  mNextSlot( nullptr ),
   mCapacity( 0 ),
   mSize( 0 )
 {
@@ -60,11 +62,12 @@ MessageBuffer::~MessageBuffer()
   free( mData );
 }
 
-unsigned int* MessageBuffer::ReserveMessageSlot( std::size_t size )
+uint32_t* MessageBuffer::ReserveMessageSlot( std::size_t size )
 {
   DALI_ASSERT_DEBUG( 0 != size );
 
-  std::size_t requestedSize = (size + MAX_DIVISION_BY_WORD_REMAINDER) / sizeof(unsigned int);
+  // Number of aligned words required to handle a message of size in bytes
+  std::size_t requestedSize = (size + MAX_DIVISION_BY_WORD_REMAINDER) / WORD_SIZE;
   std::size_t requiredSize = requestedSize + MESSAGE_SIZE_PLUS_END_FIELD;
 
   // Keep doubling the additional capacity until we have enough
@@ -84,7 +87,7 @@ unsigned int* MessageBuffer::ReserveMessageSlot( std::size_t size )
   }
 
   // Now reserve the slot
-  unsigned int* slot = mNextSlot;
+  WordType* slot = mNextSlot;
 
   *slot++ = requestedSize; // Object size marker is stored in first word
 
@@ -94,12 +97,12 @@ unsigned int* MessageBuffer::ReserveMessageSlot( std::size_t size )
   // End marker
   *mNextSlot = 0;
 
-  return slot;
+  return reinterpret_cast<uint32_t*>(slot);
 }
 
 std::size_t MessageBuffer::GetCapacity() const
 {
-  return mCapacity * sizeof(unsigned int);
+  return mCapacity * WORD_SIZE;
 }
 
 MessageBuffer::Iterator MessageBuffer::Begin() const
@@ -109,7 +112,7 @@ MessageBuffer::Iterator MessageBuffer::Begin() const
     return Iterator( mData );
   }
 
-  return Iterator( NULL );
+  return Iterator( nullptr );
 }
 
 void MessageBuffer::Reset()
@@ -126,23 +129,33 @@ void MessageBuffer::IncreaseCapacity( std::size_t newCapacity )
   if ( mData )
   {
     // Often this avoids the need to copy memory
-    mData = reinterpret_cast<unsigned int*>( realloc( mData, newCapacity * sizeof(unsigned int) ) );
+
+    WordType* oldData = mData;
+    mData = reinterpret_cast<WordType*>( realloc( mData, newCapacity * WORD_SIZE ) );
+
+    // if realloc fails the old data is still valid
+    if( !mData )
+    {
+      // TODO: Process message queue to free up some data?
+      free(oldData);
+      DALI_ASSERT_DEBUG( false && "Realloc failed we're out of memory!" );
+    }
   }
   else
   {
-    mData = reinterpret_cast<unsigned int*>( malloc( newCapacity * sizeof(unsigned int) ) );
+    mData = reinterpret_cast<WordType*>( malloc( newCapacity * WORD_SIZE ) );
   }
-  DALI_ASSERT_ALWAYS( NULL != mData );
+  DALI_ASSERT_ALWAYS( nullptr != mData );
 
   mCapacity = newCapacity;
   mNextSlot = mData + mSize;
 }
 
-MessageBuffer::Iterator::Iterator(unsigned int* current)
+MessageBuffer::Iterator::Iterator(WordType* current)
 : mCurrent(current),
   mMessageSize(0)
 {
-  if( NULL != mCurrent )
+  if( nullptr != mCurrent )
   {
     // The first word is the size of the following object
     mMessageSize = *mCurrent++;