memprof_allocator.h 3.7 KB

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