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 | */ |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 16 | #ifndef ART_RUNTIME_MIRROR_STRING_INL_H_ |
| 17 | #define ART_RUNTIME_MIRROR_STRING_INL_H_ |
| 18 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 19 | #include "string.h" |
| 20 | |
| 21 | #include "android-base/stringprintf.h" |
| 22 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 23 | #include "array.h" |
Vladimir Marko | 4ef5226 | 2015-08-26 18:12:56 +0100 | [diff] [blame] | 24 | #include "base/bit_utils.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 25 | #include "class.h" |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 26 | #include "common_throws.h" |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 27 | #include "gc/heap-inl.h" |
Vladimir Marko | 4ef5226 | 2015-08-26 18:12:56 +0100 | [diff] [blame] | 28 | #include "globals.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 29 | #include "runtime.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 30 | #include "thread.h" |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 31 | #include "utf.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 32 | #include "utils.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | namespace mirror { |
| 36 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 37 | inline uint32_t String::ClassSize(PointerSize pointer_size) { |
Vladimir Marko | 92907f3 | 2017-02-20 14:08:30 +0000 | [diff] [blame] | 38 | uint32_t vtable_entries = Object::kVTableLength + 56; |
Narayan Kamath | 5d8fa8b | 2016-04-13 14:17:44 +0100 | [diff] [blame] | 39 | 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] | 40 | } |
| 41 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 42 | // Sets string count in the allocation code path to ensure it is guarded by a CAS. |
| 43 | class SetStringCountVisitor { |
| 44 | public: |
| 45 | explicit SetStringCountVisitor(int32_t count) : count_(count) { |
| 46 | } |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 47 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 48 | void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 49 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 50 | // Avoid AsString as object is not yet in live bitmap or allocation stack. |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 51 | ObjPtr<String> string = ObjPtr<String>::DownCast(obj); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 52 | string->SetCount(count_); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 53 | DCHECK(!string->IsCompressed() || kUseStringCompression); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 54 | } |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 55 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 56 | private: |
| 57 | const int32_t count_; |
| 58 | }; |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 59 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 60 | // Sets string count and value in the allocation code path to ensure it is guarded by a CAS. |
| 61 | class SetStringCountAndBytesVisitor { |
| 62 | public: |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 63 | SetStringCountAndBytesVisitor(int32_t count, Handle<ByteArray> src_array, int32_t offset, |
| 64 | int32_t high_byte) |
| 65 | : count_(count), src_array_(src_array), offset_(offset), high_byte_(high_byte) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 68 | void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 69 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 70 | // Avoid AsString as object is not yet in live bitmap or allocation stack. |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 71 | ObjPtr<String> string = ObjPtr<String>::DownCast(obj); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 72 | string->SetCount(count_); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 73 | DCHECK(!string->IsCompressed() || kUseStringCompression); |
| 74 | int32_t length = String::GetLengthFromCount(count_); |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 75 | const uint8_t* const src = reinterpret_cast<uint8_t*>(src_array_->GetData()) + offset_; |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 76 | if (string->IsCompressed()) { |
| 77 | uint8_t* valueCompressed = string->GetValueCompressed(); |
| 78 | for (int i = 0; i < length; i++) { |
| 79 | valueCompressed[i] = (src[i] & 0xFF); |
| 80 | } |
| 81 | } else { |
| 82 | uint16_t* value = string->GetValue(); |
| 83 | for (int i = 0; i < length; i++) { |
| 84 | value[i] = high_byte_ + (src[i] & 0xFF); |
| 85 | } |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | const int32_t count_; |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 91 | Handle<ByteArray> src_array_; |
| 92 | const int32_t offset_; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 93 | const int32_t high_byte_; |
| 94 | }; |
| 95 | |
| 96 | // 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] | 97 | class SetStringCountAndValueVisitorFromCharArray { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 98 | public: |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 99 | SetStringCountAndValueVisitorFromCharArray(int32_t count, Handle<CharArray> src_array, |
| 100 | int32_t offset) : |
| 101 | count_(count), src_array_(src_array), offset_(offset) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 104 | void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 105 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 106 | // Avoid AsString as object is not yet in live bitmap or allocation stack. |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 107 | ObjPtr<String> string = ObjPtr<String>::DownCast(obj); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 108 | string->SetCount(count_); |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 109 | const uint16_t* const src = src_array_->GetData() + offset_; |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 110 | const int32_t length = String::GetLengthFromCount(count_); |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 111 | if (kUseStringCompression && String::IsCompressed(count_)) { |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 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 Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | private: |
| 121 | const int32_t count_; |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 122 | 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. |
| 127 | class SetStringCountAndValueVisitorFromString { |
| 128 | public: |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 129 | SetStringCountAndValueVisitorFromString(int32_t count, |
| 130 | Handle<String> src_string, |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 131 | int32_t offset) : |
| 132 | count_(count), src_string_(src_string), offset_(offset) { |
| 133 | } |
| 134 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 135 | void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 136 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 137 | // Avoid AsString as object is not yet in live bitmap or allocation stack. |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 138 | ObjPtr<String> string = ObjPtr<String>::DownCast(obj); |
Mathieu Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 139 | string->SetCount(count_); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 140 | const int32_t length = String::GetLengthFromCount(count_); |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 141 | bool compressible = kUseStringCompression && String::IsCompressed(count_); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 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 Chartier | 81aa012 | 2015-04-28 10:01:28 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | private: |
| 158 | const int32_t count_; |
| 159 | Handle<String> src_string_; |
| 160 | const int32_t offset_; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 161 | }; |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 162 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 163 | inline uint16_t String::CharAt(int32_t index) { |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 164 | int32_t count = GetLength(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 165 | if (UNLIKELY((index < 0) || (index >= count))) { |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 166 | ThrowStringIndexOutOfBoundsException(index, count); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 167 | return 0; |
| 168 | } |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 169 | if (IsCompressed()) { |
| 170 | return GetValueCompressed()[index]; |
| 171 | } else { |
| 172 | return GetValue()[index]; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | template <typename MemoryType> |
| 177 | int32_t String::FastIndexOf(MemoryType* chars, int32_t ch, int32_t start) { |
| 178 | const MemoryType* p = chars + start; |
| 179 | const MemoryType* end = chars + GetLength(); |
| 180 | while (p < end) { |
| 181 | if (*p++ == ch) { |
| 182 | return (p - 1) - chars; |
| 183 | } |
| 184 | } |
| 185 | return -1; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | template<VerifyObjectFlags kVerifyFlags> |
| 189 | inline size_t String::SizeOf() { |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 190 | size_t size = sizeof(String); |
| 191 | if (IsCompressed()) { |
| 192 | size += (sizeof(uint8_t) * GetLength<kVerifyFlags>()); |
| 193 | } else { |
| 194 | size += (sizeof(uint16_t) * GetLength<kVerifyFlags>()); |
| 195 | } |
Vladimir Marko | 4ef5226 | 2015-08-26 18:12:56 +0100 | [diff] [blame] | 196 | // String.equals() intrinsics assume zero-padding up to kObjectAlignment, |
Vladimir Marko | c2b35d2 | 2015-08-27 11:09:29 +0100 | [diff] [blame] | 197 | // so make sure the zero-padding is actually copied around if GC compaction |
| 198 | // chooses to copy only SizeOf() bytes. |
Vladimir Marko | 4ef5226 | 2015-08-26 18:12:56 +0100 | [diff] [blame] | 199 | // http://b/23528461 |
| 200 | return RoundUp(size, kObjectAlignment); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | template <bool kIsInstrumented, typename PreFenceVisitor> |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 204 | inline String* String::Alloc(Thread* self, int32_t utf16_length_with_flag, |
| 205 | gc::AllocatorType allocator_type, |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 206 | const PreFenceVisitor& pre_fence_visitor) { |
Vladimir Marko | c2b35d2 | 2015-08-27 11:09:29 +0100 | [diff] [blame] | 207 | constexpr size_t header_size = sizeof(String); |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 208 | const bool compressible = kUseStringCompression && String::IsCompressed(utf16_length_with_flag); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 209 | const size_t block_size = (compressible) ? sizeof(uint8_t) : sizeof(uint16_t); |
| 210 | size_t length = String::GetLengthFromCount(utf16_length_with_flag); |
| 211 | static_assert(sizeof(length) <= sizeof(size_t), |
Vladimir Marko | c2b35d2 | 2015-08-27 11:09:29 +0100 | [diff] [blame] | 212 | "static_cast<size_t>(utf16_length) must not lose bits."); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 213 | size_t data_size = block_size * length; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 214 | size_t size = header_size + data_size; |
Vladimir Marko | c2b35d2 | 2015-08-27 11:09:29 +0100 | [diff] [blame] | 215 | // String.equals() intrinsics assume zero-padding up to kObjectAlignment, |
| 216 | // so make sure the allocator clears the padding as well. |
| 217 | // http://b/23528461 |
| 218 | size_t alloc_size = RoundUp(size, kObjectAlignment); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 219 | |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 220 | Class* string_class = GetJavaLangString(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 221 | // Check for overflow and throw OutOfMemoryError if this was an unreasonable request. |
Vladimir Marko | c2b35d2 | 2015-08-27 11:09:29 +0100 | [diff] [blame] | 222 | // Do this by comparing with the maximum length that will _not_ cause an overflow. |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 223 | const size_t overflow_length = (-header_size) / block_size; // Unsigned arithmetic. |
| 224 | const size_t max_alloc_length = overflow_length - 1u; |
Vladimir Marko | c2b35d2 | 2015-08-27 11:09:29 +0100 | [diff] [blame] | 225 | static_assert(IsAligned<sizeof(uint16_t)>(kObjectAlignment), |
| 226 | "kObjectAlignment must be at least as big as Java char alignment"); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 227 | const size_t max_length = RoundDown(max_alloc_length, kObjectAlignment / block_size); |
Vladimir Marko | c2b35d2 | 2015-08-27 11:09:29 +0100 | [diff] [blame] | 228 | if (UNLIKELY(length > max_length)) { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 229 | self->ThrowOutOfMemoryError( |
| 230 | android::base::StringPrintf("%s of length %d would overflow", |
| 231 | Class::PrettyDescriptor(string_class).c_str(), |
| 232 | static_cast<int>(length)).c_str()); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 233 | return nullptr; |
| 234 | } |
Vladimir Marko | c2b35d2 | 2015-08-27 11:09:29 +0100 | [diff] [blame] | 235 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 236 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 237 | return down_cast<String*>( |
Vladimir Marko | c2b35d2 | 2015-08-27 11:09:29 +0100 | [diff] [blame] | 238 | heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, string_class, alloc_size, |
Jeff Hao | b7c8c1a | 2015-06-22 14:29:54 -0700 | [diff] [blame] | 239 | allocator_type, pre_fence_visitor)); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | template <bool kIsInstrumented> |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 243 | inline String* String::AllocEmptyString(Thread* self, gc::AllocatorType allocator_type) { |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 244 | const int32_t length_with_flag = String::GetFlaggedCount(0, /* compressible */ true); |
jessicahandojo | 7908c8e | 2016-09-09 19:05:34 -0700 | [diff] [blame] | 245 | SetStringCountVisitor visitor(length_with_flag); |
| 246 | return Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | template <bool kIsInstrumented> |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 250 | inline String* String::AllocFromByteArray(Thread* self, int32_t byte_length, |
| 251 | Handle<ByteArray> array, int32_t offset, |
| 252 | int32_t high_byte, gc::AllocatorType allocator_type) { |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 253 | const uint8_t* const src = reinterpret_cast<uint8_t*>(array->GetData()) + offset; |
Vladimir Marko | a44c445 | 2017-07-13 18:49:35 +0100 | [diff] [blame] | 254 | high_byte &= 0xff; // Extract the relevant bits before determining `compressible`. |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 255 | const bool compressible = |
| 256 | kUseStringCompression && String::AllASCII<uint8_t>(src, byte_length) && (high_byte == 0); |
| 257 | const int32_t length_with_flag = String::GetFlaggedCount(byte_length, compressible); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 258 | SetStringCountAndBytesVisitor visitor(length_with_flag, array, offset, high_byte << 8); |
| 259 | String* string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 260 | return string; |
| 261 | } |
| 262 | |
| 263 | template <bool kIsInstrumented> |
Igor Murashkin | c449e8b | 2015-06-10 15:56:42 -0700 | [diff] [blame] | 264 | inline String* String::AllocFromCharArray(Thread* self, int32_t count, |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 265 | Handle<CharArray> array, int32_t offset, |
| 266 | gc::AllocatorType allocator_type) { |
Igor Murashkin | c449e8b | 2015-06-10 15:56:42 -0700 | [diff] [blame] | 267 | // It is a caller error to have a count less than the actual array's size. |
| 268 | DCHECK_GE(array->GetLength(), count); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 269 | const bool compressible = kUseStringCompression && |
| 270 | String::AllASCII<uint16_t>(array->GetData() + offset, count); |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 271 | const int32_t length_with_flag = String::GetFlaggedCount(count, compressible); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 272 | SetStringCountAndValueVisitorFromCharArray visitor(length_with_flag, array, offset); |
| 273 | String* new_string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 274 | return new_string; |
| 275 | } |
| 276 | |
| 277 | template <bool kIsInstrumented> |
| 278 | inline String* String::AllocFromString(Thread* self, int32_t string_length, Handle<String> string, |
| 279 | int32_t offset, gc::AllocatorType allocator_type) { |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 280 | const bool compressible = kUseStringCompression && |
| 281 | ((string->IsCompressed()) ? true : String::AllASCII<uint16_t>(string->GetValue() + offset, |
| 282 | string_length)); |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 283 | const int32_t length_with_flag = String::GetFlaggedCount(string_length, compressible); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 284 | SetStringCountAndValueVisitorFromString visitor(length_with_flag, string, offset); |
| 285 | String* new_string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 286 | return new_string; |
| 287 | } |
| 288 | |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 289 | inline int32_t String::GetHashCode() { |
| 290 | int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_)); |
| 291 | if (UNLIKELY(result == 0)) { |
| 292 | result = ComputeHashCode(); |
| 293 | } |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 294 | if (kIsDebugBuild) { |
| 295 | if (IsCompressed()) { |
| 296 | DCHECK(result != 0 || ComputeUtf16Hash(GetValueCompressed(), GetLength()) == 0) |
| 297 | << ToModifiedUtf8() << " " << result; |
| 298 | } else { |
| 299 | DCHECK(result != 0 || ComputeUtf16Hash(GetValue(), GetLength()) == 0) |
| 300 | << ToModifiedUtf8() << " " << result; |
| 301 | } |
| 302 | } |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 303 | return result; |
| 304 | } |
| 305 | |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 306 | template<typename MemoryType> |
Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 307 | inline bool String::AllASCII(const MemoryType* chars, const int length) { |
Vladimir Marko | 16850ae | 2016-12-09 14:01:02 +0000 | [diff] [blame] | 308 | static_assert(std::is_unsigned<MemoryType>::value, "Expecting unsigned MemoryType"); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 309 | for (int i = 0; i < length; ++i) { |
Vladimir Marko | 92907f3 | 2017-02-20 14:08:30 +0000 | [diff] [blame] | 310 | if (!IsASCII(chars[i])) { |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 311 | return false; |
| 312 | } |
| 313 | } |
| 314 | return true; |
| 315 | } |
| 316 | |
Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 317 | inline bool String::DexFileStringAllASCII(const char* chars, const int length) { |
| 318 | // For strings from the dex file we just need to check that |
| 319 | // the terminating character is at the right position. |
| 320 | DCHECK_EQ(AllASCII(reinterpret_cast<const uint8_t*>(chars), length), chars[length] == 0); |
| 321 | return chars[length] == 0; |
| 322 | } |
| 323 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 324 | } // namespace mirror |
| 325 | } // namespace art |
| 326 | |
| 327 | #endif // ART_RUNTIME_MIRROR_STRING_INL_H_ |