#ifndef SCUDO_ALLOCATOR_COMMON_H_
#define SCUDO_ALLOCATOR_COMMON_H_
#include "common.h"
#include "list.h"
namespace scudo {
template <class SizeClassAllocator> struct Batch {
typedef typename SizeClassAllocator::SizeClassMap SizeClassMap;
typedef typename SizeClassAllocator::CompactPtrT CompactPtrT;
void setFromArray(CompactPtrT *Array, u16 N) {
DCHECK_LE(N, SizeClassAllocator::MaxNumBlocksInBatch);
Count = N;
memcpy(Blocks, Array, sizeof(Blocks[0]) * Count);
}
void appendFromArray(CompactPtrT *Array, u16 N) {
DCHECK_LE(N, SizeClassAllocator::MaxNumBlocksInBatch - Count);
memcpy(Blocks + Count, Array, sizeof(Blocks[0]) * N);
Count = static_cast<u16>(Count + N);
}
void appendFromBatch(Batch *B, u16 N) {
DCHECK_LE(N, SizeClassAllocator::MaxNumBlocksInBatch - Count);
DCHECK_GE(B->Count, N);
memcpy(Blocks + Count, B->Blocks + (B->Count - N), sizeof(Blocks[0]) * N);
Count = static_cast<u16>(Count + N);
B->Count = static_cast<u16>(B->Count - N);
}
void clear() { Count = 0; }
bool empty() { return Count == 0; }
void add(CompactPtrT P) {
DCHECK_LT(Count, SizeClassAllocator::MaxNumBlocksInBatch);
Blocks[Count++] = P;
}
void moveToArray(CompactPtrT *Array) {
memcpy(Array, Blocks, sizeof(Blocks[0]) * Count);
clear();
}
void moveNToArray(CompactPtrT *Array, u16 N) {
DCHECK_LE(N, Count);
memcpy(Array, Blocks + Count - N, sizeof(Blocks[0]) * N);
Count = static_cast<u16>(Count - N);
}
u16 getCount() const { return Count; }
bool isEmpty() const { return Count == 0U; }
CompactPtrT get(u16 I) const {
DCHECK_LE(I, Count);
return Blocks[I];
}
Batch *Next;
private:
u16 Count;
CompactPtrT Blocks[];
};
template <class SizeClassAllocator> struct BatchGroup {
BatchGroup *Next;
uptr CompactPtrGroupBase;
uptr BytesInBGAtLastCheckpoint;
SinglyLinkedList<Batch<SizeClassAllocator>> Batches;
u16 MaxCachedPerBatch;
};
}
#endif