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