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  struct aa_mem_region *reg() {return reg_;}
98 private:
99  struct aa_mem_region *reg_;
100  void *ptr_;
101 };
102 
103 
107 template<class T>
109 
110 public :
111  aa_mem_region *region;
112 
113  /* Typedefs */
114  typedef T value_type;
115  typedef value_type* pointer;
116  typedef const value_type* const_pointer;
117  typedef value_type& reference;
118  typedef const value_type& const_reference;
119  typedef std::size_t size_type;
120  typedef std::ptrdiff_t difference_type;
121 
122  /* Rebind */
123  template<class U>
124  struct rebind {
125  typedef RegionAllocator<U> other;
126  };
127 
128  /* This won't work! */
129  // inline RegionAllocator() {
130  // abort();
131  // }
132 
133  inline RegionAllocator(aa_mem_region_t *r) :
134  region(r) {}
135 
136  inline RegionAllocator(const RegionAllocator &other) :
137  region(other.region) {}
138 
139  template<class U>
140  inline RegionAllocator( const RegionAllocator<U> & other) :
141  region(other.region) {}
142 
143  inline ~RegionAllocator() {}
144 
145  inline pointer address(reference r) { return &r; }
146  inline const_pointer address(const_reference r) { return &r; }
147 
148  inline pointer allocate(size_type cnt,
149  typename std::allocator<void>::const_pointer = 0) {
150  return reinterpret_cast<pointer>( aa_mem_region_alloc(region, cnt*sizeof(value_type)) );
151  }
152  inline void deallocate(pointer p, size_type) { /* nop */ }
153 
154  inline size_type max_size() const {
155  return std::numeric_limits<size_type>::max() / sizeof(T);
156 
157  }
158 
159  template <class U>
160  RegionAllocator& operator=(const RegionAllocator<U>&) { return *this; }
161 
162 
163  inline void construct(pointer p, const T& t) { new(p) T(t); }
164  inline void destroy(pointer p) { p->~T(); }
165 
166  inline bool operator==(RegionAllocator const& a) { return region == a.region; }
167  inline bool operator!=(RegionAllocator const& a) { return !operator==(a); }
168 
169 };
170 
174 template<class T>
176 {
177  typedef T value_type;
179  typedef std::list<T, allocator > type;
180  typedef typename type::iterator iterator;
181 };
182 
186 template<class T>
188 {
189  typedef T value_type;
191  typedef std::vector<T, allocator > type;
192  typedef typename type::iterator iterator;
193 };
194 
195 
199 template<class K, class T, class C=std::less<K> >
200 struct RegionMap
201 {
202  typedef std::pair<const K, T> value_type;
204  typedef std::map<K, T, C, allocator > type;
205  typedef typename type::iterator iterator;
206 };
207 
208 }
209 
210 
211 
212 #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:200
Data Structure for Region-Based memory allocation.
Definition: mem.h:198
AA_API aa_mem_region_t * aa_mem_region_local_get(void)
Return pointer to a thread-local memory region.
Typedefs for STL vector using region allocator.
Definition: mem.hpp:187
amino namespace
Definition: amino.hpp:60
Typedefs for STL lists using region allocator.
Definition: mem.hpp:175
AA_API void * aa_mem_region_ptr(aa_mem_region_t *region)
Pointer to start of free space in region.
An STL allocator that allocates out of a memory region.
Definition: mem.hpp:108
AA_API void aa_mem_region_pop(aa_mem_region_t *region, void *ptr)
Deallocates ptr and all blocks allocated after ptr was allocated.