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