1 : // Copyright 2014 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Internal implementation details for allocators.h. Not meant to be
16 : // included directly.
17 :
18 : #ifndef SYZYGY_AGENT_ASAN_ALLOCATORS_IMPL_H_
19 : #define SYZYGY_AGENT_ASAN_ALLOCATORS_IMPL_H_
20 :
21 : #include "base/logging.h"
22 :
23 : namespace agent {
24 : namespace asan {
25 :
26 : template <typename T>
27 : MemoryNotifierAllocator<T>::MemoryNotifierAllocator(
28 : MemoryNotifierInterface* memory_notifier)
29 E : : memory_notifier_(memory_notifier) {
30 E : DCHECK_NE(static_cast<MemoryNotifierInterface*>(NULL), memory_notifier_);
31 E : }
32 :
33 : template <typename T>
34 : MemoryNotifierAllocator<T>::MemoryNotifierAllocator(
35 : const MemoryNotifierAllocator& other)
36 E : : memory_notifier_(other.memory_notifier_) {
37 E : DCHECK_NE(static_cast<MemoryNotifierInterface*>(NULL), memory_notifier_);
38 E : }
39 :
40 : template <typename T>
41 : template <typename T2>
42 : MemoryNotifierAllocator<T>::MemoryNotifierAllocator(
43 : const MemoryNotifierAllocator<T2>& other)
44 E : : memory_notifier_(other.memory_notifier()) {
45 E : DCHECK_NE(static_cast<MemoryNotifierInterface*>(NULL), memory_notifier_);
46 E : }
47 :
48 : template <typename T>
49 : typename MemoryNotifierAllocator<T>::pointer
50 : MemoryNotifierAllocator<T>::allocate(
51 E : size_type count, const void* hint) {
52 E : DCHECK_NE(static_cast<MemoryNotifierInterface*>(NULL), memory_notifier_);
53 :
54 E : pointer objects = std::allocator<T>::allocate(count, hint);
55 E : if (count > 0)
56 E : memory_notifier_->NotifyInternalUse(objects, sizeof(T) * count);
57 :
58 E : return objects;
59 E : }
60 :
61 : template <typename T>
62 : void MemoryNotifierAllocator<T>::deallocate(
63 E : pointer objects, size_type count) {
64 E : DCHECK_NE(static_cast<MemoryNotifierInterface*>(NULL), memory_notifier_);
65 :
66 E : if (count > 0)
67 E : memory_notifier_->NotifyReturnedToOS(objects, sizeof(T) * count);
68 E : std::allocator<T>::deallocate(objects, count);
69 E : }
70 :
71 : template <typename T>
72 : MemoryNotifierInterface*
73 E : MemoryNotifierAllocator<T>::memory_notifier() const {
74 E : DCHECK_NE(static_cast<MemoryNotifierInterface*>(NULL), memory_notifier_);
75 :
76 E : return memory_notifier_;
77 E : }
78 :
79 : template <typename T>
80 : HeapAllocator<T>::HeapAllocator(
81 : HeapInterface* heap)
82 E : : heap_(heap) {
83 E : DCHECK_NE(static_cast<HeapInterface*>(NULL), heap_);
84 E : }
85 :
86 : template <typename T>
87 : HeapAllocator<T>::HeapAllocator(
88 : const HeapAllocator& other)
89 E : : heap_(other.heap_) {
90 E : DCHECK_NE(static_cast<HeapInterface*>(NULL), heap_);
91 E : }
92 :
93 : template <typename T>
94 : template <typename T2>
95 : HeapAllocator<T>::HeapAllocator(
96 : const HeapAllocator<T2>& other)
97 E : : heap_(other.heap()) {
98 E : DCHECK_NE(static_cast<HeapInterface*>(NULL), heap_);
99 E : }
100 :
101 : template <typename T>
102 : typename HeapAllocator<T>::pointer
103 : HeapAllocator<T>::allocate(
104 E : size_type count, const void* hint) {
105 E : DCHECK_NE(static_cast<HeapInterface*>(NULL), heap_);
106 :
107 : pointer objects = reinterpret_cast<pointer>(
108 E : heap_->Allocate(count * sizeof(T)));
109 :
110 E : return objects;
111 E : }
112 :
113 : template <typename T>
114 : void HeapAllocator<T>::deallocate(
115 E : pointer objects, size_type count) {
116 E : DCHECK_NE(static_cast<HeapInterface*>(NULL), heap_);
117 :
118 E : heap_->Free(objects);
119 E : }
120 :
121 : template <typename T>
122 : HeapInterface*
123 E : HeapAllocator<T>::heap() const {
124 E : DCHECK_NE(static_cast<HeapInterface*>(NULL), heap_);
125 :
126 E : return heap_;
127 E : }
128 :
129 : } // namespace asan
130 : } // namespace agent
131 :
132 : #endif // SYZYGY_AGENT_ASAN_ALLOCATORS_IMPL_H_
|