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