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 : // Declares a handful of STL compatible allocators that interact with
16 : // SyzyAsan subsystems. This is all with the goal of enhanced redzone
17 : // reporting.
18 :
19 : #ifndef SYZYGY_AGENT_ASAN_ALLOCATORS_H_
20 : #define SYZYGY_AGENT_ASAN_ALLOCATORS_H_
21 :
22 : #include <memory>
23 :
24 : #include "syzygy/agent/asan/heap.h"
25 : #include "syzygy/agent/asan/memory_notifier.h"
26 :
27 m : namespace agent {
28 m : namespace asan {
29 :
30 : // An STL-compatible allocator that notifies a MemoryNotifier object of
31 : // memory use.
32 : // @tparam T The type of object that is returned by the allocator.
33 m : template <typename T>
34 m : class MemoryNotifierAllocator : public std::allocator<T> {
35 m : public:
36 m : typedef size_t size_type;
37 m : typedef T* pointer;
38 m : typedef const T* const_pointer;
39 :
40 : // Functor that converts this allocator to an equivalent one for another
41 : // type.
42 : // @tparam T2 The type being casted to.
43 m : template <typename T2>
44 m : struct rebind {
45 m : typedef MemoryNotifierAllocator<T2> other;
46 m : };
47 :
48 : // Constructor with a notifier object.
49 : // @param memory_notifier A pointer to the memory notifier object
50 : // that this allocate will notify.
51 m : explicit MemoryNotifierAllocator(
52 m : MemoryNotifierInterface* memory_notifier);
53 :
54 : // Copy constructor. Necessary for STL compatibility.
55 m : MemoryNotifierAllocator(const MemoryNotifierAllocator& other);
56 :
57 : // Copy constructor from another type. Necessary for STL compatibility.
58 : // This simply copies the memory notifier API.
59 : // @tparam T2 The type of the other allocator.
60 : // @param other The allocator being copied.
61 m : template <typename T2>
62 m : MemoryNotifierAllocator(const MemoryNotifierAllocator<T2>& other);
63 :
64 : // Allocates @p count objects of type T.
65 : // @param count The number of objects to allocate.
66 : // @param hint A hint as to where the objects should be allocated.
67 : // @returns a pointer to the allocated objects, NULL if the allocation
68 : // failed.
69 m : pointer allocate(size_type count, const void* hint = NULL);
70 :
71 : // Deallocates a group of @p n objects.
72 : // @param objects A pointer to the allocated objects. This must have
73 : // previously been returned a call to 'allocate'.
74 : // @param count The number of objects in the allocation.
75 m : void deallocate(pointer objects, size_type count);
76 :
77 : // @returns the MemoryNotifier object used by this allocator.
78 m : MemoryNotifierInterface* memory_notifier() const;
79 :
80 m : protected:
81 m : MemoryNotifierInterface* memory_notifier_;
82 m : };
83 :
84 : // An STL-compatible allocator that uses a HeapInterface object under the
85 : // hood.
86 : // @tparam T The type of object that is returned by the allocator.
87 m : template <typename T>
88 m : class HeapAllocator : public std::allocator<T> {
89 m : public:
90 m : typedef size_t size_type;
91 m : typedef T* pointer;
92 m : typedef const T* const_pointer;
93 :
94 : // Functor that converts this allocator to an equivalent one for another
95 : // type.
96 : // @tparam T2 The type being casted to.
97 m : template <typename T2>
98 m : struct rebind {
99 m : typedef HeapAllocator<T2> other;
100 m : };
101 :
102 : // Constructor with a notifier object.
103 : // @param heap A pointer to the heap object that will be used to make the
104 : // allocations.
105 m : explicit HeapAllocator(HeapInterface* heap);
106 :
107 : // Copy constructor. Necessary for STL compatibility.
108 m : HeapAllocator(const HeapAllocator& other);
109 :
110 : // Copy constructor from another type. Necessary for STL compatibility.
111 : // This simply copies the memory notifier API.
112 : // @tparam T2 The type of the other allocator.
113 : // @param other The allocator being copied.
114 m : template <typename T2>
115 m : HeapAllocator(const HeapAllocator<T2>& other);
116 :
117 : // Allocates @p count objects of type T.
118 : // @param count The number of objects to allocate.
119 : // @param hint A hint as to where the objects should be allocated.
120 : // @returns a pointer to the allocated objects, NULL if the allocation
121 : // failed.
122 m : pointer allocate(size_type count, const void* hint = NULL);
123 :
124 : // Deallocates a group of @p n objects.
125 : // @param objects A pointer to the allocated objects. This must have
126 : // previously been returned a call to 'allocate'.
127 : // @param count The number of objects in the allocation.
128 m : void deallocate(pointer objects, size_type count);
129 :
130 : // @returns the Heap object used by this allocator.
131 m : HeapInterface* heap() const;
132 :
133 m : protected:
134 m : HeapInterface* heap_;
135 m : };
136 :
137 m : } // namespace asan
138 m : } // namespace agent
139 :
140 : #include "syzygy/agent/asan/allocators_impl.h"
141 :
142 : #endif // SYZYGY_AGENT_ASAN_ALLOCATORS_H_
|