xref: /freebsd/contrib/llvm-project/compiler-rt/lib/tsan/rtl/tsan_mman.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===-- tsan_mman.h ---------------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file is a part of ThreadSanitizer (TSan), a race detector.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric #ifndef TSAN_MMAN_H
130b57cec5SDimitry Andric #define TSAN_MMAN_H
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "tsan_defs.h"
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric namespace __tsan {
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric const uptr kDefaultAlignment = 16;
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric void InitializeAllocator();
220b57cec5SDimitry Andric void InitializeAllocatorLate();
230b57cec5SDimitry Andric void ReplaceSystemMalloc();
240b57cec5SDimitry Andric void AllocatorProcStart(Processor *proc);
250b57cec5SDimitry Andric void AllocatorProcFinish(Processor *proc);
260b57cec5SDimitry Andric void AllocatorPrintStats();
27*0fca6ea1SDimitry Andric void AllocatorLockBeforeFork();
28*0fca6ea1SDimitry Andric void AllocatorUnlockAfterFork(bool child);
290eae32dcSDimitry Andric void GlobalProcessorLock();
300eae32dcSDimitry Andric void GlobalProcessorUnlock();
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric // For user allocations.
330b57cec5SDimitry Andric void *user_alloc_internal(ThreadState *thr, uptr pc, uptr sz,
340b57cec5SDimitry Andric                           uptr align = kDefaultAlignment, bool signal = true);
350b57cec5SDimitry Andric // Does not accept NULL.
360b57cec5SDimitry Andric void user_free(ThreadState *thr, uptr pc, void *p, bool signal = true);
370b57cec5SDimitry Andric // Interceptor implementations.
380b57cec5SDimitry Andric void *user_alloc(ThreadState *thr, uptr pc, uptr sz);
390b57cec5SDimitry Andric void *user_calloc(ThreadState *thr, uptr pc, uptr sz, uptr n);
400b57cec5SDimitry Andric void *user_realloc(ThreadState *thr, uptr pc, void *p, uptr sz);
410b57cec5SDimitry Andric void *user_reallocarray(ThreadState *thr, uptr pc, void *p, uptr sz, uptr n);
420b57cec5SDimitry Andric void *user_memalign(ThreadState *thr, uptr pc, uptr align, uptr sz);
430b57cec5SDimitry Andric int user_posix_memalign(ThreadState *thr, uptr pc, void **memptr, uptr align,
440b57cec5SDimitry Andric                         uptr sz);
450b57cec5SDimitry Andric void *user_aligned_alloc(ThreadState *thr, uptr pc, uptr align, uptr sz);
460b57cec5SDimitry Andric void *user_valloc(ThreadState *thr, uptr pc, uptr sz);
470b57cec5SDimitry Andric void *user_pvalloc(ThreadState *thr, uptr pc, uptr sz);
480b57cec5SDimitry Andric uptr user_alloc_usable_size(const void *p);
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric // Invoking malloc/free hooks that may be installed by the user.
510b57cec5SDimitry Andric void invoke_malloc_hook(void *ptr, uptr size);
520b57cec5SDimitry Andric void invoke_free_hook(void *ptr);
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric // For internal data structures.
55349cc55cSDimitry Andric void *Alloc(uptr sz);
56349cc55cSDimitry Andric void FreeImpl(void *p);
57349cc55cSDimitry Andric 
58349cc55cSDimitry Andric template <typename T, typename... Args>
New(Args &&...args)59349cc55cSDimitry Andric T *New(Args &&...args) {
60349cc55cSDimitry Andric   return new (Alloc(sizeof(T))) T(static_cast<Args &&>(args)...);
61349cc55cSDimitry Andric }
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric template <typename T>
Free(T * & p)64349cc55cSDimitry Andric void Free(T *&p) {
65349cc55cSDimitry Andric   if (p == nullptr)
66349cc55cSDimitry Andric     return;
67349cc55cSDimitry Andric   FreeImpl(p);
68349cc55cSDimitry Andric   p = nullptr;
69349cc55cSDimitry Andric }
70349cc55cSDimitry Andric 
71349cc55cSDimitry Andric template <typename T>
DestroyAndFree(T * & p)72349cc55cSDimitry Andric void DestroyAndFree(T *&p) {
73349cc55cSDimitry Andric   if (p == nullptr)
74349cc55cSDimitry Andric     return;
750b57cec5SDimitry Andric   p->~T();
76349cc55cSDimitry Andric   Free(p);
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric }  // namespace __tsan
800b57cec5SDimitry Andric #endif  // TSAN_MMAN_H
81