memprof_allocator.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #ifndef MEMPROF_ALLOCATOR_H
  14. #define MEMPROF_ALLOCATOR_H
  15. #include "memprof_flags.h"
  16. #include "memprof_interceptors.h"
  17. #include "memprof_internal.h"
  18. #include "sanitizer_common/sanitizer_allocator.h"
  19. #include "sanitizer_common/sanitizer_list.h"
  20. #if !defined(__x86_64__)
  21. #error Unsupported platform
  22. #endif
  23. #if !SANITIZER_CAN_USE_ALLOCATOR64
  24. #error Only 64-bit allocator supported
  25. #endif
  26. namespace __memprof {
  27. enum AllocType {
  28. FROM_MALLOC = 1, // Memory block came from malloc, calloc, realloc, etc.
  29. FROM_NEW = 2, // Memory block came from operator new.
  30. FROM_NEW_BR = 3 // Memory block came from operator new [ ]
  31. };
  32. void InitializeAllocator();
  33. struct MemprofMapUnmapCallback {
  34. void OnMap(uptr p, uptr size) const;
  35. void OnMapSecondary(uptr p, uptr size, uptr user_begin,
  36. uptr user_size) const {
  37. OnMap(p, size);
  38. }
  39. void OnUnmap(uptr p, uptr size) const;
  40. };
  41. constexpr uptr kAllocatorSpace = 0x600000000000ULL;
  42. constexpr uptr kAllocatorSize = 0x40000000000ULL; // 4T.
  43. typedef DefaultSizeClassMap SizeClassMap;
  44. template <typename AddressSpaceViewTy>
  45. struct AP64 { // Allocator64 parameters. Deliberately using a short name.
  46. static const uptr kSpaceBeg = kAllocatorSpace;
  47. static const uptr kSpaceSize = kAllocatorSize;
  48. static const uptr kMetadataSize = 0;
  49. typedef __memprof::SizeClassMap SizeClassMap;
  50. typedef MemprofMapUnmapCallback MapUnmapCallback;
  51. static const uptr kFlags = 0;
  52. using AddressSpaceView = AddressSpaceViewTy;
  53. };
  54. template <typename AddressSpaceView>
  55. using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>;
  56. using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;
  57. static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses;
  58. template <typename AddressSpaceView>
  59. using MemprofAllocatorASVT =
  60. CombinedAllocator<PrimaryAllocatorASVT<AddressSpaceView>>;
  61. using MemprofAllocator = MemprofAllocatorASVT<LocalAddressSpaceView>;
  62. using AllocatorCache = MemprofAllocator::AllocatorCache;
  63. struct MemprofThreadLocalMallocStorage {
  64. AllocatorCache allocator_cache;
  65. void CommitBack();
  66. private:
  67. // These objects are allocated via mmap() and are zero-initialized.
  68. MemprofThreadLocalMallocStorage() {}
  69. };
  70. void *memprof_memalign(uptr alignment, uptr size, BufferedStackTrace *stack,
  71. AllocType alloc_type);
  72. void memprof_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type);
  73. void memprof_delete(void *ptr, uptr size, uptr alignment,
  74. BufferedStackTrace *stack, AllocType alloc_type);
  75. void *memprof_malloc(uptr size, BufferedStackTrace *stack);
  76. void *memprof_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack);
  77. void *memprof_realloc(void *p, uptr size, BufferedStackTrace *stack);
  78. void *memprof_reallocarray(void *p, uptr nmemb, uptr size,
  79. BufferedStackTrace *stack);
  80. void *memprof_valloc(uptr size, BufferedStackTrace *stack);
  81. void *memprof_pvalloc(uptr size, BufferedStackTrace *stack);
  82. void *memprof_aligned_alloc(uptr alignment, uptr size,
  83. BufferedStackTrace *stack);
  84. int memprof_posix_memalign(void **memptr, uptr alignment, uptr size,
  85. BufferedStackTrace *stack);
  86. uptr memprof_malloc_usable_size(const void *ptr, uptr pc, uptr bp);
  87. void PrintInternalAllocatorStats();
  88. } // namespace __memprof
  89. #endif // MEMPROF_ALLOCATOR_H