SCIP-SDP  3.2.0
BlockMemoryAllocator.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of SCIPSDP - a solving framework for mixed-integer */
4 /* semidefinite programs based on SCIP. */
5 /* */
6 /* Copyright (C) 2011-2013 Discrete Optimization, TU Darmstadt */
7 /* EDOM, FAU Erlangen-Nürnberg */
8 /* 2014-2020 Discrete Optimization, TU Darmstadt */
9 /* */
10 /* */
11 /* This program is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public License */
13 /* as published by the Free Software Foundation; either version 3 */
14 /* of the License, or (at your option) any later version. */
15 /* */
16 /* This program is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
19 /* GNU Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public License */
22 /* along with this program; if not, write to the Free Software */
23 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.*/
24 /* */
25 /* */
26 /* Based on SCIP - Solving Constraint Integer Programs */
27 /* Copyright (C) 2002-2020 Zuse Institute Berlin */
28 /* SCIP is distributed under the terms of the SCIP Academic Licence, */
29 /* see file COPYING in the SCIP distribution. */
30 /* */
31 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
32 
38 #ifndef BLOCKMEMORYALLOCATOR_H
39 #define BLOCKMEMORYALLOCATOR_H
40 
45 template <class T> class BlockMemoryAllocator
46 {
47 public:
48  typedef T value_type;
49  typedef value_type* pointer;
50  typedef const value_type* const_pointer;
51  typedef value_type& reference;
52  typedef const value_type& const_reference;
53  typedef std::size_t size_type;
54  typedef std::ptrdiff_t difference_type;
55 
56  template <class U>
57  struct rebind { typedef BlockMemoryAllocator<U> other; };
58 
59  BlockMemoryAllocator(SCIP* scip) : scip_(scip) {}
60 
62 
63  template <class U>
65 
67  {
68  scip_ = NULL;
69  }
70 
71  pointer address(reference x) const { return &x; }
72 
73  const_pointer address(const_reference x) const
74  {
75  return x;
76  }
77 
78  pointer allocate(size_type n, const_pointer = 0)
79  {
80  void* p;
81  SCIP_CALL_ABORT(SCIPallocBlockMemorySize(scip_, &p, n * sizeof(T)));
82 
83  if ( ! p )
84  throw std::bad_alloc();
85  return static_cast<pointer>(p);
86  }
87 
88  void deallocate(pointer p, size_type n)
89  {
90  SCIPfreeBlockMemorySize(scip_, &p, n * sizeof(T));
91  }
92 
93  size_type max_size() const
94  {
95  return static_cast<size_type>(-1) / sizeof(T);
96  }
97 
98  void construct(pointer p, const value_type& x)
99  {
100  new(p) value_type(x);
101  }
102 
103  void destroy(pointer p)
104  {
105  p->~value_type();
106  }
107 
108  template<typename S> friend inline bool operator==(const BlockMemoryAllocator<S>& left, const BlockMemoryAllocator<S>& right);
109  template<typename S> friend inline bool operator!=(const BlockMemoryAllocator<S>& left, const BlockMemoryAllocator<S>& right);
110 
112  {
113  scip_ = b.scip_;
114  return *this;
115  }
116 
117 private:
118  SCIP* scip_;
119 };
120 
121 template<> class BlockMemoryAllocator<void>
122 {
123  typedef void value_type;
124  typedef void* pointer;
125  typedef const void* const_pointer;
126 
127  template <class U>
128  struct rebind { typedef BlockMemoryAllocator<U> other; };
129 };
130 
131 
132 template <class T>
133 inline bool operator==(const BlockMemoryAllocator<T>& left,
134  const BlockMemoryAllocator<T>& right)
135 {
136  return left.scip_ == right.scip_;
137 }
138 
139 template <class T>
140 inline bool operator!=(const BlockMemoryAllocator<T>& left,
141  const BlockMemoryAllocator<T>& right)
142 {
143  return !(left == right);
144 }
145 
146 #endif
BlockMemoryAllocator & operator=(BlockMemoryAllocator const &b)
const_pointer address(const_reference x) const
const value_type & const_reference
friend bool operator==(const BlockMemoryAllocator< S > &left, const BlockMemoryAllocator< S > &right)
friend bool operator!=(const BlockMemoryAllocator< S > &left, const BlockMemoryAllocator< S > &right)
void construct(pointer p, const value_type &x)
BlockMemoryAllocator(const BlockMemoryAllocator< U > &other)
pointer address(reference x) const
const value_type * const_pointer
std::ptrdiff_t difference_type
void deallocate(pointer p, size_type n)
pointer allocate(size_type n, const_pointer=0)
size_type max_size() const
BlockMemoryAllocator< U > other
BlockMemoryAllocator(const BlockMemoryAllocator &other)