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