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.
31 * Utility class to reserve a buffer for storing messages.
36 typedef std::ptrdiff_t WordType;
39 * Create a new MessageBuffer
40 * @param[in] The smallest capacity which the buffer will allocate, with respect to the size of type "char".
41 * @note The buffer will not allocate memory until the first call to ReserveMessageSlot().
43 MessageBuffer( std::size_t initialCapacity );
46 * Non-virtual destructor; not suitable as a base class
51 * Reserve space for another message in the buffer.
52 * @pre size is greater than zero.
53 * @param[in] size The message size with respect to the size of type "char".
54 * @return A pointer to the address allocated for the message, aligned to a word boundary
56 unsigned int* ReserveMessageSlot( std::size_t size );
59 * Query the capacity of the message buffer.
60 * @return The capacity with respect to the size of type "char".
62 std::size_t GetCapacity() const;
65 * Used to iterate though the messages in the buffer.
72 Iterator(WordType* current);
74 // Inlined for performance
77 // Valid until end marker has been found
78 return 0 != mMessageSize;
81 // Inlined for performance
84 return ( 0 != mMessageSize ) ? mCurrent : NULL;
87 // Inlined for performance
90 // Jump to next object and read size
91 mCurrent += mMessageSize;
92 mMessageSize = *mCurrent++;
96 Iterator(const Iterator& copy);
101 Iterator& operator=(const Iterator& rhs);
106 std::size_t mMessageSize;
110 * Returns an iterator to the first message in the buffer.
111 * There is no past-the-end iterator; use Iterator::IsValid() to determine when the has been reached.
112 * @note Adding more messages with ReserveMessageSlot() may corrupt this iterator.
113 * @return The iterator.
115 Iterator Begin() const;
118 * Sets the size of the buffer to zero (does not deallocate memory)
125 MessageBuffer(const MessageBuffer&);
128 MessageBuffer& operator=(const MessageBuffer& rhs);
131 * Helper to increase the capacity of the buffer.
132 * @pre The newCapacity is greater than mCapacity.
133 * @param[in] The newCapacity
135 void IncreaseCapacity( std::size_t newCapacity );
139 std::size_t mInitialCapacity; ///< The capacity to allocate during first call to ReserveMessageSlot
141 WordType* mData; ///< The data allocated for the message buffer
142 WordType* mNextSlot; ///< The next free location in the buffer
144 std::size_t mCapacity; ///< The memory allocated with respect to sizeof(WordType)
145 std::size_t mSize; ///< The memory reserved for messages with respect to sizeof(WordType)
148 } // namespace Internal
152 #endif // __DALI_INTERNAL_MESSAGE_BUFFER_H__