Path: blob/main/contrib/llvm-project/compiler-rt/lib/memprof/memprof_allocator.h
35236 views
//===-- memprof_allocator.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//===----------------------------------------------------------------------===//7//8// This file is a part of MemProfiler, a memory profiler.9//10// MemProf-private header for memprof_allocator.cpp.11//===----------------------------------------------------------------------===//1213#ifndef MEMPROF_ALLOCATOR_H14#define MEMPROF_ALLOCATOR_H1516#include "memprof_flags.h"17#include "memprof_interceptors.h"18#include "memprof_internal.h"19#include "sanitizer_common/sanitizer_allocator.h"20#include "sanitizer_common/sanitizer_list.h"2122#if !defined(__x86_64__)23#error Unsupported platform24#endif25#if !SANITIZER_CAN_USE_ALLOCATOR6426#error Only 64-bit allocator supported27#endif2829namespace __memprof {3031enum AllocType {32FROM_MALLOC = 1, // Memory block came from malloc, calloc, realloc, etc.33FROM_NEW = 2, // Memory block came from operator new.34FROM_NEW_BR = 3 // Memory block came from operator new [ ]35};3637void InitializeAllocator();3839struct MemprofMapUnmapCallback {40void OnMap(uptr p, uptr size) const;41void OnMapSecondary(uptr p, uptr size, uptr user_begin,42uptr user_size) const {43OnMap(p, size);44}45void OnUnmap(uptr p, uptr size) const;46};4748constexpr uptr kAllocatorSpace = ~(uptr)0;49constexpr uptr kAllocatorSize = 0x40000000000ULL; // 4T.50typedef DefaultSizeClassMap SizeClassMap;51template <typename AddressSpaceViewTy>52struct AP64 { // Allocator64 parameters. Deliberately using a short name.53static const uptr kSpaceBeg = kAllocatorSpace;54static const uptr kSpaceSize = kAllocatorSize;55static const uptr kMetadataSize = 0;56typedef __memprof::SizeClassMap SizeClassMap;57typedef MemprofMapUnmapCallback MapUnmapCallback;58static const uptr kFlags = 0;59using AddressSpaceView = AddressSpaceViewTy;60};6162template <typename AddressSpaceView>63using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>;64using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;6566static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses;6768template <typename AddressSpaceView>69using MemprofAllocatorASVT =70CombinedAllocator<PrimaryAllocatorASVT<AddressSpaceView>>;71using MemprofAllocator = MemprofAllocatorASVT<LocalAddressSpaceView>;72using AllocatorCache = MemprofAllocator::AllocatorCache;7374struct MemprofThreadLocalMallocStorage {75AllocatorCache allocator_cache;76void CommitBack();7778private:79// These objects are allocated via mmap() and are zero-initialized.80MemprofThreadLocalMallocStorage() {}81};8283void *memprof_memalign(uptr alignment, uptr size, BufferedStackTrace *stack,84AllocType alloc_type);85void memprof_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type);86void memprof_delete(void *ptr, uptr size, uptr alignment,87BufferedStackTrace *stack, AllocType alloc_type);8889void *memprof_malloc(uptr size, BufferedStackTrace *stack);90void *memprof_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack);91void *memprof_realloc(void *p, uptr size, BufferedStackTrace *stack);92void *memprof_reallocarray(void *p, uptr nmemb, uptr size,93BufferedStackTrace *stack);94void *memprof_valloc(uptr size, BufferedStackTrace *stack);95void *memprof_pvalloc(uptr size, BufferedStackTrace *stack);9697void *memprof_aligned_alloc(uptr alignment, uptr size,98BufferedStackTrace *stack);99int memprof_posix_memalign(void **memptr, uptr alignment, uptr size,100BufferedStackTrace *stack);101uptr memprof_malloc_usable_size(const void *ptr);102103void PrintInternalAllocatorStats();104105} // namespace __memprof106#endif // MEMPROF_ALLOCATOR_H107108109