Home | History | Annotate | Download | only in mirror
      1 /*
      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_CLASS_ALLOC_INL_H_
     18 #define ART_RUNTIME_MIRROR_CLASS_ALLOC_INL_H_
     19 
     20 #include "class-inl.h"
     21 
     22 #include "gc/heap-inl.h"
     23 #include "object-inl.h"
     24 #include "runtime.h"
     25 
     26 namespace art {
     27 namespace mirror {
     28 
     29 inline void Class::CheckObjectAlloc() {
     30   DCHECK(!IsArrayClass())
     31       << PrettyClass()
     32       << "A array shouldn't be allocated through this "
     33       << "as it requires a pre-fence visitor that sets the class size.";
     34   DCHECK(!IsClassClass())
     35       << PrettyClass()
     36       << "A class object shouldn't be allocated through this "
     37       << "as it requires a pre-fence visitor that sets the class size.";
     38   DCHECK(!IsStringClass())
     39       << PrettyClass()
     40       << "A string shouldn't be allocated through this "
     41       << "as it requires a pre-fence visitor that sets the class size.";
     42   DCHECK(IsInstantiable()) << PrettyClass();
     43   // TODO: decide whether we want this check. It currently fails during bootstrap.
     44   // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass();
     45   DCHECK_GE(this->object_size_, sizeof(Object));
     46 }
     47 
     48 template<bool kIsInstrumented, bool kCheckAddFinalizer>
     49 inline ObjPtr<Object> Class::Alloc(Thread* self, gc::AllocatorType allocator_type) {
     50   CheckObjectAlloc();
     51   gc::Heap* heap = Runtime::Current()->GetHeap();
     52   const bool add_finalizer = kCheckAddFinalizer && IsFinalizable();
     53   if (!kCheckAddFinalizer) {
     54     DCHECK(!IsFinalizable());
     55   }
     56   // Note that the this pointer may be invalidated after the allocation.
     57   ObjPtr<Object> obj =
     58       heap->AllocObjectWithAllocator<kIsInstrumented, false>(self,
     59                                                              this,
     60                                                              this->object_size_,
     61                                                              allocator_type,
     62                                                              VoidFunctor());
     63   if (add_finalizer && LIKELY(obj != nullptr)) {
     64     heap->AddFinalizerReference(self, &obj);
     65     if (UNLIKELY(self->IsExceptionPending())) {
     66       // Failed to allocate finalizer reference, it means that the whole allocation failed.
     67       obj = nullptr;
     68     }
     69   }
     70   return obj;
     71 }
     72 
     73 inline ObjPtr<Object> Class::AllocObject(Thread* self) {
     74   return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
     75 }
     76 
     77 inline ObjPtr<Object> Class::AllocNonMovableObject(Thread* self) {
     78   return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentNonMovingAllocator());
     79 }
     80 
     81 }  // namespace mirror
     82 }  // namespace art
     83 
     84 #endif  // ART_RUNTIME_MIRROR_CLASS_ALLOC_INL_H_
     85