Set array length before fence in allocation code path.

Could not delete SetLength since it is required by
space_test.

Bug: 11747779

Change-Id: Icf1ead216b6ff1b519240ab0d0ca30d68429d5b6
diff --git a/runtime/mirror/array-inl.h b/runtime/mirror/array-inl.h
index 2955faa..46ffaae 100644
--- a/runtime/mirror/array-inl.h
+++ b/runtime/mirror/array-inl.h
@@ -58,13 +58,20 @@
   return size;
 }
 
-static inline Array* SetArrayLength(Array* array, size_t length) {
-  if (LIKELY(array != nullptr)) {
-    DCHECK(array->IsArrayInstance());
-    array->SetLength(length);
+class SetLengthVisitor {
+ public:
+  explicit SetLengthVisitor(int32_t length) : length_(length) {
   }
-  return array;
-}
+
+  void operator()(mirror::Object* obj) const {
+    mirror::Array* array = obj->AsArray();
+    DCHECK(array->IsArrayInstance());
+    array->SetLength(length_);
+  }
+
+ private:
+  const int32_t length_;
+};
 
 template <bool kIsInstrumented>
 inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
@@ -74,9 +81,10 @@
     return nullptr;
   }
   gc::Heap* heap = Runtime::Current()->GetHeap();
-  Array* array = down_cast<Array*>(
-      heap->AllocObjectWithAllocator<kIsInstrumented>(self, array_class, size, allocator_type));
-  return SetArrayLength(array, component_count);
+  SetLengthVisitor visitor(component_count);
+  return down_cast<Array*>(
+      heap->AllocObjectWithAllocator<kIsInstrumented>(self, array_class, size, allocator_type,
+                                                      visitor));
 }
 
 template <bool kIsInstrumented>