2 * Copyright (c) 2011 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
23 #include <dpl/binary_queue.h>
24 #include <dpl/assert.h>
25 #include <dpl/scoped_free.h>
32 BinaryQueue::BinaryQueue() :
36 BinaryQueue::BinaryQueue(const BinaryQueue &other) :
39 AppendCopyFrom(other);
42 BinaryQueue::~BinaryQueue()
44 // Remove all remainig buckets
48 BinaryQueue &BinaryQueue::operator=(const BinaryQueue &other)
52 AppendCopyFrom(other);
58 void BinaryQueue::AppendCopyFrom(const BinaryQueue &other)
60 // To speed things up, always copy as one bucket
61 void *bufferCopy = malloc(other.m_size);
63 if (bufferCopy == NULL) {
64 throw std::bad_alloc();
68 other.Flatten(bufferCopy, other.m_size);
69 AppendUnmanaged(bufferCopy, other.m_size, &BufferDeleterFree, NULL);
70 } catch (const std::bad_alloc &) {
71 // Free allocated memory
77 void BinaryQueue::AppendMoveFrom(BinaryQueue &other)
80 std::copy(other.m_buckets.begin(),
81 other.m_buckets.end(), std::back_inserter(m_buckets));
82 m_size += other.m_size;
84 // Clear other, but do not free memory
85 other.m_buckets.clear();
89 void BinaryQueue::AppendCopyTo(BinaryQueue &other) const
91 other.AppendCopyFrom(*this);
94 void BinaryQueue::AppendMoveTo(BinaryQueue &other)
96 other.AppendMoveFrom(*this);
99 void BinaryQueue::Clear()
101 std::for_each(m_buckets.begin(), m_buckets.end(), &DeleteBucket);
106 void BinaryQueue::AppendCopy(const void* buffer, size_t bufferSize)
108 // Create data copy with malloc/free
109 void *bufferCopy = malloc(bufferSize);
111 // Check if allocation succeded
112 if (bufferCopy == NULL) {
113 throw std::bad_alloc();
117 memcpy(bufferCopy, buffer, bufferSize);
120 // Try to append new bucket
121 AppendUnmanaged(bufferCopy, bufferSize, &BufferDeleterFree, NULL);
122 } catch (const std::bad_alloc &) {
123 // Free allocated memory
129 void BinaryQueue::AppendUnmanaged(const void* buffer,
131 BufferDeleter deleter,
134 // Do not attach empty buckets
135 if (bufferSize == 0) {
136 deleter(buffer, bufferSize, userParam);
140 // Just add new bucket with selected deleter
141 m_buckets.push_back(new Bucket(buffer, bufferSize, deleter, userParam));
143 // Increase total queue size
144 m_size += bufferSize;
147 size_t BinaryQueue::Size() const
152 bool BinaryQueue::Empty() const
157 void BinaryQueue::Consume(size_t size)
161 Throw(Exception::OutOfData);
164 size_t bytesLeft = size;
166 // Consume data and/or remove buckets
167 while (bytesLeft > 0) {
169 size_t count = std::min(bytesLeft, m_buckets.front()->left);
171 m_buckets.front()->ptr =
172 static_cast<const char *>(m_buckets.front()->ptr) + count;
173 m_buckets.front()->left -= count;
177 if (m_buckets.front()->left == 0) {
178 DeleteBucket(m_buckets.front());
179 m_buckets.pop_front();
184 void BinaryQueue::Flatten(void *buffer, size_t bufferSize) const
187 if (bufferSize == 0) {
191 if (bufferSize > m_size) {
192 Throw(Exception::OutOfData);
195 size_t bytesLeft = bufferSize;
197 BucketList::const_iterator bucketIterator = m_buckets.begin();
198 Assert(m_buckets.end() != bucketIterator);
201 while (bytesLeft > 0) {
203 size_t count = std::min(bytesLeft, (*bucketIterator)->left);
205 // Copy data to user pointer
206 memcpy(ptr, (*bucketIterator)->ptr, count);
208 // Update flattened bytes count
210 ptr = static_cast<char *>(ptr) + count;
217 void BinaryQueue::FlattenConsume(void *buffer, size_t bufferSize)
220 Flatten(buffer, bufferSize);
224 void BinaryQueue::DeleteBucket(BinaryQueue::Bucket *bucket)
229 void BinaryQueue::BufferDeleterFree(const void* data,
236 // Default free deleter
237 free(const_cast<void *>(data));
240 BinaryQueue::Bucket::Bucket(const void* data,
242 BufferDeleter dataDeleter,
248 deleter(dataDeleter),
251 Assert(data != NULL);
252 Assert(deleter != NULL);
255 BinaryQueue::Bucket::~Bucket()
257 // Invoke deleter on bucket data
258 deleter(buffer, size, param);
261 BinaryQueue::BucketVisitor::~BucketVisitor()
264 BinaryQueue::BucketVisitorCall::BucketVisitorCall(BucketVisitor *visitor) :
268 BinaryQueue::BucketVisitorCall::~BucketVisitorCall()
271 void BinaryQueue::BucketVisitorCall::operator()(Bucket *bucket) const
273 m_visitor->OnVisitBucket(bucket->ptr, bucket->left);
276 void BinaryQueue::VisitBuckets(BucketVisitor *visitor) const
278 Assert(visitor != NULL);
281 std::for_each(m_buckets.begin(), m_buckets.end(), BucketVisitorCall(visitor));
284 BinaryQueueAutoPtr BinaryQueue::Read(size_t size)
286 // Simulate input stream
287 size_t available = std::min(size, m_size);
289 ScopedFree<void> bufferCopy(malloc(available));
292 throw std::bad_alloc();
295 BinaryQueueAutoPtr result(new BinaryQueue());
297 Flatten(bufferCopy.Get(), available);
298 result->AppendUnmanaged(
299 bufferCopy.Get(), available, &BufferDeleterFree, NULL);
300 bufferCopy.Release();
306 size_t BinaryQueue::Write(const BinaryQueue &buffer, size_t bufferSize)
308 // Simulate output stream
309 AppendCopyFrom(buffer);
312 } // namespace VcoreDPL