Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_MIRROR_STRING_H_ |
| 18 | #define ART_RUNTIME_MIRROR_STRING_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
Vladimir Marko | 5924a4a | 2018-05-29 17:40:41 +0100 | [diff] [blame] | 20 | #include "base/bit_utils.h" |
| 21 | #include "base/globals.h" |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 22 | #include "gc/allocator_type.h" |
Vladimir Marko | 6834d34 | 2018-05-25 13:12:09 +0100 | [diff] [blame] | 23 | #include "class.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 24 | #include "object.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 28 | template<class T> class Handle; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | struct StringOffsets; |
| 30 | class StringPiece; |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 31 | class StubTest_ReadBarrierForRoot_Test; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | |
| 33 | namespace mirror { |
| 34 | |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 35 | // String Compression |
Vladimir Marko | bcf716f | 2017-02-23 10:43:09 +0000 | [diff] [blame] | 36 | static constexpr bool kUseStringCompression = true; |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 37 | enum class StringCompressionFlag : uint32_t { |
| 38 | kCompressed = 0u, |
| 39 | kUncompressed = 1u |
| 40 | }; |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 41 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 42 | // C++ mirror of java.lang.String |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame^] | 43 | class MANAGED String final : public Object { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 44 | public: |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 45 | // Size of java.lang.String.class. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 46 | static uint32_t ClassSize(PointerSize pointer_size); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 47 | |
| 48 | // Size of an instance of java.lang.String not including its value array. |
| 49 | static constexpr uint32_t InstanceSize() { |
| 50 | return sizeof(String); |
| 51 | } |
| 52 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 53 | static MemberOffset CountOffset() { |
| 54 | return OFFSET_OF_OBJECT_MEMBER(String, count_); |
| 55 | } |
| 56 | |
| 57 | static MemberOffset ValueOffset() { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 58 | return OFFSET_OF_OBJECT_MEMBER(String, value_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 61 | uint16_t* GetValue() REQUIRES_SHARED(Locks::mutator_lock_) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 62 | return &value_[0]; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 65 | uint8_t* GetValueCompressed() REQUIRES_SHARED(Locks::mutator_lock_) { |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 66 | return &value_compressed_[0]; |
| 67 | } |
| 68 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 69 | template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> |
Vladimir Marko | 5924a4a | 2018-05-29 17:40:41 +0100 | [diff] [blame] | 70 | size_t SizeOf() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 71 | size_t size = sizeof(String); |
| 72 | if (IsCompressed()) { |
| 73 | size += (sizeof(uint8_t) * GetLength<kVerifyFlags>()); |
| 74 | } else { |
| 75 | size += (sizeof(uint16_t) * GetLength<kVerifyFlags>()); |
| 76 | } |
| 77 | // String.equals() intrinsics assume zero-padding up to kObjectAlignment, |
| 78 | // so make sure the zero-padding is actually copied around if GC compaction |
| 79 | // chooses to copy only SizeOf() bytes. |
| 80 | // http://b/23528461 |
| 81 | return RoundUp(size, kObjectAlignment); |
| 82 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 83 | |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 84 | // Taking out the first/uppermost bit because it is not part of actual length value |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 85 | template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 86 | int32_t GetLength() REQUIRES_SHARED(Locks::mutator_lock_) { |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 87 | return GetLengthFromCount(GetCount<kVerifyFlags>()); |
| 88 | } |
| 89 | |
| 90 | template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 91 | int32_t GetCount() REQUIRES_SHARED(Locks::mutator_lock_) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 92 | return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(String, count_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 95 | void SetCount(int32_t new_count) REQUIRES_SHARED(Locks::mutator_lock_) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 96 | // Count is invariant so use non-transactional mode. Also disable check as we may run inside |
| 97 | // a transaction. |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 98 | SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count); |
| 99 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 100 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 101 | int32_t GetHashCode() REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 102 | |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 103 | // Computes, stores, and returns the hash code. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 104 | int32_t ComputeHashCode() REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 105 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 106 | int32_t GetUtfLength() REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 107 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 108 | uint16_t CharAt(int32_t index) REQUIRES_SHARED(Locks::mutator_lock_); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 109 | |
Vladimir Marko | 92907f3 | 2017-02-20 14:08:30 +0000 | [diff] [blame] | 110 | // Create a new string where all occurences of `old_c` are replaced with `new_c`. |
| 111 | // String.doReplace(char, char) is called from String.replace(char, char) when there is a match. |
Vladimir Marko | 9e57aba | 2017-03-16 10:45:40 +0000 | [diff] [blame] | 112 | static ObjPtr<String> DoReplace(Thread* self, Handle<String> src, uint16_t old_c, uint16_t new_c) |
Vladimir Marko | 92907f3 | 2017-02-20 14:08:30 +0000 | [diff] [blame] | 113 | REQUIRES_SHARED(Locks::mutator_lock_); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 114 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 115 | ObjPtr<String> Intern() REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 116 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 117 | template <bool kIsInstrumented> |
| 118 | ALWAYS_INLINE static String* AllocFromByteArray(Thread* self, int32_t byte_length, |
| 119 | Handle<ByteArray> array, int32_t offset, |
| 120 | int32_t high_byte, |
| 121 | gc::AllocatorType allocator_type) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 122 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 123 | |
| 124 | template <bool kIsInstrumented> |
Igor Murashkin | c449e8b | 2015-06-10 15:56:42 -0700 | [diff] [blame] | 125 | ALWAYS_INLINE static String* AllocFromCharArray(Thread* self, int32_t count, |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 126 | Handle<CharArray> array, int32_t offset, |
| 127 | gc::AllocatorType allocator_type) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 128 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 129 | |
| 130 | template <bool kIsInstrumented> |
| 131 | ALWAYS_INLINE static String* AllocFromString(Thread* self, int32_t string_length, |
| 132 | Handle<String> string, int32_t offset, |
| 133 | gc::AllocatorType allocator_type) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 134 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 135 | |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 136 | template <bool kIsInstrumented> |
| 137 | ALWAYS_INLINE static String* AllocEmptyString(Thread* self, |
| 138 | gc::AllocatorType allocator_type) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 139 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 140 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 141 | static String* AllocFromStrings(Thread* self, Handle<String> string, Handle<String> string2) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 142 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 143 | |
| 144 | static String* AllocFromUtf16(Thread* self, int32_t utf16_length, const uint16_t* utf16_data_in) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 145 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 146 | |
| 147 | static String* AllocFromModifiedUtf8(Thread* self, const char* utf) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 148 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 149 | |
Bruce Hoult | 1646d7a | 2015-10-28 15:06:12 +0300 | [diff] [blame] | 150 | static String* AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, |
| 151 | const char* utf8_data_in, int32_t utf8_length) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 152 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); |
Bruce Hoult | 1646d7a | 2015-10-28 15:06:12 +0300 | [diff] [blame] | 153 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 154 | static String* AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, const char* utf8_data_in) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 155 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 156 | |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 157 | // TODO: This is only used in the interpreter to compare against |
| 158 | // entries from a dex files constant pool (ArtField names). Should |
| 159 | // we unify this with Equals(const StringPiece&); ? |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 160 | bool Equals(const char* modified_utf8) REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 161 | |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 162 | // TODO: This is only used to compare DexCache.location with |
| 163 | // a dex_file's location (which is an std::string). Do we really |
| 164 | // need this in mirror::String just for that one usage ? |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 165 | bool Equals(const StringPiece& modified_utf8) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 166 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 167 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 168 | bool Equals(ObjPtr<String> that) REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 169 | |
| 170 | // Compare UTF-16 code point values not in a locale-sensitive manner |
| 171 | int Compare(int32_t utf16_length, const char* utf8_data_in); |
| 172 | |
| 173 | // TODO: do we need this overload? give it a more intention-revealing name. |
| 174 | bool Equals(const uint16_t* that_chars, int32_t that_offset, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 175 | int32_t that_length) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 176 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 177 | |
| 178 | // Create a modified UTF-8 encoded std::string from a java/lang/String object. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 179 | std::string ToModifiedUtf8() REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 180 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 181 | int32_t FastIndexOf(int32_t ch, int32_t start) REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 182 | |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 183 | template <typename MemoryType> |
| 184 | int32_t FastIndexOf(MemoryType* chars, int32_t ch, int32_t start) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 185 | REQUIRES_SHARED(Locks::mutator_lock_); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 186 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 187 | int32_t CompareTo(ObjPtr<String> other) REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 188 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 189 | CharArray* ToCharArray(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 190 | REQUIRES(!Roles::uninterruptible_); |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 191 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 192 | void GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 193 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 194 | |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 195 | template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 196 | bool IsCompressed() REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 197 | return kUseStringCompression && IsCompressed(GetCount()); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 200 | bool IsValueNull() REQUIRES_SHARED(Locks::mutator_lock_); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 201 | |
| 202 | template<typename MemoryType> |
Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 203 | static bool AllASCII(const MemoryType* chars, const int length); |
| 204 | |
| 205 | static bool DexFileStringAllASCII(const char* chars, const int length); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 206 | |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 207 | ALWAYS_INLINE static bool IsCompressed(int32_t count) { |
| 208 | return GetCompressionFlagFromCount(count) == StringCompressionFlag::kCompressed; |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 211 | ALWAYS_INLINE static StringCompressionFlag GetCompressionFlagFromCount(int32_t count) { |
| 212 | return kUseStringCompression |
| 213 | ? static_cast<StringCompressionFlag>(static_cast<uint32_t>(count) & 1u) |
| 214 | : StringCompressionFlag::kUncompressed; |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 217 | ALWAYS_INLINE static int32_t GetLengthFromCount(int32_t count) { |
| 218 | return kUseStringCompression ? static_cast<int32_t>(static_cast<uint32_t>(count) >> 1) : count; |
| 219 | } |
| 220 | |
| 221 | ALWAYS_INLINE static int32_t GetFlaggedCount(int32_t length, bool compressible) { |
| 222 | return kUseStringCompression |
| 223 | ? static_cast<int32_t>((static_cast<uint32_t>(length) << 1) | |
| 224 | (static_cast<uint32_t>(compressible |
| 225 | ? StringCompressionFlag::kCompressed |
| 226 | : StringCompressionFlag::kUncompressed))) |
| 227 | : length; |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 228 | } |
| 229 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 230 | // Returns a human-readable equivalent of 'descriptor'. So "I" would be "int", |
| 231 | // "[[I" would be "int[][]", "[Ljava/lang/String;" would be |
| 232 | // "java.lang.String[]", and so forth. |
| 233 | static std::string PrettyStringDescriptor(ObjPtr<mirror::String> descriptor) |
| 234 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 235 | std::string PrettyStringDescriptor() |
| 236 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 237 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 238 | private: |
Vladimir Marko | 92907f3 | 2017-02-20 14:08:30 +0000 | [diff] [blame] | 239 | static constexpr bool IsASCII(uint16_t c) { |
| 240 | // Valid ASCII characters are in range 1..0x7f. Zero is not considered ASCII |
| 241 | // because it would complicate the detection of ASCII strings in Modified-UTF8. |
| 242 | return (c - 1u) < 0x7fu; |
| 243 | } |
| 244 | |
| 245 | static bool AllASCIIExcept(const uint16_t* chars, int32_t length, uint16_t non_ascii); |
| 246 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 247 | void SetHashCode(int32_t new_hash_code) REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 248 | // Hash code is invariant so use non-transactional mode. Also disable check as we may run inside |
| 249 | // a transaction. |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 250 | DCHECK_EQ(0, GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_))); |
| 251 | SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), new_hash_code); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 252 | } |
| 253 | |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 254 | template <bool kIsInstrumented, typename PreFenceVisitor> |
| 255 | ALWAYS_INLINE static String* Alloc(Thread* self, int32_t utf16_length_with_flag, |
| 256 | gc::AllocatorType allocator_type, |
| 257 | const PreFenceVisitor& pre_fence_visitor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 258 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 259 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 260 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
Vladimir Marko | 595beb3 | 2017-02-06 14:11:54 +0000 | [diff] [blame] | 261 | |
| 262 | // If string compression is enabled, count_ holds the StringCompressionFlag in the |
| 263 | // least significant bit and the length in the remaining bits, length = count_ >> 1. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 264 | int32_t count_; |
| 265 | |
| 266 | uint32_t hash_code_; |
| 267 | |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 268 | // Compression of all-ASCII into 8-bit memory leads to usage one of these fields |
| 269 | union { |
| 270 | uint16_t value_[0]; |
| 271 | uint8_t value_compressed_[0]; |
| 272 | }; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 273 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 274 | friend struct art::StringOffsets; // for verifying offset information |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 275 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 276 | DISALLOW_IMPLICIT_CONSTRUCTORS(String); |
| 277 | }; |
| 278 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 279 | } // namespace mirror |
| 280 | } // namespace art |
| 281 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 282 | #endif // ART_RUNTIME_MIRROR_STRING_H_ |