Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 1 | //===-- 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 | |
| 18 | namespace __tsan { |
| 19 | |
| 20 | // Descriptor of user's memory block. |
| 21 | struct MBlock { |
| 22 | uptr size; |
| 23 | }; |
| 24 | |
| 25 | // For user allocations. |
| 26 | void *user_alloc(ThreadState *thr, uptr pc, uptr sz); |
| 27 | // Does not accept NULL. |
| 28 | void user_free(ThreadState *thr, uptr pc, void *p); |
| 29 | void *user_realloc(ThreadState *thr, uptr pc, void *p, uptr sz); |
| 30 | void *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. |
| 33 | MBlock *user_mblock(ThreadState *thr, void *p); |
| 34 | |
| 35 | enum MBlockType { |
| 36 | MBlockScopedBuf, |
| 37 | MBlockString, |
| 38 | MBlockStackTrace, |
| 39 | MBlockSync, |
| 40 | MBlockClock, |
| 41 | MBlockThreadContex, |
Dmitry Vyukov | f6985e3 | 2012-05-22 14:34:43 +0000 | [diff] [blame] | 42 | MBlockDeadInfo, |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 43 | MBlockRacyStacks, |
| 44 | MBlockRacyAddresses, |
| 45 | MBlockAtExit, |
| 46 | MBlockFlag, |
| 47 | MBlockReport, |
| 48 | MBlockReportMop, |
| 49 | MBlockReportThread, |
| 50 | MBlockReportMutex, |
| 51 | MBlockReportLoc, |
| 52 | MBlockReportStack, |
| 53 | MBlockSuppression, |
| 54 | MBlockExpectRace, |
Dmitry Vyukov | 97c26bd | 2012-06-27 16:05:06 +0000 | [diff] [blame^] | 55 | MBlockSignal, |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 56 | |
| 57 | // This must be the last. |
| 58 | MBlockTypeCount, |
| 59 | }; |
| 60 | |
| 61 | // For internal data structures. |
| 62 | void *internal_alloc(MBlockType typ, uptr sz); |
| 63 | void internal_free(void *p); |
| 64 | |
| 65 | template<typename T> |
| 66 | void DestroyAndFree(T *&p) { |
| 67 | p->~T(); |
| 68 | internal_free(p); |
| 69 | p = 0; |
| 70 | } |
| 71 | |
| 72 | template<typename T> |
| 73 | class 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 |