Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 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_STRING_INL_H_ |
| 18 | #define ART_RUNTIME_MIRROR_STRING_INL_H_ |
| 19 | |
| 20 | #include "array.h" |
Vladimir Marko | 4ef5226 | 2015-08-26 18:12:56 +0100 | [diff] [blame] | 21 | #include "base/bit_utils.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 22 | #include "class.h" |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 23 | #include "common_throws.h" |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 24 | #include "gc/heap-inl.h" |
Vladimir Marko | 4ef5226 | 2015-08-26 18:12:56 +0100 | [diff] [blame] | 25 | #include "globals.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 26 | #include "intern_table.h" |
| 27 | #include "runtime.h" |
| 28 | #include "string.h" |
| 29 | #include "thread.h" |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 30 | #include "utf.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 31 | #include "utils.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | namespace mirror { |
| 35 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 36 | inline uint32_t String::ClassSize(PointerSize pointer_size) { |
Przemyslaw Szczepaniak | a4fa2e7 | 2016-06-22 13:30:36 +0100 | [diff] [blame] | 37 | uint32_t vtable_entries = Object::kVTableLength + 57; |
Narayan Kamath | 5d8fa8b | 2016-04-13 14:17:44 +0100 | [diff] [blame] | 38 | return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 1, 2, pointer_size); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 41 | // Sets string count in the allocation code path to ensure it is guarded by a CAS. |
| 42 | class SetStringCountVisitor { |
| 43 | public: |
| 44 | explicit SetStringCountVisitor(int32_t count) : count_(count) { |
| 45 | } |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 46 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 47 | void operator()(Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 48 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 49 | // Avoid AsString as object is not yet in live bitmap or allocation stack. |
| 50 | String* string = down_cast<String*>(obj); |
| 51 | string->SetCount(count_); |
| 52 | } |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 53 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 54 | private: |
| 55 | const int32_t count_; |
| 56 | }; |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 57 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 58 | // Sets string count and value in the allocation code path to ensure it is guarded by a CAS. |
| 59 | class SetStringCountAndBytesVisitor { |
| 60 | public: |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 61 | 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 Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void operator()(Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 67 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 68 | // Avoid AsString as object is not yet in live bitmap or allocation stack. |
| 69 | String* string = down_cast<String*>(obj); |
| 70 | string->SetCount(count_); |
| 71 | uint16_t* value = string->GetValue(); |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 72 | const uint8_t* const src = reinterpret_cast<uint8_t*>(src_array_->GetData()) + offset_; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 73 | for (int i = 0; i < count_; i++) { |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 74 | value[i] = high_byte_ + (src[i] & 0xFF); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | const int32_t count_; |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 80 | Handle<ByteArray> src_array_; |
| 81 | const int32_t offset_; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 82 | const int32_t high_byte_; |
| 83 | }; |
| 84 | |
| 85 | // Sets string count and value in the allocation code path to ensure it is guarded by a CAS. |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 86 | class SetStringCountAndValueVisitorFromCharArray { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 87 | public: |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 88 | SetStringCountAndValueVisitorFromCharArray(int32_t count, Handle<CharArray> src_array, |
| 89 | int32_t offset) : |
| 90 | count_(count), src_array_(src_array), offset_(offset) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 93 | void operator()(Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 94 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 95 | // Avoid AsString as object is not yet in live bitmap or allocation stack. |
| 96 | String* string = down_cast<String*>(obj); |
| 97 | string->SetCount(count_); |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 98 | const uint16_t* const src = src_array_->GetData() + offset_; |
| 99 | memcpy(string->GetValue(), src, count_ * sizeof(uint16_t)); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | private: |
| 103 | const int32_t count_; |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 104 | Handle<CharArray> src_array_; |
| 105 | const int32_t offset_; |
| 106 | }; |
| 107 | |
| 108 | // Sets string count and value in the allocation code path to ensure it is guarded by a CAS. |
| 109 | class SetStringCountAndValueVisitorFromString { |
| 110 | public: |
| 111 | SetStringCountAndValueVisitorFromString(int32_t count, Handle<String> src_string, |
| 112 | int32_t offset) : |
| 113 | count_(count), src_string_(src_string), offset_(offset) { |
| 114 | } |
| 115 | |
| 116 | void operator()(Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 117 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 118 | // Avoid AsString as object is not yet in live bitmap or allocation stack. |
| 119 | String* string = down_cast<String*>(obj); |
| 120 | string->SetCount(count_); |
| 121 | const uint16_t* const src = src_string_->GetValue() + offset_; |
| 122 | memcpy(string->GetValue(), src, count_ * sizeof(uint16_t)); |
| 123 | } |
| 124 | |
| 125 | private: |
| 126 | const int32_t count_; |
| 127 | Handle<String> src_string_; |
| 128 | const int32_t offset_; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 129 | }; |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 130 | |
| 131 | inline String* String::Intern() { |
| 132 | return Runtime::Current()->GetInternTable()->InternWeak(this); |
| 133 | } |
| 134 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 135 | inline uint16_t String::CharAt(int32_t index) { |
| 136 | int32_t count = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_)); |
| 137 | if (UNLIKELY((index < 0) || (index >= count))) { |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 138 | ThrowStringIndexOutOfBoundsException(index, count); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | return GetValue()[index]; |
| 142 | } |
| 143 | |
| 144 | template<VerifyObjectFlags kVerifyFlags> |
| 145 | inline size_t String::SizeOf() { |
Vladimir Marko | 4ef5226 | 2015-08-26 18:12:56 +0100 | [diff] [blame] | 146 | size_t size = sizeof(String) + (sizeof(uint16_t) * GetLength<kVerifyFlags>()); |
| 147 | // String.equals() intrinsics assume zero-padding up to kObjectAlignment, |
Vladimir Marko | c2b35d2 | 2015-08-27 11:09:29 +0100 | [diff] [blame] | 148 | // so make sure the zero-padding is actually copied around if GC compaction |
| 149 | // chooses to copy only SizeOf() bytes. |
Vladimir Marko | 4ef5226 | 2015-08-26 18:12:56 +0100 | [diff] [blame] | 150 | // http://b/23528461 |
| 151 | return RoundUp(size, kObjectAlignment); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | template <bool kIsInstrumented, typename PreFenceVisitor> |
| 155 | inline String* String::Alloc(Thread* self, int32_t utf16_length, gc::AllocatorType allocator_type, |
| 156 | const PreFenceVisitor& pre_fence_visitor) { |
Vladimir Marko | c2b35d2 | 2015-08-27 11:09:29 +0100 | [diff] [blame] | 157 | constexpr size_t header_size = sizeof(String); |
| 158 | static_assert(sizeof(utf16_length) <= sizeof(size_t), |
| 159 | "static_cast<size_t>(utf16_length) must not lose bits."); |
| 160 | size_t length = static_cast<size_t>(utf16_length); |
| 161 | size_t data_size = sizeof(uint16_t) * length; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 162 | size_t size = header_size + data_size; |
Vladimir Marko | c2b35d2 | 2015-08-27 11:09:29 +0100 | [diff] [blame] | 163 | // String.equals() intrinsics assume zero-padding up to kObjectAlignment, |
| 164 | // so make sure the allocator clears the padding as well. |
| 165 | // http://b/23528461 |
| 166 | size_t alloc_size = RoundUp(size, kObjectAlignment); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 167 | Class* string_class = GetJavaLangString(); |
| 168 | |
| 169 | // Check for overflow and throw OutOfMemoryError if this was an unreasonable request. |
Vladimir Marko | c2b35d2 | 2015-08-27 11:09:29 +0100 | [diff] [blame] | 170 | // Do this by comparing with the maximum length that will _not_ cause an overflow. |
| 171 | constexpr size_t overflow_length = (-header_size) / sizeof(uint16_t); // Unsigned arithmetic. |
| 172 | constexpr size_t max_alloc_length = overflow_length - 1u; |
| 173 | static_assert(IsAligned<sizeof(uint16_t)>(kObjectAlignment), |
| 174 | "kObjectAlignment must be at least as big as Java char alignment"); |
| 175 | constexpr size_t max_length = RoundDown(max_alloc_length, kObjectAlignment / sizeof(uint16_t)); |
| 176 | if (UNLIKELY(length > max_length)) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 177 | self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow", |
| 178 | PrettyDescriptor(string_class).c_str(), |
| 179 | utf16_length).c_str()); |
| 180 | return nullptr; |
| 181 | } |
Vladimir Marko | c2b35d2 | 2015-08-27 11:09:29 +0100 | [diff] [blame] | 182 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 183 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 184 | return down_cast<String*>( |
Vladimir Marko | c2b35d2 | 2015-08-27 11:09:29 +0100 | [diff] [blame] | 185 | heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, string_class, alloc_size, |
Jeff Hao | b7c8c1a | 2015-06-22 14:29:54 -0700 | [diff] [blame] | 186 | allocator_type, pre_fence_visitor)); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | template <bool kIsInstrumented> |
| 190 | inline String* String::AllocFromByteArray(Thread* self, int32_t byte_length, |
| 191 | Handle<ByteArray> array, int32_t offset, |
| 192 | int32_t high_byte, gc::AllocatorType allocator_type) { |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 193 | SetStringCountAndBytesVisitor visitor(byte_length, array, offset, high_byte << 8); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 194 | String* string = Alloc<kIsInstrumented>(self, byte_length, allocator_type, visitor); |
| 195 | return string; |
| 196 | } |
| 197 | |
| 198 | template <bool kIsInstrumented> |
Igor Murashkin | c449e8b | 2015-06-10 15:56:42 -0700 | [diff] [blame] | 199 | inline String* String::AllocFromCharArray(Thread* self, int32_t count, |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 200 | Handle<CharArray> array, int32_t offset, |
| 201 | gc::AllocatorType allocator_type) { |
Igor Murashkin | c449e8b | 2015-06-10 15:56:42 -0700 | [diff] [blame] | 202 | // It is a caller error to have a count less than the actual array's size. |
| 203 | DCHECK_GE(array->GetLength(), count); |
| 204 | SetStringCountAndValueVisitorFromCharArray visitor(count, array, offset); |
| 205 | String* new_string = Alloc<kIsInstrumented>(self, count, allocator_type, visitor); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 206 | return new_string; |
| 207 | } |
| 208 | |
| 209 | template <bool kIsInstrumented> |
| 210 | inline String* String::AllocFromString(Thread* self, int32_t string_length, Handle<String> string, |
| 211 | int32_t offset, gc::AllocatorType allocator_type) { |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 212 | SetStringCountAndValueVisitorFromString visitor(string_length, string, offset); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 213 | String* new_string = Alloc<kIsInstrumented>(self, string_length, allocator_type, visitor); |
| 214 | return new_string; |
| 215 | } |
| 216 | |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 217 | inline int32_t String::GetHashCode() { |
| 218 | int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_)); |
| 219 | if (UNLIKELY(result == 0)) { |
| 220 | result = ComputeHashCode(); |
| 221 | } |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 222 | DCHECK(result != 0 || ComputeUtf16Hash(GetValue(), GetLength()) == 0) |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 223 | << ToModifiedUtf8() << " " << result; |
| 224 | return result; |
| 225 | } |
| 226 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 227 | } // namespace mirror |
| 228 | } // namespace art |
| 229 | |
| 230 | #endif // ART_RUNTIME_MIRROR_STRING_INL_H_ |