blob: 409c6c2896a067836f307a7b2174d37f50ab45f8 [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;
Vladimir Markofdaf0f42016-10-13 19:29:53 +010036enum class StringCompressionFlag : uint32_t {
37 kCompressed = 0u,
38 kUncompressed = 1u
39};
jessicahandojo3aaa37b2016-07-29 14:46:37 -070040
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041// C++ mirror of java.lang.String
Mingyao Yang98d1cc82014-05-15 17:02:16 -070042class MANAGED String FINAL : public Object {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043 public:
Mingyao Yang98d1cc82014-05-15 17:02:16 -070044 // Size of java.lang.String.class.
Andreas Gampe542451c2016-07-26 09:02:02 -070045 static uint32_t ClassSize(PointerSize pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070046
47 // Size of an instance of java.lang.String not including its value array.
48 static constexpr uint32_t InstanceSize() {
49 return sizeof(String);
50 }
51
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052 static MemberOffset CountOffset() {
53 return OFFSET_OF_OBJECT_MEMBER(String, count_);
54 }
55
56 static MemberOffset ValueOffset() {
Jeff Hao848f70a2014-01-15 13:49:50 -080057 return OFFSET_OF_OBJECT_MEMBER(String, value_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058 }
59
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070060 uint16_t* GetValue() REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080061 return &value_[0];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062 }
63
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070064 uint8_t* GetValueCompressed() REQUIRES_SHARED(Locks::mutator_lock_) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -070065 return &value_compressed_[0];
66 }
67
Jeff Hao848f70a2014-01-15 13:49:50 -080068 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070069 size_t SizeOf() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070
jessicahandojo3aaa37b2016-07-29 14:46:37 -070071 // Taking out the first/uppermost bit because it is not part of actual length value
Jeff Hao848f70a2014-01-15 13:49:50 -080072 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070073 int32_t GetLength() REQUIRES_SHARED(Locks::mutator_lock_) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -070074 return GetLengthFromCount(GetCount<kVerifyFlags>());
75 }
76
77 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070078 int32_t GetCount() REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080079 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(String, count_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080 }
81
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070082 void SetCount(int32_t new_count) REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080083 // Count is invariant so use non-transactional mode. Also disable check as we may run inside
84 // a transaction.
Jeff Hao848f70a2014-01-15 13:49:50 -080085 SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count);
86 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070088 int32_t GetHashCode() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070090 // Computes, stores, and returns the hash code.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070091 int32_t ComputeHashCode() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070093 int32_t GetUtfLength() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070095 uint16_t CharAt(int32_t index) REQUIRES_SHARED(Locks::mutator_lock_);
Jeff Hao848f70a2014-01-15 13:49:50 -080096
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070097 void SetCharAt(int32_t index, uint16_t c) REQUIRES_SHARED(Locks::mutator_lock_);
Jeff Hao848f70a2014-01-15 13:49:50 -080098
Mathieu Chartier9e868092016-10-31 14:58:04 -070099 ObjPtr<String> Intern() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100
Jeff Hao848f70a2014-01-15 13:49:50 -0800101 template <bool kIsInstrumented>
102 ALWAYS_INLINE static String* AllocFromByteArray(Thread* self, int32_t byte_length,
103 Handle<ByteArray> array, int32_t offset,
104 int32_t high_byte,
105 gc::AllocatorType allocator_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700106 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800107
108 template <bool kIsInstrumented>
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700109 ALWAYS_INLINE static String* AllocFromCharArray(Thread* self, int32_t count,
Jeff Hao848f70a2014-01-15 13:49:50 -0800110 Handle<CharArray> array, int32_t offset,
111 gc::AllocatorType allocator_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700112 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800113
114 template <bool kIsInstrumented>
115 ALWAYS_INLINE static String* AllocFromString(Thread* self, int32_t string_length,
116 Handle<String> string, int32_t offset,
117 gc::AllocatorType allocator_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700118 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800119
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700120 template <bool kIsInstrumented>
121 ALWAYS_INLINE static String* AllocEmptyString(Thread* self,
122 gc::AllocatorType allocator_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700123 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700124
Jeff Hao848f70a2014-01-15 13:49:50 -0800125 static String* AllocFromStrings(Thread* self, Handle<String> string, Handle<String> string2)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700126 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800127
128 static String* AllocFromUtf16(Thread* self, int32_t utf16_length, const uint16_t* utf16_data_in)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700129 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130
131 static String* AllocFromModifiedUtf8(Thread* self, const char* utf)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700132 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133
Bruce Hoult1646d7a2015-10-28 15:06:12 +0300134 static String* AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
135 const char* utf8_data_in, int32_t utf8_length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700136 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Bruce Hoult1646d7a2015-10-28 15:06:12 +0300137
Jeff Hao848f70a2014-01-15 13:49:50 -0800138 static String* AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, const char* utf8_data_in)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700139 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000141 // TODO: This is only used in the interpreter to compare against
142 // entries from a dex files constant pool (ArtField names). Should
143 // we unify this with Equals(const StringPiece&); ?
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700144 bool Equals(const char* modified_utf8) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000146 // TODO: This is only used to compare DexCache.location with
147 // a dex_file's location (which is an std::string). Do we really
148 // need this in mirror::String just for that one usage ?
Ian Rogersef7d42f2014-01-06 12:55:46 -0800149 bool Equals(const StringPiece& modified_utf8)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700150 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151
Mathieu Chartier31e88222016-10-14 18:43:19 -0700152 bool Equals(ObjPtr<String> that) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153
154 // Compare UTF-16 code point values not in a locale-sensitive manner
155 int Compare(int32_t utf16_length, const char* utf8_data_in);
156
157 // TODO: do we need this overload? give it a more intention-revealing name.
158 bool Equals(const uint16_t* that_chars, int32_t that_offset,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800159 int32_t that_length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700160 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161
162 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700163 std::string ToModifiedUtf8() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700165 int32_t FastIndexOf(int32_t ch, int32_t start) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800166
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700167 template <typename MemoryType>
168 int32_t FastIndexOf(MemoryType* chars, int32_t ch, int32_t start)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700169 REQUIRES_SHARED(Locks::mutator_lock_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700170
Mathieu Chartier31e88222016-10-14 18:43:19 -0700171 int32_t CompareTo(ObjPtr<String> other) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700173 CharArray* ToCharArray(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700174 REQUIRES(!Roles::uninterruptible_);
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800175
Jeff Hao848f70a2014-01-15 13:49:50 -0800176 void GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700177 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800178
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700179 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700180 bool IsCompressed() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100181 return kUseStringCompression && IsCompressed(GetCount());
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700182 }
183
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700184 bool IsValueNull() REQUIRES_SHARED(Locks::mutator_lock_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700185
186 template<typename MemoryType>
187 static bool AllASCII(const MemoryType* const chars, const int length);
188
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100189 ALWAYS_INLINE static bool IsCompressed(int32_t count) {
190 return GetCompressionFlagFromCount(count) == StringCompressionFlag::kCompressed;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700191 }
192
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100193 ALWAYS_INLINE static StringCompressionFlag GetCompressionFlagFromCount(int32_t count) {
194 return kUseStringCompression
195 ? static_cast<StringCompressionFlag>(static_cast<uint32_t>(count) & 1u)
196 : StringCompressionFlag::kUncompressed;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700197 }
198
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100199 ALWAYS_INLINE static int32_t GetLengthFromCount(int32_t count) {
200 return kUseStringCompression ? static_cast<int32_t>(static_cast<uint32_t>(count) >> 1) : count;
201 }
202
203 ALWAYS_INLINE static int32_t GetFlaggedCount(int32_t length, bool compressible) {
204 return kUseStringCompression
205 ? static_cast<int32_t>((static_cast<uint32_t>(length) << 1) |
206 (static_cast<uint32_t>(compressible
207 ? StringCompressionFlag::kCompressed
208 : StringCompressionFlag::kUncompressed)))
209 : length;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700210 }
211
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700212 static Class* GetJavaLangString() REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700213 DCHECK(!java_lang_String_.IsNull());
214 return java_lang_String_.Read();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215 }
216
Mathieu Chartier31e88222016-10-14 18:43:19 -0700217 static void SetClass(ObjPtr<Class> java_lang_String) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700218 static void ResetClass() REQUIRES_SHARED(Locks::mutator_lock_);
219 static void VisitRoots(RootVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220
David Sehr709b0702016-10-13 09:12:37 -0700221 // Returns a human-readable equivalent of 'descriptor'. So "I" would be "int",
222 // "[[I" would be "int[][]", "[Ljava/lang/String;" would be
223 // "java.lang.String[]", and so forth.
224 static std::string PrettyStringDescriptor(ObjPtr<mirror::String> descriptor)
225 REQUIRES_SHARED(Locks::mutator_lock_);
226 std::string PrettyStringDescriptor()
227 REQUIRES_SHARED(Locks::mutator_lock_);
228
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800229 private:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700230 void SetHashCode(int32_t new_hash_code) REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100231 // Hash code is invariant so use non-transactional mode. Also disable check as we may run inside
232 // a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700233 DCHECK_EQ(0, GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_)));
234 SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), new_hash_code);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235 }
236
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700237 template <bool kIsInstrumented, typename PreFenceVisitor>
238 ALWAYS_INLINE static String* Alloc(Thread* self, int32_t utf16_length_with_flag,
239 gc::AllocatorType allocator_type,
240 const PreFenceVisitor& pre_fence_visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700241 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700242
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800243 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Vladimir Marko595beb32017-02-06 14:11:54 +0000244
245 // If string compression is enabled, count_ holds the StringCompressionFlag in the
246 // least significant bit and the length in the remaining bits, length = count_ >> 1.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 int32_t count_;
248
249 uint32_t hash_code_;
250
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700251 // Compression of all-ASCII into 8-bit memory leads to usage one of these fields
252 union {
253 uint16_t value_[0];
254 uint8_t value_compressed_[0];
255 };
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800256
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700257 static GcRoot<Class> java_lang_String_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800258
259 friend struct art::StringOffsets; // for verifying offset information
Roland Levillain0d5a2812015-11-13 10:07:31 +0000260 ART_FRIEND_TEST(art::StubTest, ReadBarrierForRoot); // For java_lang_String_.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700261
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800262 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
263};
264
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265} // namespace mirror
266} // namespace art
267
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700268#endif // ART_RUNTIME_MIRROR_STRING_H_