26class QSSGPerFrameAllocator
34 Alignment =
sizeof(
void *),
35 SlabSize = ChunkSize -
sizeof(Slab *),
36 MaxAlloc = ChunkSize/2
42 previous->next =
this;
45 quint8 data[SlabSize];
47 Q_STATIC_ASSERT(
sizeof(Slab) == ChunkSize);
48 Q_STATIC_ASSERT(
alignof(Slab) == Alignment);
50 Slab *first =
nullptr;
51 Slab *current =
nullptr;
56 first = current =
new Slab;
68 void *allocate(size_t size)
70 size = (size + Alignment - 1) & ~(Alignment - 1);
71 Q_ASSERT(size <= SlabSize);
72 Q_ASSERT(!(offset % Alignment));
74 size_t amountLeftInSlab = SlabSize - offset;
75 if (size > amountLeftInSlab) {
77 current = current->next;
79 current =
new Slab(current);
83 quint8 *data = current->data + offset;
89 void reset() { current = first; offset = 0; }
97 Slab *current =
nullptr;
102 ~LargeAllocator() { deallocateAll(); }
107 Slab *n = current->next;
114 void *allocate(size_t size)
116 quint8 *mem =
reinterpret_cast<quint8 *>(::malloc(
sizeof(Slab) + size));
117 Slab *s =
reinterpret_cast<Slab *>(mem);
120 return mem +
sizeof(Slab);
124 FastAllocator m_fastAllocator;
125 LargeAllocator m_largeAllocator;
128 QSSGPerFrameAllocator() {}
130 inline void *allocate(size_t size)
132 if (size < FastAllocator::MaxAlloc)
133 return m_fastAllocator.allocate(size);
135 return m_largeAllocator.allocate(size);
140 m_fastAllocator.reset();
141 m_largeAllocator.deallocateAll();