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