GRASS 8 Programmer's Manual  8.5.0dev(2025)-e5e60895a1
allocation.c
Go to the documentation of this file.
1 /*
2  ****************************************************************************
3  *
4  * MODULE: Vector library
5  *
6  * AUTHOR(S): Original author CERL, probably Dave Gerdes.
7  * Update to GRASS 5.7 Radim Blazek.
8  *
9  * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
10  *
11  * COPYRIGHT: (C) 2001 by the GRASS Development Team
12  *
13  * This program is free software under the GNU General Public
14  * License (>=v2). Read the file COPYING that comes with GRASS
15  * for details.
16  *
17  *****************************************************************************/
18 
19 #include <unistd.h>
20 #include <stdlib.h>
21 #include <grass/vector.h>
22 
23 /* functions - alloc_space(), falloc(), frealloc() _falloc() _frealloc() */
24 
25 /* alloc_space () allocates space if needed.
26  * All allocated space is created by calloc (2).
27  *
28  * args: number of elements wanted, pointer to number of currently allocated
29  * elements, size of chunks to allocate, pointer to current array, sizeof
30  * an element.
31  */
32 void *dig_alloc_space(int n_wanted, int *n_elements, int chunk_size, void *ptr,
33  int element_size)
34 {
35  char *p;
36 
37  p = dig__alloc_space(n_wanted, n_elements, chunk_size, ptr, element_size);
38 
39  if (p == NULL) {
40  fprintf(stderr, "\nERROR: out of memory. memory asked for: %d\n",
41  n_wanted);
42  exit(EXIT_FAILURE);
43  }
44 
45  return (p);
46 }
47 
49  int n_wanted, int *n_elements, int chunk_size,
50  void *ptr, /* changed char -> void instead of casting. WBH 8/16/1998 */
51  int element_size)
52 {
53  int to_alloc;
54 
55  to_alloc = *n_elements;
56 
57  /* do we need to allocate more space */
58  if (n_wanted < to_alloc)
59  return (ptr);
60 
61  /* calculate the number needed by chunk size */
62  /* ORIGINAL
63  while (n_wanted >= to_alloc)
64  to_alloc += chunk_size;
65  */
66  /*
67  ** This was changed as a test on Aug 21, 1990
68  ** Build.vect was taking outrageous amounts of
69  ** memory to run, so instead of blaming my
70  ** code, I decided that it could be the realloc/malloc
71  ** stuff not making efficient use of the space.
72  ** So the fix is to instead of asking for many small
73  ** increments, ask for twice as much space as we are currently
74  ** using, each time we need more space.
75  */
76  while (n_wanted >= to_alloc)
77  to_alloc += *n_elements ? *n_elements : chunk_size;
78 
79  /* first time called allocate initial storage */
80  if (*n_elements == 0)
81  ptr = G_calloc(to_alloc, element_size);
82  else
83  ptr = dig__frealloc((char *)ptr, to_alloc, element_size, *n_elements);
84 
85  *n_elements = to_alloc;
86 
87  return (ptr);
88 }
89 
90 void *dig_falloc(int nelem, int elsize)
91 {
92  void *ret;
93 
94  if ((ret = dig__falloc(nelem, elsize)) == NULL) {
95  fprintf(stderr, "Out of Memory.\n");
96  G_sleep(2);
97  exit(EXIT_FAILURE);
98  }
99  return (ret);
100 }
101 
102 void *dig_frealloc(void *oldptr, int nelem, int elsize, int oldnelem)
103 {
104  char *ret;
105 
106  if ((ret = dig__frealloc(oldptr, nelem, elsize, oldnelem)) == NULL) {
107  fprintf(stderr, "\nOut of Memory on realloc.\n");
108  G_sleep(2);
109  exit(EXIT_FAILURE);
110  }
111  return (ret);
112 }
113 
114 /* these functions don't exit on "no more memory", calling function should
115  check the return value */
116 
117 void *dig__falloc(int nelem, int elsize)
118 {
119  char *ptr;
120 
121  if (elsize == 0) {
122  elsize = 4;
123  }
124  if (nelem == 0) {
125  nelem = 1;
126  }
127 
128  ptr = G_calloc(nelem, elsize);
129  return (ptr);
130 }
131 
132 void *dig__frealloc(void *oldptr, int nelem, int elsize, int oldnelem)
133 {
134  char *ptr;
135 
136  if (elsize == 0) {
137  elsize = 4;
138  }
139  if (nelem == 0) {
140  nelem = 1;
141  }
142 
143  ptr = G_calloc(nelem, elsize);
144 
145  /* out of memory */
146  if (!ptr)
147  return (ptr);
148 
149  {
150  register char *a;
151  register char *b;
152  register size_t n;
153 
154  n = oldnelem * elsize;
155  a = ptr;
156  b = oldptr;
157  while (n--)
158  *a++ = *b++;
159  }
160 
161  G_free(oldptr);
162  return (ptr);
163 }
void * dig_falloc(int nelem, int elsize)
Definition: allocation.c:90
void * dig_alloc_space(int n_wanted, int *n_elements, int chunk_size, void *ptr, int element_size)
Definition: allocation.c:32
void * dig__frealloc(void *oldptr, int nelem, int elsize, int oldnelem)
Definition: allocation.c:132
void * dig_frealloc(void *oldptr, int nelem, int elsize, int oldnelem)
Definition: allocation.c:102
void * dig__alloc_space(int n_wanted, int *n_elements, int chunk_size, void *ptr, int element_size)
Definition: allocation.c:48
void * dig__falloc(int nelem, int elsize)
Definition: allocation.c:117
#define NULL
Definition: ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:147
#define G_calloc(m, n)
Definition: defs/gis.h:95
void G_sleep(unsigned int)
Definition: sleep.c:11
double b
Definition: r_raster.c:39