1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 2 /* */ 3 /* This file is part of the class library */ 4 /* SoPlex --- the Sequential object-oriented simPlex. */ 5 /* */ 6 /* Copyright (c) 1996-2023 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 40 namespace 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 57 template <class T> 58 inline void spx_alloc(T& p, int n = 1) 59 { 60 assert(p == 0); 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 */ 89 template <class T> 90 inline 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(0 == 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 120 template <class T> 121 inline void spx_free(T& p) 122 { 123 assert(p != 0); 124 free(p); 125 126 p = 0; 127 } 128 129 ///@} 130 } // namespace soplex 131 132 133 #endif // _SPXALLOC_H_ 134