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-2025 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, size_t n = 1)
59{
60 assert(p == nullptr);
61
62 if(n == 0)
63 n = 1;
64
65 try
66 {
67 p = reinterpret_cast<T>(malloc(sizeof(*p) * n));
68 }
69 catch(const std::bad_alloc&)
70 {
71 throw(SPxMemoryException("Error allocating memory"));
72 }
73
74 if(p == nullptr)
75 {
76 // coverity[suspicious_sizeof]
77 std::cerr << "EMALLC01 malloc: Out of memory - cannot allocate "
78 << sizeof(*p) * n << " bytes" << std::endl;
79 throw(SPxMemoryException("XMALLC01 malloc: Could not allocate enough memory"));
80 }
81}
82
83/**@brief Change amount of allocated memory.
84 * @param p some pointer
85 * @param n the number of elements p should point to.
86 * @throw SPxMemoryException if memory could not be allocated.
87 */
88template <class T>
89inline void spx_realloc(T& p, size_t n)
90{
91 /* new pointer to not lose old one in case of problems */
92 T pp = nullptr;
93
94 if(n == 0)
95 n = 1;
96
97 try
98 {
99 pp = reinterpret_cast<T>(realloc(p, sizeof(*p) * n));
100 }
101 catch(const std::bad_alloc&)
102 {
103 throw(SPxMemoryException("Error reallocating memory"));
104 }
105
106 if(pp == nullptr)
107 {
108 std::cerr << "EMALLC02 realloc: Out of memory - cannot allocate "
109 << sizeof(*p) * n << " bytes" << std::endl;
110 throw(SPxMemoryException("XMALLC02 realloc: Could not allocate enough memory"));
111 }
112
113 p = pp;
114}
115
116/// Release memory
117template <class T>
118inline void spx_free(T& p)
119{
120 assert(p != nullptr);
121 free(p);
122
123 p = nullptr;
124}
125
126///@}
127} // namespace soplex
128
129
130#endif // _SPXALLOC_H_
Exception class for out of memory exceptions.
Definition: exceptions.h:82
Exception classes for SoPlex.
Everything should be within this namespace.
void spx_realloc(T &p, size_t n)
Change amount of allocated memory.
Definition: spxalloc.h:89
void spx_free(T &p)
Release memory.
Definition: spxalloc.h:118
void spx_alloc(T &p, size_t n=1)
Allocate memory.
Definition: spxalloc.h:58
Debugging, floating point type and parameter definitions.
Wrapper for different output streams and verbosity levels.