Scippy

SoPlex

Sequential object-oriented simPlex

spxalloc.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the class library */
4 /* SoPlex --- the Sequential object-oriented simPlex. */
5 /* */
6 /* Copyright (C) 1996-2019 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SoPlex is distributed under the terms of the ZIB Academic Licence. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SoPlex; see the file COPYING. If not email to soplex@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file spxalloc.h
17  * @brief Memory allocation routines.
18  */
19 #ifndef _SPXALLOC_H_
20 #define _SPXALLOC_H_
21 
22 #include <iostream>
23 #include <stdlib.h>
24 #include <assert.h>
25 
26 #include "soplex/spxdefines.h"
27 #include "soplex/spxout.h"
28 
29 #include "soplex/exceptions.h"
30 
31 namespace soplex
32 {
33 /**@name Memory allocation routines
34  * @ingroup Elementary
35  * Here we have cover functions for malloc/realloc/free, to make sure
36  * that we allays succeed. Otherwise an exception is thrown.
37  *
38  * We use templates to get the types right, otherwise casts would have
39  * been neccessary.
40  */
41 //@{
42 /**@brief Allocate memory.
43  * @param p some pointer
44  * @param n the number of elements \p p will point to.
45  * @throw SPxMemoryException if memory could not be allocated.
46  */
47 template <class T>
48 inline void spx_alloc(T& p, int n = 1)
49 {
50  assert(p == 0);
51  assert(n >= 0);
52 
53  if(n == 0)
54  n = 1;
55 
56  try
57  {
58  p = reinterpret_cast<T>(malloc(sizeof(*p) * (unsigned int) n));
59  }
60  catch(const std::bad_alloc&)
61  {
62  throw(SPxMemoryException("Error allocating memory"));
63  }
64 
65  if(0 == p)
66  {
67  std::cerr << "EMALLC01 malloc: Out of memory - cannot allocate "
68  << sizeof(*p) * (unsigned int) n << " bytes" << std::endl;
69  throw(SPxMemoryException("XMALLC01 malloc: Could not allocate enough memory"));
70  }
71 }
72 
73 /**@brief Change amount of allocated memory.
74  * @param p some pointer
75  * @param n the number of elements p should point to.
76  * @throw SPxMemoryException if memory could not be allocated.
77  */
78 template <class T>
79 inline void spx_realloc(T& p, int n)
80 {
81  assert(n >= 0);
82 
83  /* new pointer to not lose old one in case of problems */
84  T pp;
85 
86  if(n == 0)
87  n = 1;
88 
89  try
90  {
91  pp = reinterpret_cast<T>(realloc(p, sizeof(*p) * (unsigned int) n));
92  }
93  catch(const std::bad_alloc&)
94  {
95  throw(SPxMemoryException("Error reallocating memory"));
96  }
97 
98  if(0 == pp)
99  {
100  std::cerr << "EMALLC02 realloc: Out of memory - cannot allocate "
101  << sizeof(*p) * (unsigned int) n << " bytes" << std::endl;
102  throw(SPxMemoryException("XMALLC02 realloc: Could not allocate enough memory"));
103  }
104 
105  p = pp;
106 }
107 
108 /// Release memory
109 template <class T>
110 inline void spx_free(T& p)
111 {
112  assert(p != 0);
113  free(p);
114 
115  p = 0;
116 }
117 
118 //@}
119 } // namespace soplex
120 
121 
122 #endif // _SPXALLOC_H_
Exception classes for SoPlex.
Wrapper for different output streams and verbosity levels.
Exception class for out of memory exceptions.This class is derived from the SoPlex exception base cla...
Definition: exceptions.h:70
void spx_alloc(T &p, int n=1)
Allocate memory.
Definition: spxalloc.h:48
Debugging, floating point type and parameter definitions.
void spx_realloc(T &p, int n)
Change amount of allocated memory.
Definition: spxalloc.h:79
Everything should be within this namespace.
void spx_free(T &p)
Release memory.
Definition: spxalloc.h:110