GRASS GIS 7 Programmer's Manual  7.0.4(2016)-r00000
alloc.c
Go to the documentation of this file.
1 
14 #include <stdlib.h>
15 #include <grass/gis.h>
16 #include <grass/glocale.h>
17 
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 
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 
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 
149 void G_free(void *buf)
150 {
151  free(buf);
152 }
153 
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 G_get_window(struct Cell_head *window)
Get the current region.
Definition: get_window.c:47
void G_important_message(const char *msg,...)
Print a message to stderr even in brief mode (verbosity=1)
Definition: gis/error.c:130
void * G__calloc(const char *file, int line, size_t m, size_t n)
Memory allocation.
Definition: alloc.c:72
void * G__malloc(const char *file, int line, size_t n)
Memory allocation.
Definition: alloc.c:32
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:159
void * G__realloc(const char *file, int line, void *buf, size_t n)
Memory reallocation.
Definition: alloc.c:118
#define file
void * G_incr_void_ptr(const void *ptr, size_t size)
Advance void pointer.
Definition: alloc.c:186
void G_free(void *buf)
Free allocated memory.
Definition: alloc.c:149