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