1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the class library */
4 /* SoPlex --- the Sequential object-oriented simPlex. */
5 /* */
6 /* Copyright (C) 1996-2022 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
48 template <class T>
49 inline void spx_alloc(T& p, int n = 1)
50 {
51 assert(p == 0);
52 assert(n >= 0);
53
54 if(n == 0)
55 n = 1;
56
57 try
58 {
59 p = reinterpret_cast<T>(malloc(sizeof(*p) * (unsigned int) n));
60 }
61 catch(const std::bad_alloc&)
62 {
63 throw(SPxMemoryException("Error allocating memory"));
64 }
65
66 if(nullptr == p)
67 {
(1) Event suspicious_sizeof: |
Passing argument "std::cerr << "EMALLC01 malloc: Out of memory - cannot allocate "" of type "std::ostream *" and argument "8UL /* sizeof (*p) */ * (unsigned int)n" to function "operator <<" is suspicious. |
(2) Event remediation: |
Did you intend to use "sizeof (**p)" instead of "sizeof (*p)"? |
68 std::cerr << "EMALLC01 malloc: Out of memory - cannot allocate "
69 << sizeof(*p) * (unsigned int) n << " bytes" << std::endl; // coverity[suspicious_sizeof]
70 throw(SPxMemoryException("XMALLC01 malloc: Could not allocate enough memory"));
71 }
72 }
73
74 /**@brief Change amount of allocated memory.
75 * @param p some pointer
76 * @param n the number of elements p should point to.
77 * @throw SPxMemoryException if memory could not be allocated.
78 */
79 template <class T>
80 inline void spx_realloc(T& p, int n)
81 {
82 assert(n >= 0);
83
84 /* new pointer to not lose old one in case of problems */
85 T pp;
86
87 if(n == 0)
88 n = 1;
89
90 try
91 {
92 pp = reinterpret_cast<T>(realloc(p, sizeof(*p) * (unsigned int) n));
93 }
94 catch(const std::bad_alloc&)
95 {
96 throw(SPxMemoryException("Error reallocating memory"));
97 }
98
99 if(0 == pp)
100 {
101 std::cerr << "EMALLC02 realloc: Out of memory - cannot allocate "
102 << sizeof(*p) * (unsigned int) n << " bytes" << std::endl;
103 throw(SPxMemoryException("XMALLC02 realloc: Could not allocate enough memory"));
104 }
105
106 p = pp;
107 }
108
109 /// Release memory
110 template <class T>
111 inline void spx_free(T& p)
112 {
113 assert(p != 0);
114 free(p);
115
116 p = 0;
117 }
118
119 ///@}
120 } // namespace soplex
121
122
123 #endif // _SPXALLOC_H_
124