1 #ifndef __DALI_INTERNAL_MESSAGE_BUFFER_H__
2 #define __DALI_INTERNAL_MESSAGE_BUFFER_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
25 #include <dali/devel-api/common/mutex.h>
34 * Utility class to reserve a buffer for storing messages.
39 typedef std::ptrdiff_t WordType;
42 * Create a new MessageBuffer
43 * @param[in] The smallest capacity which the buffer will allocate, with respect to the size of type "char".
44 * @note The buffer will not allocate memory until the first call to ReserveMessageSlot().
46 MessageBuffer( std::size_t initialCapacity );
49 * Non-virtual destructor; not suitable as a base class
54 * Reserve space for another message in the buffer.
55 * @pre size is greater than zero.
56 * @param[in] size The message size with respect to the size of type "char".
57 * @return A pointer to the address allocated for the message, aligned to a word boundary
59 unsigned int* ReserveMessageSlot( std::size_t size );
62 * Query the capacity of the message buffer.
63 * @return The capacity with respect to the size of type "char".
65 std::size_t GetCapacity() const;
68 * Used to iterate though the messages in the buffer.
75 Iterator(WordType* current);
77 // Inlined for performance
80 // Valid until end marker has been found
81 return 0 != mMessageSize;
84 // Inlined for performance
87 return ( 0 != mMessageSize ) ? mCurrent : NULL;
90 // Inlined for performance
93 // Jump to next object and read size
94 mCurrent += mMessageSize;
95 mMessageSize = *mCurrent++;
99 Iterator(const Iterator& copy);
104 Iterator& operator=(const Iterator& rhs);
109 std::size_t mMessageSize;
113 * Returns an iterator to the first message in the buffer.
114 * There is no past-the-end iterator; use Iterator::IsValid() to determine when the has been reached.
115 * @note Adding more messages with ReserveMessageSlot() may corrupt this iterator.
116 * @return The iterator.
118 Iterator Begin() const;
121 * Sets the size of the buffer to zero (does not deallocate memory)
128 MessageBuffer(const MessageBuffer&);
131 MessageBuffer& operator=(const MessageBuffer& rhs);
134 * Helper to increase the capacity of the buffer.
135 * @pre The newCapacity is greater than mCapacity.
136 * @param[in] The newCapacity
138 void IncreaseCapacity( std::size_t newCapacity );
142 std::size_t mInitialCapacity; ///< The capacity to allocate during first call to ReserveMessageSlot
144 WordType* mData; ///< The data allocated for the message buffer
145 WordType* mNextSlot; ///< The next free location in the buffer
147 std::size_t mCapacity; ///< The memory allocated with respect to sizeof(WordType)
148 std::size_t mSize; ///< The memory reserved for messages with respect to sizeof(WordType)
149 Dali::Mutex mMutex; ///< Mutex to ensure correct access locking
152 } // namespace Internal
156 #endif // __DALI_INTERNAL_MESSAGE_BUFFER_H__