blob: 4c4e2af3e7e3caf72dd94a2744a246319610211a [file] [log] [blame]
Andreas Gampefd63bbf2018-10-29 12:55:35 -07001/*
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#ifndef ART_RUNTIME_MIRROR_STRING_ALLOC_INL_H_
17#define ART_RUNTIME_MIRROR_STRING_ALLOC_INL_H_
18
19#include "string-inl.h"
20
21#include "android-base/stringprintf.h"
22
23#include "array.h"
24#include "base/bit_utils.h"
Andreas Gampefd63bbf2018-10-29 12:55:35 -070025#include "class.h"
26#include "class_root.h"
27#include "gc/heap-inl.h"
28#include "runtime.h"
Andreas Gampe5a0430d2019-01-04 14:33:57 -080029#include "runtime_globals.h"
Andreas Gampefd63bbf2018-10-29 12:55:35 -070030#include "thread.h"
31
32namespace art {
33namespace mirror {
34
35// Sets string count in the allocation code path to ensure it is guarded by a CAS.
36class SetStringCountVisitor {
37 public:
38 explicit SetStringCountVisitor(int32_t count) : count_(count) {
39 }
40
41 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
42 REQUIRES_SHARED(Locks::mutator_lock_) {
43 // Avoid AsString as object is not yet in live bitmap or allocation stack.
44 ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
45 string->SetCount(count_);
46 DCHECK(!string->IsCompressed() || kUseStringCompression);
47 }
48
49 private:
50 const int32_t count_;
51};
52
53// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
54class SetStringCountAndBytesVisitor {
55 public:
56 SetStringCountAndBytesVisitor(int32_t count, Handle<ByteArray> src_array, int32_t offset,
57 int32_t high_byte)
58 : count_(count), src_array_(src_array), offset_(offset), high_byte_(high_byte) {
59 }
60
61 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
62 REQUIRES_SHARED(Locks::mutator_lock_) {
63 // Avoid AsString as object is not yet in live bitmap or allocation stack.
64 ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
65 string->SetCount(count_);
66 DCHECK(!string->IsCompressed() || kUseStringCompression);
67 int32_t length = String::GetLengthFromCount(count_);
68 const uint8_t* const src = reinterpret_cast<uint8_t*>(src_array_->GetData()) + offset_;
69 if (string->IsCompressed()) {
70 uint8_t* valueCompressed = string->GetValueCompressed();
71 for (int i = 0; i < length; i++) {
72 valueCompressed[i] = (src[i] & 0xFF);
73 }
74 } else {
75 uint16_t* value = string->GetValue();
76 for (int i = 0; i < length; i++) {
77 value[i] = high_byte_ + (src[i] & 0xFF);
78 }
79 }
80 }
81
82 private:
83 const int32_t count_;
84 Handle<ByteArray> src_array_;
85 const int32_t offset_;
86 const int32_t high_byte_;
87};
88
89// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
90class SetStringCountAndValueVisitorFromCharArray {
91 public:
92 SetStringCountAndValueVisitorFromCharArray(int32_t count, Handle<CharArray> src_array,
93 int32_t offset) :
94 count_(count), src_array_(src_array), offset_(offset) {
95 }
96
97 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
98 REQUIRES_SHARED(Locks::mutator_lock_) {
99 // Avoid AsString as object is not yet in live bitmap or allocation stack.
100 ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
101 string->SetCount(count_);
102 const uint16_t* const src = src_array_->GetData() + offset_;
103 const int32_t length = String::GetLengthFromCount(count_);
104 if (kUseStringCompression && String::IsCompressed(count_)) {
105 for (int i = 0; i < length; ++i) {
106 string->GetValueCompressed()[i] = static_cast<uint8_t>(src[i]);
107 }
108 } else {
109 memcpy(string->GetValue(), src, length * sizeof(uint16_t));
110 }
111 }
112
113 private:
114 const int32_t count_;
115 Handle<CharArray> src_array_;
116 const int32_t offset_;
117};
118
119// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
120class SetStringCountAndValueVisitorFromString {
121 public:
122 SetStringCountAndValueVisitorFromString(int32_t count,
123 Handle<String> src_string,
124 int32_t offset) :
125 count_(count), src_string_(src_string), offset_(offset) {
126 }
127
128 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
129 REQUIRES_SHARED(Locks::mutator_lock_) {
130 // Avoid AsString as object is not yet in live bitmap or allocation stack.
131 ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
132 string->SetCount(count_);
133 const int32_t length = String::GetLengthFromCount(count_);
134 bool compressible = kUseStringCompression && String::IsCompressed(count_);
135 if (src_string_->IsCompressed()) {
136 const uint8_t* const src = src_string_->GetValueCompressed() + offset_;
137 memcpy(string->GetValueCompressed(), src, length * sizeof(uint8_t));
138 } else {
139 const uint16_t* const src = src_string_->GetValue() + offset_;
140 if (compressible) {
141 for (int i = 0; i < length; ++i) {
142 string->GetValueCompressed()[i] = static_cast<uint8_t>(src[i]);
143 }
144 } else {
145 memcpy(string->GetValue(), src, length * sizeof(uint16_t));
146 }
147 }
148 }
149
150 private:
151 const int32_t count_;
152 Handle<String> src_string_;
153 const int32_t offset_;
154};
155
156template <bool kIsInstrumented, typename PreFenceVisitor>
157inline String* String::Alloc(Thread* self,
158 int32_t utf16_length_with_flag,
159 gc::AllocatorType allocator_type,
160 const PreFenceVisitor& pre_fence_visitor) {
161 constexpr size_t header_size = sizeof(String);
162 const bool compressible = kUseStringCompression && String::IsCompressed(utf16_length_with_flag);
163 const size_t block_size = (compressible) ? sizeof(uint8_t) : sizeof(uint16_t);
164 size_t length = String::GetLengthFromCount(utf16_length_with_flag);
165 static_assert(sizeof(length) <= sizeof(size_t),
166 "static_cast<size_t>(utf16_length) must not lose bits.");
167 size_t data_size = block_size * length;
168 size_t size = header_size + data_size;
169 // String.equals() intrinsics assume zero-padding up to kObjectAlignment,
170 // so make sure the allocator clears the padding as well.
171 // http://b/23528461
172 size_t alloc_size = RoundUp(size, kObjectAlignment);
173
174 Runtime* runtime = Runtime::Current();
175 ObjPtr<Class> string_class = GetClassRoot<String>(runtime->GetClassLinker());
176 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
177 // Do this by comparing with the maximum length that will _not_ cause an overflow.
178 const size_t overflow_length = (-header_size) / block_size; // Unsigned arithmetic.
179 const size_t max_alloc_length = overflow_length - 1u;
180 static_assert(IsAligned<sizeof(uint16_t)>(kObjectAlignment),
181 "kObjectAlignment must be at least as big as Java char alignment");
182 const size_t max_length = RoundDown(max_alloc_length, kObjectAlignment / block_size);
183 if (UNLIKELY(length > max_length)) {
184 self->ThrowOutOfMemoryError(
185 android::base::StringPrintf("%s of length %d would overflow",
186 Class::PrettyDescriptor(string_class).c_str(),
187 static_cast<int>(length)).c_str());
188 return nullptr;
189 }
190
191 gc::Heap* heap = runtime->GetHeap();
192 return down_cast<String*>(
193 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self,
194 string_class,
195 alloc_size,
196 allocator_type,
197 pre_fence_visitor));
198}
199
200template <bool kIsInstrumented>
201inline String* String::AllocEmptyString(Thread* self, gc::AllocatorType allocator_type) {
202 const int32_t length_with_flag = String::GetFlaggedCount(0, /* compressible= */ true);
203 SetStringCountVisitor visitor(length_with_flag);
204 return Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
205}
206
207template <bool kIsInstrumented>
208inline String* String::AllocFromByteArray(Thread* self,
209 int32_t byte_length,
210 Handle<ByteArray> array,
211 int32_t offset,
212 int32_t high_byte,
213 gc::AllocatorType allocator_type) {
214 const uint8_t* const src = reinterpret_cast<uint8_t*>(array->GetData()) + offset;
215 high_byte &= 0xff; // Extract the relevant bits before determining `compressible`.
216 const bool compressible =
217 kUseStringCompression && String::AllASCII<uint8_t>(src, byte_length) && (high_byte == 0);
218 const int32_t length_with_flag = String::GetFlaggedCount(byte_length, compressible);
219 SetStringCountAndBytesVisitor visitor(length_with_flag, array, offset, high_byte << 8);
220 String* string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
221 return string;
222}
223
224template <bool kIsInstrumented>
225inline String* String::AllocFromCharArray(Thread* self,
226 int32_t count,
227 Handle<CharArray> array,
228 int32_t offset,
229 gc::AllocatorType allocator_type) {
230 // It is a caller error to have a count less than the actual array's size.
231 DCHECK_GE(array->GetLength(), count);
232 const bool compressible = kUseStringCompression &&
233 String::AllASCII<uint16_t>(array->GetData() + offset, count);
234 const int32_t length_with_flag = String::GetFlaggedCount(count, compressible);
235 SetStringCountAndValueVisitorFromCharArray visitor(length_with_flag, array, offset);
236 String* new_string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
237 return new_string;
238}
239
240template <bool kIsInstrumented>
241inline String* String::AllocFromString(Thread* self,
242 int32_t string_length,
243 Handle<String> string,
244 int32_t offset,
245 gc::AllocatorType allocator_type) {
246 const bool compressible = kUseStringCompression &&
247 ((string->IsCompressed()) ? true : String::AllASCII<uint16_t>(string->GetValue() + offset,
248 string_length));
249 const int32_t length_with_flag = String::GetFlaggedCount(string_length, compressible);
250 SetStringCountAndValueVisitorFromString visitor(length_with_flag, string, offset);
251 String* new_string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
252 return new_string;
253}
254
255} // namespace mirror
256} // namespace art
257
258#endif // ART_RUNTIME_MIRROR_STRING_ALLOC_INL_H_