blob: b4a2d1d30a5e8ee639a5b3e4312830c8cb505cbe [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;
30
31namespace mirror {
32
33// C++ mirror of java.lang.String
Mingyao Yang98d1cc82014-05-15 17:02:16 -070034class MANAGED String FINAL : public Object {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035 public:
Mingyao Yang98d1cc82014-05-15 17:02:16 -070036 // Size of java.lang.String.class.
Mathieu Chartiere401d142015-04-22 13:56:20 -070037 static uint32_t ClassSize(size_t pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070038
39 // Size of an instance of java.lang.String not including its value array.
40 static constexpr uint32_t InstanceSize() {
41 return sizeof(String);
42 }
43
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044 static MemberOffset CountOffset() {
45 return OFFSET_OF_OBJECT_MEMBER(String, count_);
46 }
47
48 static MemberOffset ValueOffset() {
Jeff Hao848f70a2014-01-15 13:49:50 -080049 return OFFSET_OF_OBJECT_MEMBER(String, value_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050 }
51
Mathieu Chartier90443472015-07-16 20:32:27 -070052 uint16_t* GetValue() SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080053 return &value_[0];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054 }
55
Jeff Hao848f70a2014-01-15 13:49:50 -080056 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Mathieu Chartier90443472015-07-16 20:32:27 -070057 size_t SizeOf() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058
Jeff Hao848f70a2014-01-15 13:49:50 -080059 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Mathieu Chartier90443472015-07-16 20:32:27 -070060 int32_t GetLength() SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080061 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(String, count_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062 }
63
Mathieu Chartier90443472015-07-16 20:32:27 -070064 void SetCount(int32_t new_count) SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080065 // Count is invariant so use non-transactional mode. Also disable check as we may run inside
66 // a transaction.
67 DCHECK_LE(0, new_count);
68 SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count);
69 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070
Mathieu Chartier90443472015-07-16 20:32:27 -070071 int32_t GetHashCode() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070073 // Computes, stores, and returns the hash code.
Mathieu Chartier90443472015-07-16 20:32:27 -070074 int32_t ComputeHashCode() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080075
Mathieu Chartier90443472015-07-16 20:32:27 -070076 int32_t GetUtfLength() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077
Mathieu Chartier90443472015-07-16 20:32:27 -070078 uint16_t CharAt(int32_t index) SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao848f70a2014-01-15 13:49:50 -080079
Mathieu Chartier90443472015-07-16 20:32:27 -070080 void SetCharAt(int32_t index, uint16_t c) SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao848f70a2014-01-15 13:49:50 -080081
Mathieu Chartier90443472015-07-16 20:32:27 -070082 String* Intern() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080083
Jeff Hao848f70a2014-01-15 13:49:50 -080084 template <bool kIsInstrumented, typename PreFenceVisitor>
85 ALWAYS_INLINE static String* Alloc(Thread* self, int32_t utf16_length,
86 gc::AllocatorType allocator_type,
87 const PreFenceVisitor& pre_fence_visitor)
Mathieu Chartier90443472015-07-16 20:32:27 -070088 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao848f70a2014-01-15 13:49:50 -080089
90 template <bool kIsInstrumented>
91 ALWAYS_INLINE static String* AllocFromByteArray(Thread* self, int32_t byte_length,
92 Handle<ByteArray> array, int32_t offset,
93 int32_t high_byte,
94 gc::AllocatorType allocator_type)
Mathieu Chartier90443472015-07-16 20:32:27 -070095 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao848f70a2014-01-15 13:49:50 -080096
97 template <bool kIsInstrumented>
Igor Murashkinc449e8b2015-06-10 15:56:42 -070098 ALWAYS_INLINE static String* AllocFromCharArray(Thread* self, int32_t count,
Jeff Hao848f70a2014-01-15 13:49:50 -080099 Handle<CharArray> array, int32_t offset,
100 gc::AllocatorType allocator_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700101 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800102
103 template <bool kIsInstrumented>
104 ALWAYS_INLINE static String* AllocFromString(Thread* self, int32_t string_length,
105 Handle<String> string, int32_t offset,
106 gc::AllocatorType allocator_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700107 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800108
109 static String* AllocFromStrings(Thread* self, Handle<String> string, Handle<String> string2)
Mathieu Chartier90443472015-07-16 20:32:27 -0700110 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800111
112 static String* AllocFromUtf16(Thread* self, int32_t utf16_length, const uint16_t* utf16_data_in)
Mathieu Chartier90443472015-07-16 20:32:27 -0700113 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114
115 static String* AllocFromModifiedUtf8(Thread* self, const char* utf)
Mathieu Chartier90443472015-07-16 20:32:27 -0700116 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800117
Jeff Hao848f70a2014-01-15 13:49:50 -0800118 static String* AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, const char* utf8_data_in)
Mathieu Chartier90443472015-07-16 20:32:27 -0700119 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000121 // TODO: This is only used in the interpreter to compare against
122 // entries from a dex files constant pool (ArtField names). Should
123 // we unify this with Equals(const StringPiece&); ?
Mathieu Chartier90443472015-07-16 20:32:27 -0700124 bool Equals(const char* modified_utf8) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800125
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000126 // TODO: This is only used to compare DexCache.location with
127 // a dex_file's location (which is an std::string). Do we really
128 // need this in mirror::String just for that one usage ?
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129 bool Equals(const StringPiece& modified_utf8)
Mathieu Chartier90443472015-07-16 20:32:27 -0700130 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131
Mathieu Chartier90443472015-07-16 20:32:27 -0700132 bool Equals(String* that) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133
134 // Compare UTF-16 code point values not in a locale-sensitive manner
135 int Compare(int32_t utf16_length, const char* utf8_data_in);
136
137 // TODO: do we need this overload? give it a more intention-revealing name.
138 bool Equals(const uint16_t* that_chars, int32_t that_offset,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800139 int32_t that_length)
Mathieu Chartier90443472015-07-16 20:32:27 -0700140 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141
142 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
Mathieu Chartier90443472015-07-16 20:32:27 -0700143 std::string ToModifiedUtf8() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144
Mathieu Chartier90443472015-07-16 20:32:27 -0700145 int32_t FastIndexOf(int32_t ch, int32_t start) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146
Mathieu Chartier90443472015-07-16 20:32:27 -0700147 int32_t CompareTo(String* other) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800148
Mathieu Chartier90443472015-07-16 20:32:27 -0700149 CharArray* ToCharArray(Thread* self) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800150
Jeff Hao848f70a2014-01-15 13:49:50 -0800151 void GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index)
Mathieu Chartier90443472015-07-16 20:32:27 -0700152 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800153
Mathieu Chartier90443472015-07-16 20:32:27 -0700154 static Class* GetJavaLangString() SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700155 DCHECK(!java_lang_String_.IsNull());
156 return java_lang_String_.Read();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800157 }
158
159 static void SetClass(Class* java_lang_String);
160 static void ResetClass();
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700161 static void VisitRoots(RootVisitor* visitor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700162 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800163
164 private:
Mathieu Chartier90443472015-07-16 20:32:27 -0700165 void SetHashCode(int32_t new_hash_code) SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100166 // Hash code is invariant so use non-transactional mode. Also disable check as we may run inside
167 // a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700168 DCHECK_EQ(0, GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_)));
169 SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), new_hash_code);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170 }
171
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 int32_t count_;
174
175 uint32_t hash_code_;
176
Jeff Hao848f70a2014-01-15 13:49:50 -0800177 uint16_t value_[0];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700179 static GcRoot<Class> java_lang_String_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180
181 friend struct art::StringOffsets; // for verifying offset information
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700182 ART_FRIEND_TEST(ObjectTest, StringLength); // for SetOffset and SetCount
183
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800184 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
185};
186
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800187} // namespace mirror
188} // namespace art
189
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700190#endif // ART_RUNTIME_MIRROR_STRING_H_