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