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 
62 inline void *
63 operator new ( size_t n, aa_mem_region_t *reg )
64 {
65  return aa_mem_region_alloc( reg, n );
66 }
67 
68 namespace amino {
69 
73 template<class T>
75 
76 public :
77  aa_mem_region *region;
78 
79  /* Typedefs */
80  typedef T value_type;
81  typedef value_type* pointer;
82  typedef const value_type* const_pointer;
83  typedef value_type& reference;
84  typedef const value_type& const_reference;
85  typedef std::size_t size_type;
86  typedef std::ptrdiff_t difference_type;
87 
88  /* Rebind */
89  template<class U>
90  struct rebind {
91  typedef RegionAllocator<U> other;
92  };
93 
94  /* This won't work! */
95  // inline RegionAllocator() {
96  // abort();
97  // }
98 
99  inline RegionAllocator(aa_mem_region_t *r) :
100  region(r) {}
101 
102  inline RegionAllocator(const RegionAllocator &other) :
103  region(other.region) {}
104 
105  template<class U>
106  inline RegionAllocator( const RegionAllocator<U> & other) :
107  region(other.region) {}
108 
109  inline ~RegionAllocator() {}
110 
111  inline pointer address(reference r) { return &r; }
112  inline const_pointer address(const_reference r) { return &r; }
113 
114  inline pointer allocate(size_type cnt,
115  typename std::allocator<void>::const_pointer = 0) {
116  return reinterpret_cast<pointer>( aa_mem_region_alloc(region, cnt*sizeof(value_type)) );
117  }
118  inline void deallocate(pointer p, size_type) { /* nop */ }
119 
120  inline size_type max_size() const {
121  return std::numeric_limits<size_type>::max() / sizeof(T);
122 
123  }
124 
125  template <class U>
126  RegionAllocator& operator=(const RegionAllocator<U>&) { return *this; }
127 
128 
129  inline void construct(pointer p, const T& t) { new(p) T(t); }
130  inline void destroy(pointer p) { p->~T(); }
131 
132  inline bool operator==(RegionAllocator const& a) { return region == a.region; }
133  inline bool operator!=(RegionAllocator const& a) { return !operator==(a); }
134 
135 };
136 
140 template<class T>
142 {
143  typedef T value_type;
145  typedef std::list<T, allocator > type;
146  typedef typename type::iterator iterator;
147 };
148 
152 template<class T>
154 {
155  typedef T value_type;
157  typedef std::vector<T, allocator > type;
158  typedef typename type::iterator iterator;
159 };
160 
161 
165 template<class K, class T, class C=std::less<K> >
166 struct RegionMap
167 {
168  typedef std::pair<const K, T> value_type;
170  typedef std::map<K, T, C, allocator > type;
171  typedef typename type::iterator iterator;
172 };
173 
174 }
175 
176 
177 
178 #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.
Typedefs for STL map using region allocator.
Definition: mem.hpp:166
Data Structure for Region-Based memory allocation.
Definition: mem.h:198
Typedefs for STL vector using region allocator.
Definition: mem.hpp:153
amino namespace
Definition: amino.hpp:60
Typedefs for STL lists using region allocator.
Definition: mem.hpp:141
An STL allocator that allocates out of a memory region.
Definition: mem.hpp:74