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 | |
30 | namespace __memprof { |
31 | |
32 | enum 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 | |
38 | void InitializeAllocator(); |
39 | |
40 | struct 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 |
50 | constexpr uptr kAllocatorSpace = 0x600000000000ULL; |
51 | #else |
52 | constexpr uptr kAllocatorSpace = 0x500000000000ULL; |
53 | #endif |
54 | constexpr uptr kAllocatorSize = 0x40000000000ULL; // 4T. |
55 | typedef DefaultSizeClassMap SizeClassMap; |
56 | template <typename AddressSpaceViewTy> |
57 | struct 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 | |
67 | template <typename AddressSpaceView> |
68 | using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>; |
69 | using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>; |
70 | |
71 | static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses; |
72 | |
73 | template <typename AddressSpaceView> |
74 | using MemprofAllocatorASVT = |
75 | CombinedAllocator<PrimaryAllocatorASVT<AddressSpaceView>>; |
76 | using MemprofAllocator = MemprofAllocatorASVT<LocalAddressSpaceView>; |
77 | using AllocatorCache = MemprofAllocator::AllocatorCache; |
78 | |
79 | struct MemprofThreadLocalMallocStorage { |
80 | AllocatorCache allocator_cache; |
81 | void CommitBack(); |
82 | |
83 | private: |
84 | // These objects are allocated via mmap() and are zero-initialized. |
85 | MemprofThreadLocalMallocStorage() {} |
86 | }; |
87 | |
88 | void *memprof_memalign(uptr alignment, uptr size, BufferedStackTrace *stack, |
89 | AllocType alloc_type); |
90 | void memprof_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type); |
91 | void memprof_delete(void *ptr, uptr size, uptr alignment, |
92 | BufferedStackTrace *stack, AllocType alloc_type); |
93 | |
94 | void *memprof_malloc(uptr size, BufferedStackTrace *stack); |
95 | void *memprof_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack); |
96 | void *memprof_realloc(void *p, uptr size, BufferedStackTrace *stack); |
97 | void *memprof_reallocarray(void *p, uptr nmemb, uptr size, |
98 | BufferedStackTrace *stack); |
99 | void *memprof_valloc(uptr size, BufferedStackTrace *stack); |
100 | void *memprof_pvalloc(uptr size, BufferedStackTrace *stack); |
101 | |
102 | void *memprof_aligned_alloc(uptr alignment, uptr size, |
103 | BufferedStackTrace *stack); |
104 | int memprof_posix_memalign(void **memptr, uptr alignment, uptr size, |
105 | BufferedStackTrace *stack); |
106 | uptr memprof_malloc_usable_size(const void *ptr); |
107 | |
108 | void PrintInternalAllocatorStats(); |
109 | |
110 | } // namespace __memprof |
111 | #endif // MEMPROF_ALLOCATOR_H |
112 | |