Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 ART_RUNTIME_LENGTH_PREFIXED_ARRAY_H_ |
| 18 | #define ART_RUNTIME_LENGTH_PREFIXED_ARRAY_H_ |
| 19 | |
| 20 | #include <stddef.h> // for offsetof() |
| 21 | |
| 22 | #include "linear_alloc.h" |
| 23 | #include "stride_iterator.h" |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame^] | 24 | #include "base/bit_utils.h" |
| 25 | #include "base/casts.h" |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 26 | #include "base/iteration_range.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | template<typename T> |
| 31 | class LengthPrefixedArray { |
| 32 | public: |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame^] | 33 | explicit LengthPrefixedArray(size_t length) |
| 34 | : length_(dchecked_integral_cast<uint32_t>(length)) {} |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 35 | |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame^] | 36 | T& At(size_t index, size_t element_size = sizeof(T), size_t alignment = alignof(T)) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 37 | DCHECK_LT(index, length_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame^] | 38 | return AtUnchecked(index, element_size, alignment); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame^] | 41 | StrideIterator<T> Begin(size_t element_size = sizeof(T), size_t alignment = alignof(T)) { |
| 42 | return StrideIterator<T>(&AtUnchecked(0, element_size, alignment), element_size); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame^] | 45 | StrideIterator<T> End(size_t element_size = sizeof(T), size_t alignment = alignof(T)) { |
| 46 | return StrideIterator<T>(&AtUnchecked(length_, element_size, alignment), element_size); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame^] | 49 | static size_t OffsetOfElement(size_t index, |
| 50 | size_t element_size = sizeof(T), |
| 51 | size_t alignment = alignof(T)) { |
| 52 | DCHECK_ALIGNED_PARAM(element_size, alignment); |
| 53 | return RoundUp(offsetof(LengthPrefixedArray<T>, data), alignment) + index * element_size; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame^] | 56 | static size_t ComputeSize(size_t num_elements, |
| 57 | size_t element_size = sizeof(T), |
| 58 | size_t alignment = alignof(T)) { |
| 59 | size_t result = OffsetOfElement(num_elements, element_size, alignment); |
| 60 | DCHECK_ALIGNED_PARAM(result, alignment); |
| 61 | return result; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | uint64_t Length() const { |
| 65 | return length_; |
| 66 | } |
| 67 | |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 68 | // Update the length but does not reallocate storage. |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame^] | 69 | void SetLength(size_t length) { |
| 70 | length_ = dchecked_integral_cast<uint32_t>(length); |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 73 | private: |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame^] | 74 | T& AtUnchecked(size_t index, size_t element_size, size_t alignment) { |
| 75 | return *reinterpret_cast<T*>( |
| 76 | reinterpret_cast<uintptr_t>(this) + OffsetOfElement(index, element_size, alignment)); |
| 77 | } |
| 78 | |
| 79 | uint32_t length_; |
| 80 | uint8_t data[0]; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | // Returns empty iteration range if the array is null. |
| 84 | template<typename T> |
| 85 | IterationRange<StrideIterator<T>> MakeIterationRangeFromLengthPrefixedArray( |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame^] | 86 | LengthPrefixedArray<T>* arr, size_t element_size = sizeof(T), size_t alignment = alignof(T)) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 87 | return arr != nullptr ? |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame^] | 88 | MakeIterationRange(arr->Begin(element_size, alignment), arr->End(element_size, alignment)) : |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 89 | MakeEmptyIterationRange(StrideIterator<T>(nullptr, 0)); |
| 90 | } |
| 91 | |
| 92 | } // namespace art |
| 93 | |
| 94 | #endif // ART_RUNTIME_LENGTH_PREFIXED_ARRAY_H_ |