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