• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_INL_H_
18 #define ART_RUNTIME_MIRROR_STRING_INL_H_
19 
20 #include "array.h"
21 #include "base/bit_utils.h"
22 #include "class.h"
23 #include "gc/heap-inl.h"
24 #include "globals.h"
25 #include "intern_table.h"
26 #include "runtime.h"
27 #include "string.h"
28 #include "thread.h"
29 #include "utf.h"
30 #include "utils.h"
31 
32 namespace art {
33 namespace mirror {
34 
ClassSize(size_t pointer_size)35 inline uint32_t String::ClassSize(size_t pointer_size) {
36   uint32_t vtable_entries = Object::kVTableLength + 56;
37   return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 1, 2, pointer_size);
38 }
39 
40 // Sets string count in the allocation code path to ensure it is guarded by a CAS.
41 class SetStringCountVisitor {
42  public:
SetStringCountVisitor(int32_t count)43   explicit SetStringCountVisitor(int32_t count) : count_(count) {
44   }
45 
operator()46   void operator()(Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
47       SHARED_REQUIRES(Locks::mutator_lock_) {
48     // Avoid AsString as object is not yet in live bitmap or allocation stack.
49     String* string = down_cast<String*>(obj);
50     string->SetCount(count_);
51   }
52 
53  private:
54   const int32_t count_;
55 };
56 
57 // Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
58 class SetStringCountAndBytesVisitor {
59  public:
SetStringCountAndBytesVisitor(int32_t count,Handle<ByteArray> src_array,int32_t offset,int32_t high_byte)60   SetStringCountAndBytesVisitor(int32_t count, Handle<ByteArray> src_array, int32_t offset,
61                                 int32_t high_byte)
62       : count_(count), src_array_(src_array), offset_(offset), high_byte_(high_byte) {
63   }
64 
operator()65   void operator()(Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
66       SHARED_REQUIRES(Locks::mutator_lock_) {
67     // Avoid AsString as object is not yet in live bitmap or allocation stack.
68     String* string = down_cast<String*>(obj);
69     string->SetCount(count_);
70     uint16_t* value = string->GetValue();
71     const uint8_t* const src = reinterpret_cast<uint8_t*>(src_array_->GetData()) + offset_;
72     for (int i = 0; i < count_; i++) {
73       value[i] = high_byte_ + (src[i] & 0xFF);
74     }
75   }
76 
77  private:
78   const int32_t count_;
79   Handle<ByteArray> src_array_;
80   const int32_t offset_;
81   const int32_t high_byte_;
82 };
83 
84 // Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
85 class SetStringCountAndValueVisitorFromCharArray {
86  public:
SetStringCountAndValueVisitorFromCharArray(int32_t count,Handle<CharArray> src_array,int32_t offset)87   SetStringCountAndValueVisitorFromCharArray(int32_t count, Handle<CharArray> src_array,
88                                              int32_t offset) :
89     count_(count), src_array_(src_array), offset_(offset) {
90   }
91 
operator()92   void operator()(Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
93       SHARED_REQUIRES(Locks::mutator_lock_) {
94     // Avoid AsString as object is not yet in live bitmap or allocation stack.
95     String* string = down_cast<String*>(obj);
96     string->SetCount(count_);
97     const uint16_t* const src = src_array_->GetData() + offset_;
98     memcpy(string->GetValue(), src, count_ * sizeof(uint16_t));
99   }
100 
101  private:
102   const int32_t count_;
103   Handle<CharArray> src_array_;
104   const int32_t offset_;
105 };
106 
107 // Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
108 class SetStringCountAndValueVisitorFromString {
109  public:
SetStringCountAndValueVisitorFromString(int32_t count,Handle<String> src_string,int32_t offset)110   SetStringCountAndValueVisitorFromString(int32_t count, Handle<String> src_string,
111                                           int32_t offset) :
112     count_(count), src_string_(src_string), offset_(offset) {
113   }
114 
operator()115   void operator()(Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
116       SHARED_REQUIRES(Locks::mutator_lock_) {
117     // Avoid AsString as object is not yet in live bitmap or allocation stack.
118     String* string = down_cast<String*>(obj);
119     string->SetCount(count_);
120     const uint16_t* const src = src_string_->GetValue() + offset_;
121     memcpy(string->GetValue(), src, count_ * sizeof(uint16_t));
122   }
123 
124  private:
125   const int32_t count_;
126   Handle<String> src_string_;
127   const int32_t offset_;
128 };
129 
Intern()130 inline String* String::Intern() {
131   return Runtime::Current()->GetInternTable()->InternWeak(this);
132 }
133 
CharAt(int32_t index)134 inline uint16_t String::CharAt(int32_t index) {
135   int32_t count = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_));
136   if (UNLIKELY((index < 0) || (index >= count))) {
137     Thread* self = Thread::Current();
138     self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
139                              "length=%i; index=%i", count, index);
140     return 0;
141   }
142   return GetValue()[index];
143 }
144 
145 template<VerifyObjectFlags kVerifyFlags>
SizeOf()146 inline size_t String::SizeOf() {
147   size_t size = sizeof(String) + (sizeof(uint16_t) * GetLength<kVerifyFlags>());
148   // String.equals() intrinsics assume zero-padding up to kObjectAlignment,
149   // so make sure the zero-padding is actually copied around if GC compaction
150   // chooses to copy only SizeOf() bytes.
151   // http://b/23528461
152   return RoundUp(size, kObjectAlignment);
153 }
154 
155 template <bool kIsInstrumented, typename PreFenceVisitor>
Alloc(Thread * self,int32_t utf16_length,gc::AllocatorType allocator_type,const PreFenceVisitor & pre_fence_visitor)156 inline String* String::Alloc(Thread* self, int32_t utf16_length, gc::AllocatorType allocator_type,
157                              const PreFenceVisitor& pre_fence_visitor) {
158   constexpr size_t header_size = sizeof(String);
159   static_assert(sizeof(utf16_length) <= sizeof(size_t),
160                 "static_cast<size_t>(utf16_length) must not lose bits.");
161   size_t length = static_cast<size_t>(utf16_length);
162   size_t data_size = sizeof(uint16_t) * length;
163   size_t size = header_size + data_size;
164   // String.equals() intrinsics assume zero-padding up to kObjectAlignment,
165   // so make sure the allocator clears the padding as well.
166   // http://b/23528461
167   size_t alloc_size = RoundUp(size, kObjectAlignment);
168   Class* string_class = GetJavaLangString();
169 
170   // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
171   // Do this by comparing with the maximum length that will _not_ cause an overflow.
172   constexpr size_t overflow_length = (-header_size) / sizeof(uint16_t);  // Unsigned arithmetic.
173   constexpr size_t max_alloc_length = overflow_length - 1u;
174   static_assert(IsAligned<sizeof(uint16_t)>(kObjectAlignment),
175                 "kObjectAlignment must be at least as big as Java char alignment");
176   constexpr size_t max_length = RoundDown(max_alloc_length, kObjectAlignment / sizeof(uint16_t));
177   if (UNLIKELY(length > max_length)) {
178     self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow",
179                                              PrettyDescriptor(string_class).c_str(),
180                                              utf16_length).c_str());
181     return nullptr;
182   }
183 
184   gc::Heap* heap = Runtime::Current()->GetHeap();
185   return down_cast<String*>(
186       heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, string_class, alloc_size,
187                                                             allocator_type, pre_fence_visitor));
188 }
189 
190 template <bool kIsInstrumented>
AllocFromByteArray(Thread * self,int32_t byte_length,Handle<ByteArray> array,int32_t offset,int32_t high_byte,gc::AllocatorType allocator_type)191 inline String* String::AllocFromByteArray(Thread* self, int32_t byte_length,
192                                           Handle<ByteArray> array, int32_t offset,
193                                           int32_t high_byte, gc::AllocatorType allocator_type) {
194   SetStringCountAndBytesVisitor visitor(byte_length, array, offset, high_byte << 8);
195   String* string = Alloc<kIsInstrumented>(self, byte_length, allocator_type, visitor);
196   return string;
197 }
198 
199 template <bool kIsInstrumented>
AllocFromCharArray(Thread * self,int32_t count,Handle<CharArray> array,int32_t offset,gc::AllocatorType allocator_type)200 inline String* String::AllocFromCharArray(Thread* self, int32_t count,
201                                           Handle<CharArray> array, int32_t offset,
202                                           gc::AllocatorType allocator_type) {
203   // It is a caller error to have a count less than the actual array's size.
204   DCHECK_GE(array->GetLength(), count);
205   SetStringCountAndValueVisitorFromCharArray visitor(count, array, offset);
206   String* new_string = Alloc<kIsInstrumented>(self, count, allocator_type, visitor);
207   return new_string;
208 }
209 
210 template <bool kIsInstrumented>
AllocFromString(Thread * self,int32_t string_length,Handle<String> string,int32_t offset,gc::AllocatorType allocator_type)211 inline String* String::AllocFromString(Thread* self, int32_t string_length, Handle<String> string,
212                                        int32_t offset, gc::AllocatorType allocator_type) {
213   SetStringCountAndValueVisitorFromString visitor(string_length, string, offset);
214   String* new_string = Alloc<kIsInstrumented>(self, string_length, allocator_type, visitor);
215   return new_string;
216 }
217 
GetHashCode()218 inline int32_t String::GetHashCode() {
219   int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_));
220   if (UNLIKELY(result == 0)) {
221     result = ComputeHashCode();
222   }
223   DCHECK(result != 0 || ComputeUtf16Hash(GetValue(), GetLength()) == 0)
224       << ToModifiedUtf8() << " " << result;
225   return result;
226 }
227 
228 }  // namespace mirror
229 }  // namespace art
230 
231 #endif  // ART_RUNTIME_MIRROR_STRING_INL_H_
232