24class QSSGPerFrameAllocator
32 Alignment =
sizeof(
void *),
33 SlabSize = ChunkSize -
sizeof(Slab *),
34 MaxAlloc = ChunkSize/2
40 previous->next =
this;
43 quint8 data[SlabSize];
45 Q_STATIC_ASSERT(
sizeof(Slab) == ChunkSize);
46 Q_STATIC_ASSERT(
alignof(Slab) == Alignment);
48 Slab *first =
nullptr;
49 Slab *current =
nullptr;
54 first = current =
new Slab;
66 void *allocate(size_t size)
68 size = (size + Alignment - 1) & ~(Alignment - 1);
69 Q_ASSERT(size <= SlabSize);
70 Q_ASSERT(!(offset % Alignment));
72 size_t amountLeftInSlab = SlabSize - offset;
73 if (size > amountLeftInSlab) {
75 current = current->next;
77 current =
new Slab(current);
81 quint8 *data = current->data + offset;
87 void reset() { current = first; offset = 0; }
95 Slab *current =
nullptr;
100 ~LargeAllocator() { deallocateAll(); }
105 Slab *n = current->next;
112 void *allocate(size_t size)
114 quint8 *mem =
reinterpret_cast<quint8 *>(::malloc(
sizeof(Slab) + size));
115 Slab *s =
reinterpret_cast<Slab *>(mem);
118 return mem +
sizeof(Slab);
122 FastAllocator m_fastAllocator;
123 LargeAllocator m_largeAllocator;
126 QSSGPerFrameAllocator() {}
128 inline void *allocate(size_t size)
130 if (size < FastAllocator::MaxAlloc)
131 return m_fastAllocator.allocate(size);
133 return m_largeAllocator.allocate(size);
138 m_fastAllocator.reset();
139 m_largeAllocator.deallocateAll();