blob: 0ff6d7a9aa80cfebe9ebff28dfc214114846e6b9 [file] [log] [blame]
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001/*
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 Chartier54d220e2015-07-30 16:20:06 -070022#include "stride_iterator.h"
Vladimir Markocf36d492015-08-12 19:27:26 +010023#include "base/bit_utils.h"
24#include "base/casts.h"
Mathieu Chartier54d220e2015-07-30 16:20:06 -070025#include "base/iteration_range.h"
26
27namespace art {
28
29template<typename T>
30class LengthPrefixedArray {
31 public:
Vladimir Markocf36d492015-08-12 19:27:26 +010032 explicit LengthPrefixedArray(size_t length)
33 : length_(dchecked_integral_cast<uint32_t>(length)) {}
Mathieu Chartier54d220e2015-07-30 16:20:06 -070034
Vladimir Markocf36d492015-08-12 19:27:26 +010035 T& At(size_t index, size_t element_size = sizeof(T), size_t alignment = alignof(T)) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -070036 DCHECK_LT(index, length_);
Vladimir Markocf36d492015-08-12 19:27:26 +010037 return AtUnchecked(index, element_size, alignment);
Mathieu Chartier54d220e2015-07-30 16:20:06 -070038 }
39
Vladimir Markocf36d492015-08-12 19:27:26 +010040 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 Chartier54d220e2015-07-30 16:20:06 -070042 }
43
Vladimir Markocf36d492015-08-12 19:27:26 +010044 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 Chartier54d220e2015-07-30 16:20:06 -070046 }
47
Vladimir Markocf36d492015-08-12 19:27:26 +010048 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 Chartier54d220e2015-07-30 16:20:06 -070053 }
54
Vladimir Markocf36d492015-08-12 19:27:26 +010055 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 Chartier54d220e2015-07-30 16:20:06 -070061 }
62
63 uint64_t Length() const {
64 return length_;
65 }
66
Mathieu Chartierc0fe56a2015-08-11 13:01:23 -070067 // Update the length but does not reallocate storage.
Vladimir Markocf36d492015-08-12 19:27:26 +010068 void SetLength(size_t length) {
69 length_ = dchecked_integral_cast<uint32_t>(length);
Mathieu Chartierc0fe56a2015-08-11 13:01:23 -070070 }
71
Mathieu Chartier54d220e2015-07-30 16:20:06 -070072 private:
Vladimir Markocf36d492015-08-12 19:27:26 +010073 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 Chartier54d220e2015-07-30 16:20:06 -070080};
81
82// Returns empty iteration range if the array is null.
83template<typename T>
84IterationRange<StrideIterator<T>> MakeIterationRangeFromLengthPrefixedArray(
Vladimir Markocf36d492015-08-12 19:27:26 +010085 LengthPrefixedArray<T>* arr, size_t element_size = sizeof(T), size_t alignment = alignof(T)) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -070086 return arr != nullptr ?
Vladimir Markocf36d492015-08-12 19:27:26 +010087 MakeIterationRange(arr->Begin(element_size, alignment), arr->End(element_size, alignment)) :
Mathieu Chartier54d220e2015-07-30 16:20:06 -070088 MakeEmptyIterationRange(StrideIterator<T>(nullptr, 0));
89}
90
91} // namespace art
92
93#endif // ART_RUNTIME_LENGTH_PREFIXED_ARRAY_H_