SCIP-SDP  2.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 programms based on SCIP. */
5 /* */
6 /* Copyright (C) 2011-2013 Discrete Optimization, TU Darmstadt */
7 /* EDOM, FAU Erlangen-Nürnberg */
8 /* 2014-2016 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-2016 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;
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  template <class U>
65  {
66  scip_ = NULL;
67  }
68 
69  pointer address(reference x) const { return &x; }
70 
72  {
73  return x;
74  }
75 
77  {
78  void* p;
79  SCIP_CALL_ABORT(SCIPallocBlockMemorySize(scip_, &p, n * sizeof(T)));
80 
81  if (!p)
82  throw std::bad_alloc();
83  return static_cast<pointer>(p);
84  }
85 
87  {
88  SCIPfreeBlockMemorySize(scip_, &p, n * sizeof(T));
89  }
90 
91  size_type max_size() const {
92  return static_cast<size_type>(-1) / sizeof(T);
93  }
94 
95  void construct(pointer p, const value_type& x) {
96  new(p) value_type(x);
97  }
98  void destroy(pointer p) { p->~value_type(); }
99 
100  template<typename S> friend inline bool operator==(const BlockMemoryAllocator<S>& left, const BlockMemoryAllocator<S>& right);
101  template<typename S> friend inline bool operator!=(const BlockMemoryAllocator<S>& left, const BlockMemoryAllocator<S>& right);
102 
104  {
105  scip_ = b.scip_;
106  }
107 
108  private:
109  SCIP* scip_;
110 };
111 
112 template<> class BlockMemoryAllocator<void>
113 {
114  typedef void value_type;
115  typedef void* pointer;
116  typedef const void* const_pointer;
117 
118  template <class U>
119  struct rebind { typedef BlockMemoryAllocator<U> other; };
120 };
121 
122 
123 template <class T>
124 inline bool operator==(const BlockMemoryAllocator<T>& left,
125  const BlockMemoryAllocator<T>& right) {
126  return left.scip_ == right.scip_;
127 }
128 
129 template <class T>
130 inline bool operator!=(const BlockMemoryAllocator<T>& left,
131  const BlockMemoryAllocator<T>& right) {
132  return !(left == right);
133 }
134 
135 #endif
const_pointer address(const_reference x) const
void operator=(BlockMemoryAllocator const &b)
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)
bool operator==(const BlockMemoryAllocator< T > &left, const BlockMemoryAllocator< T > &right)
bool operator!=(const BlockMemoryAllocator< T > &left, const BlockMemoryAllocator< T > &right)