GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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  * (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 
32 void *G__malloc(const char *file, int line, size_t n)
33 {
34  void *buf;
35 
36  if (n <= 0)
37  n = 1; /* make sure we get a valid request */
38 
39  buf = malloc(n);
40  if (!buf) {
41  struct Cell_head window;
42 
43  G_get_window(&window);
44  G_important_message(_("Current region rows: %d, cols: %d"),
45  window.rows, window.cols);
46 
47  G_fatal_error(_("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 
72 void *G__calloc(const char *file, int line, size_t m, size_t n)
73 {
74  void *buf;
75 
76  if (m <= 0)
77  m = 1; /* make sure we get a valid requests */
78  if (n <= 0)
79  n = 1;
80 
81  buf = calloc(m, n);
82  if (!buf) {
83  struct Cell_head window;
84 
85  G_get_window(&window);
86  G_important_message(_("Current region rows: %d, cols: %d"),
87  window.rows, window.cols);
88 
89  G_fatal_error(_("G_calloc: unable to allocate %lu * %lu bytes of memory at %s:%d"),
90  (unsigned long) m, (unsigned long) n, file, line);
91  }
92 
93  return buf;
94 }
95 
96 
97 /*!
98  * \brief Memory reallocation.
99  *
100  * Changes the <i>size</i> of a previously allocated block of memory
101  * at <i>ptr</i> and returns a pointer to the new block of memory. The
102  * <i>size</i> may be larger or smaller than the original size. If the
103  * original block cannot be extended "in place", then a new block is
104  * allocated and the original block copied to the new block.
105  *
106  * <b>Note:</b> If <i>buf</i> is NULL, then this routine simply
107  * allocates a block of <i>n</i> bytes else <i>buf</i> must point to
108  * memory that has been dynamically allocated by G_malloc(),
109  * G_calloc(), G_realloc(), malloc(3), alloc(3), or realloc(3).. This
110  * routine works around broken realloc() routines, which do not
111  * handle a NULL <i>buf</i>.
112  *
113  * \param file file name
114  * \param line line number
115  * \param[in,out] buf buffer holding original data
116  * \param[in] n array size
117  */
118 void *G__realloc(const char *file, int line, void *buf, size_t n)
119 {
120  if (n <= 0)
121  n = 1; /* make sure we get a valid request */
122 
123  if (!buf)
124  buf = malloc(n);
125  else
126  buf = realloc(buf, n);
127 
128  if (!buf) {
129  struct Cell_head window;
130 
131  G_get_window(&window);
132  G_important_message(_("Current region rows: %d, cols: %d"),
133  window.rows, window.cols);
134 
135  G_fatal_error(_("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 /*!
144  * \brief Free allocated memory.
145  *
146  * \param[in,out] buf buffer holding original data
147  */
148 
149 void G_free(void *buf)
150 {
151  free(buf);
152 }
153 
154 /*!
155  * \brief Advance void pointer
156  *
157  * Advances void pointer by <i>size</i> bytes. Returns new pointer
158  * value.
159  *
160  * Useful in raster row processing loops, substitutes
161  *
162  \code
163  CELL *cell;
164  cell += n;
165  \endcode
166  *
167  * Now
168  \code
169  rast = G_incr_void_ptr(rast, Rast_cell_size(data_type))
170  \endcode
171  *
172  * (where rast is void* and <i>data_type</i> is RASTER_MAP_TYPE can be
173  * used instead of rast++.)
174  *
175  * Very useful to generalize the row processing - loop i.e.
176  * \code
177  * void * buf_ptr += Rast_cell_size(data_type)
178  * \endcode
179  *
180  * \param ptr pointer
181  * \param size buffer size
182  *
183  * \return pointer to the data
184  */
185 #ifndef G_incr_void_ptr
186 void *G_incr_void_ptr(const void *ptr, size_t size)
187 {
188  /* assuming that the size of unsigned char is 1 */
189  return (void *)((const unsigned char *)ptr + size);
190 }
191 #endif
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:149
void void void G_important_message(const char *,...) __attribute__((format(printf
2D/3D raster map header (used also for region)
Definition: gis.h:412
#define G_incr_void_ptr(ptr, size)
Definition: defs/gis.h:100
void free(void *)
void * G__realloc(const char *file, int line, void *buf, size_t n)
Memory reallocation.
Definition: gis/alloc.c:118
void * malloc(YYSIZE_T)
void * G__malloc(const char *file, int line, size_t n)
Memory allocation.
Definition: gis/alloc.c:32
void * G__calloc(const char *file, int line, size_t m, size_t n)
Memory allocation.
Definition: gis/alloc.c:72
int cols
Number of columns for 2D data.
Definition: gis.h:431
#define file
#define _(str)
Definition: glocale.h:10
int rows
Number of rows for 2D data.
Definition: gis.h:427
void G_get_window(struct Cell_head *)
Get the current region.
Definition: get_window.c:47