array-inl.h revision ef7d42fca18c16fbaf103822ad16f23246e2905d
1/*
2 * Copyright (C) 2011 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_MIRROR_ARRAY_INL_H_
18#define ART_RUNTIME_MIRROR_ARRAY_INL_H_
19
20#include "array.h"
21
22#include "class.h"
23#include "gc/heap-inl.h"
24#include "thread.h"
25#include "utils.h"
26
27namespace art {
28namespace mirror {
29
30inline size_t Array::SizeOf() {
31  // This is safe from overflow because the array was already allocated, so we know it's sane.
32  size_t component_size = GetClass()->GetComponentSize();
33  int32_t component_count = GetLength();
34  size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
35  size_t data_size = component_count * component_size;
36  return header_size + data_size;
37}
38
39static inline size_t ComputeArraySize(Thread* self, Class* array_class, int32_t component_count,
40                                      size_t component_size)
41    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
42  DCHECK(array_class != NULL);
43  DCHECK_GE(component_count, 0);
44  DCHECK(array_class->IsArrayClass());
45
46  size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
47  size_t data_size = component_count * component_size;
48  size_t size = header_size + data_size;
49
50  // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
51  size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
52  if (UNLIKELY(data_size >> component_shift != size_t(component_count) || size < data_size)) {
53    self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow",
54                                             PrettyDescriptor(array_class).c_str(),
55                                             component_count).c_str());
56    return 0;  // failure
57  }
58  return size;
59}
60
61// Used for setting the array length in the allocation code path to ensure it is guarded by a CAS.
62class SetLengthVisitor {
63 public:
64  explicit SetLengthVisitor(int32_t length) : length_(length) {
65  }
66
67  void operator()(Object* obj) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
68    // Avoid AsArray as object is not yet in live bitmap or allocation stack.
69    Array* array = down_cast<Array*>(obj);
70    // DCHECK(array->IsArrayInstance());
71    array->SetLength(length_);
72  }
73
74 private:
75  const int32_t length_;
76};
77
78template <bool kIsInstrumented>
79inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
80                           size_t component_size, gc::AllocatorType allocator_type) {
81  size_t size = ComputeArraySize(self, array_class, component_count, component_size);
82  if (UNLIKELY(size == 0)) {
83    return nullptr;
84  }
85  gc::Heap* heap = Runtime::Current()->GetHeap();
86  SetLengthVisitor visitor(component_count);
87  DCHECK(allocator_type != gc::kAllocatorTypeLOS);
88  return down_cast<Array*>(
89      heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
90                                                            allocator_type, visitor));
91}
92
93template <bool kIsInstrumented>
94inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
95                           gc::AllocatorType allocator_type) {
96  DCHECK(array_class->IsArrayClass());
97  return Alloc<kIsInstrumented>(self, array_class, component_count, array_class->GetComponentSize(),
98                                allocator_type);
99}
100template <bool kIsInstrumented>
101inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count) {
102  return Alloc<kIsInstrumented>(self, array_class, component_count,
103               Runtime::Current()->GetHeap()->GetCurrentAllocator());
104}
105
106template <bool kIsInstrumented>
107inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
108                           size_t component_size) {
109  return Alloc<kIsInstrumented>(self, array_class, component_count, component_size,
110               Runtime::Current()->GetHeap()->GetCurrentAllocator());
111}
112
113template<class T>
114inline void PrimitiveArray<T>::VisitRoots(RootVisitor* visitor, void* arg) {
115  if (array_class_ != nullptr) {
116    array_class_ = down_cast<Class*>(visitor(array_class_, arg));
117  }
118}
119
120// Similar to memmove except elements are of aligned appropriately for T, count is in T sized units
121// copies are guaranteed not to tear when T is less-than 64bit.
122template<typename T>
123static inline void ArrayBackwardCopy(T* d, const T* s, int32_t count) {
124  d += count;
125  s += count;
126  for (int32_t i = 0; i < count; ++i) {
127    d--;
128    s--;
129    *d = *s;
130  }
131}
132
133template<class T>
134void PrimitiveArray<T>::Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
135                                int32_t count) {
136  if (UNLIKELY(count == 0)) {
137    return;
138  }
139  DCHECK_GE(dst_pos, 0);
140  DCHECK_GE(src_pos, 0);
141  DCHECK_GT(count, 0);
142  DCHECK(src != nullptr);
143  DCHECK_LT(dst_pos, GetLength());
144  DCHECK_LE(dst_pos, GetLength() - count);
145  DCHECK_LT(src_pos, src->GetLength());
146  DCHECK_LE(src_pos, src->GetLength() - count);
147
148  // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
149  // in our implementation, because they may copy byte-by-byte.
150  if (LIKELY(src != this) || (dst_pos < src_pos) || (dst_pos - src_pos >= count)) {
151    // Forward copy ok.
152    Memcpy(dst_pos, src, src_pos, count);
153  } else {
154    // Backward copy necessary.
155    void* dst_raw = GetRawData(sizeof(T), dst_pos);
156    const void* src_raw = src->GetRawData(sizeof(T), src_pos);
157    if (sizeof(T) == sizeof(uint8_t)) {
158      // TUNING: use memmove here?
159      uint8_t* d = reinterpret_cast<uint8_t*>(dst_raw);
160      const uint8_t* s = reinterpret_cast<const uint8_t*>(src_raw);
161      ArrayBackwardCopy<uint8_t>(d, s, count);
162    } else if (sizeof(T) == sizeof(uint16_t)) {
163      uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
164      const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
165      ArrayBackwardCopy<uint16_t>(d, s, count);
166    } else if (sizeof(T) == sizeof(uint32_t)) {
167      uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
168      const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
169      ArrayBackwardCopy<uint32_t>(d, s, count);
170    } else {
171      DCHECK_EQ(sizeof(T), sizeof(uint64_t));
172      uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
173      const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
174      ArrayBackwardCopy<uint64_t>(d, s, count);
175    }
176  }
177}
178
179// Similar to memcpy except elements are of aligned appropriately for T, count is in T sized units
180// copies are guaranteed not to tear when T is less-than 64bit.
181template<typename T>
182static inline void ArrayForwardCopy(T* d, const T* s, int32_t count) {
183  for (int32_t i = 0; i < count; ++i) {
184    *d = *s;
185    d++;
186    s++;
187  }
188}
189
190
191template<class T>
192void PrimitiveArray<T>::Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
193                               int32_t count) {
194  if (UNLIKELY(count == 0)) {
195    return;
196  }
197  DCHECK_GE(dst_pos, 0);
198  DCHECK_GE(src_pos, 0);
199  DCHECK_GT(count, 0);
200  DCHECK(src != nullptr);
201  DCHECK_LT(dst_pos, GetLength());
202  DCHECK_LE(dst_pos, GetLength() - count);
203  DCHECK_LT(src_pos, src->GetLength());
204  DCHECK_LE(src_pos, src->GetLength() - count);
205
206  // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
207  // in our implementation, because they may copy byte-by-byte.
208  void* dst_raw = GetRawData(sizeof(T), dst_pos);
209  const void* src_raw = src->GetRawData(sizeof(T), src_pos);
210  if (sizeof(T) == sizeof(uint8_t)) {
211    memcpy(dst_raw, src_raw, count);
212  } else if (sizeof(T) == sizeof(uint16_t)) {
213    uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
214    const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
215    ArrayForwardCopy<uint16_t>(d, s, count);
216  } else if (sizeof(T) == sizeof(uint32_t)) {
217    uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
218    const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
219    ArrayForwardCopy<uint32_t>(d, s, count);
220  } else {
221    DCHECK_EQ(sizeof(T), sizeof(uint64_t));
222    uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
223    const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
224    ArrayForwardCopy<uint64_t>(d, s, count);
225  }
226}
227
228}  // namespace mirror
229}  // namespace art
230
231#endif  // ART_RUNTIME_MIRROR_ARRAY_INL_H_
232