Selaa lähdekoodia

G_* versions for memory management

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@36851 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Metz 16 vuotta sitten
vanhempi
commit
449ac9aceb

+ 4 - 4
lib/vector/diglib/allocation.c

@@ -78,7 +78,7 @@ void *dig__alloc_space(int n_wanted, int *n_elements, int chunk_size, void *ptr,
 
     /*  first time called allocate initial storage  */
     if (*n_elements == 0)
-	ptr = calloc((unsigned)to_alloc, (unsigned)element_size);
+	ptr = G_calloc((unsigned)to_alloc, (unsigned)element_size);
     else
 	ptr = dig__frealloc((char *)ptr, to_alloc, element_size, *n_elements);
 
@@ -126,7 +126,7 @@ void *dig__falloc(int nelem, int elsize)
 	nelem = 1;
     }
 
-    ptr = calloc((unsigned)nelem, (unsigned)elsize);
+    ptr = G_calloc((unsigned)nelem, (unsigned)elsize);
     return (ptr);
 }
 
@@ -141,7 +141,7 @@ void *dig__frealloc(void *oldptr, int nelem, int elsize, int oldnelem)
 	nelem = 1;
     }
 
-    ptr = calloc((unsigned)nelem, (unsigned)elsize);
+    ptr = G_calloc((unsigned)nelem, (unsigned)elsize);
 
     /*  out of memory  */
     if (!ptr)
@@ -159,6 +159,6 @@ void *dig__frealloc(void *oldptr, int nelem, int elsize, int oldnelem)
 	    *a++ = *b++;
     }
 
-    free(oldptr);
+    G_free(oldptr);
     return (ptr);
 }

+ 1 - 1
lib/vector/diglib/cindex.c

@@ -49,7 +49,7 @@ void dig_cidx_free(struct Plus_head *Plus)
     G_debug(2, "dig_cidx_free()");
     for (i = 0; i < Plus->n_cidx; i++) {
 	ci = &(Plus->cidx[0]);
-	free(ci->cat);
+	G_free(ci->cat);
 	ci->cat = NULL;
 	ci->field = ci->n_cats = ci->a_cats = ci->n_types = 0;
     }

+ 1 - 1
lib/vector/diglib/list.c

@@ -37,7 +37,7 @@ int dig_list_add(struct ilist *list, int val)
 
     if (list->n_values == list->alloc_values) {
 	size = (list->n_values + 1000) * sizeof(int);
-	p = realloc((void *)list->value, size);
+	p = G_realloc((void *)list->value, size);
 	if (p == NULL)
 	    return 0;
 	list->value = (int *)p;

+ 4 - 4
lib/vector/diglib/plus.c

@@ -122,7 +122,7 @@ void dig_free_plus_nodes(struct Plus_head *Plus)
 
 	    dig_free_node(Node);
 	}
-	free(Plus->Node);
+	G_free(Plus->Node);
     }
     Plus->Node = NULL;
     Plus->n_nodes = 0;
@@ -150,7 +150,7 @@ void dig_free_plus_lines(struct Plus_head *Plus)
 
 	    dig_free_line(Line);
 	}
-	free(Plus->Line);
+	G_free(Plus->Line);
     }
 
     Plus->Line = NULL;
@@ -186,7 +186,7 @@ void dig_free_plus_areas(struct Plus_head *Plus)
 
 	    dig_free_area(Area);
 	}
-	free(Plus->Area);
+	G_free(Plus->Area);
     }
     Plus->Area = NULL;
     Plus->n_areas = 0;
@@ -214,7 +214,7 @@ void dig_free_plus_isles(struct Plus_head *Plus)
 
 	    dig_free_isle(Isle);
 	}
-	free(Plus->Isle);
+	G_free(Plus->Isle);
     }
 
     Plus->Isle = NULL;

+ 8 - 8
lib/vector/diglib/rbtree.c

@@ -49,7 +49,7 @@ int is_red(struct RB_NODE *);
  */
 struct RB_TREE *rbtree_create(rb_compare_fn *compare, size_t rb_datasize)
 {
-    struct RB_TREE *tree = malloc(sizeof(*tree));
+    struct RB_TREE *tree = G_malloc(sizeof(*tree));
 
     if (tree == NULL) {
 	G_warning("RB tree: Out of memory!");
@@ -220,10 +220,10 @@ int rbtree_remove(struct RB_TREE *tree, const void *data)
 
     /* Replace and remove if found */
     if (f != NULL) {
-	free(f->data);
+	G_free(f->data);
 	f->data = q->data;
 	p->link[p->link[1] == q] = q->link[q->link[0] == NULL];
-	free(q);
+	G_free(q);
 	tree->count--;
 	removed = 1;
     }
@@ -413,7 +413,7 @@ void *rbtree_next(struct RB_TRAV *trav)
 /* destroy the tree */
 void rbtree_destroy(struct RB_TREE *tree) {
     rbtree_destroy2(tree->root);
-    free(tree);
+    G_free(tree);
 }
 
 void rbtree_destroy2(struct RB_NODE *root)
@@ -421,8 +421,8 @@ void rbtree_destroy2(struct RB_NODE *root)
     if (root != NULL) {
 	rbtree_destroy2(root->link[0]);
 	rbtree_destroy2(root->link[1]);
-	free(root->data);
-	free(root);
+	G_free(root->data);
+	G_free(root);
     }
 }
 
@@ -488,12 +488,12 @@ int rbtree_debug(struct RB_TREE *tree, struct RB_NODE *root)
 /* add a new node to the tree */
 struct RB_NODE *rbtree_make_node(size_t datasize, void *data)
 {
-    struct RB_NODE *new_node = malloc(sizeof(*new_node));
+    struct RB_NODE *new_node = G_malloc(sizeof(*new_node));
 
     if (new_node == NULL)
 	G_fatal_error("RB Search Tree: Out of memory!");
 
-    new_node->data = malloc(datasize);
+    new_node->data = G_malloc(datasize);
     if (new_node->data == NULL)
 	G_fatal_error("RB Search Tree: Out of memory!");
 	

+ 20 - 22
lib/vector/diglib/struct_alloc.c

@@ -35,14 +35,12 @@
  ** Allocate array space to add 'add' elements
  */
 
-/* TODO: use G_malloc and friends instead of malloc et al.? */
-
 /* allocate new node structure */
 P_NODE *dig_alloc_node()
 {
     P_NODE *Node;
 
-    Node = (P_NODE *) malloc(sizeof(P_NODE));
+    Node = (P_NODE *) G_malloc(sizeof(P_NODE));
     if (Node == NULL)
         return NULL;
 
@@ -58,11 +56,11 @@ P_NODE *dig_alloc_node()
 void dig_free_node(P_NODE *Node)
 {
     if (Node->alloc_lines > 0) {
-        free(Node->lines);
-        free(Node->angles);
+        G_free(Node->lines);
+        G_free(Node->angles);
     }
 
-    free(Node);
+    G_free(Node);
 }
 
 /* dig_node_alloc_line (node, add)
@@ -81,12 +79,12 @@ int dig_node_alloc_line(P_NODE * node, int add)
 
     num = node->n_lines + add;
 
-    p = realloc(node->lines, num * sizeof(plus_t));
+    p = G_realloc(node->lines, num * sizeof(plus_t));
     if (p == NULL)
         return -1;
     node->lines = (plus_t *) p;
 
-    p = realloc(node->angles, num * sizeof(float));
+    p = G_realloc(node->angles, num * sizeof(float));
     if (p == NULL)
         return -1;
     node->angles = (float *)p;
@@ -108,7 +106,7 @@ int dig_alloc_nodes(struct Plus_head *Plus, int add)
     char *p;
 
     size = Plus->alloc_nodes + 1 + add;
-    p = realloc(Plus->Node, size * sizeof(P_NODE *));
+    p = G_realloc(Plus->Node, size * sizeof(P_NODE *));
     if (p == NULL)
         return -1;
 
@@ -123,7 +121,7 @@ P_LINE *dig_alloc_line()
 {
     P_LINE *Line;
 
-    Line = (P_LINE *) malloc(sizeof(P_LINE));
+    Line = (P_LINE *) G_malloc(sizeof(P_LINE));
     if (Line == NULL)
         return NULL;
 
@@ -133,7 +131,7 @@ P_LINE *dig_alloc_line()
 /* free line structure */
 void dig_free_line(P_LINE *Line)
 {
-    free(Line);
+    G_free(Line);
 }
 
 /* Reallocate array of pointers to lines.
@@ -148,7 +146,7 @@ int dig_alloc_lines(struct Plus_head *Plus, int add)
     char *p;
 
     size = Plus->alloc_lines + 1 + add;
-    p = realloc(Plus->Line, size * sizeof(P_LINE *));
+    p = G_realloc(Plus->Line, size * sizeof(P_LINE *));
     if (p == NULL)
         return -1;
 
@@ -170,7 +168,7 @@ int dig_alloc_areas(struct Plus_head *Plus, int add)
     char *p;
 
     size = Plus->alloc_areas + 1 + add;
-    p = realloc(Plus->Area, size * sizeof(P_AREA *));
+    p = G_realloc(Plus->Area, size * sizeof(P_AREA *));
     if (p == NULL)
         return -1;
 
@@ -193,7 +191,7 @@ int dig_alloc_isles(struct Plus_head *Plus, int add)
 
     G_debug(3, "dig_alloc_isle():");
     size = Plus->alloc_isles + 1 + add;
-    p = realloc(Plus->Isle, size * sizeof(P_ISLE *));
+    p = G_realloc(Plus->Isle, size * sizeof(P_ISLE *));
     if (p == NULL)
         return -1;
 
@@ -208,7 +206,7 @@ P_AREA *dig_alloc_area()
 {
     P_AREA *Area;
 
-    Area = (P_AREA *) malloc(sizeof(P_AREA));
+    Area = (P_AREA *) G_malloc(sizeof(P_AREA));
     if (Area == NULL)
         return NULL;
 
@@ -234,7 +232,7 @@ void dig_free_area(P_AREA *Area)
     if (Area->alloc_isles > 0)
         free(Area->isles);
 
-    free(Area);
+    G_free(Area);
 }
 
 /* alloc new isle structure */
@@ -242,7 +240,7 @@ P_ISLE *dig_alloc_isle()
 {
     P_ISLE *Isle;
 
-    Isle = (P_ISLE *) malloc(sizeof(P_ISLE));
+    Isle = (P_ISLE *) G_malloc(sizeof(P_ISLE));
     if (Isle == NULL)
         return NULL;
 
@@ -259,9 +257,9 @@ P_ISLE *dig_alloc_isle()
 void dig_free_isle(P_ISLE *Isle)
 {
     if (Isle->alloc_lines > 0)
-        free(Isle->lines);
+        G_free(Isle->lines);
 
-    free(Isle);
+    G_free(Isle);
 }
 
 /* allocate room for  'num'   X and Y  arrays in struct line_pnts 
@@ -345,7 +343,7 @@ int dig_area_alloc_line(P_AREA * area, int add)
 
     num = area->alloc_lines + add;
 
-    p = realloc(area->lines, num * sizeof(plus_t));
+    p = G_realloc(area->lines, num * sizeof(plus_t));
     if (p == NULL)
         return -1;
     area->lines = (plus_t *) p;
@@ -368,7 +366,7 @@ int dig_area_alloc_isle(P_AREA * area, int add)
     G_debug(5, "dig_area_alloc_isle(): add = %d", add);
     num = area->alloc_isles + add;
 
-    p = realloc(area->isles, num * sizeof(plus_t));
+    p = G_realloc(area->isles, num * sizeof(plus_t));
     if (p == NULL)
         return -1;
     area->isles = (plus_t *) p;
@@ -391,7 +389,7 @@ int dig_isle_alloc_line(P_ISLE * isle, int add)
     G_debug(3, "dig_isle_alloc_line():");
     num = isle->alloc_lines + add;
 
-    p = realloc(isle->lines, num * sizeof(plus_t));
+    p = G_realloc(isle->lines, num * sizeof(plus_t));
     if (p == NULL)
         return -1;
     isle->lines = (plus_t *) p;