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
22 #include <dpl/binary_queue.h>
23 #include <dpl/assert.h>
24 #include <dpl/scoped_free.h>
32 BinaryQueue::BinaryQueue()
37 BinaryQueue::BinaryQueue(const BinaryQueue &other)
40 AppendCopyFrom(other);
43 BinaryQueue::~BinaryQueue()
45 // Remove all remainig buckets
49 const BinaryQueue &BinaryQueue::operator=(const BinaryQueue &other)
54 AppendCopyFrom(other);
60 void BinaryQueue::AppendCopyFrom(const BinaryQueue &other)
62 // To speed things up, always copy as one bucket
63 void *bufferCopy = malloc(other.m_size);
65 if (bufferCopy == NULL)
66 throw std::bad_alloc();
70 other.Flatten(bufferCopy, other.m_size);
71 AppendUnmanaged(bufferCopy, other.m_size, &BufferDeleterFree, NULL);
73 catch (const std::bad_alloc &)
75 // Free allocated memory
81 void BinaryQueue::AppendMoveFrom(BinaryQueue &other)
84 std::copy(other.m_buckets.begin(), other.m_buckets.end(), std::back_inserter(m_buckets));
85 m_size += other.m_size;
87 // Clear other, but do not free memory
88 other.m_buckets.clear();
92 void BinaryQueue::AppendCopyTo(BinaryQueue &other) const
94 other.AppendCopyFrom(*this);
97 void BinaryQueue::AppendMoveTo(BinaryQueue &other)
99 other.AppendMoveFrom(*this);
102 void BinaryQueue::Clear()
104 std::for_each(m_buckets.begin(), m_buckets.end(), &DeleteBucket);
109 void BinaryQueue::AppendCopy(const void* buffer, size_t bufferSize)
111 // Create data copy with malloc/free
112 void *bufferCopy = malloc(bufferSize);
114 // Check if allocation succeded
115 if (bufferCopy == NULL)
116 throw std::bad_alloc();
119 memcpy(bufferCopy, buffer, bufferSize);
123 // Try to append new bucket
124 AppendUnmanaged(bufferCopy, bufferSize, &BufferDeleterFree, NULL);
126 catch (const std::bad_alloc &)
128 // Free allocated memory
134 void BinaryQueue::AppendUnmanaged(const void* buffer, size_t bufferSize, BufferDeleter deleter, void* userParam)
136 // Do not attach empty buckets
139 deleter(buffer, bufferSize, userParam);
143 // Just add new bucket with selected deleter
144 m_buckets.push_back(new Bucket(buffer, bufferSize, deleter, userParam));
146 // Increase total queue size
147 m_size += bufferSize;
150 size_t BinaryQueue::Size() const
155 bool BinaryQueue::Empty() const
160 void BinaryQueue::Consume(size_t size)
164 Throw(Exception::OutOfData);
166 size_t bytesLeft = size;
168 // Consume data and/or remove buckets
169 while (bytesLeft > 0)
172 size_t count = std::min(bytesLeft, m_buckets.front()->left);
174 m_buckets.front()->ptr = static_cast<const char *>(m_buckets.front()->ptr) + count;
175 m_buckets.front()->left -= count;
179 if (m_buckets.front()->left == 0)
181 DeleteBucket(m_buckets.front());
182 m_buckets.pop_front();
187 void BinaryQueue::Flatten(void *buffer, size_t bufferSize) const
193 if (bufferSize > m_size)
194 Throw(Exception::OutOfData);
196 size_t bytesLeft = bufferSize;
198 BucketList::const_iterator bucketIterator = m_buckets.begin();
199 Assert(m_buckets.end() != bucketIterator);
202 while (bytesLeft > 0)
205 size_t count = std::min(bytesLeft, (*bucketIterator)->left);
207 // Copy data to user pointer
208 memcpy(ptr, (*bucketIterator)->ptr, count);
210 // Update flattened bytes count
212 ptr = static_cast<char *>(ptr) + count;
219 void BinaryQueue::FlattenConsume(void *buffer, size_t bufferSize)
222 Flatten(buffer, bufferSize);
226 void BinaryQueue::DeleteBucket(BinaryQueue::Bucket *bucket)
231 void BinaryQueue::BufferDeleterFree(const void* data, size_t dataSize, void* userParam)
236 // Default free deleter
237 free(const_cast<void *>(data));
240 BinaryQueue::Bucket::Bucket(const void* data, size_t dataSize, BufferDeleter dataDeleter, void* userParam)
245 deleter(dataDeleter),
248 Assert(data != NULL);
249 Assert(deleter != NULL);
252 BinaryQueue::Bucket::~Bucket()
254 // Invoke deleter on bucket data
255 deleter(buffer, size, param);
258 BinaryQueue::BucketVisitor::~BucketVisitor()
262 BinaryQueue::BucketVisitorCall::BucketVisitorCall(BucketVisitor *visitor)
267 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();
294 BinaryQueueAutoPtr result(new BinaryQueue());
296 Flatten(bufferCopy.Get(), available);
297 result->AppendUnmanaged(bufferCopy.Get(), available, &BufferDeleterFree, NULL);
298 bufferCopy.Release();
304 size_t BinaryQueue::Write(const BinaryQueue &buffer, size_t bufferSize)
306 // Simulate output stream
307 AppendCopyFrom(buffer);