blob: b338a9c53431f018c7bb5c04aec15e9e346b4de6 [file] [log] [blame]
Kostya Serebryany4ad375f2012-05-10 13:48:04 +00001//===-- tsan_mman.h ---------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of ThreadSanitizer (TSan), a race detector.
11//
12//===----------------------------------------------------------------------===//
13#ifndef TSAN_MMAN_H
14#define TSAN_MMAN_H
15
16#include "tsan_defs.h"
17
18namespace __tsan {
19
20// Descriptor of user's memory block.
21struct MBlock {
22 uptr size;
23};
24
25// For user allocations.
26void *user_alloc(ThreadState *thr, uptr pc, uptr sz);
27// Does not accept NULL.
28void user_free(ThreadState *thr, uptr pc, void *p);
29void *user_realloc(ThreadState *thr, uptr pc, void *p, uptr sz);
30void *user_alloc_aligned(ThreadState *thr, uptr pc, uptr sz, uptr align);
31// Given the pointer p into a valid allocated block,
32// returns the descriptor of the block.
33MBlock *user_mblock(ThreadState *thr, void *p);
34
35enum MBlockType {
36 MBlockScopedBuf,
37 MBlockString,
38 MBlockStackTrace,
39 MBlockSync,
40 MBlockClock,
41 MBlockThreadContex,
Dmitry Vyukovf6985e32012-05-22 14:34:43 +000042 MBlockDeadInfo,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000043 MBlockRacyStacks,
44 MBlockRacyAddresses,
45 MBlockAtExit,
46 MBlockFlag,
47 MBlockReport,
48 MBlockReportMop,
49 MBlockReportThread,
50 MBlockReportMutex,
51 MBlockReportLoc,
52 MBlockReportStack,
53 MBlockSuppression,
54 MBlockExpectRace,
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +000055 MBlockSignal,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000056
57 // This must be the last.
58 MBlockTypeCount,
59};
60
61// For internal data structures.
62void *internal_alloc(MBlockType typ, uptr sz);
63void internal_free(void *p);
64
65template<typename T>
66void DestroyAndFree(T *&p) {
67 p->~T();
68 internal_free(p);
69 p = 0;
70}
71
72template<typename T>
73class InternalScopedBuf {
74 public:
75 explicit InternalScopedBuf(uptr cnt) {
76 cnt_ = cnt;
77 ptr_ = (T*)internal_alloc(MBlockScopedBuf, cnt * sizeof(T));
78 }
79
80 ~InternalScopedBuf() {
81 internal_free(ptr_);
82 }
83
84 operator T *() {
85 return ptr_;
86 }
87
88 T &operator[](uptr i) {
89 return ptr_[i];
90 }
91
92 T *Ptr() {
93 return ptr_;
94 }
95
96 uptr Count() {
97 return cnt_;
98 }
99
100 uptr Size() {
101 return cnt_ * sizeof(T);
102 }
103
104 private:
105 T *ptr_;
106 uptr cnt_;
107
108 InternalScopedBuf(const InternalScopedBuf&);
109 void operator = (const InternalScopedBuf&);
110};
111
112} // namespace __tsan
113#endif // TSAN_MMAN_H