1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef BERBERIS_BASE_ARENA_ZEROED_ARRAY_H_ 18 #define BERBERIS_BASE_ARENA_ZEROED_ARRAY_H_ 19 20 #include "berberis/base/arena_alloc.h" 21 #include "berberis/base/logging.h" 22 23 #include <string.h> 24 25 namespace berberis { 26 27 // A zero-initialized array of fixed size (at construction time). 28 // TODO(b/117224636): This is a workaround for slow zero-initialized ArenaVector. 29 // Alternatively, we could zero-initialize memory when Arena allocates memory, eliminating 30 // the need to zero-initialize memory in every data structure allocated from Arena. 31 template <typename Type> 32 class ArenaZeroedArray { 33 public: ArenaZeroedArray(size_t size,Arena * arena)34 ArenaZeroedArray(size_t size, Arena* arena) 35 : size_(size), array_(NewArrayInArena<Type>(arena, size)) { 36 memset(array_, 0, sizeof(Type) * size); 37 } 38 39 const Type& operator[](size_t i) const { return array_[i]; } 40 Type& operator[](size_t i) { return array_[i]; } 41 at(size_t i)42 const Type& at(size_t i) const { 43 CHECK_LT(i, size_); 44 return array_[i]; 45 } 46 at(size_t i)47 Type& at(size_t i) { 48 CHECK_LT(i, size_); 49 return array_[i]; 50 } 51 52 private: 53 size_t size_; 54 Type* array_; 55 }; 56 57 } // namespace berberis 58 59 #endif // BERBERIS_BASE_ARENA_ZEROED_ARRAY_H_ 60