blob: 8695fe84f48cbac57b23955443fd2acc2f43ebe7 [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
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070020#include "gc_root.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080021#include "gc/allocator_type.h"
Ian Rogerse63db272014-07-15 15:36:11 -070022#include "object.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080023#include "object_callbacks.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024
25namespace art {
26
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070027template<class T> class Handle;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028struct StringOffsets;
29class StringPiece;
Roland Levillain0d5a2812015-11-13 10:07:31 +000030class StubTest_ReadBarrierForRoot_Test;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031
32namespace mirror {
33
jessicahandojo3aaa37b2016-07-29 14:46:37 -070034// String Compression
35static constexpr bool kUseStringCompression = false;
36
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037// C++ mirror of java.lang.String
Mingyao Yang98d1cc82014-05-15 17:02:16 -070038class MANAGED String FINAL : public Object {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039 public:
Mingyao Yang98d1cc82014-05-15 17:02:16 -070040 // Size of java.lang.String.class.
Andreas Gampe542451c2016-07-26 09:02:02 -070041 static uint32_t ClassSize(PointerSize pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070042
43 // Size of an instance of java.lang.String not including its value array.
44 static constexpr uint32_t InstanceSize() {
45 return sizeof(String);
46 }
47
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 static MemberOffset CountOffset() {
49 return OFFSET_OF_OBJECT_MEMBER(String, count_);
50 }
51
52 static MemberOffset ValueOffset() {
Jeff Hao848f70a2014-01-15 13:49:50 -080053 return OFFSET_OF_OBJECT_MEMBER(String, value_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054 }
55
Mathieu Chartier90443472015-07-16 20:32:27 -070056 uint16_t* GetValue() SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080057 return &value_[0];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058 }
59
jessicahandojo3aaa37b2016-07-29 14:46:37 -070060 uint8_t* GetValueCompressed() SHARED_REQUIRES(Locks::mutator_lock_) {
61 return &value_compressed_[0];
62 }
63
Jeff Hao848f70a2014-01-15 13:49:50 -080064 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Mathieu Chartier90443472015-07-16 20:32:27 -070065 size_t SizeOf() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066
jessicahandojo3aaa37b2016-07-29 14:46:37 -070067 // Taking out the first/uppermost bit because it is not part of actual length value
Jeff Hao848f70a2014-01-15 13:49:50 -080068 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Mathieu Chartier90443472015-07-16 20:32:27 -070069 int32_t GetLength() SHARED_REQUIRES(Locks::mutator_lock_) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -070070 return GetLengthFromCount(GetCount<kVerifyFlags>());
71 }
72
73 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
74 int32_t GetCount() SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080075 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(String, count_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076 }
77
Mathieu Chartier90443472015-07-16 20:32:27 -070078 void SetCount(int32_t new_count) SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080079 // Count is invariant so use non-transactional mode. Also disable check as we may run inside
80 // a transaction.
jessicahandojo3aaa37b2016-07-29 14:46:37 -070081 DCHECK_LE(0, (new_count & INT32_MAX));
Jeff Hao848f70a2014-01-15 13:49:50 -080082 SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count);
83 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084
Mathieu Chartier90443472015-07-16 20:32:27 -070085 int32_t GetHashCode() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070087 // Computes, stores, and returns the hash code.
Mathieu Chartier90443472015-07-16 20:32:27 -070088 int32_t ComputeHashCode() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089
Mathieu Chartier90443472015-07-16 20:32:27 -070090 int32_t GetUtfLength() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091
Mathieu Chartier90443472015-07-16 20:32:27 -070092 uint16_t CharAt(int32_t index) SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao848f70a2014-01-15 13:49:50 -080093
Mathieu Chartier90443472015-07-16 20:32:27 -070094 void SetCharAt(int32_t index, uint16_t c) SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao848f70a2014-01-15 13:49:50 -080095
Mathieu Chartier90443472015-07-16 20:32:27 -070096 String* Intern() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097
Jeff Hao848f70a2014-01-15 13:49:50 -080098 template <bool kIsInstrumented>
99 ALWAYS_INLINE static String* AllocFromByteArray(Thread* self, int32_t byte_length,
100 Handle<ByteArray> array, int32_t offset,
101 int32_t high_byte,
102 gc::AllocatorType allocator_type)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700103 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800104
105 template <bool kIsInstrumented>
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700106 ALWAYS_INLINE static String* AllocFromCharArray(Thread* self, int32_t count,
Jeff Hao848f70a2014-01-15 13:49:50 -0800107 Handle<CharArray> array, int32_t offset,
108 gc::AllocatorType allocator_type)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700109 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800110
111 template <bool kIsInstrumented>
112 ALWAYS_INLINE static String* AllocFromString(Thread* self, int32_t string_length,
113 Handle<String> string, int32_t offset,
114 gc::AllocatorType allocator_type)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700115 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800116
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700117 template <bool kIsInstrumented>
118 ALWAYS_INLINE static String* AllocEmptyString(Thread* self,
119 gc::AllocatorType allocator_type)
120 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
121
Jeff Hao848f70a2014-01-15 13:49:50 -0800122 static String* AllocFromStrings(Thread* self, Handle<String> string, Handle<String> string2)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700123 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800124
125 static String* AllocFromUtf16(Thread* self, int32_t utf16_length, const uint16_t* utf16_data_in)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700126 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127
128 static String* AllocFromModifiedUtf8(Thread* self, const char* utf)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700129 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130
Bruce Hoult1646d7a2015-10-28 15:06:12 +0300131 static String* AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
132 const char* utf8_data_in, int32_t utf8_length)
133 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
134
Jeff Hao848f70a2014-01-15 13:49:50 -0800135 static String* AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, const char* utf8_data_in)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700136 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800137
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000138 // TODO: This is only used in the interpreter to compare against
139 // entries from a dex files constant pool (ArtField names). Should
140 // we unify this with Equals(const StringPiece&); ?
Mathieu Chartier90443472015-07-16 20:32:27 -0700141 bool Equals(const char* modified_utf8) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000143 // TODO: This is only used to compare DexCache.location with
144 // a dex_file's location (which is an std::string). Do we really
145 // need this in mirror::String just for that one usage ?
Ian Rogersef7d42f2014-01-06 12:55:46 -0800146 bool Equals(const StringPiece& modified_utf8)
Mathieu Chartier90443472015-07-16 20:32:27 -0700147 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800148
Mathieu Chartier90443472015-07-16 20:32:27 -0700149 bool Equals(String* that) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800150
151 // Compare UTF-16 code point values not in a locale-sensitive manner
152 int Compare(int32_t utf16_length, const char* utf8_data_in);
153
154 // TODO: do we need this overload? give it a more intention-revealing name.
155 bool Equals(const uint16_t* that_chars, int32_t that_offset,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800156 int32_t that_length)
Mathieu Chartier90443472015-07-16 20:32:27 -0700157 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158
159 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
Mathieu Chartier90443472015-07-16 20:32:27 -0700160 std::string ToModifiedUtf8() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161
Mathieu Chartier90443472015-07-16 20:32:27 -0700162 int32_t FastIndexOf(int32_t ch, int32_t start) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800163
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700164 template <typename MemoryType>
165 int32_t FastIndexOf(MemoryType* chars, int32_t ch, int32_t start)
166 SHARED_REQUIRES(Locks::mutator_lock_);
167
Mathieu Chartier90443472015-07-16 20:32:27 -0700168 int32_t CompareTo(String* other) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700170 CharArray* ToCharArray(Thread* self) SHARED_REQUIRES(Locks::mutator_lock_)
171 REQUIRES(!Roles::uninterruptible_);
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800172
Jeff Hao848f70a2014-01-15 13:49:50 -0800173 void GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index)
Mathieu Chartier90443472015-07-16 20:32:27 -0700174 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800175
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700176 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
177 bool IsCompressed() SHARED_REQUIRES(Locks::mutator_lock_) {
178 return kUseStringCompression && GetCompressionFlagFromCount(GetCount());
179 }
180
181 bool IsValueNull() SHARED_REQUIRES(Locks::mutator_lock_);
182
183 template<typename MemoryType>
184 static bool AllASCII(const MemoryType* const chars, const int length);
185
186 ALWAYS_INLINE static bool GetCompressionFlagFromCount(const int32_t count) {
187 return kUseStringCompression && ((count & (1u << 31)) != 0);
188 }
189
190 ALWAYS_INLINE static int32_t GetLengthFromCount(const int32_t count) {
191 return kUseStringCompression ? (count & INT32_MAX) : count;
192 }
193
194 ALWAYS_INLINE static int32_t GetFlaggedCount(const int32_t count) {
195 return kUseStringCompression ? (count | (1u << 31)) : count;
196 }
197
Mathieu Chartier90443472015-07-16 20:32:27 -0700198 static Class* GetJavaLangString() SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700199 DCHECK(!java_lang_String_.IsNull());
200 return java_lang_String_.Read();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201 }
202
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -0700203 static void SetClass(Class* java_lang_String) SHARED_REQUIRES(Locks::mutator_lock_);
204 static void ResetClass() SHARED_REQUIRES(Locks::mutator_lock_);
205 static void VisitRoots(RootVisitor* visitor) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800206
207 private:
Mathieu Chartier90443472015-07-16 20:32:27 -0700208 void SetHashCode(int32_t new_hash_code) SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100209 // Hash code is invariant so use non-transactional mode. Also disable check as we may run inside
210 // a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700211 DCHECK_EQ(0, GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_)));
212 SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), new_hash_code);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213 }
214
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700215 template <bool kIsInstrumented, typename PreFenceVisitor>
216 ALWAYS_INLINE static String* Alloc(Thread* self, int32_t utf16_length_with_flag,
217 gc::AllocatorType allocator_type,
218 const PreFenceVisitor& pre_fence_visitor)
219 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
220
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700222 // First bit (uppermost/leftmost) is taken out for Compressed/Uncompressed flag
223 // [0] Uncompressed: string uses 16-bit memory | [1] Compressed: 8-bit memory
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 int32_t count_;
225
226 uint32_t hash_code_;
227
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700228 // Compression of all-ASCII into 8-bit memory leads to usage one of these fields
229 union {
230 uint16_t value_[0];
231 uint8_t value_compressed_[0];
232 };
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800233
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700234 static GcRoot<Class> java_lang_String_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235
236 friend struct art::StringOffsets; // for verifying offset information
Roland Levillain0d5a2812015-11-13 10:07:31 +0000237 ART_FRIEND_TEST(art::StubTest, ReadBarrierForRoot); // For java_lang_String_.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700238
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800239 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
240};
241
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800242} // namespace mirror
243} // namespace art
244
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700245#endif // ART_RUNTIME_MIRROR_STRING_H_