blob: 966c276f42a45a2d53437ef0e2beec9ad70b3cd1 [file] [log] [blame]
Ian Rogers6fac4472014-02-25 17:01:10 -08001/*
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
24namespace art {
25namespace gc {
26namespace 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.
30static constexpr size_t kValgrindRedZoneBytes = 8;
31
32template <typename S, typename A>
33mirror::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 }
Ian Rogers6fac4472014-02-25 17:01:10 -080041 mirror::Object* result = reinterpret_cast<mirror::Object*>(
42 reinterpret_cast<byte*>(obj_with_rdz) + kValgrindRedZoneBytes);
43 // Make redzones as no access.
44 VALGRIND_MAKE_MEM_NOACCESS(obj_with_rdz, kValgrindRedZoneBytes);
45 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<byte*>(result) + num_bytes, kValgrindRedZoneBytes);
46 return result;
47}
48
49template <typename S, typename A>
50mirror::Object* ValgrindMallocSpace<S, A>::Alloc(Thread* self, size_t num_bytes,
51 size_t* bytes_allocated,
52 size_t* usable_size) {
53 void* obj_with_rdz = S::Alloc(self, num_bytes + 2 * kValgrindRedZoneBytes, bytes_allocated,
54 usable_size);
55 if (obj_with_rdz == nullptr) {
56 return nullptr;
57 }
Ian Rogers6fac4472014-02-25 17:01:10 -080058 mirror::Object* result = reinterpret_cast<mirror::Object*>(
59 reinterpret_cast<byte*>(obj_with_rdz) + kValgrindRedZoneBytes);
60 // Make redzones as no access.
61 VALGRIND_MAKE_MEM_NOACCESS(obj_with_rdz, kValgrindRedZoneBytes);
62 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<byte*>(result) + num_bytes, kValgrindRedZoneBytes);
63 return result;
64}
65
66template <typename S, typename A>
67size_t ValgrindMallocSpace<S, A>::AllocationSize(mirror::Object* obj, size_t* usable_size) {
68 size_t result = S::AllocationSize(reinterpret_cast<mirror::Object*>(
69 reinterpret_cast<byte*>(obj) - kValgrindRedZoneBytes), usable_size);
Mathieu Chartier661974a2014-01-09 11:23:53 -080070 return result;
Ian Rogers6fac4472014-02-25 17:01:10 -080071}
72
73template <typename S, typename A>
74size_t ValgrindMallocSpace<S, A>::Free(Thread* self, mirror::Object* ptr) {
75 void* obj_after_rdz = reinterpret_cast<void*>(ptr);
76 void* obj_with_rdz = reinterpret_cast<byte*>(obj_after_rdz) - kValgrindRedZoneBytes;
77 // Make redzones undefined.
Mathieu Chartier661974a2014-01-09 11:23:53 -080078 size_t usable_size = 0;
79 AllocationSize(ptr, &usable_size);
80 VALGRIND_MAKE_MEM_UNDEFINED(obj_with_rdz, usable_size);
81 return S::Free(self, reinterpret_cast<mirror::Object*>(obj_with_rdz));
Ian Rogers6fac4472014-02-25 17:01:10 -080082}
83
84template <typename S, typename A>
85size_t ValgrindMallocSpace<S, A>::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
86 size_t freed = 0;
87 for (size_t i = 0; i < num_ptrs; i++) {
88 freed += Free(self, ptrs[i]);
Mathieu Chartier661974a2014-01-09 11:23:53 -080089 ptrs[i] = nullptr;
Ian Rogers6fac4472014-02-25 17:01:10 -080090 }
91 return freed;
92}
93
94template <typename S, typename A>
95ValgrindMallocSpace<S, A>::ValgrindMallocSpace(const std::string& name, MemMap* mem_map,
96 A allocator, byte* begin,
97 byte* end, byte* limit, size_t growth_limit,
Mathieu Chartier31f44142014-04-08 14:40:03 -070098 size_t initial_size,
99 bool can_move_objects, size_t starting_size) :
100 S(name, mem_map, allocator, begin, end, limit, growth_limit, can_move_objects, starting_size,
101 initial_size) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800102 VALGRIND_MAKE_MEM_UNDEFINED(mem_map->Begin() + initial_size, mem_map->Size() - initial_size);
103}
104
105} // namespace space
106} // namespace gc
107} // namespace art
108
109#endif // ART_RUNTIME_GC_SPACE_VALGRIND_MALLOC_SPACE_INL_H_