1 //===-- tsan_vector.h -------------------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
10 //===----------------------------------------------------------------------===//
12 // Low-fat STL-like vector container.
17 #include "tsan_defs.h"
18 #include "tsan_mman.h"
25 explicit Vector(MBlockType typ
)
34 internal_free(begin_
);
39 internal_free(begin_
);
49 T
&operator[](uptr i
) {
50 DCHECK_LT(i
, end_
- begin_
);
54 const T
&operator[](uptr i
) const {
55 DCHECK_LT(i
, end_
- begin_
);
60 EnsureSize(Size() + 1);
62 internal_memset(p
, 0, sizeof(*p
));
66 T
*PushBack(const T
& v
) {
67 EnsureSize(Size() + 1);
69 internal_memcpy(p
, &v
, sizeof(*p
));
74 DCHECK_GT(end_
, begin_
);
78 void Resize(uptr size
) {
83 uptr old_size
= Size();
85 if (old_size
< size
) {
86 for (uptr i
= old_size
; i
< size
; i
++)
87 internal_memset(&begin_
[i
], 0, sizeof(begin_
[i
]));
92 const MBlockType typ_
;
97 void EnsureSize(uptr size
) {
100 if (size
<= (uptr
)(last_
- begin_
)) {
101 end_
= begin_
+ size
;
104 uptr cap0
= last_
- begin_
;
105 uptr cap
= cap0
* 5 / 4; // 25% growth
110 T
*p
= (T
*)internal_alloc(typ_
, cap
* sizeof(T
));
112 internal_memcpy(p
, begin_
, cap0
* sizeof(T
));
113 internal_free(begin_
);
116 end_
= begin_
+ size
;
117 last_
= begin_
+ cap
;
120 Vector(const Vector
&);
121 void operator=(const Vector
&);
123 } // namespace __tsan
125 #endif // #ifndef TSAN_VECTOR_H