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, |
| 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. |
| 60 | void *internal_alloc(MBlockType typ, uptr sz); |
| 61 | void internal_free(void *p); |
| 62 | |
| 63 | template<typename T> |
| 64 | void DestroyAndFree(T *&p) { |
| 65 | p->~T(); |
| 66 | internal_free(p); |
| 67 | p = 0; |
| 68 | } |
| 69 | |
| 70 | template<typename T> |
| 71 | class 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 |