2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/common/message-buffer.h>
26 #include <dali/integration-api/debug.h>
28 namespace // unnamed namespace
31 // Increase capacity by 1.5 when buffer limit reached
32 const unsigned int INCREMENT_NUMERATOR = 3u;
33 const unsigned int INCREMENT_DENOMINATOR = 2u;
35 const unsigned int MESSAGE_SIZE_FIELD = 1u; // Size required to mark the message size
36 const unsigned int MESSAGE_END_FIELD = 1u; // Size required to mark the end of messages
38 const unsigned int MESSAGE_SIZE_PLUS_END_FIELD = MESSAGE_SIZE_FIELD + MESSAGE_END_FIELD;
40 const unsigned int MAX_DIVISION_BY_WORD_REMAINDER = sizeof(unsigned int) - 1u; // For word alignment on ARM
42 } // unnamed namespace
50 MessageBuffer::MessageBuffer( std::size_t initialCapacity )
51 : mInitialCapacity( initialCapacity / sizeof(unsigned int) ),
59 MessageBuffer::~MessageBuffer()
64 unsigned int* MessageBuffer::ReserveMessageSlot( std::size_t size )
66 DALI_ASSERT_DEBUG( 0 != size );
68 std::size_t requestedSize = (size + MAX_DIVISION_BY_WORD_REMAINDER) / sizeof(unsigned int);
69 std::size_t requiredSize = requestedSize + MESSAGE_SIZE_PLUS_END_FIELD;
71 // Keep doubling the additional capacity until we have enough
72 std::size_t nextCapacity = mCapacity ? mCapacity : mInitialCapacity;
74 if ( (nextCapacity - mSize) < requiredSize )
76 nextCapacity = nextCapacity * INCREMENT_NUMERATOR / INCREMENT_DENOMINATOR;
78 // Something has gone badly wrong if requiredSize is this big
79 DALI_ASSERT_DEBUG( (nextCapacity - mSize) > requiredSize );
82 if ( nextCapacity > mCapacity )
84 IncreaseCapacity( nextCapacity );
87 // Now reserve the slot
88 unsigned int* slot = mNextSlot;
90 *slot++ = requestedSize; // Object size marker is stored in first word
92 mSize += requestedSize + MESSAGE_SIZE_FIELD;
93 mNextSlot = mData + mSize;
101 std::size_t MessageBuffer::GetCapacity() const
103 return mCapacity * sizeof(unsigned int);
106 MessageBuffer::Iterator MessageBuffer::Begin() const
110 return Iterator( mData );
113 return Iterator( NULL );
116 void MessageBuffer::Reset()
118 // All messages have been processed, reset the buffer
123 void MessageBuffer::IncreaseCapacity( std::size_t newCapacity )
125 DALI_ASSERT_DEBUG( newCapacity > mCapacity );
129 // Often this avoids the need to copy memory
130 mData = reinterpret_cast<unsigned int*>( realloc( mData, newCapacity * sizeof(unsigned int) ) );
134 mData = reinterpret_cast<unsigned int*>( malloc( newCapacity * sizeof(unsigned int) ) );
136 DALI_ASSERT_ALWAYS( NULL != mData );
138 mCapacity = newCapacity;
139 mNextSlot = mData + mSize;
142 MessageBuffer::Iterator::Iterator(unsigned int* current)
146 if( NULL != mCurrent )
148 // The first word is the size of the following object
149 mMessageSize = *mCurrent++;
153 MessageBuffer::Iterator::Iterator(const Iterator& copy)
154 : mCurrent( copy.mCurrent ),
155 mMessageSize( copy.mMessageSize )
159 } // namespace Internal