blob: 96f2098ab0535d306024cf6516b98fcbbfaf94b3 [file] [log] [blame]
Ian Rogersb0fa5dc2014-04-28 16:47:08 -07001/*
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"
Vladimir Marko4ef52262015-08-26 18:12:56 +010021#include "base/bit_utils.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070022#include "class.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010023#include "common_throws.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080024#include "gc/heap-inl.h"
Vladimir Marko4ef52262015-08-26 18:12:56 +010025#include "globals.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070026#include "intern_table.h"
27#include "runtime.h"
28#include "string.h"
29#include "thread.h"
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070030#include "utf.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010031#include "utils.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070032
33namespace art {
34namespace mirror {
35
Mathieu Chartiere401d142015-04-22 13:56:20 -070036inline uint32_t String::ClassSize(size_t pointer_size) {
Przemyslaw Szczepaniaka4fa2e72016-06-22 13:30:36 +010037 uint32_t vtable_entries = Object::kVTableLength + 57;
Narayan Kamath5d8fa8b2016-04-13 14:17:44 +010038 return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 1, 2, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070039}
40
Jeff Hao848f70a2014-01-15 13:49:50 -080041// Sets string count in the allocation code path to ensure it is guarded by a CAS.
42class SetStringCountVisitor {
43 public:
44 explicit SetStringCountVisitor(int32_t count) : count_(count) {
45 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +000046
Jeff Hao848f70a2014-01-15 13:49:50 -080047 void operator()(Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
Mathieu Chartier90443472015-07-16 20:32:27 -070048 SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080049 // Avoid AsString as object is not yet in live bitmap or allocation stack.
50 String* string = down_cast<String*>(obj);
51 string->SetCount(count_);
52 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070053
Jeff Hao848f70a2014-01-15 13:49:50 -080054 private:
55 const int32_t count_;
56};
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070057
Jeff Hao848f70a2014-01-15 13:49:50 -080058// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
59class SetStringCountAndBytesVisitor {
60 public:
Mathieu Chartier81aa0122015-04-28 10:01:28 -070061 SetStringCountAndBytesVisitor(int32_t count, Handle<ByteArray> src_array, int32_t offset,
62 int32_t high_byte)
63 : count_(count), src_array_(src_array), offset_(offset), high_byte_(high_byte) {
Jeff Hao848f70a2014-01-15 13:49:50 -080064 }
65
66 void operator()(Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
Mathieu Chartier90443472015-07-16 20:32:27 -070067 SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080068 // Avoid AsString as object is not yet in live bitmap or allocation stack.
69 String* string = down_cast<String*>(obj);
70 string->SetCount(count_);
71 uint16_t* value = string->GetValue();
Mathieu Chartier81aa0122015-04-28 10:01:28 -070072 const uint8_t* const src = reinterpret_cast<uint8_t*>(src_array_->GetData()) + offset_;
Jeff Hao848f70a2014-01-15 13:49:50 -080073 for (int i = 0; i < count_; i++) {
Mathieu Chartier81aa0122015-04-28 10:01:28 -070074 value[i] = high_byte_ + (src[i] & 0xFF);
Jeff Hao848f70a2014-01-15 13:49:50 -080075 }
76 }
77
78 private:
79 const int32_t count_;
Mathieu Chartier81aa0122015-04-28 10:01:28 -070080 Handle<ByteArray> src_array_;
81 const int32_t offset_;
Jeff Hao848f70a2014-01-15 13:49:50 -080082 const int32_t high_byte_;
83};
84
85// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
Mathieu Chartier81aa0122015-04-28 10:01:28 -070086class SetStringCountAndValueVisitorFromCharArray {
Jeff Hao848f70a2014-01-15 13:49:50 -080087 public:
Mathieu Chartier81aa0122015-04-28 10:01:28 -070088 SetStringCountAndValueVisitorFromCharArray(int32_t count, Handle<CharArray> src_array,
89 int32_t offset) :
90 count_(count), src_array_(src_array), offset_(offset) {
Jeff Hao848f70a2014-01-15 13:49:50 -080091 }
92
Mathieu Chartier81aa0122015-04-28 10:01:28 -070093 void operator()(Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
Mathieu Chartier90443472015-07-16 20:32:27 -070094 SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080095 // Avoid AsString as object is not yet in live bitmap or allocation stack.
96 String* string = down_cast<String*>(obj);
97 string->SetCount(count_);
Mathieu Chartier81aa0122015-04-28 10:01:28 -070098 const uint16_t* const src = src_array_->GetData() + offset_;
99 memcpy(string->GetValue(), src, count_ * sizeof(uint16_t));
Jeff Hao848f70a2014-01-15 13:49:50 -0800100 }
101
102 private:
103 const int32_t count_;
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700104 Handle<CharArray> src_array_;
105 const int32_t offset_;
106};
107
108// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
109class SetStringCountAndValueVisitorFromString {
110 public:
111 SetStringCountAndValueVisitorFromString(int32_t count, Handle<String> src_string,
112 int32_t offset) :
113 count_(count), src_string_(src_string), offset_(offset) {
114 }
115
116 void operator()(Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700117 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700118 // Avoid AsString as object is not yet in live bitmap or allocation stack.
119 String* string = down_cast<String*>(obj);
120 string->SetCount(count_);
121 const uint16_t* const src = src_string_->GetValue() + offset_;
122 memcpy(string->GetValue(), src, count_ * sizeof(uint16_t));
123 }
124
125 private:
126 const int32_t count_;
127 Handle<String> src_string_;
128 const int32_t offset_;
Jeff Hao848f70a2014-01-15 13:49:50 -0800129};
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700130
131inline String* String::Intern() {
132 return Runtime::Current()->GetInternTable()->InternWeak(this);
133}
134
Jeff Hao848f70a2014-01-15 13:49:50 -0800135inline uint16_t String::CharAt(int32_t index) {
136 int32_t count = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_));
137 if (UNLIKELY((index < 0) || (index >= count))) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100138 ThrowStringIndexOutOfBoundsException(index, count);
Jeff Hao848f70a2014-01-15 13:49:50 -0800139 return 0;
140 }
141 return GetValue()[index];
142}
143
144template<VerifyObjectFlags kVerifyFlags>
145inline size_t String::SizeOf() {
Vladimir Marko4ef52262015-08-26 18:12:56 +0100146 size_t size = sizeof(String) + (sizeof(uint16_t) * GetLength<kVerifyFlags>());
147 // String.equals() intrinsics assume zero-padding up to kObjectAlignment,
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100148 // so make sure the zero-padding is actually copied around if GC compaction
149 // chooses to copy only SizeOf() bytes.
Vladimir Marko4ef52262015-08-26 18:12:56 +0100150 // http://b/23528461
151 return RoundUp(size, kObjectAlignment);
Jeff Hao848f70a2014-01-15 13:49:50 -0800152}
153
154template <bool kIsInstrumented, typename PreFenceVisitor>
155inline String* String::Alloc(Thread* self, int32_t utf16_length, gc::AllocatorType allocator_type,
156 const PreFenceVisitor& pre_fence_visitor) {
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100157 constexpr size_t header_size = sizeof(String);
158 static_assert(sizeof(utf16_length) <= sizeof(size_t),
159 "static_cast<size_t>(utf16_length) must not lose bits.");
160 size_t length = static_cast<size_t>(utf16_length);
161 size_t data_size = sizeof(uint16_t) * length;
Jeff Hao848f70a2014-01-15 13:49:50 -0800162 size_t size = header_size + data_size;
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100163 // String.equals() intrinsics assume zero-padding up to kObjectAlignment,
164 // so make sure the allocator clears the padding as well.
165 // http://b/23528461
166 size_t alloc_size = RoundUp(size, kObjectAlignment);
Jeff Hao848f70a2014-01-15 13:49:50 -0800167 Class* string_class = GetJavaLangString();
168
169 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100170 // Do this by comparing with the maximum length that will _not_ cause an overflow.
171 constexpr size_t overflow_length = (-header_size) / sizeof(uint16_t); // Unsigned arithmetic.
172 constexpr size_t max_alloc_length = overflow_length - 1u;
173 static_assert(IsAligned<sizeof(uint16_t)>(kObjectAlignment),
174 "kObjectAlignment must be at least as big as Java char alignment");
175 constexpr size_t max_length = RoundDown(max_alloc_length, kObjectAlignment / sizeof(uint16_t));
176 if (UNLIKELY(length > max_length)) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800177 self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow",
178 PrettyDescriptor(string_class).c_str(),
179 utf16_length).c_str());
180 return nullptr;
181 }
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100182
Jeff Hao848f70a2014-01-15 13:49:50 -0800183 gc::Heap* heap = Runtime::Current()->GetHeap();
184 return down_cast<String*>(
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100185 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, string_class, alloc_size,
Jeff Haob7c8c1a2015-06-22 14:29:54 -0700186 allocator_type, pre_fence_visitor));
Jeff Hao848f70a2014-01-15 13:49:50 -0800187}
188
189template <bool kIsInstrumented>
190inline String* String::AllocFromByteArray(Thread* self, int32_t byte_length,
191 Handle<ByteArray> array, int32_t offset,
192 int32_t high_byte, gc::AllocatorType allocator_type) {
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700193 SetStringCountAndBytesVisitor visitor(byte_length, array, offset, high_byte << 8);
Jeff Hao848f70a2014-01-15 13:49:50 -0800194 String* string = Alloc<kIsInstrumented>(self, byte_length, allocator_type, visitor);
195 return string;
196}
197
198template <bool kIsInstrumented>
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700199inline String* String::AllocFromCharArray(Thread* self, int32_t count,
Jeff Hao848f70a2014-01-15 13:49:50 -0800200 Handle<CharArray> array, int32_t offset,
201 gc::AllocatorType allocator_type) {
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700202 // It is a caller error to have a count less than the actual array's size.
203 DCHECK_GE(array->GetLength(), count);
204 SetStringCountAndValueVisitorFromCharArray visitor(count, array, offset);
205 String* new_string = Alloc<kIsInstrumented>(self, count, allocator_type, visitor);
Jeff Hao848f70a2014-01-15 13:49:50 -0800206 return new_string;
207}
208
209template <bool kIsInstrumented>
210inline String* String::AllocFromString(Thread* self, int32_t string_length, Handle<String> string,
211 int32_t offset, gc::AllocatorType allocator_type) {
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700212 SetStringCountAndValueVisitorFromString visitor(string_length, string, offset);
Jeff Hao848f70a2014-01-15 13:49:50 -0800213 String* new_string = Alloc<kIsInstrumented>(self, string_length, allocator_type, visitor);
214 return new_string;
215}
216
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700217inline int32_t String::GetHashCode() {
218 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_));
219 if (UNLIKELY(result == 0)) {
220 result = ComputeHashCode();
221 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800222 DCHECK(result != 0 || ComputeUtf16Hash(GetValue(), GetLength()) == 0)
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700223 << ToModifiedUtf8() << " " << result;
224 return result;
225}
226
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700227} // namespace mirror
228} // namespace art
229
230#endif // ART_RUNTIME_MIRROR_STRING_INL_H_