[GRASS-SVN] r34193 - in grass/trunk: include lib/gis

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Nov 8 10:02:59 EST 2008


Author: glynn
Date: 2008-11-08 10:02:59 -0500 (Sat, 08 Nov 2008)
New Revision: 34193

Modified:
   grass/trunk/include/gisdefs.h
   grass/trunk/lib/gis/alloc.c
Log:
Macro-ise G_*alloc() functions
 (to allow calling function to be identified in case of error)


Modified: grass/trunk/include/gisdefs.h
===================================================================
--- grass/trunk/include/gisdefs.h	2008-11-08 15:01:09 UTC (rev 34192)
+++ grass/trunk/include/gisdefs.h	2008-11-08 15:02:59 UTC (rev 34193)
@@ -39,11 +39,15 @@
 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);

Modified: grass/trunk/lib/gis/alloc.c
===================================================================
--- grass/trunk/lib/gis/alloc.c	2008-11-08 15:01:09 UTC (rev 34192)
+++ grass/trunk/lib/gis/alloc.c	2008-11-08 15:02:59 UTC (rev 34193)
@@ -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 @@
  * \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 @@
 	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 @@
  * \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 @@
     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;
 }
 
 



More information about the grass-commit mailing list