Krita Source Code Documentation
Loading...
Searching...
No Matches
KisOptimizedByteArray::PooledMemoryAllocator Struct Reference

#include <KisOptimizedByteArray.h>

+ Inheritance diagram for KisOptimizedByteArray::PooledMemoryAllocator:

Public Member Functions

MemoryChunk alloc (int size) override
 
void free (MemoryChunk chunk) override
 
 PooledMemoryAllocator ()
 
 ~PooledMemoryAllocator ()
 
- Public Member Functions inherited from KisOptimizedByteArray::MemoryAllocator
virtual ~MemoryAllocator ()=default
 

Private Attributes

QVector< MemoryChunkm_chunks
 
KisRollingMeanAccumulatorWrapper m_meanSize
 
QMutex m_mutex
 

Detailed Description

Definition at line 34 of file KisOptimizedByteArray.h.

Constructor & Destructor Documentation

◆ PooledMemoryAllocator()

KisOptimizedByteArray::PooledMemoryAllocator::PooledMemoryAllocator ( )

Definition at line 67 of file KisOptimizedByteArray.cpp.

68 : m_meanSize(500)
69{
70}

◆ ~PooledMemoryAllocator()

KisOptimizedByteArray::PooledMemoryAllocator::~PooledMemoryAllocator ( )

Definition at line 72 of file KisOptimizedByteArray.cpp.

73{
74 Q_FOREACH (const MemoryChunk &chunk, m_chunks) {
75 delete[] chunk.first;
76 }
77}
std::pair< quint8 *, int > MemoryChunk

Member Function Documentation

◆ alloc()

KisOptimizedByteArray::MemoryChunk KisOptimizedByteArray::PooledMemoryAllocator::alloc ( int size)
overridevirtual

Implements KisOptimizedByteArray::MemoryAllocator.

Definition at line 80 of file KisOptimizedByteArray.cpp.

81{
82 MemoryChunk chunk;
83
84 {
85 QMutexLocker l(&m_mutex);
86 if (!m_chunks.isEmpty()) {
87 chunk = m_chunks.takeLast();
88 }
89
91 }
92
93 if (chunk.second < size) {
94 delete[] chunk.first;
95
96 // we alloc a bit more memory for the dabs to let the chunks
97 // be more reusable
98 const int allocSize = 1.2 * size;
99 chunk = KisOptimizedByteArray::MemoryChunk(new quint8[allocSize], allocSize);
100 }
101
102 return chunk;
103}

References KisOptimizedByteArray::size().

◆ free()

void KisOptimizedByteArray::PooledMemoryAllocator::free ( KisOptimizedByteArray::MemoryChunk chunk)
overridevirtual

Implements KisOptimizedByteArray::MemoryAllocator.

Definition at line 105 of file KisOptimizedByteArray.cpp.

106{
107 if (chunk.first) {
108 QMutexLocker l(&m_mutex);
109
110 // keep bigger chunks for ourselves and return the
111 // smaller ones to the system
112 if (chunk.second > 0.8 * m_meanSize.rollingMean()) {
113 m_chunks.append(chunk);
114 } else {
115 delete[] chunk.first;
116 }
117 }
118}

Member Data Documentation

◆ m_chunks

QVector<MemoryChunk> KisOptimizedByteArray::PooledMemoryAllocator::m_chunks
private

Definition at line 43 of file KisOptimizedByteArray.h.

◆ m_meanSize

KisRollingMeanAccumulatorWrapper KisOptimizedByteArray::PooledMemoryAllocator::m_meanSize
private

Definition at line 44 of file KisOptimizedByteArray.h.

◆ m_mutex

QMutex KisOptimizedByteArray::PooledMemoryAllocator::m_mutex
private

Definition at line 42 of file KisOptimizedByteArray.h.


The documentation for this struct was generated from the following files: