Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 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 | |
Hiroshi Yamauchi | 3c2856e | 2013-11-22 13:42:53 -0800 | [diff] [blame] | 20 | #include "gc/allocator/rosalloc-inl.h" |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame^] | 21 | #include "gc/space/valgrind_settings.h" |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 22 | #include "rosalloc_space.h" |
| 23 | #include "thread.h" |
| 24 | |
| 25 | namespace art { |
| 26 | namespace gc { |
| 27 | namespace space { |
| 28 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 29 | inline size_t RosAllocSpace::AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 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>(); |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame^] | 33 | bool running_on_valgrind = RUNNING_ON_VALGRIND != 0; |
| 34 | if (running_on_valgrind) { |
| 35 | size += 2 * kDefaultValgrindRedZoneBytes; |
| 36 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 37 | size_t size_by_size = rosalloc_->UsableSize(size); |
| 38 | if (kIsDebugBuild) { |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame^] | 39 | // On valgrind, the red zone has an impact... |
| 40 | const uint8_t* obj_ptr = reinterpret_cast<const uint8_t*>(obj); |
| 41 | size_t size_by_ptr = rosalloc_->UsableSize( |
| 42 | obj_ptr - (running_on_valgrind ? kDefaultValgrindRedZoneBytes : 0)); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 43 | if (size_by_size != size_by_ptr) { |
| 44 | LOG(INFO) << "Found a bad sized obj of size " << size |
| 45 | << " at " << std::hex << reinterpret_cast<intptr_t>(obj_ptr) << std::dec |
| 46 | << " size_by_size=" << size_by_size << " size_by_ptr=" << size_by_ptr; |
| 47 | } |
| 48 | DCHECK_EQ(size_by_size, size_by_ptr); |
| 49 | } |
| 50 | if (usable_size != nullptr) { |
| 51 | *usable_size = size_by_size; |
| 52 | } |
| 53 | return size_by_size; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 56 | template<bool kThreadSafe> |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 57 | inline mirror::Object* RosAllocSpace::AllocCommon(Thread* self, size_t num_bytes, |
| 58 | size_t* bytes_allocated, size_t* usable_size) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 59 | size_t rosalloc_size = 0; |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 60 | if (!kThreadSafe) { |
| 61 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
| 62 | } |
Hiroshi Yamauchi | 4ce1f00 | 2013-11-18 14:49:09 -0800 | [diff] [blame] | 63 | mirror::Object* result = reinterpret_cast<mirror::Object*>( |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 64 | rosalloc_->Alloc<kThreadSafe>(self, num_bytes, &rosalloc_size)); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 65 | if (LIKELY(result != NULL)) { |
| 66 | if (kDebugSpaces) { |
| 67 | CHECK(Contains(result)) << "Allocation (" << reinterpret_cast<void*>(result) |
| 68 | << ") not in bounds of allocation space " << *this; |
| 69 | } |
| 70 | DCHECK(bytes_allocated != NULL); |
| 71 | *bytes_allocated = rosalloc_size; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 72 | DCHECK_EQ(rosalloc_size, rosalloc_->UsableSize(result)); |
| 73 | if (usable_size != nullptr) { |
| 74 | *usable_size = rosalloc_size; |
| 75 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 76 | } |
| 77 | return result; |
| 78 | } |
| 79 | |
| 80 | } // namespace space |
| 81 | } // namespace gc |
| 82 | } // namespace art |
| 83 | |
| 84 | #endif // ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_INL_H_ |