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