GRASS 8 Programmer's Manual
8.5.0(2026)-8d6ceba290
Toggle main menu visibility
Loading...
Searching...
No Matches
alloc.c
Go to the documentation of this file.
1
/*!
2
* \file lib/gis/alloc.c
3
*
4
* \brief GIS Library - Memory allocation routines.
5
*
6
* (C) 1999-2009 by the GRASS Development Team
7
*
8
* This program is free software under the GNU General Public License
9
* (>=v2). Read the file COPYING that comes with GRASS for details.
10
*
11
* \author Original author CERL
12
*/
13
14
#include <stdlib.h>
15
#include <grass/gis.h>
16
#include <grass/glocale.h>
17
18
/*!
19
* \brief Memory allocation.
20
*
21
* Allocates a block of memory at least <i>n</i> bytes which is
22
* aligned properly for all data types. A pointer to the aligned block
23
* is returned.
24
*
25
* Dies with error message on memory allocation fail.
26
*
27
* \param file file name
28
* \param line line number
29
* \param n number of elements
30
*/
31
void
*
G__malloc
(
const
char
*
file
,
int
line,
size_t
n)
32
{
33
void
*buf;
34
35
if
(n <= 0)
36
n = 1;
/* make sure we get a valid request */
37
38
buf = malloc(n);
39
if
(!buf) {
40
struct
Cell_head window;
41
42
G_get_window
(&window);
43
G_important_message
(_(
"Current region rows: %d, cols: %d"
), window.rows,
44
window.cols);
45
46
G_fatal_error
(
47
_(
"G_malloc: unable to allocate %lu bytes of memory at %s:%d"
),
48
(
unsigned
long
)n,
file
, line);
49
}
50
51
return
buf;
52
}
53
54
/*!
55
* \brief Memory allocation.
56
*
57
* Allocates a properly aligned block of memory <i>n</i>*<i>m</i>
58
* bytes in length, initializes the allocated memory to zero, and
59
* returns a pointer to the allocated block of memory.
60
*
61
* Dies with error message on memory allocation fail.
62
*
63
* <b>Note:</b> Allocating memory for reading and writing raster maps
64
* is discussed in \ref Allocating_Raster_I_O_Buffers.
65
*
66
* \param file fine name
67
* \param line line number
68
* \param m element size
69
* \param n number of elements
70
*/
71
void
*
G__calloc
(
const
char
*
file
,
int
line,
size_t
m,
size_t
n)
72
{
73
void
*buf;
74
75
if
(m <= 0)
76
m = 1;
/* make sure we get a valid requests */
77
if
(n <= 0)
78
n = 1;
79
80
buf = calloc(m, n);
81
if
(!buf) {
82
struct
Cell_head window;
83
84
G_get_window
(&window);
85
G_important_message
(_(
"Current region rows: %d, cols: %d"
), window.rows,
86
window.cols);
87
88
G_fatal_error
(_(
"G_calloc: unable to allocate %lu * %lu bytes of "
89
"memory at %s:%d"
),
90
(
unsigned
long
)m, (
unsigned
long
)n,
file
, line);
91
}
92
93
return
buf;
94
}
95
96
/*!
97
* \brief Memory reallocation.
98
*
99
* Changes the <i>size</i> of a previously allocated block of memory
100
* at <i>ptr</i> and returns a pointer to the new block of memory. The
101
* <i>size</i> may be larger or smaller than the original size. If the
102
* original block cannot be extended "in place", then a new block is
103
* allocated and the original block copied to the new block.
104
*
105
* <b>Note:</b> If <i>buf</i> is NULL, then this routine simply
106
* allocates a block of <i>n</i> bytes else <i>buf</i> must point to
107
* memory that has been dynamically allocated by G_malloc(),
108
* G_calloc(), G_realloc(), malloc(3), alloc(3), or realloc(3).. This
109
* routine works around broken realloc() routines, which do not
110
* handle a NULL <i>buf</i>.
111
*
112
* \param file file name
113
* \param line line number
114
* \param[in,out] buf buffer holding original data
115
* \param[in] n array size
116
*/
117
void
*
G__realloc
(
const
char
*
file
,
int
line,
void
*buf,
size_t
n)
118
{
119
if
(n <= 0)
120
n = 1;
/* make sure we get a valid request */
121
122
if
(!buf)
123
buf = malloc(n);
124
else
125
buf = realloc(buf, n);
126
127
if
(!buf) {
128
struct
Cell_head window;
129
130
G_get_window
(&window);
131
G_important_message
(_(
"Current region rows: %d, cols: %d"
), window.rows,
132
window.cols);
133
134
G_fatal_error
(
135
_(
"G_realloc: unable to allocate %lu bytes of memory at %s:%d"
),
136
(
unsigned
long
)n,
file
, line);
137
}
138
139
return
buf;
140
}
141
142
/*!
143
* \brief Free allocated memory.
144
*
145
* \param[in,out] buf buffer holding original data
146
*/
147
void
G_free
(
void
*buf)
148
{
149
free(buf);
150
}
151
152
/*!
153
* \brief Advance void pointer
154
*
155
* Advances void pointer by <i>size</i> bytes. Returns new pointer
156
* value.
157
*
158
* Useful in raster row processing loops, substitutes
159
*
160
\code
161
CELL *cell;
162
cell += n;
163
\endcode
164
*
165
* Now
166
\code
167
rast = G_incr_void_ptr(rast, Rast_cell_size(data_type))
168
\endcode
169
*
170
* (where rast is void* and <i>data_type</i> is RASTER_MAP_TYPE can be
171
* used instead of rast++.)
172
*
173
* Very useful to generalize the row processing - loop i.e.
174
* \code
175
* void * buf_ptr += Rast_cell_size(data_type)
176
* \endcode
177
*
178
* \param ptr pointer
179
* \param size buffer size
180
*
181
* \return pointer to the data
182
*/
183
#ifndef G_incr_void_ptr
184
void
*
G_incr_void_ptr
(
const
void
*ptr,
size_t
size)
185
{
186
/* assuming that the size of unsigned char is 1 */
187
return
(
void
*)((
const
unsigned
char
*)ptr + size);
188
}
189
#endif
G_incr_void_ptr
void * G_incr_void_ptr(const void *ptr, size_t size)
Advance void pointer.
Definition
alloc.c:184
G__realloc
void * G__realloc(const char *file, int line, void *buf, size_t n)
Memory reallocation.
Definition
alloc.c:117
G__calloc
void * G__calloc(const char *file, int line, size_t m, size_t n)
Memory allocation.
Definition
alloc.c:71
G_free
void G_free(void *buf)
Free allocated memory.
Definition
alloc.c:147
G__malloc
void * G__malloc(const char *file, int line, size_t n)
Memory allocation.
Definition
alloc.c:31
G_get_window
void G_get_window(struct Cell_head *window)
Get the current region.
Definition
get_window.c:47
G_important_message
void G_important_message(const char *msg,...)
Print a message to stderr even in brief mode (verbosity=1).
Definition
gis/error.c:130
G_fatal_error
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition
gis/error.c:159
file
#define file
gis
alloc.c
Generated on
for GRASS 8 Programmer's Manual by
1.17.0