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