blob: d492ba3162a77118819976968a728a926751d478 [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
34// C++ mirror of java.lang.String
Mingyao Yang98d1cc82014-05-15 17:02:16 -070035class MANAGED String FINAL : public Object {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036 public:
Mingyao Yang98d1cc82014-05-15 17:02:16 -070037 // Size of java.lang.String.class.
Andreas Gampe542451c2016-07-26 09:02:02 -070038 static uint32_t ClassSize(PointerSize pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070039
40 // Size of an instance of java.lang.String not including its value array.
41 static constexpr uint32_t InstanceSize() {
42 return sizeof(String);
43 }
44
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045 static MemberOffset CountOffset() {
46 return OFFSET_OF_OBJECT_MEMBER(String, count_);
47 }
48
49 static MemberOffset ValueOffset() {
Jeff Hao848f70a2014-01-15 13:49:50 -080050 return OFFSET_OF_OBJECT_MEMBER(String, value_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051 }
52
Mathieu Chartier90443472015-07-16 20:32:27 -070053 uint16_t* GetValue() SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080054 return &value_[0];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055 }
56
Jeff Hao848f70a2014-01-15 13:49:50 -080057 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Mathieu Chartier90443472015-07-16 20:32:27 -070058 size_t SizeOf() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059
Jeff Hao848f70a2014-01-15 13:49:50 -080060 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Mathieu Chartier90443472015-07-16 20:32:27 -070061 int32_t GetLength() SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080062 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(String, count_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 }
64
Mathieu Chartier90443472015-07-16 20:32:27 -070065 void SetCount(int32_t new_count) SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080066 // Count is invariant so use non-transactional mode. Also disable check as we may run inside
67 // a transaction.
68 DCHECK_LE(0, new_count);
69 SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count);
70 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071
Mathieu Chartier90443472015-07-16 20:32:27 -070072 int32_t GetHashCode() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070074 // Computes, stores, and returns the hash code.
Mathieu Chartier90443472015-07-16 20:32:27 -070075 int32_t ComputeHashCode() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076
Mathieu Chartier90443472015-07-16 20:32:27 -070077 int32_t GetUtfLength() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078
Mathieu Chartier90443472015-07-16 20:32:27 -070079 uint16_t CharAt(int32_t index) SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao848f70a2014-01-15 13:49:50 -080080
Mathieu Chartier90443472015-07-16 20:32:27 -070081 void SetCharAt(int32_t index, uint16_t c) SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao848f70a2014-01-15 13:49:50 -080082
Mathieu Chartier90443472015-07-16 20:32:27 -070083 String* Intern() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084
Jeff Hao848f70a2014-01-15 13:49:50 -080085 template <bool kIsInstrumented, typename PreFenceVisitor>
86 ALWAYS_INLINE static String* Alloc(Thread* self, int32_t utf16_length,
87 gc::AllocatorType allocator_type,
88 const PreFenceVisitor& pre_fence_visitor)
Mathieu Chartiered8990a2015-07-23 14:11:16 -070089 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -080090
91 template <bool kIsInstrumented>
92 ALWAYS_INLINE static String* AllocFromByteArray(Thread* self, int32_t byte_length,
93 Handle<ByteArray> array, int32_t offset,
94 int32_t high_byte,
95 gc::AllocatorType allocator_type)
Mathieu Chartiered8990a2015-07-23 14:11:16 -070096 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -080097
98 template <bool kIsInstrumented>
Igor Murashkinc449e8b2015-06-10 15:56:42 -070099 ALWAYS_INLINE static String* AllocFromCharArray(Thread* self, int32_t count,
Jeff Hao848f70a2014-01-15 13:49:50 -0800100 Handle<CharArray> array, int32_t offset,
101 gc::AllocatorType allocator_type)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700102 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800103
104 template <bool kIsInstrumented>
105 ALWAYS_INLINE static String* AllocFromString(Thread* self, int32_t string_length,
106 Handle<String> string, int32_t offset,
107 gc::AllocatorType allocator_type)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700108 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800109
110 static String* AllocFromStrings(Thread* self, Handle<String> string, Handle<String> string2)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700111 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800112
113 static String* AllocFromUtf16(Thread* self, int32_t utf16_length, const uint16_t* utf16_data_in)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700114 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115
116 static String* AllocFromModifiedUtf8(Thread* self, const char* utf)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700117 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800118
Bruce Hoult1646d7a2015-10-28 15:06:12 +0300119 static String* AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
120 const char* utf8_data_in, int32_t utf8_length)
121 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
122
Jeff Hao848f70a2014-01-15 13:49:50 -0800123 static String* AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, const char* utf8_data_in)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700124 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800125
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000126 // TODO: This is only used in the interpreter to compare against
127 // entries from a dex files constant pool (ArtField names). Should
128 // we unify this with Equals(const StringPiece&); ?
Mathieu Chartier90443472015-07-16 20:32:27 -0700129 bool Equals(const char* modified_utf8) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000131 // TODO: This is only used to compare DexCache.location with
132 // a dex_file's location (which is an std::string). Do we really
133 // need this in mirror::String just for that one usage ?
Ian Rogersef7d42f2014-01-06 12:55:46 -0800134 bool Equals(const StringPiece& modified_utf8)
Mathieu Chartier90443472015-07-16 20:32:27 -0700135 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136
Mathieu Chartier90443472015-07-16 20:32:27 -0700137 bool Equals(String* that) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138
139 // Compare UTF-16 code point values not in a locale-sensitive manner
140 int Compare(int32_t utf16_length, const char* utf8_data_in);
141
142 // TODO: do we need this overload? give it a more intention-revealing name.
143 bool Equals(const uint16_t* that_chars, int32_t that_offset,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800144 int32_t that_length)
Mathieu Chartier90443472015-07-16 20:32:27 -0700145 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146
147 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
Mathieu Chartier90443472015-07-16 20:32:27 -0700148 std::string ToModifiedUtf8() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149
Mathieu Chartier90443472015-07-16 20:32:27 -0700150 int32_t FastIndexOf(int32_t ch, int32_t start) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151
Mathieu Chartier90443472015-07-16 20:32:27 -0700152 int32_t CompareTo(String* other) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700154 CharArray* ToCharArray(Thread* self) SHARED_REQUIRES(Locks::mutator_lock_)
155 REQUIRES(!Roles::uninterruptible_);
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800156
Jeff Hao848f70a2014-01-15 13:49:50 -0800157 void GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index)
Mathieu Chartier90443472015-07-16 20:32:27 -0700158 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800159
Mathieu Chartier90443472015-07-16 20:32:27 -0700160 static Class* GetJavaLangString() SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700161 DCHECK(!java_lang_String_.IsNull());
162 return java_lang_String_.Read();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800163 }
164
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -0700165 static void SetClass(Class* java_lang_String) SHARED_REQUIRES(Locks::mutator_lock_);
166 static void ResetClass() SHARED_REQUIRES(Locks::mutator_lock_);
167 static void VisitRoots(RootVisitor* visitor) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168
169 private:
Mathieu Chartier90443472015-07-16 20:32:27 -0700170 void SetHashCode(int32_t new_hash_code) SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100171 // Hash code is invariant so use non-transactional mode. Also disable check as we may run inside
172 // a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700173 DCHECK_EQ(0, GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_)));
174 SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), new_hash_code);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175 }
176
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800177 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178 int32_t count_;
179
180 uint32_t hash_code_;
181
Jeff Hao848f70a2014-01-15 13:49:50 -0800182 uint16_t value_[0];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800183
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700184 static GcRoot<Class> java_lang_String_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800185
186 friend struct art::StringOffsets; // for verifying offset information
Roland Levillain0d5a2812015-11-13 10:07:31 +0000187 ART_FRIEND_TEST(art::StubTest, ReadBarrierForRoot); // For java_lang_String_.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700188
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800189 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
190};
191
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800192} // namespace mirror
193} // namespace art
194
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700195#endif // ART_RUNTIME_MIRROR_STRING_H_