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-2024 Zuse Institute Berlin (ZIB) */
7/* */
8/* Licensed under the Apache License, Version 2.0 (the "License"); */
9/* you may not use this file except in compliance with the License. */
10/* You may obtain a copy of the License at */
11/* */
12/* http://www.apache.org/licenses/LICENSE-2.0 */
13/* */
14/* Unless required by applicable law or agreed to in writing, software */
15/* distributed under the License is distributed on an "AS IS" BASIS, */
16/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17/* See the License for the specific language governing permissions and */
18/* limitations under the License. */
19/* */
20/* You should have received a copy of the Apache-2.0 license */
21/* along with SoPlex; see the file LICENSE. If not email to soplex@zib.de. */
22/* */
23/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24
25/**@file spxalloc.h
26 * @brief Memory allocation routines.
27 */
28#ifndef _SPXALLOC_H_
29#define _SPXALLOC_H_
30
31#include <iostream>
32#include <stdlib.h>
33#include <assert.h>
34
35#include "soplex/spxdefines.h"
36#include "soplex/spxout.h"
37
38#include "soplex/exceptions.h"
39
40namespace soplex
41{
42/**@name Memory allocation routines
43 * @ingroup Elementary
44 * Here we have cover functions for malloc/realloc/free, to make sure
45 * that we allays succeed. Otherwise an exception is thrown.
46 *
47 * We use templates to get the types right, otherwise casts would have
48 * been neccessary.
49 */
50///@{
51/**@brief Allocate memory.
52 * @param p some pointer
53 * @param n the number of elements \p p will point to.
54 * @throw SPxMemoryException if memory could not be allocated.
55 */
56
57template <class T>
58inline void spx_alloc(T& p, int n = 1)
59{
60 assert(p == nullptr);
61 assert(n >= 0);
62
63 if(n == 0)
64 n = 1;
65
66 try
67 {
68 p = reinterpret_cast<T>(malloc(sizeof(*p) * (unsigned int) n));
69 }
70 catch(const std::bad_alloc&)
71 {
72 throw(SPxMemoryException("Error allocating memory"));
73 }
74
75 if(nullptr == p)
76 {
77 // coverity[suspicious_sizeof]
78 std::cerr << "EMALLC01 malloc: Out of memory - cannot allocate "
79 << sizeof(*p) * (unsigned int) n << " bytes" << std::endl;
80 throw(SPxMemoryException("XMALLC01 malloc: Could not allocate enough memory"));
81 }
82}
83
84/**@brief Change amount of allocated memory.
85 * @param p some pointer
86 * @param n the number of elements p should point to.
87 * @throw SPxMemoryException if memory could not be allocated.
88 */
89template <class T>
90inline void spx_realloc(T& p, int n)
91{
92 assert(n >= 0);
93
94 /* new pointer to not lose old one in case of problems */
95 T pp;
96
97 if(n == 0)
98 n = 1;
99
100 try
101 {
102 pp = reinterpret_cast<T>(realloc(p, sizeof(*p) * (unsigned int) n));
103 }
104 catch(const std::bad_alloc&)
105 {
106 throw(SPxMemoryException("Error reallocating memory"));
107 }
108
109 if(nullptr == pp)
110 {
111 std::cerr << "EMALLC02 realloc: Out of memory - cannot allocate "
112 << sizeof(*p) * (unsigned int) n << " bytes" << std::endl;
113 throw(SPxMemoryException("XMALLC02 realloc: Could not allocate enough memory"));
114 }
115
116 p = pp;
117}
118
119/// Release memory
120template <class T>
121inline void spx_free(T& p)
122{
123 assert(p != nullptr);
124 free(p);
125
126 p = nullptr;
127}
128
129///@}
130} // namespace soplex
131
132
133#endif // _SPXALLOC_H_
Exception class for out of memory exceptions.
Definition: exceptions.h:80
Exception classes for SoPlex.
Everything should be within this namespace.
void spx_realloc(T &p, int n)
Change amount of allocated memory.
Definition: spxalloc.h:90
void spx_free(T &p)
Release memory.
Definition: spxalloc.h:121
void spx_alloc(T &p, int n=1)
Allocate memory.
Definition: spxalloc.h:58
Debugging, floating point type and parameter definitions.
Wrapper for different output streams and verbosity levels.