1 //===-- sanitizer_vector.h -------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is shared between sanitizers run-time libraries.
11 //===----------------------------------------------------------------------===//
13 // Low-fat STL-like vector container.
15 #ifndef SANITIZER_VECTOR_H
16 #define SANITIZER_VECTOR_H
18 #include "sanitizer_common/sanitizer_allocator_internal.h"
19 #include "sanitizer_common/sanitizer_libc.h"
21 namespace __sanitizer
{
26 Vector() : begin_(), end_(), last_() {}
45 T
&operator[](uptr i
) {
46 DCHECK_LT(i
, end_
- begin_
);
50 const T
&operator[](uptr i
) const {
51 DCHECK_LT(i
, end_
- begin_
);
56 EnsureSize(Size() + 1);
58 internal_memset(p
, 0, sizeof(*p
));
62 T
*PushBack(const T
& v
) {
63 EnsureSize(Size() + 1);
65 internal_memcpy(p
, &v
, sizeof(*p
));
70 DCHECK_GT(end_
, begin_
);
74 void Resize(uptr size
) {
79 uptr old_size
= Size();
80 if (size
<= old_size
) {
85 if (old_size
< size
) {
86 internal_memset(&begin_
[old_size
], 0,
87 sizeof(begin_
[old_size
]) * (size
- old_size
));
96 void EnsureSize(uptr size
) {
99 if (size
<= (uptr
)(last_
- begin_
)) {
100 end_
= begin_
+ size
;
103 uptr cap0
= last_
- begin_
;
104 uptr cap
= cap0
* 5 / 4; // 25% growth
109 T
*p
= (T
*)InternalAlloc(cap
* sizeof(T
));
111 internal_memcpy(p
, begin_
, cap0
* sizeof(T
));
112 InternalFree(begin_
);
115 end_
= begin_
+ size
;
116 last_
= begin_
+ cap
;
119 Vector(const Vector
&);
120 void operator=(const Vector
&);
122 } // namespace __sanitizer
124 #endif // #ifndef SANITIZER_VECTOR_H