1//===-- memprof_allocator.h ------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is a part of MemProfiler, a memory profiler.
10//
11// MemProf-private header for memprof_allocator.cpp.
12//===----------------------------------------------------------------------===//
13
14#ifndef MEMPROF_ALLOCATOR_H
15#define MEMPROF_ALLOCATOR_H
16
17#include "memprof_flags.h"
18#include "memprof_interceptors.h"
19#include "memprof_internal.h"
20#include "sanitizer_common/sanitizer_allocator.h"
21#include "sanitizer_common/sanitizer_list.h"
22
23#if !defined(__x86_64__)
24#error Unsupported platform
25#endif
26#if !SANITIZER_CAN_USE_ALLOCATOR64
27#error Only 64-bit allocator supported
28#endif
29
30namespace __memprof {
31
32enum AllocType {
33 FROM_MALLOC = 1, // Memory block came from malloc, calloc, realloc, etc.
34 FROM_NEW = 2, // Memory block came from operator new.
35 FROM_NEW_BR = 3 // Memory block came from operator new [ ]
36};
37
38void InitializeAllocator();
39
40struct MemprofMapUnmapCallback {
41 void OnMap(uptr p, uptr size) const;
42 void OnMapSecondary(uptr p, uptr size, uptr user_begin,
43 uptr user_size) const {
44 OnMap(p, size);
45 }
46 void OnUnmap(uptr p, uptr size) const;
47};
48
49#if SANITIZER_APPLE
50constexpr uptr kAllocatorSpace = 0x600000000000ULL;
51#else
52constexpr uptr kAllocatorSpace = 0x500000000000ULL;
53#endif
54constexpr uptr kAllocatorSize = 0x40000000000ULL; // 4T.
55typedef DefaultSizeClassMap SizeClassMap;
56template <typename AddressSpaceViewTy>
57struct AP64 { // Allocator64 parameters. Deliberately using a short name.
58 static const uptr kSpaceBeg = kAllocatorSpace;
59 static const uptr kSpaceSize = kAllocatorSize;
60 static const uptr kMetadataSize = 0;
61 typedef __memprof::SizeClassMap SizeClassMap;
62 typedef MemprofMapUnmapCallback MapUnmapCallback;
63 static const uptr kFlags = 0;
64 using AddressSpaceView = AddressSpaceViewTy;
65};
66
67template <typename AddressSpaceView>
68using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>;
69using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;
70
71static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses;
72
73template <typename AddressSpaceView>
74using MemprofAllocatorASVT =
75 CombinedAllocator<PrimaryAllocatorASVT<AddressSpaceView>>;
76using MemprofAllocator = MemprofAllocatorASVT<LocalAddressSpaceView>;
77using AllocatorCache = MemprofAllocator::AllocatorCache;
78
79struct MemprofThreadLocalMallocStorage {
80 AllocatorCache allocator_cache;
81 void CommitBack();
82
83private:
84 // These objects are allocated via mmap() and are zero-initialized.
85 MemprofThreadLocalMallocStorage() {}
86};
87
88void *memprof_memalign(uptr alignment, uptr size, BufferedStackTrace *stack,
89 AllocType alloc_type);
90void memprof_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type);
91void memprof_delete(void *ptr, uptr size, uptr alignment,
92 BufferedStackTrace *stack, AllocType alloc_type);
93
94void *memprof_malloc(uptr size, BufferedStackTrace *stack);
95void *memprof_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack);
96void *memprof_realloc(void *p, uptr size, BufferedStackTrace *stack);
97void *memprof_reallocarray(void *p, uptr nmemb, uptr size,
98 BufferedStackTrace *stack);
99void *memprof_valloc(uptr size, BufferedStackTrace *stack);
100void *memprof_pvalloc(uptr size, BufferedStackTrace *stack);
101
102void *memprof_aligned_alloc(uptr alignment, uptr size,
103 BufferedStackTrace *stack);
104int memprof_posix_memalign(void **memptr, uptr alignment, uptr size,
105 BufferedStackTrace *stack);
106uptr memprof_malloc_usable_size(const void *ptr);
107
108void PrintInternalAllocatorStats();
109
110} // namespace __memprof
111#endif // MEMPROF_ALLOCATOR_H
112

source code of compiler-rt/lib/memprof/memprof_allocator.h