ART: Add string-alloc-inl.h

In an effort to reduce the (transitive) proliferation of heap-inl
add a specific inline header for string allocation.

Bug: 118385392
Test: mmma art
Test: m test-art-host
Change-Id: I68d3bcf2b3615d12231010940d0a24a1dbc03826
diff --git a/runtime/debugger.cc b/runtime/debugger.cc
index 328a9bb..3ad7fc9 100644
--- a/runtime/debugger.cc
+++ b/runtime/debugger.cc
@@ -60,6 +60,7 @@
 #include "mirror/class_loader.h"
 #include "mirror/object-inl.h"
 #include "mirror/object_array-inl.h"
+#include "mirror/string-alloc-inl.h"
 #include "mirror/string-inl.h"
 #include "mirror/throwable.h"
 #include "nativehelper/scoped_local_ref.h"
diff --git a/runtime/entrypoints/quick/quick_alloc_entrypoints.cc b/runtime/entrypoints/quick/quick_alloc_entrypoints.cc
index 257cd41..abefa4a 100644
--- a/runtime/entrypoints/quick/quick_alloc_entrypoints.cc
+++ b/runtime/entrypoints/quick/quick_alloc_entrypoints.cc
@@ -25,7 +25,7 @@
 #include "mirror/class-inl.h"
 #include "mirror/object-inl.h"
 #include "mirror/object_array-inl.h"
-#include "mirror/string-inl.h"
+#include "mirror/string-alloc-inl.h"
 
 namespace art {
 
diff --git a/runtime/interpreter/interpreter_switch_impl-inl.h b/runtime/interpreter/interpreter_switch_impl-inl.h
index c430de2..b2ea862 100644
--- a/runtime/interpreter/interpreter_switch_impl-inl.h
+++ b/runtime/interpreter/interpreter_switch_impl-inl.h
@@ -27,6 +27,7 @@
 #include "interpreter_common.h"
 #include "jit/jit.h"
 #include "jvalue-inl.h"
+#include "mirror/string-alloc-inl.h"
 #include "nth_caller_visitor.h"
 #include "safe_math.h"
 #include "shadow_frame-inl.h"
diff --git a/runtime/interpreter/mterp/mterp.cc b/runtime/interpreter/mterp/mterp.cc
index ba109bc..43f2cc5 100644
--- a/runtime/interpreter/mterp/mterp.cc
+++ b/runtime/interpreter/mterp/mterp.cc
@@ -25,6 +25,7 @@
 #include "interpreter/interpreter_common.h"
 #include "interpreter/interpreter_intrinsics.h"
 #include "interpreter/shadow_frame-inl.h"
+#include "mirror/string-alloc-inl.h"
 
 namespace art {
 namespace interpreter {
diff --git a/runtime/interpreter/unstarted_runtime.cc b/runtime/interpreter/unstarted_runtime.cc
index 5def395..9bc2179 100644
--- a/runtime/interpreter/unstarted_runtime.cc
+++ b/runtime/interpreter/unstarted_runtime.cc
@@ -52,6 +52,7 @@
 #include "mirror/object-inl.h"
 #include "mirror/object_array-alloc-inl.h"
 #include "mirror/object_array-inl.h"
+#include "mirror/string-alloc-inl.h"
 #include "mirror/string-inl.h"
 #include "nativehelper/scoped_local_ref.h"
 #include "nth_caller_visitor.h"
diff --git a/runtime/jni/jni_internal.cc b/runtime/jni/jni_internal.cc
index b07d2c2..5e01b79 100644
--- a/runtime/jni/jni_internal.cc
+++ b/runtime/jni/jni_internal.cc
@@ -53,6 +53,7 @@
 #include "mirror/object-inl.h"
 #include "mirror/object_array-alloc-inl.h"
 #include "mirror/object_array-inl.h"
+#include "mirror/string-alloc-inl.h"
 #include "mirror/string-inl.h"
 #include "mirror/throwable.h"
 #include "nativehelper/scoped_local_ref.h"
diff --git a/runtime/mirror/string-alloc-inl.h b/runtime/mirror/string-alloc-inl.h
new file mode 100644
index 0000000..c026c67
--- /dev/null
+++ b/runtime/mirror/string-alloc-inl.h
@@ -0,0 +1,259 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef ART_RUNTIME_MIRROR_STRING_ALLOC_INL_H_
+#define ART_RUNTIME_MIRROR_STRING_ALLOC_INL_H_
+
+#include "string-inl.h"
+
+#include "android-base/stringprintf.h"
+
+#include "array.h"
+#include "base/bit_utils.h"
+#include "base/globals.h"
+#include "base/utils.h"
+#include "class.h"
+#include "class_root.h"
+#include "gc/heap-inl.h"
+#include "runtime.h"
+#include "thread.h"
+
+namespace art {
+namespace mirror {
+
+// Sets string count in the allocation code path to ensure it is guarded by a CAS.
+class SetStringCountVisitor {
+ public:
+  explicit SetStringCountVisitor(int32_t count) : count_(count) {
+  }
+
+  void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
+      REQUIRES_SHARED(Locks::mutator_lock_) {
+    // Avoid AsString as object is not yet in live bitmap or allocation stack.
+    ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
+    string->SetCount(count_);
+    DCHECK(!string->IsCompressed() || kUseStringCompression);
+  }
+
+ private:
+  const int32_t count_;
+};
+
+// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
+class SetStringCountAndBytesVisitor {
+ public:
+  SetStringCountAndBytesVisitor(int32_t count, Handle<ByteArray> src_array, int32_t offset,
+                                int32_t high_byte)
+      : count_(count), src_array_(src_array), offset_(offset), high_byte_(high_byte) {
+  }
+
+  void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
+      REQUIRES_SHARED(Locks::mutator_lock_) {
+    // Avoid AsString as object is not yet in live bitmap or allocation stack.
+    ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
+    string->SetCount(count_);
+    DCHECK(!string->IsCompressed() || kUseStringCompression);
+    int32_t length = String::GetLengthFromCount(count_);
+    const uint8_t* const src = reinterpret_cast<uint8_t*>(src_array_->GetData()) + offset_;
+    if (string->IsCompressed()) {
+      uint8_t* valueCompressed = string->GetValueCompressed();
+      for (int i = 0; i < length; i++) {
+        valueCompressed[i] = (src[i] & 0xFF);
+      }
+    } else {
+      uint16_t* value = string->GetValue();
+      for (int i = 0; i < length; i++) {
+        value[i] = high_byte_ + (src[i] & 0xFF);
+      }
+    }
+  }
+
+ private:
+  const int32_t count_;
+  Handle<ByteArray> src_array_;
+  const int32_t offset_;
+  const int32_t high_byte_;
+};
+
+// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
+class SetStringCountAndValueVisitorFromCharArray {
+ public:
+  SetStringCountAndValueVisitorFromCharArray(int32_t count, Handle<CharArray> src_array,
+                                             int32_t offset) :
+    count_(count), src_array_(src_array), offset_(offset) {
+  }
+
+  void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
+      REQUIRES_SHARED(Locks::mutator_lock_) {
+    // Avoid AsString as object is not yet in live bitmap or allocation stack.
+    ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
+    string->SetCount(count_);
+    const uint16_t* const src = src_array_->GetData() + offset_;
+    const int32_t length = String::GetLengthFromCount(count_);
+    if (kUseStringCompression && String::IsCompressed(count_)) {
+      for (int i = 0; i < length; ++i) {
+        string->GetValueCompressed()[i] = static_cast<uint8_t>(src[i]);
+      }
+    } else {
+      memcpy(string->GetValue(), src, length * sizeof(uint16_t));
+    }
+  }
+
+ private:
+  const int32_t count_;
+  Handle<CharArray> src_array_;
+  const int32_t offset_;
+};
+
+// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
+class SetStringCountAndValueVisitorFromString {
+ public:
+  SetStringCountAndValueVisitorFromString(int32_t count,
+                                          Handle<String> src_string,
+                                          int32_t offset) :
+    count_(count), src_string_(src_string), offset_(offset) {
+  }
+
+  void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
+      REQUIRES_SHARED(Locks::mutator_lock_) {
+    // Avoid AsString as object is not yet in live bitmap or allocation stack.
+    ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
+    string->SetCount(count_);
+    const int32_t length = String::GetLengthFromCount(count_);
+    bool compressible = kUseStringCompression && String::IsCompressed(count_);
+    if (src_string_->IsCompressed()) {
+      const uint8_t* const src = src_string_->GetValueCompressed() + offset_;
+      memcpy(string->GetValueCompressed(), src, length * sizeof(uint8_t));
+    } else {
+      const uint16_t* const src = src_string_->GetValue() + offset_;
+      if (compressible) {
+        for (int i = 0; i < length; ++i) {
+          string->GetValueCompressed()[i] = static_cast<uint8_t>(src[i]);
+        }
+      } else {
+        memcpy(string->GetValue(), src, length * sizeof(uint16_t));
+      }
+    }
+  }
+
+ private:
+  const int32_t count_;
+  Handle<String> src_string_;
+  const int32_t offset_;
+};
+
+template <bool kIsInstrumented, typename PreFenceVisitor>
+inline String* String::Alloc(Thread* self,
+                             int32_t utf16_length_with_flag,
+                             gc::AllocatorType allocator_type,
+                             const PreFenceVisitor& pre_fence_visitor) {
+  constexpr size_t header_size = sizeof(String);
+  const bool compressible = kUseStringCompression && String::IsCompressed(utf16_length_with_flag);
+  const size_t block_size = (compressible) ? sizeof(uint8_t) : sizeof(uint16_t);
+  size_t length = String::GetLengthFromCount(utf16_length_with_flag);
+  static_assert(sizeof(length) <= sizeof(size_t),
+                "static_cast<size_t>(utf16_length) must not lose bits.");
+  size_t data_size = block_size * length;
+  size_t size = header_size + data_size;
+  // String.equals() intrinsics assume zero-padding up to kObjectAlignment,
+  // so make sure the allocator clears the padding as well.
+  // http://b/23528461
+  size_t alloc_size = RoundUp(size, kObjectAlignment);
+
+  Runtime* runtime = Runtime::Current();
+  ObjPtr<Class> string_class = GetClassRoot<String>(runtime->GetClassLinker());
+  // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
+  // Do this by comparing with the maximum length that will _not_ cause an overflow.
+  const size_t overflow_length = (-header_size) / block_size;   // Unsigned arithmetic.
+  const size_t max_alloc_length = overflow_length - 1u;
+  static_assert(IsAligned<sizeof(uint16_t)>(kObjectAlignment),
+                "kObjectAlignment must be at least as big as Java char alignment");
+  const size_t max_length = RoundDown(max_alloc_length, kObjectAlignment / block_size);
+  if (UNLIKELY(length > max_length)) {
+    self->ThrowOutOfMemoryError(
+        android::base::StringPrintf("%s of length %d would overflow",
+                                    Class::PrettyDescriptor(string_class).c_str(),
+                                    static_cast<int>(length)).c_str());
+    return nullptr;
+  }
+
+  gc::Heap* heap = runtime->GetHeap();
+  return down_cast<String*>(
+      heap->AllocObjectWithAllocator<kIsInstrumented, true>(self,
+                                                            string_class,
+                                                            alloc_size,
+                                                            allocator_type,
+                                                            pre_fence_visitor));
+}
+
+template <bool kIsInstrumented>
+inline String* String::AllocEmptyString(Thread* self, gc::AllocatorType allocator_type) {
+  const int32_t length_with_flag = String::GetFlaggedCount(0, /* compressible= */ true);
+  SetStringCountVisitor visitor(length_with_flag);
+  return Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
+}
+
+template <bool kIsInstrumented>
+inline String* String::AllocFromByteArray(Thread* self,
+                                          int32_t byte_length,
+                                          Handle<ByteArray> array,
+                                          int32_t offset,
+                                          int32_t high_byte,
+                                          gc::AllocatorType allocator_type) {
+  const uint8_t* const src = reinterpret_cast<uint8_t*>(array->GetData()) + offset;
+  high_byte &= 0xff;  // Extract the relevant bits before determining `compressible`.
+  const bool compressible =
+      kUseStringCompression && String::AllASCII<uint8_t>(src, byte_length) && (high_byte == 0);
+  const int32_t length_with_flag = String::GetFlaggedCount(byte_length, compressible);
+  SetStringCountAndBytesVisitor visitor(length_with_flag, array, offset, high_byte << 8);
+  String* string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
+  return string;
+}
+
+template <bool kIsInstrumented>
+inline String* String::AllocFromCharArray(Thread* self,
+                                          int32_t count,
+                                          Handle<CharArray> array,
+                                          int32_t offset,
+                                          gc::AllocatorType allocator_type) {
+  // It is a caller error to have a count less than the actual array's size.
+  DCHECK_GE(array->GetLength(), count);
+  const bool compressible = kUseStringCompression &&
+                            String::AllASCII<uint16_t>(array->GetData() + offset, count);
+  const int32_t length_with_flag = String::GetFlaggedCount(count, compressible);
+  SetStringCountAndValueVisitorFromCharArray visitor(length_with_flag, array, offset);
+  String* new_string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
+  return new_string;
+}
+
+template <bool kIsInstrumented>
+inline String* String::AllocFromString(Thread* self,
+                                       int32_t string_length,
+                                       Handle<String> string,
+                                       int32_t offset,
+                                       gc::AllocatorType allocator_type) {
+  const bool compressible = kUseStringCompression &&
+      ((string->IsCompressed()) ? true : String::AllASCII<uint16_t>(string->GetValue() + offset,
+                                                                    string_length));
+  const int32_t length_with_flag = String::GetFlaggedCount(string_length, compressible);
+  SetStringCountAndValueVisitorFromString visitor(length_with_flag, string, offset);
+  String* new_string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
+  return new_string;
+}
+
+}  // namespace mirror
+}  // namespace art
+
+#endif  // ART_RUNTIME_MIRROR_STRING_ALLOC_INL_H_
diff --git a/runtime/mirror/string-inl.h b/runtime/mirror/string-inl.h
index 3752d6d..d89ef1e 100644
--- a/runtime/mirror/string-inl.h
+++ b/runtime/mirror/string-inl.h
@@ -20,17 +20,11 @@
 
 #include "android-base/stringprintf.h"
 
-#include "array.h"
-#include "base/bit_utils.h"
 #include "base/globals.h"
 #include "base/utils.h"
-#include "class.h"
-#include "class_root.h"
+#include "class-inl.h"
 #include "common_throws.h"
 #include "dex/utf.h"
-#include "gc/heap-inl.h"
-#include "runtime.h"
-#include "thread.h"
 
 namespace art {
 namespace mirror {
@@ -49,127 +43,6 @@
   return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 1, 2, pointer_size);
 }
 
-// Sets string count in the allocation code path to ensure it is guarded by a CAS.
-class SetStringCountVisitor {
- public:
-  explicit SetStringCountVisitor(int32_t count) : count_(count) {
-  }
-
-  void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
-      REQUIRES_SHARED(Locks::mutator_lock_) {
-    // Avoid AsString as object is not yet in live bitmap or allocation stack.
-    ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
-    string->SetCount(count_);
-    DCHECK(!string->IsCompressed() || kUseStringCompression);
-  }
-
- private:
-  const int32_t count_;
-};
-
-// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
-class SetStringCountAndBytesVisitor {
- public:
-  SetStringCountAndBytesVisitor(int32_t count, Handle<ByteArray> src_array, int32_t offset,
-                                int32_t high_byte)
-      : count_(count), src_array_(src_array), offset_(offset), high_byte_(high_byte) {
-  }
-
-  void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
-      REQUIRES_SHARED(Locks::mutator_lock_) {
-    // Avoid AsString as object is not yet in live bitmap or allocation stack.
-    ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
-    string->SetCount(count_);
-    DCHECK(!string->IsCompressed() || kUseStringCompression);
-    int32_t length = String::GetLengthFromCount(count_);
-    const uint8_t* const src = reinterpret_cast<uint8_t*>(src_array_->GetData()) + offset_;
-    if (string->IsCompressed()) {
-      uint8_t* valueCompressed = string->GetValueCompressed();
-      for (int i = 0; i < length; i++) {
-        valueCompressed[i] = (src[i] & 0xFF);
-      }
-    } else {
-      uint16_t* value = string->GetValue();
-      for (int i = 0; i < length; i++) {
-        value[i] = high_byte_ + (src[i] & 0xFF);
-      }
-    }
-  }
-
- private:
-  const int32_t count_;
-  Handle<ByteArray> src_array_;
-  const int32_t offset_;
-  const int32_t high_byte_;
-};
-
-// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
-class SetStringCountAndValueVisitorFromCharArray {
- public:
-  SetStringCountAndValueVisitorFromCharArray(int32_t count, Handle<CharArray> src_array,
-                                             int32_t offset) :
-    count_(count), src_array_(src_array), offset_(offset) {
-  }
-
-  void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
-      REQUIRES_SHARED(Locks::mutator_lock_) {
-    // Avoid AsString as object is not yet in live bitmap or allocation stack.
-    ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
-    string->SetCount(count_);
-    const uint16_t* const src = src_array_->GetData() + offset_;
-    const int32_t length = String::GetLengthFromCount(count_);
-    if (kUseStringCompression && String::IsCompressed(count_)) {
-      for (int i = 0; i < length; ++i) {
-        string->GetValueCompressed()[i] = static_cast<uint8_t>(src[i]);
-      }
-    } else {
-      memcpy(string->GetValue(), src, length * sizeof(uint16_t));
-    }
-  }
-
- private:
-  const int32_t count_;
-  Handle<CharArray> src_array_;
-  const int32_t offset_;
-};
-
-// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
-class SetStringCountAndValueVisitorFromString {
- public:
-  SetStringCountAndValueVisitorFromString(int32_t count,
-                                          Handle<String> src_string,
-                                          int32_t offset) :
-    count_(count), src_string_(src_string), offset_(offset) {
-  }
-
-  void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
-      REQUIRES_SHARED(Locks::mutator_lock_) {
-    // Avoid AsString as object is not yet in live bitmap or allocation stack.
-    ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
-    string->SetCount(count_);
-    const int32_t length = String::GetLengthFromCount(count_);
-    bool compressible = kUseStringCompression && String::IsCompressed(count_);
-    if (src_string_->IsCompressed()) {
-      const uint8_t* const src = src_string_->GetValueCompressed() + offset_;
-      memcpy(string->GetValueCompressed(), src, length * sizeof(uint8_t));
-    } else {
-      const uint16_t* const src = src_string_->GetValue() + offset_;
-      if (compressible) {
-        for (int i = 0; i < length; ++i) {
-          string->GetValueCompressed()[i] = static_cast<uint8_t>(src[i]);
-        }
-      } else {
-        memcpy(string->GetValue(), src, length * sizeof(uint16_t));
-      }
-    }
-  }
-
- private:
-  const int32_t count_;
-  Handle<String> src_string_;
-  const int32_t offset_;
-};
-
 inline uint16_t String::CharAt(int32_t index) {
   int32_t count = GetLength();
   if (UNLIKELY((index < 0) || (index >= count))) {
@@ -195,93 +68,6 @@
   return -1;
 }
 
-template <bool kIsInstrumented, typename PreFenceVisitor>
-inline String* String::Alloc(Thread* self, int32_t utf16_length_with_flag,
-                             gc::AllocatorType allocator_type,
-                             const PreFenceVisitor& pre_fence_visitor) {
-  constexpr size_t header_size = sizeof(String);
-  const bool compressible = kUseStringCompression && String::IsCompressed(utf16_length_with_flag);
-  const size_t block_size = (compressible) ? sizeof(uint8_t) : sizeof(uint16_t);
-  size_t length = String::GetLengthFromCount(utf16_length_with_flag);
-  static_assert(sizeof(length) <= sizeof(size_t),
-                "static_cast<size_t>(utf16_length) must not lose bits.");
-  size_t data_size = block_size * length;
-  size_t size = header_size + data_size;
-  // String.equals() intrinsics assume zero-padding up to kObjectAlignment,
-  // so make sure the allocator clears the padding as well.
-  // http://b/23528461
-  size_t alloc_size = RoundUp(size, kObjectAlignment);
-
-  Runtime* runtime = Runtime::Current();
-  ObjPtr<Class> string_class = GetClassRoot<String>(runtime->GetClassLinker());
-  // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
-  // Do this by comparing with the maximum length that will _not_ cause an overflow.
-  const size_t overflow_length = (-header_size) / block_size;   // Unsigned arithmetic.
-  const size_t max_alloc_length = overflow_length - 1u;
-  static_assert(IsAligned<sizeof(uint16_t)>(kObjectAlignment),
-                "kObjectAlignment must be at least as big as Java char alignment");
-  const size_t max_length = RoundDown(max_alloc_length, kObjectAlignment / block_size);
-  if (UNLIKELY(length > max_length)) {
-    self->ThrowOutOfMemoryError(
-        android::base::StringPrintf("%s of length %d would overflow",
-                                    Class::PrettyDescriptor(string_class).c_str(),
-                                    static_cast<int>(length)).c_str());
-    return nullptr;
-  }
-
-  gc::Heap* heap = runtime->GetHeap();
-  return down_cast<String*>(
-      heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, string_class, alloc_size,
-                                                            allocator_type, pre_fence_visitor));
-}
-
-template <bool kIsInstrumented>
-inline String* String::AllocEmptyString(Thread* self, gc::AllocatorType allocator_type) {
-  const int32_t length_with_flag = String::GetFlaggedCount(0, /* compressible= */ true);
-  SetStringCountVisitor visitor(length_with_flag);
-  return Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
-}
-
-template <bool kIsInstrumented>
-inline String* String::AllocFromByteArray(Thread* self, int32_t byte_length,
-                                          Handle<ByteArray> array, int32_t offset,
-                                          int32_t high_byte, gc::AllocatorType allocator_type) {
-  const uint8_t* const src = reinterpret_cast<uint8_t*>(array->GetData()) + offset;
-  high_byte &= 0xff;  // Extract the relevant bits before determining `compressible`.
-  const bool compressible =
-      kUseStringCompression && String::AllASCII<uint8_t>(src, byte_length) && (high_byte == 0);
-  const int32_t length_with_flag = String::GetFlaggedCount(byte_length, compressible);
-  SetStringCountAndBytesVisitor visitor(length_with_flag, array, offset, high_byte << 8);
-  String* string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
-  return string;
-}
-
-template <bool kIsInstrumented>
-inline String* String::AllocFromCharArray(Thread* self, int32_t count,
-                                          Handle<CharArray> array, int32_t offset,
-                                          gc::AllocatorType allocator_type) {
-  // It is a caller error to have a count less than the actual array's size.
-  DCHECK_GE(array->GetLength(), count);
-  const bool compressible = kUseStringCompression &&
-                            String::AllASCII<uint16_t>(array->GetData() + offset, count);
-  const int32_t length_with_flag = String::GetFlaggedCount(count, compressible);
-  SetStringCountAndValueVisitorFromCharArray visitor(length_with_flag, array, offset);
-  String* new_string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
-  return new_string;
-}
-
-template <bool kIsInstrumented>
-inline String* String::AllocFromString(Thread* self, int32_t string_length, Handle<String> string,
-                                       int32_t offset, gc::AllocatorType allocator_type) {
-  const bool compressible = kUseStringCompression &&
-      ((string->IsCompressed()) ? true : String::AllASCII<uint16_t>(string->GetValue() + offset,
-                                                                    string_length));
-  const int32_t length_with_flag = String::GetFlaggedCount(string_length, compressible);
-  SetStringCountAndValueVisitorFromString visitor(length_with_flag, string, offset);
-  String* new_string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
-  return new_string;
-}
-
 inline int32_t String::GetHashCode() {
   int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_));
   if (UNLIKELY(result == 0)) {
diff --git a/runtime/mirror/string.cc b/runtime/mirror/string.cc
index ae4c7cc..01315e7 100644
--- a/runtime/mirror/string.cc
+++ b/runtime/mirror/string.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "string-inl.h"
+#include "string-alloc-inl.h"
 
 #include "arch/memcmp16.h"
 #include "array-alloc-inl.h"
diff --git a/runtime/native/java_lang_Class.cc b/runtime/native/java_lang_Class.cc
index e78c245..c7b8ad4 100644
--- a/runtime/native/java_lang_Class.cc
+++ b/runtime/native/java_lang_Class.cc
@@ -39,6 +39,7 @@
 #include "mirror/object-inl.h"
 #include "mirror/object_array-alloc-inl.h"
 #include "mirror/object_array-inl.h"
+#include "mirror/string-alloc-inl.h"
 #include "mirror/string-inl.h"
 #include "native_util.h"
 #include "nativehelper/jni_macros.h"
diff --git a/runtime/native/java_lang_String.cc b/runtime/native/java_lang_String.cc
index 78ec859..4be2086 100644
--- a/runtime/native/java_lang_String.cc
+++ b/runtime/native/java_lang_String.cc
@@ -23,8 +23,8 @@
 #include "jni/jni_internal.h"
 #include "mirror/array.h"
 #include "mirror/object-inl.h"
+#include "mirror/string-alloc-inl.h"
 #include "mirror/string-inl.h"
-#include "mirror/string.h"
 #include "native_util.h"
 #include "nativehelper/scoped_local_ref.h"
 #include "scoped_fast_native_object_access-inl.h"
diff --git a/runtime/native/java_lang_StringFactory.cc b/runtime/native/java_lang_StringFactory.cc
index c6ad4e4..13f8d5b 100644
--- a/runtime/native/java_lang_StringFactory.cc
+++ b/runtime/native/java_lang_StringFactory.cc
@@ -20,7 +20,7 @@
 #include "handle_scope-inl.h"
 #include "jni/jni_internal.h"
 #include "mirror/object-inl.h"
-#include "mirror/string-inl.h"
+#include "mirror/string-alloc-inl.h"
 #include "native_util.h"
 #include "nativehelper/jni_macros.h"
 #include "nativehelper/scoped_local_ref.h"