GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-8cbe8fef7c
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 
33 void *dig_alloc_space(int n_wanted, int *n_elements, int chunk_size, void *ptr,
34  int element_size)
35 {
36  char *p;
37 
38  p = dig__alloc_space(n_wanted, n_elements, chunk_size, ptr, element_size);
39 
40  if (p == NULL) {
41  fprintf(stderr, "\nERROR: out of memory. memory asked for: %d\n",
42  n_wanted);
43  exit(EXIT_FAILURE);
44  }
45 
46  return (p);
47 }
48 
50  int n_wanted, int *n_elements, int chunk_size,
51  void *ptr, /* changed char -> void instead of casting. WBH 8/16/1998 */
52  int element_size)
53 {
54  int to_alloc;
55 
56  to_alloc = *n_elements;
57 
58  /* do we need to allocate more space */
59  if (n_wanted < to_alloc)
60  return (ptr);
61 
62  /* calculate the number needed by chunk size */
63  /* ORIGINAL
64  while (n_wanted >= to_alloc)
65  to_alloc += chunk_size;
66  */
67  /*
68  ** This was changed as a test on Aug 21, 1990
69  ** Build.vect was taking outrageous amounts of
70  ** memory to run, so instead of blaming my
71  ** code, I decided that it could be the realloc/malloc
72  ** stuff not making efficient use of the space.
73  ** So the fix is to instead of asking for many small
74  ** increments, ask for twice as much space as we are currently
75  ** using, each time we need more space.
76  */
77  while (n_wanted >= to_alloc)
78  to_alloc += *n_elements ? *n_elements : chunk_size;
79 
80  /* first time called allocate initial storage */
81  if (*n_elements == 0)
82  ptr = G_calloc(to_alloc, element_size);
83  else
84  ptr = dig__frealloc((char *)ptr, to_alloc, element_size, *n_elements);
85 
86  *n_elements = to_alloc;
87 
88  return (ptr);
89 }
90 
91 void *dig_falloc(int nelem, int elsize)
92 {
93  void *ret;
94 
95  if ((ret = dig__falloc(nelem, elsize)) == NULL) {
96  fprintf(stderr, "Out of Memory.\n");
97  G_sleep(2);
98  exit(EXIT_FAILURE);
99  }
100  return (ret);
101 }
102 
103 void *dig_frealloc(void *oldptr, int nelem, int elsize, int oldnelem)
104 {
105  char *ret;
106 
107  if ((ret = dig__frealloc(oldptr, nelem, elsize, oldnelem)) == NULL) {
108  fprintf(stderr, "\nOut of Memory on realloc.\n");
109  G_sleep(2);
110  exit(EXIT_FAILURE);
111  }
112  return (ret);
113 }
114 
115 /* these functions don't exit on "no more memory", calling function should
116  check the return value */
117 
118 void *dig__falloc(int nelem, int elsize)
119 {
120  char *ptr;
121 
122  if (elsize == 0) {
123  elsize = 4;
124  }
125  if (nelem == 0) {
126  nelem = 1;
127  }
128 
129  ptr = G_calloc(nelem, elsize);
130  return (ptr);
131 }
132 
133 void *dig__frealloc(void *oldptr, int nelem, int elsize, int oldnelem)
134 {
135  char *ptr;
136 
137  if (elsize == 0) {
138  elsize = 4;
139  }
140  if (nelem == 0) {
141  nelem = 1;
142  }
143 
144  ptr = G_calloc(nelem, elsize);
145 
146  /* out of memory */
147  if (!ptr)
148  return (ptr);
149 
150  {
151  register char *a;
152  register char *b;
153  register size_t n;
154 
155  n = oldnelem * elsize;
156  a = ptr;
157  b = oldptr;
158  while (n--)
159  *a++ = *b++;
160  }
161 
162  G_free(oldptr);
163  return (ptr);
164 }
void * dig_falloc(int nelem, int elsize)
Definition: allocation.c:91
void * dig_alloc_space(int n_wanted, int *n_elements, int chunk_size, void *ptr, int element_size)
Definition: allocation.c:33
void * dig__frealloc(void *oldptr, int nelem, int elsize, int oldnelem)
Definition: allocation.c:133
void * dig_frealloc(void *oldptr, int nelem, int elsize, int oldnelem)
Definition: allocation.c:103
void * dig__alloc_space(int n_wanted, int *n_elements, int chunk_size, void *ptr, int element_size)
Definition: allocation.c:49
void * dig__falloc(int nelem, int elsize)
Definition: allocation.c:118
#define NULL
Definition: ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
#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