File indexing completed on 2025-01-05 04:37:29
0001 /* 0002 SPDX-FileCopyrightText: 2011 Joris Guisson <joris.guisson@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "bufferpool.h" 0008 0009 namespace bt 0010 { 0011 Buffer::Buffer(Data data, bt::Uint32 fill, bt::Uint32 cap, QWeakPointer<BufferPool> pool) 0012 : data(data) 0013 , fill(fill) 0014 , cap(cap) 0015 , pool(pool) 0016 { 0017 } 0018 0019 Buffer::~Buffer() 0020 { 0021 QSharedPointer<BufferPool> ptr = pool.toStrongRef(); 0022 if (ptr) 0023 ptr->release(data, cap); 0024 } 0025 0026 BufferPool::BufferPool() 0027 { 0028 } 0029 0030 BufferPool::~BufferPool() 0031 { 0032 } 0033 0034 Buffer::Ptr BufferPool::get(bt::Uint32 min_size) 0035 { 0036 QMutexLocker lock(&mutex); 0037 FreeBufferMap::iterator i = free_buffers.lower_bound(min_size); 0038 if (i != free_buffers.end() && !i->second.empty()) { 0039 Buffer::Data data = i->second.front(); 0040 i->second.pop_front(); 0041 return Buffer::Ptr(new Buffer(data, min_size, i->first, self)); 0042 } else { 0043 return Buffer::Ptr(new Buffer(Buffer::Data(new Uint8[min_size]), min_size, min_size, self)); 0044 } 0045 } 0046 0047 void BufferPool::release(Buffer::Data data, bt::Uint32 size) 0048 { 0049 QMutexLocker lock(&mutex); 0050 free_buffers[size].push_back(data); 0051 } 0052 0053 void BufferPool::clear() 0054 { 0055 QMutexLocker lock(&mutex); 0056 free_buffers.clear(); 0057 } 0058 0059 } /* namespace bt */