2 * Copyright (c) 2011-2020 Samsung Electronics Co., Ltd. All rights reserved
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.
17 * @file binary_queue.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of binary queue
22 #include <dpl/binary_queue.h>
28 BinaryQueue::BinaryQueue() :
32 void BinaryQueue::AppendCopy(const void *buffer, size_t bufferSize)
37 assert(buffer != NULL);
39 m_buckets.emplace_back(bufferSize);
40 memcpy(m_buckets.back().data(), buffer, bufferSize);
44 size_t BinaryQueue::Size() const
49 void BinaryQueue::FlattenConsume(void *buffer, size_t bufferSize)
55 assert(buffer != NULL);
57 if (bufferSize > m_size)
58 Throw(Exception::OutOfData);
60 size_t bytesLeft = bufferSize;
62 assert(!m_buckets.empty());
66 auto& bucket = m_buckets.front();
69 size_t bucketSize = bucket.size();
70 size_t count = std::min(bytesLeft, bucketSize);
72 // Copy data to user pointer
73 memcpy(ptr, bucket.data(), count);
76 if (count == bucketSize) {
77 m_buckets.pop_front();
79 bucket.erase(bucket.begin(), bucket.begin() + count);
80 assert(count == bytesLeft);
85 ptr = static_cast<char *>(ptr) + count;