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>
AllocWithGrowth(Thread * self,size_t num_bytes,size_t * bytes_allocated,size_t * usable_size)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 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
49 template <typename S, typename A>
Alloc(Thread * self,size_t num_bytes,size_t * bytes_allocated,size_t * usable_size)50 mirror::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 }
58 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
66 template <typename S, typename A>
AllocationSize(mirror::Object * obj,size_t * usable_size)67 size_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);
70 return result;
71 }
72
73 template <typename S, typename A>
Free(Thread * self,mirror::Object * ptr)74 size_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.
78 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));
82 }
83
84 template <typename S, typename A>
FreeList(Thread * self,size_t num_ptrs,mirror::Object ** ptrs)85 size_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]);
89 ptrs[i] = nullptr;
90 }
91 return freed;
92 }
93
94 template <typename S, typename A>
ValgrindMallocSpace(const std::string & name,MemMap * mem_map,A allocator,byte * begin,byte * end,byte * limit,size_t growth_limit,size_t initial_size,bool can_move_objects,size_t starting_size)95 ValgrindMallocSpace<S, A>::ValgrindMallocSpace(const std::string& name, MemMap* mem_map,
96 A allocator, byte* begin,
97 byte* end, byte* limit, size_t growth_limit,
98 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) {
102 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_
110