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