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, |
Dmitry Vyukov | 5bfac97 | 2012-07-16 16:44:47 +0000 | [diff] [blame^] | 39 | MBlockShadowStack, |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 40 | MBlockSync, |
| 41 | MBlockClock, |
| 42 | MBlockThreadContex, |
Dmitry Vyukov | f6985e3 | 2012-05-22 14:34:43 +0000 | [diff] [blame] | 43 | MBlockDeadInfo, |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 44 | MBlockRacyStacks, |
| 45 | MBlockRacyAddresses, |
| 46 | MBlockAtExit, |
| 47 | MBlockFlag, |
| 48 | MBlockReport, |
| 49 | MBlockReportMop, |
| 50 | MBlockReportThread, |
| 51 | MBlockReportMutex, |
| 52 | MBlockReportLoc, |
| 53 | MBlockReportStack, |
| 54 | MBlockSuppression, |
| 55 | MBlockExpectRace, |
Dmitry Vyukov | 97c26bd | 2012-06-27 16:05:06 +0000 | [diff] [blame] | 56 | MBlockSignal, |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 57 | |
| 58 | // This must be the last. |
| 59 | MBlockTypeCount, |
| 60 | }; |
| 61 | |
| 62 | // For internal data structures. |
| 63 | void *internal_alloc(MBlockType typ, uptr sz); |
| 64 | void internal_free(void *p); |
| 65 | |
| 66 | template<typename T> |
| 67 | void DestroyAndFree(T *&p) { |
| 68 | p->~T(); |
| 69 | internal_free(p); |
| 70 | p = 0; |
| 71 | } |
| 72 | |
| 73 | template<typename T> |
| 74 | class InternalScopedBuf { |
| 75 | public: |
| 76 | explicit InternalScopedBuf(uptr cnt) { |
| 77 | cnt_ = cnt; |
| 78 | ptr_ = (T*)internal_alloc(MBlockScopedBuf, cnt * sizeof(T)); |
| 79 | } |
| 80 | |
| 81 | ~InternalScopedBuf() { |
| 82 | internal_free(ptr_); |
| 83 | } |
| 84 | |
| 85 | operator T *() { |
| 86 | return ptr_; |
| 87 | } |
| 88 | |
| 89 | T &operator[](uptr i) { |
| 90 | return ptr_[i]; |
| 91 | } |
| 92 | |
| 93 | T *Ptr() { |
| 94 | return ptr_; |
| 95 | } |
| 96 | |
| 97 | uptr Count() { |
| 98 | return cnt_; |
| 99 | } |
| 100 | |
| 101 | uptr Size() { |
| 102 | return cnt_ * sizeof(T); |
| 103 | } |
| 104 | |
| 105 | private: |
| 106 | T *ptr_; |
| 107 | uptr cnt_; |
| 108 | |
| 109 | InternalScopedBuf(const InternalScopedBuf&); |
| 110 | void operator = (const InternalScopedBuf&); |
| 111 | }; |
| 112 | |
| 113 | } // namespace __tsan |
| 114 | #endif // TSAN_MMAN_H |