blob: eda6c9b0d31a9bdcbbaab0a947d20a0160d7ed04 [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) {
Jeff Hao848f70a2014-01-15 13:49:50 -080036 uint32_t vtable_entries = Object::kVTableLength + 52;
Mathieu Chartiere401d142015-04-22 13:56:20 -070037 return Class::ComputeClassSize(true, vtable_entries, 0, 1, 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,
149 // so make sure the padding is actually zero-initialized if the allocator
150 // chooses to clear, or GC compaction chooses to copy, only SizeOf() bytes.
151 // 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) {
158 size_t header_size = sizeof(String);
159 size_t data_size = sizeof(uint16_t) * utf16_length;
160 size_t size = header_size + data_size;
161 Class* string_class = GetJavaLangString();
162
163 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
164 if (UNLIKELY(size < data_size)) {
165 self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow",
166 PrettyDescriptor(string_class).c_str(),
167 utf16_length).c_str());
168 return nullptr;
169 }
170 gc::Heap* heap = Runtime::Current()->GetHeap();
171 return down_cast<String*>(
Jeff Haob7c8c1a2015-06-22 14:29:54 -0700172 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, string_class, size,
173 allocator_type, pre_fence_visitor));
Jeff Hao848f70a2014-01-15 13:49:50 -0800174}
175
176template <bool kIsInstrumented>
177inline String* String::AllocFromByteArray(Thread* self, int32_t byte_length,
178 Handle<ByteArray> array, int32_t offset,
179 int32_t high_byte, gc::AllocatorType allocator_type) {
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700180 SetStringCountAndBytesVisitor visitor(byte_length, array, offset, high_byte << 8);
Jeff Hao848f70a2014-01-15 13:49:50 -0800181 String* string = Alloc<kIsInstrumented>(self, byte_length, allocator_type, visitor);
182 return string;
183}
184
185template <bool kIsInstrumented>
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700186inline String* String::AllocFromCharArray(Thread* self, int32_t count,
Jeff Hao848f70a2014-01-15 13:49:50 -0800187 Handle<CharArray> array, int32_t offset,
188 gc::AllocatorType allocator_type) {
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700189 // It is a caller error to have a count less than the actual array's size.
190 DCHECK_GE(array->GetLength(), count);
191 SetStringCountAndValueVisitorFromCharArray visitor(count, array, offset);
192 String* new_string = Alloc<kIsInstrumented>(self, count, allocator_type, visitor);
Jeff Hao848f70a2014-01-15 13:49:50 -0800193 return new_string;
194}
195
196template <bool kIsInstrumented>
197inline String* String::AllocFromString(Thread* self, int32_t string_length, Handle<String> string,
198 int32_t offset, gc::AllocatorType allocator_type) {
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700199 SetStringCountAndValueVisitorFromString visitor(string_length, string, offset);
Jeff Hao848f70a2014-01-15 13:49:50 -0800200 String* new_string = Alloc<kIsInstrumented>(self, string_length, allocator_type, visitor);
201 return new_string;
202}
203
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700204inline int32_t String::GetHashCode() {
205 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_));
206 if (UNLIKELY(result == 0)) {
207 result = ComputeHashCode();
208 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800209 DCHECK(result != 0 || ComputeUtf16Hash(GetValue(), GetLength()) == 0)
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700210 << ToModifiedUtf8() << " " << result;
211 return result;
212}
213
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700214} // namespace mirror
215} // namespace art
216
217#endif // ART_RUNTIME_MIRROR_STRING_INL_H_