iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AllocationImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Shane Grant
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #ifndef INCLUDE_NRT_CORE_MEMORY_DETAILS_ALLOCATIONIMPL_H_
37 #define INCLUDE_NRT_CORE_MEMORY_DETAILS_ALLOCATIONIMPL_H_
38 
39 template <class T> inline
41 { }
42 
43 template <class T> inline
44 nrt::Allocator<T>::Allocator( Allocator const & other ) noexcept :
45  std::allocator<T>( other )
46 { }
47 
48 template <class T>
49 template <class U> inline
50 nrt::Allocator<T>::Allocator( Allocator<U> const & other ) noexcept
51 { }
52 
53 template <class T> inline
55 { }
56 
57 template <class T> inline
58 auto nrt::Allocator<T>::allocate( size_type n, const void * ) -> pointer
59 {
60  if ( n > this->max_size() )
61  std::__throw_bad_alloc();
62 
63  return static_cast<pointer>( nrt::allocate( n * sizeof( T ) ) );
64 }
65 
66 template <class T> inline
67 void nrt::Allocator<T>::deallocate( pointer p, size_type )
68 {
69  nrt::deallocate( p );
70 }
71 
72 // ############################################################################
73 
74 template <class T, size_t N>
76 
77 template <class T, size_t N>
79 
80 template <class T, size_t N> inline
82 { }
83 
84 template <class T, size_t N> inline
85 nrt::AlignedAllocator<T, N>::AlignedAllocator( AlignedAllocator const & other ) noexcept :
86  std::allocator<T>( other )
87 { }
88 
89 template <class T, size_t N>
90 template <class U> inline
91 nrt::AlignedAllocator<T, N>::AlignedAllocator( AlignedAllocator<U, N> const & other ) noexcept
92 { }
93 
94 template <class T, size_t N> inline
96 { }
97 
98 template <class T, size_t N> inline
99 auto nrt::AlignedAllocator<T, N>::allocate( size_type n, const void * ) -> pointer
100 {
101  if ( n > this->max_size() )
102  std::__throw_bad_alloc();
103 
104  std::lock_guard<std::mutex> lock( itsMutex );
105 
106  return static_cast<pointer>( itsAllocator.allocate( n * sizeof( T ) ) );
107 }
108 
109 template <class T, size_t N> inline
110 void nrt::AlignedAllocator<T, N>::deallocate( pointer p, size_type )
111 {
112  if( p == nullptr )
113  return;
114 
115  std::lock_guard<std::mutex> lock( itsMutex );
116 
117  itsAllocator.deallocate( p );
118 }
119 
120 // ############################################################################
121 namespace impl
122 {
123  template <class T, class ... Args>
124  std::unique_ptr<T> make_unique_helper( std::false_type, Args && ... args )
125  {
126  return std::unique_ptr<T>( new T( std::forward<Args>( args )... ) );
127  }
128 
129  template <class T, class ... Args>
130  std::unique_ptr<T> make_unique_helper( std::true_type, Args && ... args )
131  {
132  static_assert( std::extent<T>::value == 0,
133  "make_unique<T[N]>() is forbidden, use make_unique<T[]>() instead" );
134 
135  typedef typename std::remove_extent<T>::type U;
136 
137  return std::unique_ptr<T>( new U[sizeof...(Args)]{std::forward<Args>(args)...} );
138  }
139 }
140 
141 template <class T, class ... Args>
142 std::unique_ptr<T> nrt::make_unique( Args && ... args )
143 {
144  return impl::make_unique_helper<T>( std::is_array<T>(), std::forward<Args>( args )... );
145 }
146 
147 
148 #endif // INCLUDE_NRT_CORE_MEMORY_DETAILS_ALLOCATIONIMPL_H_