1 /*
2  * Copyright (C) 2013 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_GC_SPACE_ROSALLOC_SPACE_INL_H_
18 #define ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_INL_H_
19 
20 #include "gc/allocator/rosalloc-inl.h"
21 #include "rosalloc_space.h"
22 #include "thread.h"
23 
24 namespace art {
25 namespace gc {
26 namespace space {
27 
AllocationSizeNonvirtual(mirror::Object * obj,size_t * usable_size)28 inline size_t RosAllocSpace::AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size) {
29   void* obj_ptr = const_cast<void*>(reinterpret_cast<const void*>(obj));
30   // obj is a valid object. Use its class in the header to get the size.
31   // Don't use verification since the object may be dead if we are sweeping.
32   size_t size = obj->SizeOf<kVerifyNone>();
33   size_t size_by_size = rosalloc_->UsableSize(size);
34   if (kIsDebugBuild) {
35     size_t size_by_ptr = rosalloc_->UsableSize(obj_ptr);
36     if (size_by_size != size_by_ptr) {
37       LOG(INFO) << "Found a bad sized obj of size " << size
38                 << " at " << std::hex << reinterpret_cast<intptr_t>(obj_ptr) << std::dec
39                 << " size_by_size=" << size_by_size << " size_by_ptr=" << size_by_ptr;
40     }
41     DCHECK_EQ(size_by_size, size_by_ptr);
42   }
43   if (usable_size != nullptr) {
44     *usable_size = size_by_size;
45   }
46   return size_by_size;
47 }
48 
49 template<bool kThreadSafe>
AllocCommon(Thread * self,size_t num_bytes,size_t * bytes_allocated,size_t * usable_size)50 inline mirror::Object* RosAllocSpace::AllocCommon(Thread* self, size_t num_bytes,
51                                                   size_t* bytes_allocated, size_t* usable_size) {
52   size_t rosalloc_size = 0;
53   if (!kThreadSafe) {
54     Locks::mutator_lock_->AssertExclusiveHeld(self);
55   }
56   mirror::Object* result = reinterpret_cast<mirror::Object*>(
57       rosalloc_->Alloc<kThreadSafe>(self, num_bytes, &rosalloc_size));
58   if (LIKELY(result != NULL)) {
59     if (kDebugSpaces) {
60       CHECK(Contains(result)) << "Allocation (" << reinterpret_cast<void*>(result)
61             << ") not in bounds of allocation space " << *this;
62     }
63     DCHECK(bytes_allocated != NULL);
64     *bytes_allocated = rosalloc_size;
65     DCHECK_EQ(rosalloc_size, rosalloc_->UsableSize(result));
66     if (usable_size != nullptr) {
67       *usable_size = rosalloc_size;
68     }
69   }
70   return result;
71 }
72 
73 }  // namespace space
74 }  // namespace gc
75 }  // namespace art
76 
77 #endif  // ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_INL_H_
78