blob: 8b51de6f350d5014a92dc959ac05939228d2f35d [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,
42 MBlockRacyStacks,
43 MBlockRacyAddresses,
44 MBlockAtExit,
45 MBlockFlag,
46 MBlockReport,
47 MBlockReportMop,
48 MBlockReportThread,
49 MBlockReportMutex,
50 MBlockReportLoc,
51 MBlockReportStack,
52 MBlockSuppression,
53 MBlockExpectRace,
54
55 // This must be the last.
56 MBlockTypeCount,
57};
58
59// For internal data structures.
60void *internal_alloc(MBlockType typ, uptr sz);
61void internal_free(void *p);
62
63template<typename T>
64void DestroyAndFree(T *&p) {
65 p->~T();
66 internal_free(p);
67 p = 0;
68}
69
70template<typename T>
71class InternalScopedBuf {
72 public:
73 explicit InternalScopedBuf(uptr cnt) {
74 cnt_ = cnt;
75 ptr_ = (T*)internal_alloc(MBlockScopedBuf, cnt * sizeof(T));
76 }
77
78 ~InternalScopedBuf() {
79 internal_free(ptr_);
80 }
81
82 operator T *() {
83 return ptr_;
84 }
85
86 T &operator[](uptr i) {
87 return ptr_[i];
88 }
89
90 T *Ptr() {
91 return ptr_;
92 }
93
94 uptr Count() {
95 return cnt_;
96 }
97
98 uptr Size() {
99 return cnt_ * sizeof(T);
100 }
101
102 private:
103 T *ptr_;
104 uptr cnt_;
105
106 InternalScopedBuf(const InternalScopedBuf&);
107 void operator = (const InternalScopedBuf&);
108};
109
110} // namespace __tsan
111#endif // TSAN_MMAN_H