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