-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathhypre_memory.c
181 lines (154 loc) · 3.94 KB
/
hypre_memory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*BHEADER**********************************************************************
* Copyright (c) 2017, Lawrence Livermore National Security, LLC.
* Produced at the Lawrence Livermore National Laboratory.
* Written by Ulrike Yang ([email protected]) et al. CODE-LLNL-738-322.
* This file is part of AMG. See files README and COPYRIGHT for details.
*
* AMG is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License (as published by the Free
* Software Foundation) version 2.1 dated February 1999.
*
* This software is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF MERCHANTIBILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the terms and conditions of the
* GNU General Public License for more details.
*
***********************************************************************EHEADER*/
/******************************************************************************
*
* Memory management utilities
*
*****************************************************************************/
#include "_hypre_utilities.h"
#ifdef HYPRE_USE_UMALLOC
#undef HYPRE_USE_UMALLOC
#endif
/******************************************************************************
*
* Standard routines
*
*****************************************************************************/
/*--------------------------------------------------------------------------
* hypre_OutOfMemory
*--------------------------------------------------------------------------*/
HYPRE_Int
hypre_OutOfMemory( size_t size )
{
hypre_printf("Out of memory trying to allocate %d bytes\n", (HYPRE_Int) size);
fflush(stdout);
hypre_error(HYPRE_ERROR_MEMORY);
return 0;
}
/*--------------------------------------------------------------------------
* hypre_MAlloc
*--------------------------------------------------------------------------*/
char *
hypre_MAlloc( size_t size )
{
void *ptr;
if (size > 0)
{
#ifdef HYPRE_USE_UMALLOC
HYPRE_Int threadid = hypre_GetThreadID();
ptr = _umalloc_(size);
#else
ptr = malloc(size);
#endif
#if 1
if (ptr == NULL)
{
hypre_OutOfMemory(size);
}
#endif
}
else
{
ptr = NULL;
}
return (char*)ptr;
}
/*--------------------------------------------------------------------------
* hypre_CAlloc
*--------------------------------------------------------------------------*/
char *
hypre_CAlloc( size_t count,
size_t elt_size )
{
void *ptr;
size_t size = count*elt_size;
if (size > 0)
{
#ifdef HYPRE_USE_UMALLOC
HYPRE_Int threadid = hypre_GetThreadID();
ptr = _ucalloc_(count, elt_size);
#else
ptr = calloc(count, elt_size);
#endif
#if 1
if (ptr == NULL)
{
hypre_OutOfMemory(size);
}
#endif
}
else
{
ptr = NULL;
}
return(char*) ptr;
}
/*--------------------------------------------------------------------------
* hypre_ReAlloc
*--------------------------------------------------------------------------*/
char *
hypre_ReAlloc( char *ptr,
size_t size )
{
#ifdef HYPRE_USE_UMALLOC
if (ptr == NULL)
{
ptr = hypre_MAlloc(size);
}
else if (size == 0)
{
hypre_Free(ptr);
}
else
{
HYPRE_Int threadid = hypre_GetThreadID();
ptr = (char*)_urealloc_(ptr, size);
}
#else
if (ptr == NULL)
{
ptr = (char*)malloc(size);
}
else
{
ptr = (char*)realloc(ptr, size);
}
#endif
#if 1
if ((ptr == NULL) && (size > 0))
{
hypre_OutOfMemory(size);
}
#endif
return ptr;
}
/*--------------------------------------------------------------------------
* hypre_Free
*--------------------------------------------------------------------------*/
void
hypre_Free( char *ptr )
{
if (ptr)
{
#ifdef HYPRE_USE_UMALLOC
HYPRE_Int threadid = hypre_GetThreadID();
_ufree_(ptr);
#else
free(ptr);
#endif
}
}