Configuration bits for ARC port:
[official-gcc.git] / libsanitizer / tsan / tsan_vector.h
blob99e9f792c20cb1c8248e84e6dd70753b8c1b2023
1 //===-- tsan_vector.h -------------------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
9 //
10 //===----------------------------------------------------------------------===//
12 // Low-fat STL-like vector container.
14 #ifndef TSAN_VECTOR_H
15 #define TSAN_VECTOR_H
17 #include "tsan_defs.h"
18 #include "tsan_mman.h"
20 namespace __tsan {
22 template<typename T>
23 class Vector {
24 public:
25 explicit Vector(MBlockType typ)
26 : typ_(typ)
27 , begin_()
28 , end_()
29 , last_() {
32 ~Vector() {
33 if (begin_)
34 internal_free(begin_);
37 void Reset() {
38 if (begin_)
39 internal_free(begin_);
40 begin_ = 0;
41 end_ = 0;
42 last_ = 0;
45 uptr Size() const {
46 return end_ - begin_;
49 T &operator[](uptr i) {
50 DCHECK_LT(i, end_ - begin_);
51 return begin_[i];
54 const T &operator[](uptr i) const {
55 DCHECK_LT(i, end_ - begin_);
56 return begin_[i];
59 T *PushBack(T v = T()) {
60 EnsureSize(Size() + 1);
61 end_[-1] = v;
62 return &end_[-1];
65 void Resize(uptr size) {
66 uptr old_size = Size();
67 EnsureSize(size);
68 if (old_size < size) {
69 for (uptr i = old_size; i < size; i++)
70 begin_[i] = T();
74 private:
75 const MBlockType typ_;
76 T *begin_;
77 T *end_;
78 T *last_;
80 void EnsureSize(uptr size) {
81 if (size <= Size())
82 return;
83 if (size <= (uptr)(last_ - begin_)) {
84 end_ = begin_ + size;
85 return;
87 uptr cap0 = last_ - begin_;
88 uptr cap = 2 * cap0;
89 if (cap == 0)
90 cap = 16;
91 if (cap < size)
92 cap = size;
93 T *p = (T*)internal_alloc(typ_, cap * sizeof(T));
94 if (cap0) {
95 internal_memcpy(p, begin_, cap0 * sizeof(T));
96 internal_free(begin_);
98 begin_ = p;
99 end_ = begin_ + size;
100 last_ = begin_ + cap;
103 Vector(const Vector&);
104 void operator=(const Vector&);
106 } // namespace __tsan
108 #endif // #ifndef TSAN_VECTOR_H