blob: 545fe93516f5d139841e88e266588047b0c15239 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_MIRROR_STRING_H_
18#define ART_RUNTIME_MIRROR_STRING_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
Jeff Hao848f70a2014-01-15 13:49:50 -080020#include "gc/allocator_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070021#include "gc_root.h"
Ian Rogerse63db272014-07-15 15:36:11 -070022#include "object.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023
24namespace art {
25
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070026template<class T> class Handle;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027struct StringOffsets;
28class StringPiece;
Roland Levillain0d5a2812015-11-13 10:07:31 +000029class StubTest_ReadBarrierForRoot_Test;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030
31namespace mirror {
32
jessicahandojo3aaa37b2016-07-29 14:46:37 -070033// String Compression
Vladimir Markobcf716f2017-02-23 10:43:09 +000034static constexpr bool kUseStringCompression = true;
Vladimir Markofdaf0f42016-10-13 19:29:53 +010035enum class StringCompressionFlag : uint32_t {
36 kCompressed = 0u,
37 kUncompressed = 1u
38};
jessicahandojo3aaa37b2016-07-29 14:46:37 -070039
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040// C++ mirror of java.lang.String
Mingyao Yang98d1cc82014-05-15 17:02:16 -070041class MANAGED String FINAL : public Object {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042 public:
Mingyao Yang98d1cc82014-05-15 17:02:16 -070043 // Size of java.lang.String.class.
Andreas Gampe542451c2016-07-26 09:02:02 -070044 static uint32_t ClassSize(PointerSize pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070045
46 // Size of an instance of java.lang.String not including its value array.
47 static constexpr uint32_t InstanceSize() {
48 return sizeof(String);
49 }
50
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051 static MemberOffset CountOffset() {
52 return OFFSET_OF_OBJECT_MEMBER(String, count_);
53 }
54
55 static MemberOffset ValueOffset() {
Jeff Hao848f70a2014-01-15 13:49:50 -080056 return OFFSET_OF_OBJECT_MEMBER(String, value_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057 }
58
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070059 uint16_t* GetValue() REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080060 return &value_[0];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 }
62
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070063 uint8_t* GetValueCompressed() REQUIRES_SHARED(Locks::mutator_lock_) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -070064 return &value_compressed_[0];
65 }
66
Jeff Hao848f70a2014-01-15 13:49:50 -080067 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070068 size_t SizeOf() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069
jessicahandojo3aaa37b2016-07-29 14:46:37 -070070 // Taking out the first/uppermost bit because it is not part of actual length value
Jeff Hao848f70a2014-01-15 13:49:50 -080071 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070072 int32_t GetLength() REQUIRES_SHARED(Locks::mutator_lock_) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -070073 return GetLengthFromCount(GetCount<kVerifyFlags>());
74 }
75
76 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070077 int32_t GetCount() REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080078 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(String, count_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079 }
80
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070081 void SetCount(int32_t new_count) REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080082 // Count is invariant so use non-transactional mode. Also disable check as we may run inside
83 // a transaction.
Jeff Hao848f70a2014-01-15 13:49:50 -080084 SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count);
85 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070087 int32_t GetHashCode() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070089 // Computes, stores, and returns the hash code.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070090 int32_t ComputeHashCode() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070092 int32_t GetUtfLength() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070094 uint16_t CharAt(int32_t index) REQUIRES_SHARED(Locks::mutator_lock_);
Jeff Hao848f70a2014-01-15 13:49:50 -080095
Vladimir Marko92907f32017-02-20 14:08:30 +000096 // Create a new string where all occurences of `old_c` are replaced with `new_c`.
97 // String.doReplace(char, char) is called from String.replace(char, char) when there is a match.
Vladimir Marko9e57aba2017-03-16 10:45:40 +000098 static ObjPtr<String> DoReplace(Thread* self, Handle<String> src, uint16_t old_c, uint16_t new_c)
Vladimir Marko92907f32017-02-20 14:08:30 +000099 REQUIRES_SHARED(Locks::mutator_lock_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800100
Mathieu Chartier9e868092016-10-31 14:58:04 -0700101 ObjPtr<String> Intern() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102
Jeff Hao848f70a2014-01-15 13:49:50 -0800103 template <bool kIsInstrumented>
104 ALWAYS_INLINE static String* AllocFromByteArray(Thread* self, int32_t byte_length,
105 Handle<ByteArray> array, int32_t offset,
106 int32_t high_byte,
107 gc::AllocatorType allocator_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700108 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800109
110 template <bool kIsInstrumented>
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700111 ALWAYS_INLINE static String* AllocFromCharArray(Thread* self, int32_t count,
Jeff Hao848f70a2014-01-15 13:49:50 -0800112 Handle<CharArray> array, int32_t offset,
113 gc::AllocatorType allocator_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700114 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800115
116 template <bool kIsInstrumented>
117 ALWAYS_INLINE static String* AllocFromString(Thread* self, int32_t string_length,
118 Handle<String> string, int32_t offset,
119 gc::AllocatorType allocator_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700120 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800121
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700122 template <bool kIsInstrumented>
123 ALWAYS_INLINE static String* AllocEmptyString(Thread* self,
124 gc::AllocatorType allocator_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700125 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700126
Jeff Hao848f70a2014-01-15 13:49:50 -0800127 static String* AllocFromStrings(Thread* self, Handle<String> string, Handle<String> string2)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700128 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800129
130 static String* AllocFromUtf16(Thread* self, int32_t utf16_length, const uint16_t* utf16_data_in)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700131 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800132
133 static String* AllocFromModifiedUtf8(Thread* self, const char* utf)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700134 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800135
Bruce Hoult1646d7a2015-10-28 15:06:12 +0300136 static String* AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
137 const char* utf8_data_in, int32_t utf8_length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700138 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Bruce Hoult1646d7a2015-10-28 15:06:12 +0300139
Jeff Hao848f70a2014-01-15 13:49:50 -0800140 static String* AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, const char* utf8_data_in)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700141 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000143 // TODO: This is only used in the interpreter to compare against
144 // entries from a dex files constant pool (ArtField names). Should
145 // we unify this with Equals(const StringPiece&); ?
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700146 bool Equals(const char* modified_utf8) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800147
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000148 // TODO: This is only used to compare DexCache.location with
149 // a dex_file's location (which is an std::string). Do we really
150 // need this in mirror::String just for that one usage ?
Ian Rogersef7d42f2014-01-06 12:55:46 -0800151 bool Equals(const StringPiece& modified_utf8)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700152 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153
Mathieu Chartier31e88222016-10-14 18:43:19 -0700154 bool Equals(ObjPtr<String> that) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155
156 // Compare UTF-16 code point values not in a locale-sensitive manner
157 int Compare(int32_t utf16_length, const char* utf8_data_in);
158
159 // TODO: do we need this overload? give it a more intention-revealing name.
160 bool Equals(const uint16_t* that_chars, int32_t that_offset,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800161 int32_t that_length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700162 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800163
164 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700165 std::string ToModifiedUtf8() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800166
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700167 int32_t FastIndexOf(int32_t ch, int32_t start) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700169 template <typename MemoryType>
170 int32_t FastIndexOf(MemoryType* chars, int32_t ch, int32_t start)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700171 REQUIRES_SHARED(Locks::mutator_lock_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700172
Mathieu Chartier31e88222016-10-14 18:43:19 -0700173 int32_t CompareTo(ObjPtr<String> other) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700175 CharArray* ToCharArray(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700176 REQUIRES(!Roles::uninterruptible_);
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800177
Jeff Hao848f70a2014-01-15 13:49:50 -0800178 void GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700179 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800180
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700181 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700182 bool IsCompressed() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100183 return kUseStringCompression && IsCompressed(GetCount());
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700184 }
185
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700186 bool IsValueNull() REQUIRES_SHARED(Locks::mutator_lock_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700187
188 template<typename MemoryType>
Vladimir Markoe39f14f2017-02-10 15:44:25 +0000189 static bool AllASCII(const MemoryType* chars, const int length);
190
191 static bool DexFileStringAllASCII(const char* chars, const int length);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700192
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100193 ALWAYS_INLINE static bool IsCompressed(int32_t count) {
194 return GetCompressionFlagFromCount(count) == StringCompressionFlag::kCompressed;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700195 }
196
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100197 ALWAYS_INLINE static StringCompressionFlag GetCompressionFlagFromCount(int32_t count) {
198 return kUseStringCompression
199 ? static_cast<StringCompressionFlag>(static_cast<uint32_t>(count) & 1u)
200 : StringCompressionFlag::kUncompressed;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700201 }
202
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100203 ALWAYS_INLINE static int32_t GetLengthFromCount(int32_t count) {
204 return kUseStringCompression ? static_cast<int32_t>(static_cast<uint32_t>(count) >> 1) : count;
205 }
206
207 ALWAYS_INLINE static int32_t GetFlaggedCount(int32_t length, bool compressible) {
208 return kUseStringCompression
209 ? static_cast<int32_t>((static_cast<uint32_t>(length) << 1) |
210 (static_cast<uint32_t>(compressible
211 ? StringCompressionFlag::kCompressed
212 : StringCompressionFlag::kUncompressed)))
213 : length;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700214 }
215
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 static Class* GetJavaLangString() REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700217 DCHECK(!java_lang_String_.IsNull());
218 return java_lang_String_.Read();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 }
220
Mathieu Chartier31e88222016-10-14 18:43:19 -0700221 static void SetClass(ObjPtr<Class> java_lang_String) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700222 static void ResetClass() REQUIRES_SHARED(Locks::mutator_lock_);
223 static void VisitRoots(RootVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224
David Sehr709b0702016-10-13 09:12:37 -0700225 // Returns a human-readable equivalent of 'descriptor'. So "I" would be "int",
226 // "[[I" would be "int[][]", "[Ljava/lang/String;" would be
227 // "java.lang.String[]", and so forth.
228 static std::string PrettyStringDescriptor(ObjPtr<mirror::String> descriptor)
229 REQUIRES_SHARED(Locks::mutator_lock_);
230 std::string PrettyStringDescriptor()
231 REQUIRES_SHARED(Locks::mutator_lock_);
232
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800233 private:
Vladimir Marko92907f32017-02-20 14:08:30 +0000234 static constexpr bool IsASCII(uint16_t c) {
235 // Valid ASCII characters are in range 1..0x7f. Zero is not considered ASCII
236 // because it would complicate the detection of ASCII strings in Modified-UTF8.
237 return (c - 1u) < 0x7fu;
238 }
239
240 static bool AllASCIIExcept(const uint16_t* chars, int32_t length, uint16_t non_ascii);
241
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700242 void SetHashCode(int32_t new_hash_code) REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100243 // Hash code is invariant so use non-transactional mode. Also disable check as we may run inside
244 // a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700245 DCHECK_EQ(0, GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_)));
246 SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), new_hash_code);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 }
248
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700249 template <bool kIsInstrumented, typename PreFenceVisitor>
250 ALWAYS_INLINE static String* Alloc(Thread* self, int32_t utf16_length_with_flag,
251 gc::AllocatorType allocator_type,
252 const PreFenceVisitor& pre_fence_visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700253 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700254
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800255 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Vladimir Marko595beb32017-02-06 14:11:54 +0000256
257 // If string compression is enabled, count_ holds the StringCompressionFlag in the
258 // least significant bit and the length in the remaining bits, length = count_ >> 1.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800259 int32_t count_;
260
261 uint32_t hash_code_;
262
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700263 // Compression of all-ASCII into 8-bit memory leads to usage one of these fields
264 union {
265 uint16_t value_[0];
266 uint8_t value_compressed_[0];
267 };
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800268
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700269 static GcRoot<Class> java_lang_String_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800270
271 friend struct art::StringOffsets; // for verifying offset information
Roland Levillain0d5a2812015-11-13 10:07:31 +0000272 ART_FRIEND_TEST(art::StubTest, ReadBarrierForRoot); // For java_lang_String_.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700273
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800274 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
275};
276
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800277} // namespace mirror
278} // namespace art
279
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700280#endif // ART_RUNTIME_MIRROR_STRING_H_