Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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_VALGRIND_MALLOC_SPACE_INL_H_ |
| 18 | #define ART_RUNTIME_GC_SPACE_VALGRIND_MALLOC_SPACE_INL_H_ |
| 19 | |
| 20 | #include "valgrind_malloc_space.h" |
| 21 | |
| 22 | #include <memcheck/memcheck.h> |
| 23 | |
| 24 | namespace art { |
| 25 | namespace gc { |
| 26 | namespace space { |
| 27 | |
| 28 | // Number of bytes to use as a red zone (rdz). A red zone of this size will be placed before and |
| 29 | // after each allocation. 8 bytes provides long/double alignment. |
| 30 | static constexpr size_t kValgrindRedZoneBytes = 8; |
| 31 | |
| 32 | template <typename S, typename A> |
| 33 | mirror::Object* ValgrindMallocSpace<S, A>::AllocWithGrowth(Thread* self, size_t num_bytes, |
| 34 | size_t* bytes_allocated, |
| 35 | size_t* usable_size) { |
| 36 | void* obj_with_rdz = S::AllocWithGrowth(self, num_bytes + 2 * kValgrindRedZoneBytes, |
| 37 | bytes_allocated, usable_size); |
| 38 | if (obj_with_rdz == nullptr) { |
| 39 | return nullptr; |
| 40 | } |
| 41 | if (usable_size != nullptr) { |
| 42 | *usable_size -= 2 * kValgrindRedZoneBytes; |
| 43 | } |
| 44 | mirror::Object* result = reinterpret_cast<mirror::Object*>( |
| 45 | reinterpret_cast<byte*>(obj_with_rdz) + kValgrindRedZoneBytes); |
| 46 | // Make redzones as no access. |
| 47 | VALGRIND_MAKE_MEM_NOACCESS(obj_with_rdz, kValgrindRedZoneBytes); |
| 48 | VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<byte*>(result) + num_bytes, kValgrindRedZoneBytes); |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | template <typename S, typename A> |
| 53 | mirror::Object* ValgrindMallocSpace<S, A>::Alloc(Thread* self, size_t num_bytes, |
| 54 | size_t* bytes_allocated, |
| 55 | size_t* usable_size) { |
| 56 | void* obj_with_rdz = S::Alloc(self, num_bytes + 2 * kValgrindRedZoneBytes, bytes_allocated, |
| 57 | usable_size); |
| 58 | if (obj_with_rdz == nullptr) { |
| 59 | return nullptr; |
| 60 | } |
| 61 | if (usable_size != nullptr) { |
| 62 | *usable_size -= 2 * kValgrindRedZoneBytes; |
| 63 | } |
| 64 | mirror::Object* result = reinterpret_cast<mirror::Object*>( |
| 65 | reinterpret_cast<byte*>(obj_with_rdz) + kValgrindRedZoneBytes); |
| 66 | // Make redzones as no access. |
| 67 | VALGRIND_MAKE_MEM_NOACCESS(obj_with_rdz, kValgrindRedZoneBytes); |
| 68 | VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<byte*>(result) + num_bytes, kValgrindRedZoneBytes); |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | template <typename S, typename A> |
| 73 | size_t ValgrindMallocSpace<S, A>::AllocationSize(mirror::Object* obj, size_t* usable_size) { |
| 74 | size_t result = S::AllocationSize(reinterpret_cast<mirror::Object*>( |
| 75 | reinterpret_cast<byte*>(obj) - kValgrindRedZoneBytes), usable_size); |
| 76 | if (usable_size != nullptr) { |
| 77 | *usable_size -= 2 * kValgrindRedZoneBytes; |
| 78 | } |
| 79 | return result - 2 * kValgrindRedZoneBytes; |
| 80 | } |
| 81 | |
| 82 | template <typename S, typename A> |
| 83 | size_t ValgrindMallocSpace<S, A>::Free(Thread* self, mirror::Object* ptr) { |
| 84 | void* obj_after_rdz = reinterpret_cast<void*>(ptr); |
| 85 | void* obj_with_rdz = reinterpret_cast<byte*>(obj_after_rdz) - kValgrindRedZoneBytes; |
| 86 | // Make redzones undefined. |
| 87 | size_t allocation_size = |
| 88 | AllocationSize(reinterpret_cast<mirror::Object*>(obj_with_rdz), nullptr); |
| 89 | VALGRIND_MAKE_MEM_UNDEFINED(obj_with_rdz, allocation_size); |
| 90 | size_t freed = S::Free(self, reinterpret_cast<mirror::Object*>(obj_with_rdz)); |
| 91 | return freed - 2 * kValgrindRedZoneBytes; |
| 92 | } |
| 93 | |
| 94 | template <typename S, typename A> |
| 95 | size_t ValgrindMallocSpace<S, A>::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) { |
| 96 | size_t freed = 0; |
| 97 | for (size_t i = 0; i < num_ptrs; i++) { |
| 98 | freed += Free(self, ptrs[i]); |
| 99 | } |
| 100 | return freed; |
| 101 | } |
| 102 | |
| 103 | template <typename S, typename A> |
| 104 | ValgrindMallocSpace<S, A>::ValgrindMallocSpace(const std::string& name, MemMap* mem_map, |
| 105 | A allocator, byte* begin, |
| 106 | byte* end, byte* limit, size_t growth_limit, |
| 107 | size_t initial_size) : |
| 108 | S(name, mem_map, allocator, begin, end, limit, growth_limit) { |
| 109 | VALGRIND_MAKE_MEM_UNDEFINED(mem_map->Begin() + initial_size, mem_map->Size() - initial_size); |
| 110 | } |
| 111 | |
| 112 | } // namespace space |
| 113 | } // namespace gc |
| 114 | } // namespace art |
| 115 | |
| 116 | #endif // ART_RUNTIME_GC_SPACE_VALGRIND_MALLOC_SPACE_INL_H_ |