Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_MIRROR_STRING_H_ |
| 18 | #define ART_RUNTIME_MIRROR_STRING_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame^] | 20 | #include <gtest/gtest.h> |
| 21 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 22 | #include "class.h" |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 23 | #include "object_callbacks.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame^] | 27 | template<class T> class SirtRef; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | struct StringClassOffsets; |
| 29 | struct StringOffsets; |
| 30 | class StringPiece; |
| 31 | |
| 32 | namespace mirror { |
| 33 | |
| 34 | // C++ mirror of java.lang.String |
| 35 | class MANAGED String : public Object { |
| 36 | public: |
| 37 | static MemberOffset CountOffset() { |
| 38 | return OFFSET_OF_OBJECT_MEMBER(String, count_); |
| 39 | } |
| 40 | |
| 41 | static MemberOffset ValueOffset() { |
| 42 | return OFFSET_OF_OBJECT_MEMBER(String, array_); |
| 43 | } |
| 44 | |
| 45 | static MemberOffset OffsetOffset() { |
| 46 | return OFFSET_OF_OBJECT_MEMBER(String, offset_); |
| 47 | } |
| 48 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 49 | CharArray* GetCharArray() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 50 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 51 | int32_t GetOffset() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame^] | 52 | int32_t result = GetField32(OffsetOffset()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 53 | DCHECK_LE(0, result); |
| 54 | return result; |
| 55 | } |
| 56 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 57 | int32_t GetLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 58 | |
| 59 | int32_t GetHashCode() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 60 | |
| 61 | void ComputeHashCode() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 62 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 63 | int32_t GetUtfLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 64 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 65 | uint16_t CharAt(int32_t index) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 66 | |
| 67 | String* Intern() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 68 | |
| 69 | static String* AllocFromUtf16(Thread* self, |
| 70 | int32_t utf16_length, |
| 71 | const uint16_t* utf16_data_in, |
| 72 | int32_t hash_code = 0) |
| 73 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 74 | |
| 75 | static String* AllocFromModifiedUtf8(Thread* self, const char* utf) |
| 76 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 77 | |
| 78 | static String* AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, |
| 79 | const char* utf8_data_in) |
| 80 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 81 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 82 | bool Equals(const char* modified_utf8) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 83 | |
| 84 | // TODO: do we need this overload? give it a more intention-revealing name. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 85 | bool Equals(const StringPiece& modified_utf8) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 86 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 87 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 88 | bool Equals(String* that) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 89 | |
| 90 | // Compare UTF-16 code point values not in a locale-sensitive manner |
| 91 | int Compare(int32_t utf16_length, const char* utf8_data_in); |
| 92 | |
| 93 | // TODO: do we need this overload? give it a more intention-revealing name. |
| 94 | bool Equals(const uint16_t* that_chars, int32_t that_offset, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 95 | int32_t that_length) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 96 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 97 | |
| 98 | // Create a modified UTF-8 encoded std::string from a java/lang/String object. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 99 | std::string ToModifiedUtf8() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 100 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 101 | int32_t FastIndexOf(int32_t ch, int32_t start) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 102 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 103 | int32_t CompareTo(String* other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 104 | |
| 105 | static Class* GetJavaLangString() { |
| 106 | DCHECK(java_lang_String_ != NULL); |
| 107 | return java_lang_String_; |
| 108 | } |
| 109 | |
| 110 | static void SetClass(Class* java_lang_String); |
| 111 | static void ResetClass(); |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 112 | static void VisitRoots(RootCallback* callback, void* arg) |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 113 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 114 | |
| 115 | private: |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame^] | 116 | void SetHashCode(int32_t new_hash_code) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 117 | // Hash code is invariant so use non-transactional mode. Also disable check as we may run inside |
| 118 | // a transaction. |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame^] | 119 | DCHECK_EQ(0, GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_))); |
| 120 | SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), new_hash_code); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame^] | 123 | void SetCount(int32_t new_count) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | ee1d79a | 2014-02-21 15:46:30 +0100 | [diff] [blame] | 124 | // Count is invariant so use non-transactional mode. Also disable check as we may run inside |
| 125 | // a transaction. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 126 | DCHECK_LE(0, new_count); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame^] | 127 | SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 130 | void SetOffset(int32_t new_offset) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | ee1d79a | 2014-02-21 15:46:30 +0100 | [diff] [blame] | 131 | // Offset is only used during testing so use non-transactional mode. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 132 | DCHECK_LE(0, new_offset); |
| 133 | DCHECK_GE(GetLength(), new_offset); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame^] | 134 | SetField32<false>(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 137 | static String* Alloc(Thread* self, int32_t utf16_length) |
| 138 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 139 | |
| 140 | static String* Alloc(Thread* self, const SirtRef<CharArray>& array) |
| 141 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 142 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 143 | void SetArray(CharArray* new_array) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 144 | |
| 145 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 146 | HeapReference<CharArray> array_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 147 | |
| 148 | int32_t count_; |
| 149 | |
| 150 | uint32_t hash_code_; |
| 151 | |
| 152 | int32_t offset_; |
| 153 | |
| 154 | static Class* java_lang_String_; |
| 155 | |
| 156 | friend struct art::StringOffsets; // for verifying offset information |
| 157 | FRIEND_TEST(ObjectTest, StringLength); // for SetOffset and SetCount |
| 158 | DISALLOW_IMPLICIT_CONSTRUCTORS(String); |
| 159 | }; |
| 160 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 161 | class MANAGED StringClass : public Class { |
| 162 | private: |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 163 | HeapReference<CharArray> ASCII_; |
| 164 | HeapReference<Object> CASE_INSENSITIVE_ORDER_; |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 165 | uint32_t REPLACEMENT_CHAR_; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 166 | int64_t serialVersionUID_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 167 | friend struct art::StringClassOffsets; // for verifying offset information |
| 168 | DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass); |
| 169 | }; |
| 170 | |
| 171 | } // namespace mirror |
| 172 | } // namespace art |
| 173 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 174 | #endif // ART_RUNTIME_MIRROR_STRING_H_ |