blob: 628554225cd3786da50a6f3761348ccb5782c1fb [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"
Jeff Hao848f70a2014-01-15 13:49:50 -080023#include "gc/heap-inl.h"
Vladimir Marko4ef52262015-08-26 18:12:56 +010024#include "globals.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070025#include "intern_table.h"
26#include "runtime.h"
27#include "string.h"
28#include "thread.h"
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070029#include "utf.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010030#include "utils.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070031
32namespace art {
33namespace mirror {
34
Mathieu Chartiere401d142015-04-22 13:56:20 -070035inline uint32_t String::ClassSize(size_t pointer_size) {
Yi Kong478078a2016-04-12 22:38:06 +010036 uint32_t vtable_entries = Object::kVTableLength + 56;
Narayan Kamath5d8fa8b2016-04-13 14:17:44 +010037 return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 1, 2, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070038}
39
Jeff Hao848f70a2014-01-15 13:49:50 -080040// Sets string count in the allocation code path to ensure it is guarded by a CAS.
41class SetStringCountVisitor {
42 public:
43 explicit SetStringCountVisitor(int32_t count) : count_(count) {
44 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +000045
Jeff Hao848f70a2014-01-15 13:49:50 -080046 void operator()(Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
Mathieu Chartier90443472015-07-16 20:32:27 -070047 SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080048 // 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 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070052
Jeff Hao848f70a2014-01-15 13:49:50 -080053 private:
54 const int32_t count_;
55};
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070056
Jeff Hao848f70a2014-01-15 13:49:50 -080057// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
58class SetStringCountAndBytesVisitor {
59 public:
Mathieu Chartier81aa0122015-04-28 10:01:28 -070060 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) {
Jeff Hao848f70a2014-01-15 13:49:50 -080063 }
64
65 void operator()(Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
Mathieu Chartier90443472015-07-16 20:32:27 -070066 SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080067 // 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();
Mathieu Chartier81aa0122015-04-28 10:01:28 -070071 const uint8_t* const src = reinterpret_cast<uint8_t*>(src_array_->GetData()) + offset_;
Jeff Hao848f70a2014-01-15 13:49:50 -080072 for (int i = 0; i < count_; i++) {
Mathieu Chartier81aa0122015-04-28 10:01:28 -070073 value[i] = high_byte_ + (src[i] & 0xFF);
Jeff Hao848f70a2014-01-15 13:49:50 -080074 }
75 }
76
77 private:
78 const int32_t count_;
Mathieu Chartier81aa0122015-04-28 10:01:28 -070079 Handle<ByteArray> src_array_;
80 const int32_t offset_;
Jeff Hao848f70a2014-01-15 13:49:50 -080081 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.
Mathieu Chartier81aa0122015-04-28 10:01:28 -070085class SetStringCountAndValueVisitorFromCharArray {
Jeff Hao848f70a2014-01-15 13:49:50 -080086 public:
Mathieu Chartier81aa0122015-04-28 10:01:28 -070087 SetStringCountAndValueVisitorFromCharArray(int32_t count, Handle<CharArray> src_array,
88 int32_t offset) :
89 count_(count), src_array_(src_array), offset_(offset) {
Jeff Hao848f70a2014-01-15 13:49:50 -080090 }
91
Mathieu Chartier81aa0122015-04-28 10:01:28 -070092 void operator()(Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
Mathieu Chartier90443472015-07-16 20:32:27 -070093 SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080094 // Avoid AsString as object is not yet in live bitmap or allocation stack.
95 String* string = down_cast<String*>(obj);
96 string->SetCount(count_);
Mathieu Chartier81aa0122015-04-28 10:01:28 -070097 const uint16_t* const src = src_array_->GetData() + offset_;
98 memcpy(string->GetValue(), src, count_ * sizeof(uint16_t));
Jeff Hao848f70a2014-01-15 13:49:50 -080099 }
100
101 private:
102 const int32_t count_;
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700103 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.
108class SetStringCountAndValueVisitorFromString {
109 public:
110 SetStringCountAndValueVisitorFromString(int32_t count, Handle<String> src_string,
111 int32_t offset) :
112 count_(count), src_string_(src_string), offset_(offset) {
113 }
114
115 void operator()(Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700116 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700117 // 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_;
Jeff Hao848f70a2014-01-15 13:49:50 -0800128};
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700129
130inline String* String::Intern() {
131 return Runtime::Current()->GetInternTable()->InternWeak(this);
132}
133
Jeff Hao848f70a2014-01-15 13:49:50 -0800134inline 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
145template<VerifyObjectFlags kVerifyFlags>
146inline size_t String::SizeOf() {
Vladimir Marko4ef52262015-08-26 18:12:56 +0100147 size_t size = sizeof(String) + (sizeof(uint16_t) * GetLength<kVerifyFlags>());
148 // String.equals() intrinsics assume zero-padding up to kObjectAlignment,
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100149 // so make sure the zero-padding is actually copied around if GC compaction
150 // chooses to copy only SizeOf() bytes.
Vladimir Marko4ef52262015-08-26 18:12:56 +0100151 // http://b/23528461
152 return RoundUp(size, kObjectAlignment);
Jeff Hao848f70a2014-01-15 13:49:50 -0800153}
154
155template <bool kIsInstrumented, typename PreFenceVisitor>
156inline String* String::Alloc(Thread* self, int32_t utf16_length, gc::AllocatorType allocator_type,
157 const PreFenceVisitor& pre_fence_visitor) {
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100158 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;
Jeff Hao848f70a2014-01-15 13:49:50 -0800163 size_t size = header_size + data_size;
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100164 // 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);
Jeff Hao848f70a2014-01-15 13:49:50 -0800168 Class* string_class = GetJavaLangString();
169
170 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100171 // 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)) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800178 self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow",
179 PrettyDescriptor(string_class).c_str(),
180 utf16_length).c_str());
181 return nullptr;
182 }
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100183
Jeff Hao848f70a2014-01-15 13:49:50 -0800184 gc::Heap* heap = Runtime::Current()->GetHeap();
185 return down_cast<String*>(
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100186 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, string_class, alloc_size,
Jeff Haob7c8c1a2015-06-22 14:29:54 -0700187 allocator_type, pre_fence_visitor));
Jeff Hao848f70a2014-01-15 13:49:50 -0800188}
189
190template <bool kIsInstrumented>
191inline 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) {
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700194 SetStringCountAndBytesVisitor visitor(byte_length, array, offset, high_byte << 8);
Jeff Hao848f70a2014-01-15 13:49:50 -0800195 String* string = Alloc<kIsInstrumented>(self, byte_length, allocator_type, visitor);
196 return string;
197}
198
199template <bool kIsInstrumented>
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700200inline String* String::AllocFromCharArray(Thread* self, int32_t count,
Jeff Hao848f70a2014-01-15 13:49:50 -0800201 Handle<CharArray> array, int32_t offset,
202 gc::AllocatorType allocator_type) {
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700203 // 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);
Jeff Hao848f70a2014-01-15 13:49:50 -0800207 return new_string;
208}
209
210template <bool kIsInstrumented>
211inline String* String::AllocFromString(Thread* self, int32_t string_length, Handle<String> string,
212 int32_t offset, gc::AllocatorType allocator_type) {
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700213 SetStringCountAndValueVisitorFromString visitor(string_length, string, offset);
Jeff Hao848f70a2014-01-15 13:49:50 -0800214 String* new_string = Alloc<kIsInstrumented>(self, string_length, allocator_type, visitor);
215 return new_string;
216}
217
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700218inline 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 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800223 DCHECK(result != 0 || ComputeUtf16Hash(GetValue(), GetLength()) == 0)
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700224 << ToModifiedUtf8() << " " << result;
225 return result;
226}
227
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700228} // namespace mirror
229} // namespace art
230
231#endif // ART_RUNTIME_MIRROR_STRING_INL_H_