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