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_H_ 18 #define ART_RUNTIME_MIRROR_STRING_H_ 19 20 #include "base/bit_utils.h" 21 #include "class.h" 22 #include "object.h" 23 #include "runtime_globals.h" 24 25 namespace art { 26 27 namespace gc { 28 enum AllocatorType : char; 29 } // namespace gc 30 31 template<class T> class Handle; 32 class InternTable; 33 template<class MirrorType> class ObjPtr; 34 class StringBuilderAppend; 35 struct StringOffsets; 36 class StubTest_ReadBarrierForRoot_Test; 37 38 namespace mirror { 39 40 // String Compression 41 static constexpr bool kUseStringCompression = true; 42 enum class StringCompressionFlag : uint32_t { 43 kCompressed = 0u, 44 kUncompressed = 1u 45 }; 46 47 // C++ mirror of java.lang.String 48 class MANAGED String final : public Object { 49 public: 50 MIRROR_CLASS("Ljava/lang/String;"); 51 52 // Size of java.lang.String.class. 53 static uint32_t ClassSize(PointerSize pointer_size); 54 55 // Size of an instance of java.lang.String not including its value array. InstanceSize()56 static constexpr uint32_t InstanceSize() { 57 return sizeof(String); 58 } 59 CountOffset()60 static constexpr MemberOffset CountOffset() { 61 return OFFSET_OF_OBJECT_MEMBER(String, count_); 62 } 63 ValueOffset()64 static constexpr MemberOffset ValueOffset() { 65 return OFFSET_OF_OBJECT_MEMBER(String, value_); 66 } 67 GetValue()68 uint16_t* GetValue() REQUIRES_SHARED(Locks::mutator_lock_) { 69 return &value_[0]; 70 } 71 GetValueCompressed()72 uint8_t* GetValueCompressed() REQUIRES_SHARED(Locks::mutator_lock_) { 73 return &value_compressed_[0]; 74 } 75 76 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> SizeOf()77 size_t SizeOf() REQUIRES_SHARED(Locks::mutator_lock_) { 78 size_t size = sizeof(String); 79 if (IsCompressed()) { 80 size += (sizeof(uint8_t) * GetLength<kVerifyFlags>()); 81 } else { 82 size += (sizeof(uint16_t) * GetLength<kVerifyFlags>()); 83 } 84 // String.equals() intrinsics assume zero-padding up to kObjectAlignment, 85 // so make sure the zero-padding is actually copied around if GC compaction 86 // chooses to copy only SizeOf() bytes. 87 // http://b/23528461 88 return RoundUp(size, kObjectAlignment); 89 } 90 91 // Taking out the first/uppermost bit because it is not part of actual length value 92 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> GetLength()93 int32_t GetLength() REQUIRES_SHARED(Locks::mutator_lock_) { 94 return GetLengthFromCount(GetCount<kVerifyFlags>()); 95 } 96 97 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> GetCount()98 int32_t GetCount() REQUIRES_SHARED(Locks::mutator_lock_) { 99 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(String, count_)); 100 } 101 SetCount(int32_t new_count)102 void SetCount(int32_t new_count) REQUIRES_SHARED(Locks::mutator_lock_) { 103 // Count is invariant so use non-transactional mode. Also disable check as we may run inside 104 // a transaction. 105 SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count); 106 } 107 GetStoredHashCode()108 int32_t GetStoredHashCode() REQUIRES_SHARED(Locks::mutator_lock_) { 109 return GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_)); 110 } 111 112 int32_t GetHashCode() REQUIRES_SHARED(Locks::mutator_lock_); 113 114 // Computes and returns the hash code. 115 int32_t ComputeHashCode() REQUIRES_SHARED(Locks::mutator_lock_); 116 117 int32_t GetUtfLength() REQUIRES_SHARED(Locks::mutator_lock_); 118 119 uint16_t CharAt(int32_t index) REQUIRES_SHARED(Locks::mutator_lock_); 120 121 // Create a new string where all occurences of `old_c` are replaced with `new_c`. 122 // String.doReplace(char, char) is called from String.replace(char, char) when there is a match. 123 static ObjPtr<String> DoReplace(Thread* self, Handle<String> src, uint16_t old_c, uint16_t new_c) 124 REQUIRES_SHARED(Locks::mutator_lock_); 125 126 ObjPtr<String> Intern() REQUIRES_SHARED(Locks::mutator_lock_); 127 128 template <bool kIsInstrumented = true, typename PreFenceVisitor> 129 ALWAYS_INLINE static ObjPtr<String> Alloc(Thread* self, 130 int32_t utf16_length_with_flag, 131 gc::AllocatorType allocator_type, 132 const PreFenceVisitor& pre_fence_visitor) 133 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 134 135 template <bool kIsInstrumented = true> 136 ALWAYS_INLINE static ObjPtr<String> AllocFromByteArray(Thread* self, 137 int32_t byte_length, 138 Handle<ByteArray> array, 139 int32_t offset, 140 int32_t high_byte, 141 gc::AllocatorType allocator_type) 142 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 143 144 template <bool kIsInstrumented = true> 145 ALWAYS_INLINE static ObjPtr<String> AllocFromCharArray(Thread* self, 146 int32_t count, 147 Handle<CharArray> array, 148 int32_t offset, 149 gc::AllocatorType allocator_type) 150 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 151 152 template <bool kIsInstrumented = true> 153 ALWAYS_INLINE static ObjPtr<String> AllocFromString(Thread* self, 154 int32_t string_length, 155 Handle<String> string, 156 int32_t offset, 157 gc::AllocatorType allocator_type) 158 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 159 160 template <bool kIsInstrumented = true> 161 ALWAYS_INLINE static ObjPtr<String> AllocEmptyString(Thread* self, 162 gc::AllocatorType allocator_type) 163 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 164 165 static ObjPtr<String> DoConcat(Thread* self, Handle<String> h_this, Handle<String> h_arg) 166 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 167 168 static ObjPtr<String> DoRepeat(Thread* self, Handle<String> h_this, int32_t count) 169 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 170 171 static ObjPtr<String> AllocFromUtf16(Thread* self, 172 int32_t utf16_length, 173 const uint16_t* utf16_data_in) 174 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 175 176 static ObjPtr<String> AllocFromModifiedUtf8(Thread* self, const char* utf) 177 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 178 179 static ObjPtr<String> AllocFromModifiedUtf8(Thread* self, 180 int32_t utf16_length, 181 const char* utf8_data_in, 182 int32_t utf8_length) 183 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 184 185 static ObjPtr<String> AllocFromModifiedUtf8(Thread* self, 186 int32_t utf16_length, 187 const char* utf8_data_in) 188 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 189 190 bool Equals(const char* modified_utf8) REQUIRES_SHARED(Locks::mutator_lock_); 191 192 bool Equals(ObjPtr<String> that) REQUIRES_SHARED(Locks::mutator_lock_); 193 194 // Create a modified UTF-8 encoded std::string from a java/lang/String object. 195 std::string ToModifiedUtf8() REQUIRES_SHARED(Locks::mutator_lock_); 196 197 int32_t FastIndexOf(int32_t ch, int32_t start) REQUIRES_SHARED(Locks::mutator_lock_); 198 199 template <typename MemoryType> 200 int32_t FastIndexOf(MemoryType* chars, int32_t ch, int32_t start) 201 REQUIRES_SHARED(Locks::mutator_lock_); 202 203 int32_t CompareTo(ObjPtr<String> other) REQUIRES_SHARED(Locks::mutator_lock_); 204 205 static ObjPtr<CharArray> ToCharArray(Handle<String> h_this, Thread* self) 206 REQUIRES_SHARED(Locks::mutator_lock_) 207 REQUIRES(!Roles::uninterruptible_); 208 209 void GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index) 210 REQUIRES_SHARED(Locks::mutator_lock_); 211 212 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsCompressed()213 bool IsCompressed() REQUIRES_SHARED(Locks::mutator_lock_) { 214 return kUseStringCompression && IsCompressed(GetCount()); 215 } 216 217 bool IsValueNull() REQUIRES_SHARED(Locks::mutator_lock_); 218 219 template<typename MemoryType> 220 static bool AllASCII(const MemoryType* chars, const int length); 221 222 static bool DexFileStringAllASCII(const char* chars, const int length); 223 IsCompressed(int32_t count)224 ALWAYS_INLINE static bool IsCompressed(int32_t count) { 225 return GetCompressionFlagFromCount(count) == StringCompressionFlag::kCompressed; 226 } 227 GetCompressionFlagFromCount(int32_t count)228 ALWAYS_INLINE static StringCompressionFlag GetCompressionFlagFromCount(int32_t count) { 229 return kUseStringCompression 230 ? static_cast<StringCompressionFlag>(static_cast<uint32_t>(count) & 1u) 231 : StringCompressionFlag::kUncompressed; 232 } 233 GetLengthFromCount(int32_t count)234 ALWAYS_INLINE static int32_t GetLengthFromCount(int32_t count) { 235 return kUseStringCompression ? static_cast<int32_t>(static_cast<uint32_t>(count) >> 1) : count; 236 } 237 GetFlaggedCount(int32_t length,bool compressible)238 ALWAYS_INLINE static int32_t GetFlaggedCount(int32_t length, bool compressible) { 239 return kUseStringCompression 240 ? static_cast<int32_t>((static_cast<uint32_t>(length) << 1) | 241 (static_cast<uint32_t>(compressible 242 ? StringCompressionFlag::kCompressed 243 : StringCompressionFlag::kUncompressed))) 244 : length; 245 } 246 247 // Returns a human-readable equivalent of 'descriptor'. So "I" would be "int", 248 // "[[I" would be "int[][]", "[Ljava/lang/String;" would be 249 // "java.lang.String[]", and so forth. 250 static std::string PrettyStringDescriptor(ObjPtr<mirror::String> descriptor) 251 REQUIRES_SHARED(Locks::mutator_lock_); 252 std::string PrettyStringDescriptor() 253 REQUIRES_SHARED(Locks::mutator_lock_); 254 IsASCII(uint16_t c)255 static constexpr bool IsASCII(uint16_t c) { 256 // Valid ASCII characters are in range 1..0x7f. Zero is not considered ASCII 257 // because it would complicate the detection of ASCII strings in Modified-UTF8. 258 return (c - 1u) < 0x7fu; 259 } 260 261 private: 262 static bool AllASCIIExcept(const uint16_t* chars, int32_t length, uint16_t non_ascii); 263 264 // Computes, stores, and returns the hash code. 265 int32_t ComputeAndSetHashCode() REQUIRES_SHARED(Locks::mutator_lock_); 266 SetHashCode(int32_t new_hash_code)267 void SetHashCode(int32_t new_hash_code) REQUIRES_SHARED(Locks::mutator_lock_) { 268 if (kIsDebugBuild) { 269 CHECK_EQ(new_hash_code, ComputeHashCode()); 270 int32_t old_hash_code = GetStoredHashCode(); 271 // Another thread could have raced this one and set the hash code. 272 CHECK(old_hash_code == 0 || old_hash_code == new_hash_code) 273 << "old: " << old_hash_code << " new: " << new_hash_code; 274 } 275 // Hash code is invariant so use non-transactional mode, allowing a failed transaction 276 // to set the hash code anyway. Also disable check as we may run inside a transaction. 277 SetField32</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>( 278 OFFSET_OF_OBJECT_MEMBER(String, hash_code_), new_hash_code); 279 } 280 281 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". 282 283 // If string compression is enabled, count_ holds the StringCompressionFlag in the 284 // least significant bit and the length in the remaining bits, length = count_ >> 1. 285 int32_t count_; 286 287 uint32_t hash_code_; 288 289 // Compression of all-ASCII into 8-bit memory leads to usage one of these fields 290 union { 291 uint16_t value_[0]; 292 uint8_t value_compressed_[0]; 293 }; 294 295 friend class art::InternTable; // Let `InternTable` call `SetHashCode()`. 296 friend class art::StringBuilderAppend; 297 friend struct art::StringOffsets; // for verifying offset information 298 299 DISALLOW_IMPLICIT_CONSTRUCTORS(String); 300 }; 301 302 } // namespace mirror 303 } // namespace art 304 305 #endif // ART_RUNTIME_MIRROR_STRING_H_ 306