blob: d94b39fba7282ba7d65a4bfa05f9f9cf80dbf923 [file] [log] [blame]
Ian Rogersb0fa5dc2014-04-28 16:47:08 -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 */
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070016#ifndef ART_RUNTIME_MIRROR_STRING_INL_H_
17#define ART_RUNTIME_MIRROR_STRING_INL_H_
18
19#include "array.h"
Vladimir Marko4ef52262015-08-26 18:12:56 +010020#include "base/bit_utils.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070021#include "class.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010022#include "common_throws.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080023#include "gc/heap-inl.h"
Vladimir Marko4ef52262015-08-26 18:12:56 +010024#include "globals.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070025#include "intern_table.h"
26#include "runtime.h"
27#include "string.h"
28#include "thread.h"
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070029#include "utf.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010030#include "utils.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070031
32namespace art {
33namespace mirror {
34
Andreas Gampe542451c2016-07-26 09:02:02 -070035inline uint32_t String::ClassSize(PointerSize pointer_size) {
Przemyslaw Szczepaniaka4fa2e72016-06-22 13:30:36 +010036 uint32_t vtable_entries = Object::kVTableLength + 57;
Narayan Kamath5d8fa8b2016-04-13 14:17:44 +010037 return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 1, 2, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070038}
39
Jeff Hao848f70a2014-01-15 13:49:50 -080040// Sets string count in the allocation code path to ensure it is guarded by a CAS.
41class SetStringCountVisitor {
42 public:
43 explicit SetStringCountVisitor(int32_t count) : count_(count) {
44 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +000045
Mathieu Chartier9d156d52016-10-06 17:44:26 -070046 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070047 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080048 // Avoid AsString as object is not yet in live bitmap or allocation stack.
Mathieu Chartier9d156d52016-10-06 17:44:26 -070049 ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
Jeff Hao848f70a2014-01-15 13:49:50 -080050 string->SetCount(count_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -070051 DCHECK(!string->IsCompressed() || kUseStringCompression);
Jeff Hao848f70a2014-01-15 13:49:50 -080052 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070053
Jeff Hao848f70a2014-01-15 13:49:50 -080054 private:
55 const int32_t count_;
56};
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070057
Jeff Hao848f70a2014-01-15 13:49:50 -080058// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
59class SetStringCountAndBytesVisitor {
60 public:
Mathieu Chartier81aa0122015-04-28 10:01:28 -070061 SetStringCountAndBytesVisitor(int32_t count, Handle<ByteArray> src_array, int32_t offset,
62 int32_t high_byte)
63 : count_(count), src_array_(src_array), offset_(offset), high_byte_(high_byte) {
Jeff Hao848f70a2014-01-15 13:49:50 -080064 }
65
Mathieu Chartier9d156d52016-10-06 17:44:26 -070066 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070067 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080068 // Avoid AsString as object is not yet in live bitmap or allocation stack.
Mathieu Chartier9d156d52016-10-06 17:44:26 -070069 ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
Jeff Hao848f70a2014-01-15 13:49:50 -080070 string->SetCount(count_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -070071 DCHECK(!string->IsCompressed() || kUseStringCompression);
72 int32_t length = String::GetLengthFromCount(count_);
Mathieu Chartier81aa0122015-04-28 10:01:28 -070073 const uint8_t* const src = reinterpret_cast<uint8_t*>(src_array_->GetData()) + offset_;
jessicahandojo3aaa37b2016-07-29 14:46:37 -070074 if (string->IsCompressed()) {
75 uint8_t* valueCompressed = string->GetValueCompressed();
76 for (int i = 0; i < length; i++) {
77 valueCompressed[i] = (src[i] & 0xFF);
78 }
79 } else {
80 uint16_t* value = string->GetValue();
81 for (int i = 0; i < length; i++) {
82 value[i] = high_byte_ + (src[i] & 0xFF);
83 }
Jeff Hao848f70a2014-01-15 13:49:50 -080084 }
85 }
86
87 private:
88 const int32_t count_;
Mathieu Chartier81aa0122015-04-28 10:01:28 -070089 Handle<ByteArray> src_array_;
90 const int32_t offset_;
Jeff Hao848f70a2014-01-15 13:49:50 -080091 const int32_t high_byte_;
92};
93
94// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
Mathieu Chartier81aa0122015-04-28 10:01:28 -070095class SetStringCountAndValueVisitorFromCharArray {
Jeff Hao848f70a2014-01-15 13:49:50 -080096 public:
Mathieu Chartier81aa0122015-04-28 10:01:28 -070097 SetStringCountAndValueVisitorFromCharArray(int32_t count, Handle<CharArray> src_array,
98 int32_t offset) :
99 count_(count), src_array_(src_array), offset_(offset) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800100 }
101
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700102 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700103 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800104 // Avoid AsString as object is not yet in live bitmap or allocation stack.
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700105 ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
Jeff Hao848f70a2014-01-15 13:49:50 -0800106 string->SetCount(count_);
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700107 const uint16_t* const src = src_array_->GetData() + offset_;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700108 const int32_t length = String::GetLengthFromCount(count_);
109 bool compressible = kUseStringCompression && String::GetCompressionFlagFromCount(count_);
110 DCHECK(!compressible || kUseStringCompression);
111 if (compressible) {
112 for (int i = 0; i < length; ++i) {
113 string->GetValueCompressed()[i] = static_cast<uint8_t>(src[i]);
114 }
115 } else {
116 memcpy(string->GetValue(), src, length * sizeof(uint16_t));
117 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800118 }
119
120 private:
121 const int32_t count_;
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700122 Handle<CharArray> src_array_;
123 const int32_t offset_;
124};
125
126// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
127class SetStringCountAndValueVisitorFromString {
128 public:
129 SetStringCountAndValueVisitorFromString(int32_t count, Handle<String> src_string,
130 int32_t offset) :
131 count_(count), src_string_(src_string), offset_(offset) {
132 }
133
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700134 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700135 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700136 // Avoid AsString as object is not yet in live bitmap or allocation stack.
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700137 ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700138 string->SetCount(count_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700139 const int32_t length = String::GetLengthFromCount(count_);
140 bool compressible = kUseStringCompression && String::GetCompressionFlagFromCount(count_);
141 DCHECK(!compressible || kUseStringCompression);
142 if (src_string_->IsCompressed()) {
143 const uint8_t* const src = src_string_->GetValueCompressed() + offset_;
144 memcpy(string->GetValueCompressed(), src, length * sizeof(uint8_t));
145 } else {
146 const uint16_t* const src = src_string_->GetValue() + offset_;
147 if (compressible) {
148 for (int i = 0; i < length; ++i) {
149 string->GetValueCompressed()[i] = static_cast<uint8_t>(src[i]);
150 }
151 } else {
152 memcpy(string->GetValue(), src, length * sizeof(uint16_t));
153 }
154 }
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700155 }
156
157 private:
158 const int32_t count_;
159 Handle<String> src_string_;
160 const int32_t offset_;
Jeff Hao848f70a2014-01-15 13:49:50 -0800161};
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700162
Mathieu Chartier9e868092016-10-31 14:58:04 -0700163inline ObjPtr<String> String::Intern() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700164 return Runtime::Current()->GetInternTable()->InternWeak(this);
165}
166
Jeff Hao848f70a2014-01-15 13:49:50 -0800167inline uint16_t String::CharAt(int32_t index) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700168 int32_t count = GetLength();
Jeff Hao848f70a2014-01-15 13:49:50 -0800169 if (UNLIKELY((index < 0) || (index >= count))) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100170 ThrowStringIndexOutOfBoundsException(index, count);
Jeff Hao848f70a2014-01-15 13:49:50 -0800171 return 0;
172 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700173 if (IsCompressed()) {
174 return GetValueCompressed()[index];
175 } else {
176 return GetValue()[index];
177 }
178}
179
180template <typename MemoryType>
181int32_t String::FastIndexOf(MemoryType* chars, int32_t ch, int32_t start) {
182 const MemoryType* p = chars + start;
183 const MemoryType* end = chars + GetLength();
184 while (p < end) {
185 if (*p++ == ch) {
186 return (p - 1) - chars;
187 }
188 }
189 return -1;
Jeff Hao848f70a2014-01-15 13:49:50 -0800190}
191
192template<VerifyObjectFlags kVerifyFlags>
193inline size_t String::SizeOf() {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700194 size_t size = sizeof(String);
195 if (IsCompressed()) {
196 size += (sizeof(uint8_t) * GetLength<kVerifyFlags>());
197 } else {
198 size += (sizeof(uint16_t) * GetLength<kVerifyFlags>());
199 }
Vladimir Marko4ef52262015-08-26 18:12:56 +0100200 // String.equals() intrinsics assume zero-padding up to kObjectAlignment,
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100201 // so make sure the zero-padding is actually copied around if GC compaction
202 // chooses to copy only SizeOf() bytes.
Vladimir Marko4ef52262015-08-26 18:12:56 +0100203 // http://b/23528461
204 return RoundUp(size, kObjectAlignment);
Jeff Hao848f70a2014-01-15 13:49:50 -0800205}
206
207template <bool kIsInstrumented, typename PreFenceVisitor>
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700208inline String* String::Alloc(Thread* self, int32_t utf16_length_with_flag,
209 gc::AllocatorType allocator_type,
Jeff Hao848f70a2014-01-15 13:49:50 -0800210 const PreFenceVisitor& pre_fence_visitor) {
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100211 constexpr size_t header_size = sizeof(String);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700212 const bool compressible = kUseStringCompression &&
213 String::GetCompressionFlagFromCount(utf16_length_with_flag);
214 const size_t block_size = (compressible) ? sizeof(uint8_t) : sizeof(uint16_t);
215 size_t length = String::GetLengthFromCount(utf16_length_with_flag);
216 static_assert(sizeof(length) <= sizeof(size_t),
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100217 "static_cast<size_t>(utf16_length) must not lose bits.");
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700218 size_t data_size = block_size * length;
Jeff Hao848f70a2014-01-15 13:49:50 -0800219 size_t size = header_size + data_size;
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100220 // String.equals() intrinsics assume zero-padding up to kObjectAlignment,
221 // so make sure the allocator clears the padding as well.
222 // http://b/23528461
223 size_t alloc_size = RoundUp(size, kObjectAlignment);
Jeff Hao848f70a2014-01-15 13:49:50 -0800224
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700225 Class* string_class = GetJavaLangString();
Jeff Hao848f70a2014-01-15 13:49:50 -0800226 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100227 // Do this by comparing with the maximum length that will _not_ cause an overflow.
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700228 const size_t overflow_length = (-header_size) / block_size; // Unsigned arithmetic.
229 const size_t max_alloc_length = overflow_length - 1u;
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100230 static_assert(IsAligned<sizeof(uint16_t)>(kObjectAlignment),
231 "kObjectAlignment must be at least as big as Java char alignment");
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700232 const size_t max_length = RoundDown(max_alloc_length, kObjectAlignment / block_size);
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100233 if (UNLIKELY(length > max_length)) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800234 self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow",
David Sehr709b0702016-10-13 09:12:37 -0700235 Class::PrettyDescriptor(string_class).c_str(),
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700236 static_cast<int>(length)).c_str());
Jeff Hao848f70a2014-01-15 13:49:50 -0800237 return nullptr;
238 }
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100239
Jeff Hao848f70a2014-01-15 13:49:50 -0800240 gc::Heap* heap = Runtime::Current()->GetHeap();
241 return down_cast<String*>(
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100242 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, string_class, alloc_size,
Jeff Haob7c8c1a2015-06-22 14:29:54 -0700243 allocator_type, pre_fence_visitor));
Jeff Hao848f70a2014-01-15 13:49:50 -0800244}
245
246template <bool kIsInstrumented>
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700247inline String* String::AllocEmptyString(Thread* self, gc::AllocatorType allocator_type) {
jessicahandojo7908c8e2016-09-09 19:05:34 -0700248 const int32_t length_with_flag = String::GetFlaggedCount(0);
249 SetStringCountVisitor visitor(length_with_flag);
250 return Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700251}
252
253template <bool kIsInstrumented>
Jeff Hao848f70a2014-01-15 13:49:50 -0800254inline String* String::AllocFromByteArray(Thread* self, int32_t byte_length,
255 Handle<ByteArray> array, int32_t offset,
256 int32_t high_byte, gc::AllocatorType allocator_type) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700257 const uint8_t* const src = reinterpret_cast<uint8_t*>(array->GetData()) + offset;
258 const bool compressible = kUseStringCompression && String::AllASCII<uint8_t>(src, byte_length)
259 && (high_byte == 0);
260 const int32_t length_with_flag = (compressible) ? String::GetFlaggedCount(byte_length)
261 : byte_length;
262 SetStringCountAndBytesVisitor visitor(length_with_flag, array, offset, high_byte << 8);
263 String* string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
Jeff Hao848f70a2014-01-15 13:49:50 -0800264 return string;
265}
266
267template <bool kIsInstrumented>
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700268inline String* String::AllocFromCharArray(Thread* self, int32_t count,
Jeff Hao848f70a2014-01-15 13:49:50 -0800269 Handle<CharArray> array, int32_t offset,
270 gc::AllocatorType allocator_type) {
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700271 // It is a caller error to have a count less than the actual array's size.
272 DCHECK_GE(array->GetLength(), count);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700273 const bool compressible = kUseStringCompression &&
274 String::AllASCII<uint16_t>(array->GetData() + offset, count);
275 const int32_t length_with_flag = (compressible) ? String::GetFlaggedCount(count) : count;
276 SetStringCountAndValueVisitorFromCharArray visitor(length_with_flag, array, offset);
277 String* new_string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
Jeff Hao848f70a2014-01-15 13:49:50 -0800278 return new_string;
279}
280
281template <bool kIsInstrumented>
282inline String* String::AllocFromString(Thread* self, int32_t string_length, Handle<String> string,
283 int32_t offset, gc::AllocatorType allocator_type) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700284 const bool compressible = kUseStringCompression &&
285 ((string->IsCompressed()) ? true : String::AllASCII<uint16_t>(string->GetValue() + offset,
286 string_length));
287 const int32_t length_with_flag = (compressible) ? String::GetFlaggedCount(string_length)
288 : string_length;
289 SetStringCountAndValueVisitorFromString visitor(length_with_flag, string, offset);
290 String* new_string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
Jeff Hao848f70a2014-01-15 13:49:50 -0800291 return new_string;
292}
293
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700294inline int32_t String::GetHashCode() {
295 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_));
296 if (UNLIKELY(result == 0)) {
297 result = ComputeHashCode();
298 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700299 if (kIsDebugBuild) {
300 if (IsCompressed()) {
301 DCHECK(result != 0 || ComputeUtf16Hash(GetValueCompressed(), GetLength()) == 0)
302 << ToModifiedUtf8() << " " << result;
303 } else {
304 DCHECK(result != 0 || ComputeUtf16Hash(GetValue(), GetLength()) == 0)
305 << ToModifiedUtf8() << " " << result;
306 }
307 }
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700308 return result;
309}
310
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700311template<typename MemoryType>
312bool String::AllASCII(const MemoryType* const chars, const int length) {
313 for (int i = 0; i < length; ++i) {
314 if (chars[i] > 0x80) {
315 return false;
316 }
317 }
318 return true;
319}
320
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700321} // namespace mirror
322} // namespace art
323
324#endif // ART_RUNTIME_MIRROR_STRING_INL_H_