amino
Lightweight Robot Utility Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mem.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 4 -*- */
2 /* ex: set shiftwidth=4 tabstop=4 expandtab: */
3 /*
4  * Copyright (c) 2013, Georgia Tech Research Corporation
5  * All rights reserved.
6  *
7  * Author(s): Neil T. Dantam <ntd@gatech.edu>
8  * Georgia Tech Humanoid Robotics Lab
9  * Under Direction of Prof. Mike Stilman <mstilman@cc.gatech.edu>
10  *
11  *
12  * This file is provided under the following "BSD-style" License:
13  *
14  *
15  * Redistribution and use in source and binary forms, with or
16  * without modification, are permitted provided that the following
17  * conditions are met:
18  *
19  * * Redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer.
21  *
22  * * Redistributions in binary form must reproduce the above
23  * copyright notice, this list of conditions and the following
24  * disclaimer in the documentation and/or other materials provided
25  * with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
28  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
29  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
35  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
36  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  *
41  */
42 
43 #ifndef AA_MEM_HPP
44 #define AA_MEM_HPP
45 
46 #include <memory>
47 #include <limits>
48 #include <list>
49 #include <vector>
50 #include <map>
51 
61 inline void *
62 operator new ( size_t n, aa_mem_region_t *reg )
63 {
64  return aa_mem_region_alloc( reg, n );
65 }
66 
67 namespace amino {
68 
72 template<class T>
74 
75 public :
76  aa_mem_region *region;
77 
78  /* Typedefs */
79  typedef T value_type;
80  typedef value_type* pointer;
81  typedef const value_type* const_pointer;
82  typedef value_type& reference;
83  typedef const value_type& const_reference;
84  typedef std::size_t size_type;
85  typedef std::ptrdiff_t difference_type;
86 
87  /* Rebind */
88  template<class U>
89  struct rebind {
90  typedef RegionAllocator<U> other;
91  };
92 
93  /* This won't work! */
94  // inline RegionAllocator() {
95  // abort();
96  // }
97 
98  inline RegionAllocator(aa_mem_region_t *r) :
99  region(r) {}
100 
101  inline RegionAllocator(const RegionAllocator &other) :
102  region(other.region) {}
103 
104  template<class U>
105  inline RegionAllocator( const RegionAllocator<U> & other) :
106  region(other.region) {}
107 
108  inline ~RegionAllocator() {}
109 
110  inline pointer address(reference r) { return &r; }
111  inline const_pointer address(const_reference r) { return &r; }
112 
113  inline pointer allocate(size_type cnt,
114  typename std::allocator<void>::const_pointer = 0) {
115  return reinterpret_cast<pointer>( aa_mem_region_alloc(region, cnt*sizeof(value_type)) );
116  }
117  inline void deallocate(pointer p, size_type) { /* nop */ }
118 
119  inline size_type max_size() const {
120  return std::numeric_limits<size_type>::max() / sizeof(T);
121 
122  }
123 
124  template <class U>
125  RegionAllocator& operator=(const RegionAllocator<U>&) { return *this; }
126 
127 
128  inline void construct(pointer p, const T& t) { new(p) T(t); }
129  inline void destroy(pointer p) { p->~T(); }
130 
131  inline bool operator==(RegionAllocator const& a) { return region == a.region; }
132  inline bool operator!=(RegionAllocator const& a) { return !operator==(a); }
133 
134 };
135 
136 /* Typedefs for STL lists using region allocator */
137 template<class T>
139 {
140  typedef T value_type;
142  typedef std::list<T, allocator > type;
143  typedef typename type::iterator iterator;
144 };
145 
146 /* Typedefs for STL vector using region allocator */
147 template<class T>
149 {
150  typedef T value_type;
152  typedef std::vector<T, allocator > type;
153  typedef typename type::iterator iterator;
154 };
155 
156 
157 /* Typedefs for STL map using region allocator */
158 template<class K, class T, class C=std::less<K> >
159 struct RegionMap
160 {
161  typedef std::pair<const K, T> value_type;
163  typedef std::map<K, T, C, allocator > type;
164  typedef typename type::iterator iterator;
165 };
166 
167 }
168 
169 
170 
171 #endif //AA_MEM_HPP
AA_API void * aa_mem_region_alloc(aa_mem_region_t *region, size_t size)
Allocate size bytes from the region.
Data Structure for Region-Based memory allocation.
Definition: mem.h:197
amino namespace
Definition: amino.hpp:60
An STL allocator that allocates out of a memory region.
Definition: mem.hpp:73