tsan_mman.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //===-- tsan_mman.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 ThreadSanitizer (TSan), a race detector.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef TSAN_MMAN_H
  13. #define TSAN_MMAN_H
  14. #include "tsan_defs.h"
  15. namespace __tsan {
  16. const uptr kDefaultAlignment = 16;
  17. void InitializeAllocator();
  18. void InitializeAllocatorLate();
  19. void ReplaceSystemMalloc();
  20. void AllocatorProcStart(Processor *proc);
  21. void AllocatorProcFinish(Processor *proc);
  22. void AllocatorPrintStats();
  23. void AllocatorLock();
  24. void AllocatorUnlock();
  25. void GlobalProcessorLock();
  26. void GlobalProcessorUnlock();
  27. // For user allocations.
  28. void *user_alloc_internal(ThreadState *thr, uptr pc, uptr sz,
  29. uptr align = kDefaultAlignment, bool signal = true);
  30. // Does not accept NULL.
  31. void user_free(ThreadState *thr, uptr pc, void *p, bool signal = true);
  32. // Interceptor implementations.
  33. void *user_alloc(ThreadState *thr, uptr pc, uptr sz);
  34. void *user_calloc(ThreadState *thr, uptr pc, uptr sz, uptr n);
  35. void *user_realloc(ThreadState *thr, uptr pc, void *p, uptr sz);
  36. void *user_reallocarray(ThreadState *thr, uptr pc, void *p, uptr sz, uptr n);
  37. void *user_memalign(ThreadState *thr, uptr pc, uptr align, uptr sz);
  38. int user_posix_memalign(ThreadState *thr, uptr pc, void **memptr, uptr align,
  39. uptr sz);
  40. void *user_aligned_alloc(ThreadState *thr, uptr pc, uptr align, uptr sz);
  41. void *user_valloc(ThreadState *thr, uptr pc, uptr sz);
  42. void *user_pvalloc(ThreadState *thr, uptr pc, uptr sz);
  43. uptr user_alloc_usable_size(const void *p);
  44. // Invoking malloc/free hooks that may be installed by the user.
  45. void invoke_malloc_hook(void *ptr, uptr size);
  46. void invoke_free_hook(void *ptr);
  47. // For internal data structures.
  48. void *Alloc(uptr sz);
  49. void FreeImpl(void *p);
  50. template <typename T, typename... Args>
  51. T *New(Args &&...args) {
  52. return new (Alloc(sizeof(T))) T(static_cast<Args &&>(args)...);
  53. }
  54. template <typename T>
  55. void Free(T *&p) {
  56. if (p == nullptr)
  57. return;
  58. FreeImpl(p);
  59. p = nullptr;
  60. }
  61. template <typename T>
  62. void DestroyAndFree(T *&p) {
  63. if (p == nullptr)
  64. return;
  65. p->~T();
  66. Free(p);
  67. }
  68. } // namespace __tsan
  69. #endif // TSAN_MMAN_H