amino  1.0-beta2
Lightweight Robot Utility Library
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  * Copyright (c) 2019, Colorado School of Mines
6  * All rights reserved.
7  *
8  * Author(s): Neil T. Dantam <ntd@gatech.edu>
9  * Georgia Tech Humanoid Robotics Lab
10  * Under Direction of Prof. Mike Stilman <mstilman@cc.gatech.edu>
11  *
12  *
13  * This file is provided under the following "BSD-style" License:
14  *
15  *
16  * Redistribution and use in source and binary forms, with or
17  * without modification, are permitted provided that the following
18  * conditions are met:
19  *
20  * * Redistributions of source code must retain the above copyright
21  * notice, this list of conditions and the following disclaimer.
22  *
23  * * Redistributions in binary form must reproduce the above
24  * copyright notice, this list of conditions and the following
25  * disclaimer in the documentation and/or other materials provided
26  * with the distribution.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
29  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
30  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
33  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
36  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
37  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
39  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40  * POSSIBILITY OF SUCH DAMAGE.
41  *
42  */
43 
44 #ifndef AA_MEM_HPP
45 #define AA_MEM_HPP
46 
47 #include <memory>
48 #include <limits>
49 #include <list>
50 #include <vector>
51 #include <map>
52 
63 inline void *
64 operator new ( size_t n, aa_mem_region_t *reg )
65 {
66  return aa_mem_region_alloc( reg, n );
67 }
68 
73 inline void *
74 operator new[] ( size_t n, aa_mem_region_t *reg )
75 {
76  return aa_mem_region_alloc( reg, n );
77 }
78 
79 namespace amino {
80 
81 class RegionScope {
82  public:
83  RegionScope (struct aa_mem_region *r) :
84  reg_(r),
85  ptr_(aa_mem_region_ptr(reg_))
86  {}
87 
88  RegionScope () :
90  ptr_(aa_mem_region_ptr(reg_))
91  {}
92 
93  ~RegionScope() {
94  aa_mem_region_pop(reg_,ptr_);
95  }
96 
97  operator aa_mem_region*() const { return reg(); }
98 
99  struct aa_mem_region *reg() const {return reg_;}
100 
101 private:
102  struct aa_mem_region * const reg_;
103  void * const ptr_;
104 };
105 
106 
110 template<class T>
112 
113 public :
114  aa_mem_region *region;
115 
116  /* Typedefs */
117  typedef T value_type;
118  typedef value_type* pointer;
119  typedef const value_type* const_pointer;
120  typedef value_type& reference;
121  typedef const value_type& const_reference;
122  typedef std::size_t size_type;
123  typedef std::ptrdiff_t difference_type;
124 
125  /* Rebind */
126  template<class U>
127  struct rebind {
128  typedef RegionAllocator<U> other;
129  };
130 
131  /* This won't work! */
132  // inline RegionAllocator() {
133  // abort();
134  // }
135 
136  inline RegionAllocator(aa_mem_region_t *r) :
137  region(r) {}
138 
139  inline RegionAllocator(const RegionAllocator &other) :
140  region(other.region) {}
141 
142  template<class U>
143  inline RegionAllocator( const RegionAllocator<U> & other) :
144  region(other.region) {}
145 
146  inline ~RegionAllocator() {}
147 
148  inline pointer address(reference r) { return &r; }
149  inline const_pointer address(const_reference r) { return &r; }
150 
151  inline pointer allocate(size_type cnt,
152  typename std::allocator<void>::const_pointer = 0) {
153  return reinterpret_cast<pointer>( aa_mem_region_alloc(region, cnt*sizeof(value_type)) );
154  }
155  inline void deallocate(pointer p, size_type) { /* nop */ }
156 
157  inline size_type max_size() const {
158  return std::numeric_limits<size_type>::max() / sizeof(T);
159 
160  }
161 
162  template <class U>
163  RegionAllocator& operator=(const RegionAllocator<U>&) { return *this; }
164 
165 
166  inline void construct(pointer p, const T& t) { new(static_cast<void*>(p)) T(t); }
167  inline void destroy(pointer p) { p->~T(); }
168 
169  inline bool operator==(RegionAllocator const& a) { return region == a.region; }
170  inline bool operator!=(RegionAllocator const& a) { return !operator==(a); }
171 
172 };
173 
177 template<class T>
179 {
180  typedef T value_type;
182  typedef std::list<T, allocator > type;
183  typedef typename type::iterator iterator;
184 };
185 
189 template<class T>
191 {
192  typedef T value_type;
194  typedef std::vector<T, allocator > type;
195  typedef typename type::iterator iterator;
196 };
197 
198 
202 template<class K, class T, class C=std::less<K> >
203 struct RegionMap
204 {
205  typedef std::pair<const K, T> value_type;
207  typedef std::map<K, T, C, allocator > type;
208  typedef typename type::iterator iterator;
209 };
210 
211 }
212 
213 
214 
215 #endif //AA_MEM_HPP
An STL allocator that allocates out of a memory region.
Definition: mem.hpp:111
AA_API void aa_mem_region_pop(aa_mem_region_t *region, void *ptr)
Deallocates ptr and all blocks allocated after ptr was allocated.
AA_API aa_mem_region_t * aa_mem_region_local_get(void)
Return pointer to a thread-local memory region.
AA_API void * aa_mem_region_ptr(aa_mem_region_t *region)
Pointer to start of free space in region.
AA_API void * aa_mem_region_alloc(aa_mem_region_t *region, size_t size)
Allocate size bytes from the region.
amino namespace
Definition: amino.hpp:60
Data Structure for Region-Based memory allocation.
Definition: mem.h:199
Typedefs for STL lists using region allocator.
Definition: mem.hpp:179
Typedefs for STL map using region allocator.
Definition: mem.hpp:204
Typedefs for STL vector using region allocator.
Definition: mem.hpp:191