Path: blob/main/contrib/llvm-project/compiler-rt/lib/scudo/standalone/allocator_common.h
35292 views
//===-- allocator_common.h --------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef SCUDO_ALLOCATOR_COMMON_H_9#define SCUDO_ALLOCATOR_COMMON_H_1011#include "common.h"12#include "list.h"1314namespace scudo {1516template <class SizeClassAllocator> struct TransferBatch {17typedef typename SizeClassAllocator::SizeClassMap SizeClassMap;18typedef typename SizeClassAllocator::CompactPtrT CompactPtrT;1920static const u16 MaxNumCached = SizeClassMap::MaxNumCachedHint;21void setFromArray(CompactPtrT *Array, u16 N) {22DCHECK_LE(N, MaxNumCached);23Count = N;24memcpy(Batch, Array, sizeof(Batch[0]) * Count);25}26void appendFromArray(CompactPtrT *Array, u16 N) {27DCHECK_LE(N, MaxNumCached - Count);28memcpy(Batch + Count, Array, sizeof(Batch[0]) * N);29// u16 will be promoted to int by arithmetic type conversion.30Count = static_cast<u16>(Count + N);31}32void appendFromTransferBatch(TransferBatch *B, u16 N) {33DCHECK_LE(N, MaxNumCached - Count);34DCHECK_GE(B->Count, N);35// Append from the back of `B`.36memcpy(Batch + Count, B->Batch + (B->Count - N), sizeof(Batch[0]) * N);37// u16 will be promoted to int by arithmetic type conversion.38Count = static_cast<u16>(Count + N);39B->Count = static_cast<u16>(B->Count - N);40}41void clear() { Count = 0; }42bool empty() { return Count == 0; }43void add(CompactPtrT P) {44DCHECK_LT(Count, MaxNumCached);45Batch[Count++] = P;46}47void moveToArray(CompactPtrT *Array) {48memcpy(Array, Batch, sizeof(Batch[0]) * Count);49clear();50}5152void moveNToArray(CompactPtrT *Array, u16 N) {53DCHECK_LE(N, Count);54memcpy(Array, Batch + Count - N, sizeof(Batch[0]) * N);55Count = static_cast<u16>(Count - N);56}57u16 getCount() const { return Count; }58bool isEmpty() const { return Count == 0U; }59CompactPtrT get(u16 I) const {60DCHECK_LE(I, Count);61return Batch[I];62}63TransferBatch *Next;6465private:66CompactPtrT Batch[MaxNumCached];67u16 Count;68};6970// A BatchGroup is used to collect blocks. Each group has a group id to71// identify the group kind of contained blocks.72template <class SizeClassAllocator> struct BatchGroup {73// `Next` is used by IntrusiveList.74BatchGroup *Next;75// The compact base address of each group76uptr CompactPtrGroupBase;77// Cache value of SizeClassAllocatorLocalCache::getMaxCached()78u16 MaxCachedPerBatch;79// Number of blocks pushed into this group. This is an increment-only80// counter.81uptr PushedBlocks;82// This is used to track how many bytes are not in-use since last time we83// tried to release pages.84uptr BytesInBGAtLastCheckpoint;85// Blocks are managed by TransferBatch in a list.86SinglyLinkedList<TransferBatch<SizeClassAllocator>> Batches;87};8889} // namespace scudo9091#endif // SCUDO_ALLOCATOR_COMMON_H_929394