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 constexpr uptr kAllocatorSpace = 0x600000000000ULL; 50 constexpr uptr kAllocatorSize = 0x40000000000ULL; // 4T. 51 typedef DefaultSizeClassMap SizeClassMap; 52 template <typename AddressSpaceViewTy> 53 struct AP64 { // Allocator64 parameters. Deliberately using a short name. 54 static const uptr kSpaceBeg = kAllocatorSpace; 55 static const uptr kSpaceSize = kAllocatorSize; 56 static const uptr kMetadataSize = 0; 57 typedef __memprof::SizeClassMap SizeClassMap; 58 typedef MemprofMapUnmapCallback MapUnmapCallback; 59 static const uptr kFlags = 0; 60 using AddressSpaceView = AddressSpaceViewTy; 61 }; 62 63 template <typename AddressSpaceView> 64 using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>; 65 using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>; 66 67 static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses; 68 69 template <typename AddressSpaceView> 70 using MemprofAllocatorASVT = 71 CombinedAllocator<PrimaryAllocatorASVT<AddressSpaceView>>; 72 using MemprofAllocator = MemprofAllocatorASVT<LocalAddressSpaceView>; 73 using AllocatorCache = MemprofAllocator::AllocatorCache; 74 75 struct MemprofThreadLocalMallocStorage { 76 AllocatorCache allocator_cache; 77 void CommitBack(); 78 79 private: 80 // These objects are allocated via mmap() and are zero-initialized. 81 MemprofThreadLocalMallocStorage() {} 82 }; 83 84 void *memprof_memalign(uptr alignment, uptr size, BufferedStackTrace *stack, 85 AllocType alloc_type); 86 void memprof_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type); 87 void memprof_delete(void *ptr, uptr size, uptr alignment, 88 BufferedStackTrace *stack, AllocType alloc_type); 89 90 void *memprof_malloc(uptr size, BufferedStackTrace *stack); 91 void *memprof_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack); 92 void *memprof_realloc(void *p, uptr size, BufferedStackTrace *stack); 93 void *memprof_reallocarray(void *p, uptr nmemb, uptr size, 94 BufferedStackTrace *stack); 95 void *memprof_valloc(uptr size, BufferedStackTrace *stack); 96 void *memprof_pvalloc(uptr size, BufferedStackTrace *stack); 97 98 void *memprof_aligned_alloc(uptr alignment, uptr size, 99 BufferedStackTrace *stack); 100 int memprof_posix_memalign(void **memptr, uptr alignment, uptr size, 101 BufferedStackTrace *stack); 102 uptr memprof_malloc_usable_size(const void *ptr, uptr pc, uptr bp); 103 104 void PrintInternalAllocatorStats(); 105 106 } // namespace __memprof 107 #endif // MEMPROF_ALLOCATOR_H 108