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-2015 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 "spxdefines.h"
27 #include "spxout.h"
28 
29 #include "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  p=pp;
105 }
106 
107 /// Release memory
108 template <class T>
109 inline void spx_free(T& p)
110 {
111  assert(p != 0);
112  free(p);
113 
114  p = 0;
115 }
116 
117 //@}
118 } // namespace soplex
119 
120 
121 #endif // _SPXALLOC_H_