blob: f94ec23115883c2603ef833cfcae07eeff87e7ea [file] [log] [blame]
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001/*
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
Mathieu Chartier3cf22532015-07-09 15:15:09 -070020#include <valgrind.h>
21
Hiroshi Yamauchi3c2856e2013-11-22 13:42:53 -080022#include "gc/allocator/rosalloc-inl.h"
Andreas Gamped7576322014-10-24 22:13:45 -070023#include "gc/space/valgrind_settings.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070024#include "rosalloc_space.h"
25#include "thread.h"
26
27namespace art {
28namespace gc {
29namespace space {
30
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070031template<bool kMaybeRunningOnValgrind>
Ian Rogers6fac4472014-02-25 17:01:10 -080032inline size_t RosAllocSpace::AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size) {
Ian Rogers6fac4472014-02-25 17:01:10 -080033 // obj is a valid object. Use its class in the header to get the size.
34 // Don't use verification since the object may be dead if we are sweeping.
35 size_t size = obj->SizeOf<kVerifyNone>();
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070036 bool running_on_valgrind = false;
37 if (kMaybeRunningOnValgrind) {
38 running_on_valgrind = RUNNING_ON_VALGRIND != 0;
39 if (running_on_valgrind) {
40 size += 2 * kDefaultValgrindRedZoneBytes;
41 }
42 } else {
43 DCHECK_EQ(RUNNING_ON_VALGRIND, 0U);
Andreas Gamped7576322014-10-24 22:13:45 -070044 }
Ian Rogers6fac4472014-02-25 17:01:10 -080045 size_t size_by_size = rosalloc_->UsableSize(size);
46 if (kIsDebugBuild) {
Andreas Gamped7576322014-10-24 22:13:45 -070047 // On valgrind, the red zone has an impact...
48 const uint8_t* obj_ptr = reinterpret_cast<const uint8_t*>(obj);
49 size_t size_by_ptr = rosalloc_->UsableSize(
50 obj_ptr - (running_on_valgrind ? kDefaultValgrindRedZoneBytes : 0));
Ian Rogers6fac4472014-02-25 17:01:10 -080051 if (size_by_size != size_by_ptr) {
52 LOG(INFO) << "Found a bad sized obj of size " << size
53 << " at " << std::hex << reinterpret_cast<intptr_t>(obj_ptr) << std::dec
54 << " size_by_size=" << size_by_size << " size_by_ptr=" << size_by_ptr;
55 }
56 DCHECK_EQ(size_by_size, size_by_ptr);
57 }
58 if (usable_size != nullptr) {
59 *usable_size = size_by_size;
60 }
61 return size_by_size;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070062}
63
Mathieu Chartier0651d412014-04-29 14:37:57 -070064template<bool kThreadSafe>
Ian Rogers6fac4472014-02-25 17:01:10 -080065inline mirror::Object* RosAllocSpace::AllocCommon(Thread* self, size_t num_bytes,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070066 size_t* bytes_allocated, size_t* usable_size,
67 size_t* bytes_tl_bulk_allocated) {
68 size_t rosalloc_bytes_allocated = 0;
69 size_t rosalloc_usable_size = 0;
70 size_t rosalloc_bytes_tl_bulk_allocated = 0;
Mathieu Chartier0651d412014-04-29 14:37:57 -070071 if (!kThreadSafe) {
72 Locks::mutator_lock_->AssertExclusiveHeld(self);
73 }
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -080074 mirror::Object* result = reinterpret_cast<mirror::Object*>(
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070075 rosalloc_->Alloc<kThreadSafe>(self, num_bytes, &rosalloc_bytes_allocated,
76 &rosalloc_usable_size,
77 &rosalloc_bytes_tl_bulk_allocated));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070078 if (LIKELY(result != nullptr)) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070079 if (kDebugSpaces) {
80 CHECK(Contains(result)) << "Allocation (" << reinterpret_cast<void*>(result)
81 << ") not in bounds of allocation space " << *this;
82 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -070083 DCHECK(bytes_allocated != nullptr);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070084 *bytes_allocated = rosalloc_bytes_allocated;
85 DCHECK_EQ(rosalloc_usable_size, rosalloc_->UsableSize(result));
Ian Rogers6fac4472014-02-25 17:01:10 -080086 if (usable_size != nullptr) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070087 *usable_size = rosalloc_usable_size;
Ian Rogers6fac4472014-02-25 17:01:10 -080088 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -070089 DCHECK(bytes_tl_bulk_allocated != nullptr);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070090 *bytes_tl_bulk_allocated = rosalloc_bytes_tl_bulk_allocated;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070091 }
92 return result;
93}
94
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070095inline bool RosAllocSpace::CanAllocThreadLocal(Thread* self, size_t num_bytes) {
96 return rosalloc_->CanAllocFromThreadLocalRun(self, num_bytes);
97}
98
99inline mirror::Object* RosAllocSpace::AllocThreadLocal(Thread* self, size_t num_bytes,
100 size_t* bytes_allocated) {
101 DCHECK(bytes_allocated != nullptr);
102 return reinterpret_cast<mirror::Object*>(
103 rosalloc_->AllocFromThreadLocalRun(self, num_bytes, bytes_allocated));
104}
105
106inline size_t RosAllocSpace::MaxBytesBulkAllocatedForNonvirtual(size_t num_bytes) {
107 return rosalloc_->MaxBytesBulkAllocatedFor(num_bytes);
108}
109
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700110} // namespace space
111} // namespace gc
112} // namespace art
113
114#endif // ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_INL_H_