OLD | NEW |
---|---|
(Empty) | |
1 //===- subzero/src/IceMemory.h - Memory management declarations -*- C++ -*-===// | |
2 // | |
3 // The Subzero Code Generator | |
4 // | |
5 // This file is distributed under the University of Illinois Open Source | |
6 // License. See LICENSE.TXT for details. | |
7 // | |
8 //===----------------------------------------------------------------------===// | |
9 /// | |
10 /// \file | |
11 /// \brief Declares some useful data structures and routines dealing with | |
12 /// memory management in Subzero (mostly, allocator types.) | |
13 /// | |
14 //===----------------------------------------------------------------------===// | |
15 | |
16 #ifndef SUBZERO_SRC_ICEMEMORY_H | |
17 #define SUBZERO_SRC_ICEMEMORY_H | |
18 | |
19 #include "IceTLS.h" | |
20 | |
21 #include "llvm/Support/Allocator.h" | |
22 | |
23 #include <cstddef> | |
24 #include <mutex> | |
25 | |
26 namespace Ice { | |
27 | |
28 class Cfg; | |
29 class GlobalContext; | |
30 | |
31 using ArenaAllocator = | |
32 llvm::BumpPtrAllocatorImpl<llvm::MallocAllocator, /*SlabSize=*/1024 * 1024>; | |
33 | |
34 class LockedArenaAllocator { | |
35 LockedArenaAllocator() = delete; | |
36 LockedArenaAllocator(const LockedArenaAllocator &) = delete; | |
37 LockedArenaAllocator &operator=(const LockedArenaAllocator &) = delete; | |
38 | |
39 public: | |
40 LockedArenaAllocator(ArenaAllocator *Alloc, std::mutex *Mutex) : Alloc(Alloc), AutoLock(*Mutex) {} | |
Jim Stichnoth
2016/02/24 23:23:32
80-col
John
2016/02/24 23:46:05
Done.
| |
41 LockedArenaAllocator(LockedArenaAllocator &&) = default; | |
42 LockedArenaAllocator &operator=(LockedArenaAllocator &&) = default; | |
43 ~LockedArenaAllocator() = default; | |
44 | |
45 ArenaAllocator *operator->() { return Alloc; } | |
46 | |
47 private: | |
48 ArenaAllocator *Alloc; | |
49 std::unique_lock<std::mutex> AutoLock; | |
50 }; | |
51 | |
52 template <typename T, typename Traits> struct sz_allocator { | |
Jim Stichnoth
2016/02/24 23:23:32
sz_allocator/allocator_type/manager_type don't fol
John
2016/02/24 23:46:06
they follow c++'s naming convention.
| |
53 /// std::allocator interface implementation. | |
54 /// @{ | |
55 using value_type = T; | |
56 using pointer = T *; | |
57 using const_pointer = const T *; | |
58 using reference = T &; | |
59 using const_reference = const T &; | |
60 using size_type = std::size_t; | |
61 using difference_type = std::ptrdiff_t; | |
62 | |
63 sz_allocator() = default; | |
64 template <class U> sz_allocator(const sz_allocator<U, Traits> &) {} | |
65 | |
66 pointer address(reference x) const { | |
67 return reinterpret_cast<pointer>(&reinterpret_cast<char &>(x)); | |
68 } | |
69 const_pointer address(const_reference x) const { | |
70 return reinterpret_cast<const_pointer>(&reinterpret_cast<const char &>(x)); | |
71 } | |
72 | |
73 pointer allocate(size_type num) { | |
74 return current()->template Allocate<T>(num); | |
75 } | |
76 | |
77 template <typename... A> void construct(pointer P, A &&... Args) { | |
78 new (static_cast<void *>(P)) T(std::forward<A>(Args)...); | |
79 } | |
80 | |
81 void deallocate(pointer, size_type) {} | |
82 | |
83 template <class U> struct rebind { typedef sz_allocator<U, Traits> other; }; | |
84 | |
85 void destroy(pointer P) { P->~T(); } | |
86 /// @} | |
87 | |
88 /// Manages the current underlying allocator. | |
89 /// @{ | |
90 static typename Traits::allocator_type current() { return Traits::current(); } | |
91 static void init() { Traits::init(); } | |
92 /// @} | |
93 }; | |
94 | |
95 template <class Traits> struct sz_allocator_scope { | |
96 explicit sz_allocator_scope(typename Traits::manager_type *Manager) { | |
97 Traits::set_current(Manager); | |
98 } | |
99 | |
100 ~sz_allocator_scope() { Traits::set_current(nullptr); } | |
101 }; | |
102 | |
103 template <typename T, typename U, typename Traits> | |
104 inline bool operator==(const sz_allocator<T, Traits> &, | |
105 const sz_allocator<U, Traits> &) { | |
106 return true; | |
107 } | |
108 | |
109 template <typename T, typename U, typename Traits> | |
110 inline bool operator!=(const sz_allocator<T, Traits> &, | |
111 const sz_allocator<U, Traits> &) { | |
112 return false; | |
113 } | |
114 | |
115 class CfgAllocatorTraits { | |
116 CfgAllocatorTraits() = delete; | |
117 CfgAllocatorTraits(const CfgAllocatorTraits &) = delete; | |
118 CfgAllocatorTraits &operator=(const CfgAllocatorTraits &) = delete; | |
119 ~CfgAllocatorTraits() = delete; | |
120 | |
121 public: | |
122 using allocator_type = ArenaAllocator *; | |
123 using manager_type = Cfg; | |
124 | |
125 static void init() { ICE_TLS_INIT_FIELD(CfgAllocator); }; | |
126 | |
127 static allocator_type current(); | |
128 static void set_current(const manager_type *Manager); | |
129 | |
130 private: | |
131 ICE_TLS_DECLARE_FIELD(ArenaAllocator *, CfgAllocator); | |
132 }; | |
133 | |
134 template <typename T> | |
135 using CfgLocalAllocator = sz_allocator<T, CfgAllocatorTraits>; | |
136 | |
137 using CfgLocalAllocatorScope = sz_allocator_scope<CfgAllocatorTraits>; | |
138 | |
139 } // end of namespace Ice | |
140 | |
141 #endif // SUBZERO_SRC_ICEMEMORY_H | |
OLD | NEW |