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