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>
33 BinaryQueue::BinaryQueue()
38 BinaryQueue::BinaryQueue(const BinaryQueue &other)
41 AppendCopyFrom(other);
44 BinaryQueue::~BinaryQueue()
46 // Remove all remainig buckets
50 const BinaryQueue &BinaryQueue::operator=(const BinaryQueue &other)
55 AppendCopyFrom(other);
61 void BinaryQueue::AppendCopyFrom(const BinaryQueue &other)
63 // To speed things up, always copy as one bucket
64 void *bufferCopy = malloc(other.m_size);
66 if (bufferCopy == NULL)
67 throw std::bad_alloc();
71 other.Flatten(bufferCopy, other.m_size);
72 AppendUnmanaged(bufferCopy, other.m_size, &BufferDeleterFree, NULL);
74 catch (const std::bad_alloc &)
76 // Free allocated memory
82 void BinaryQueue::AppendMoveFrom(BinaryQueue &other)
85 std::copy(other.m_buckets.begin(), other.m_buckets.end(), std::back_inserter(m_buckets));
86 m_size += other.m_size;
88 // Clear other, but do not free memory
89 other.m_buckets.clear();
93 void BinaryQueue::AppendCopyTo(BinaryQueue &other) const
95 other.AppendCopyFrom(*this);
98 void BinaryQueue::AppendMoveTo(BinaryQueue &other)
100 other.AppendMoveFrom(*this);
103 void BinaryQueue::Clear()
105 std::for_each(m_buckets.begin(), m_buckets.end(), &DeleteBucket);
110 void BinaryQueue::AppendCopy(const void* buffer, size_t bufferSize)
112 // Create data copy with malloc/free
113 void *bufferCopy = malloc(bufferSize);
115 // Check if allocation succeded
116 if (bufferCopy == NULL)
117 throw std::bad_alloc();
120 memcpy(bufferCopy, buffer, bufferSize);
124 // Try to append new bucket
125 AppendUnmanaged(bufferCopy, bufferSize, &BufferDeleterFree, NULL);
127 catch (const std::bad_alloc &)
129 // Free allocated memory
135 void BinaryQueue::AppendUnmanaged(const void* buffer, size_t bufferSize, BufferDeleter deleter, void* userParam)
137 // Do not attach empty buckets
140 deleter(buffer, bufferSize, userParam);
144 // Just add new bucket with selected deleter
145 m_buckets.push_back(new Bucket(buffer, bufferSize, deleter, userParam));
147 // Increase total queue size
148 m_size += bufferSize;
151 size_t BinaryQueue::Size() const
156 bool BinaryQueue::Empty() const
161 void BinaryQueue::Consume(size_t size)
165 Throw(Exception::OutOfData);
167 size_t bytesLeft = size;
169 // Consume data and/or remove buckets
170 while (bytesLeft > 0)
173 size_t count = std::min(bytesLeft, m_buckets.front()->left);
175 m_buckets.front()->ptr = static_cast<const char *>(m_buckets.front()->ptr) + count;
176 m_buckets.front()->left -= count;
180 if (m_buckets.front()->left == 0)
182 DeleteBucket(m_buckets.front());
183 m_buckets.pop_front();
188 void BinaryQueue::Flatten(void *buffer, size_t bufferSize) const
194 if (bufferSize > m_size)
195 Throw(Exception::OutOfData);
197 size_t bytesLeft = bufferSize;
199 BucketList::const_iterator bucketIterator = m_buckets.begin();
200 Assert(m_buckets.end() != bucketIterator);
203 while (bytesLeft > 0)
206 size_t count = std::min(bytesLeft, (*bucketIterator)->left);
208 // Copy data to user pointer
209 memcpy(ptr, (*bucketIterator)->ptr, count);
211 // Update flattened bytes count
213 ptr = static_cast<char *>(ptr) + count;
220 void BinaryQueue::FlattenConsume(void *buffer, size_t bufferSize)
223 Flatten(buffer, bufferSize);
227 void BinaryQueue::DeleteBucket(BinaryQueue::Bucket *bucket)
232 void BinaryQueue::BufferDeleterFree(const void* data, size_t dataSize, void* userParam)
237 // Default free deleter
238 free(const_cast<void *>(data));
241 BinaryQueue::Bucket::Bucket(const void* data, size_t dataSize, BufferDeleter dataDeleter, void* userParam)
246 deleter(dataDeleter),
249 Assert(data != NULL);
250 Assert(deleter != NULL);
253 BinaryQueue::Bucket::~Bucket()
255 // Invoke deleter on bucket data
256 deleter(buffer, size, param);
259 BinaryQueue::BucketVisitor::~BucketVisitor()
263 BinaryQueue::BucketVisitorCall::BucketVisitorCall(BucketVisitor *visitor)
268 BinaryQueue::BucketVisitorCall::~BucketVisitorCall()
272 void BinaryQueue::BucketVisitorCall::operator()(Bucket *bucket) const
274 m_visitor->OnVisitBucket(bucket->ptr, bucket->left);
277 void BinaryQueue::VisitBuckets(BucketVisitor *visitor) const
279 Assert(visitor != NULL);
282 std::for_each(m_buckets.begin(), m_buckets.end(), BucketVisitorCall(visitor));
285 BinaryQueueAutoPtr BinaryQueue::Read(size_t size)
287 // Simulate input stream
288 size_t available = std::min(size, m_size);
290 ScopedFree<void> bufferCopy(malloc(available));
293 throw std::bad_alloc();
295 BinaryQueueAutoPtr result(new BinaryQueue());
297 Flatten(bufferCopy.Get(), available);
298 result->AppendUnmanaged(bufferCopy.Get(), available, &BufferDeleterFree, NULL);
299 bufferCopy.Release();
305 size_t BinaryQueue::Write(const BinaryQueue &buffer, size_t bufferSize)
307 // Simulate output stream
308 AppendCopyFrom(buffer);