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