Explorar el Código

Macro-ise G_*alloc() functions
(to allow calling function to be identified in case of error)


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@34193 15284696-431f-4ddb-bdfa-cd5b030d7da7

Glynn Clements hace 16 años
padre
commit
b5ba88b751
Se han modificado 2 ficheros con 24 adiciones y 21 borrados
  1. 7 3
      include/gisdefs.h
  2. 17 18
      lib/gis/alloc.c

+ 7 - 3
include/gisdefs.h

@@ -39,11 +39,15 @@ char *G_adjust_Cell_head3(struct Cell_head *, int, int, int);
 char *G_align_window(struct Cell_head *, const struct Cell_head *);
 
 /* alloc.c */
-void *G_malloc(size_t);
-void *G_calloc(size_t, size_t);
-void *G_realloc(void *, size_t);
+void *G__malloc(const char *, int, size_t);
+void *G__calloc(const char *, int, size_t, size_t);
+void *G__realloc(const char *, int, void *, size_t);
 void G_free(void *);
 
+#define G_malloc(n)     G__malloc(__FILE__, __LINE__, (n))
+#define G_calloc(m, n)  G__calloc(__FILE__, __LINE__, (m), (n))
+#define G_realloc(p, n) G__realloc(__FILE__, __LINE__, (p), (n))
+
 /* alloc_cell.c */
 size_t G_raster_size(RASTER_MAP_TYPE);
 CELL *G_allocate_cell_buf(void);

+ 17 - 18
lib/gis/alloc.c

@@ -31,22 +31,21 @@
  * \return void * 
  */
 
-void *G_malloc(size_t n)
+void *G__malloc(const char *file, int line, size_t n)
 {
     void *buf;
-
+	
     if (n <= 0)
 	n = 1;			/* make sure we get a valid request */
-
+	
     buf = malloc(n);
-    if (buf)
-	return buf;
+    if (!buf)
+	G_fatal_error(_("G_malloc: unable to allocate %lu bytes at %s:%d"),
+		      (unsigned long) n, file, line);
 
-    G_fatal_error(_("G_malloc: out of memory"));
-    return NULL;
+    return buf;
 }
 
-
 /**
  * \brief Memory allocation.
  *
@@ -63,7 +62,7 @@ void *G_malloc(size_t n)
  * \return void * 
  */
 
-void *G_calloc(size_t m, size_t n)
+void *G__calloc(const char *file, int line, size_t m, size_t n)
 {
     void *buf;
 
@@ -73,11 +72,11 @@ void *G_calloc(size_t m, size_t n)
 	n = 1;
 
     buf = calloc(m, n);
-    if (buf)
-	return buf;
+    if (!buf)
+	G_fatal_error(_("G_calloc: unable to allocate %lu * %lu bytes at %s:%d"),
+		      (unsigned long) m, (unsigned long) n, file, line);
 
-    G_fatal_error(_("G_calloc: out of memory"));
-    return NULL;
+    return buf;
 }
 
 
@@ -101,7 +100,7 @@ void *G_calloc(size_t m, size_t n)
  * \return void * 
  */
 
-void *G_realloc(void *buf, size_t n)
+void *G__realloc(const char *file, int line, void *buf, size_t n)
 {
     if (n <= 0)
 	n = 1;			/* make sure we get a valid request */
@@ -111,11 +110,11 @@ void *G_realloc(void *buf, size_t n)
     else
 	buf = realloc(buf, n);
 
-    if (buf)
-	return buf;
+    if (!buf)
+	G_fatal_error(_("G_realloc: unable to allocate %lu bytes at %s:%d"),
+		      (unsigned long) n, file, line);
 
-    G_fatal_error(_("G_realloc: out of memory"));
-    return NULL;
+    return buf;
 }