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 {
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();
84 if (size <= old_size) {
89 if (old_size < size) {
90 for (uptr i = old_size; i < size; i++)
91 internal_memset(&begin_[i], 0, sizeof(begin_[i]));
100 void EnsureSize(uptr size) {
103 if (size <= (uptr)(last_ - begin_)) {
104 end_ = begin_ + size;
107 uptr cap0 = last_ - begin_;
108 uptr cap = cap0 * 5 / 4; // 25% growth
113 T *p = (T*)InternalAlloc(cap * sizeof(T));
115 internal_memcpy(p, begin_, cap0 * sizeof(T));
116 InternalFree(begin_);
119 end_ = begin_ + size;
120 last_ = begin_ + cap;
123 Vector(const Vector&);
124 void operator=(const Vector&);
126 } // namespace __sanitizer
128 #endif // #ifndef SANITIZER_VECTOR_H