allocation.c

Go to the documentation of this file.
00001 /*
00002 * $Id: allocation.c,v 1.5 2006/02/09 03:08:58 glynn Exp $
00003 *
00004 ****************************************************************************
00005 *
00006 * MODULE:       Vector library 
00007 *               
00008 * AUTHOR(S):    Original author CERL, probably Dave Gerdes.
00009 *               Update to GRASS 5.7 Radim Blazek.
00010 *
00011 * PURPOSE:      Lower level functions for reading/writing/manipulating vectors.
00012 *
00013 * COPYRIGHT:    (C) 2001 by the GRASS Development Team
00014 *
00015 *               This program is free software under the GNU General Public
00016 *               License (>=v2). Read the file COPYING that comes with GRASS
00017 *               for details.
00018 *
00019 *****************************************************************************/
00020 #include <unistd.h>
00021 #include <stdlib.h>
00022 #include <grass/Vect.h>
00023 
00024 /*  functions - alloc_space(), falloc(), frealloc() _falloc() _frealloc() */
00025 
00026 
00027 /*   alloc_space ()    allocates space if needed.
00028    *    All allocated space is created by calloc (2).
00029    *
00030    *   args: number of elements wanted, pointer to number of currently allocated
00031    *   elements, size of chunks to allocate,  pointer to current array, sizeof
00032    *   an element.
00033  */
00034 
00035 void *
00036 dig_alloc_space (
00037                   int n_wanted,
00038                   int *n_elements,
00039                   int chunk_size,
00040                   void *ptr,
00041                   int element_size)
00042 {
00043   char *p;
00044 
00045   p = dig__alloc_space (n_wanted, n_elements, chunk_size, ptr, element_size);
00046 
00047   if (p == NULL)
00048     {
00049       fprintf (stderr, "\nERROR: out of memory.  memory asked for: %d\n",
00050                n_wanted);
00051       exit (-1);
00052     }
00053 
00054   return (p);
00055 }
00056 
00057 void *
00058 dig__alloc_space (
00059                    int n_wanted,
00060                    int *n_elements,
00061                    int chunk_size,
00062                    void *ptr,   /* changed char -> void instead of casting.  WBH 8/16/1998  */
00063                    int element_size)
00064 {
00065   int to_alloc;
00066 
00067   to_alloc = *n_elements;
00068 
00069   /*  do we need to allocate more space  */
00070   if (n_wanted < to_alloc)
00071     return (ptr);
00072 
00073   /*  calculate the number needed by chunk size */
00074   /*  ORIGINAL
00075      while (n_wanted >= to_alloc)
00076      to_alloc += chunk_size;
00077    */
00078   /*
00079      **  This was changed as a test on Aug 21, 1990
00080      **  Build.vect was taking outrageous amounts of
00081      **  memory to run, so instead of blaming my
00082      **  code, I decided that it could be the realloc/malloc
00083      **  stuff not making efficient use of the space.
00084      **  So the fix is to instead of asking for many small
00085      **  increments, ask for twice as much space as we are currently
00086      **  using, each time we need more space.
00087    */
00088   while (n_wanted >= to_alloc)
00089     to_alloc += *n_elements ? *n_elements : chunk_size;
00090 
00091   /*  first time called allocate initial storage  */
00092   if (*n_elements == 0)
00093     ptr = calloc ((unsigned) to_alloc, (unsigned) element_size);
00094   else
00095     ptr = dig__frealloc ((char *) ptr, to_alloc, element_size, *n_elements);
00096 
00097   *n_elements = to_alloc;
00098 
00099   return (ptr);
00100 }
00101 
00102 
00103 void *
00104 dig_falloc (int nelem, int elsize)
00105 {
00106   void *ret;
00107   if ((ret = dig__falloc (nelem, elsize)) == NULL)
00108     {
00109       fprintf (stderr, "Out of Memory.\n");
00110       G_sleep (2);
00111       exit (-1);
00112     }
00113   return (ret);
00114 }
00115 
00116 void *
00117 dig_frealloc (
00118                void *oldptr,
00119                int nelem, int elsize,
00120                int oldnelem)
00121 {
00122   char *ret;
00123 
00124   if ((ret = dig__frealloc (oldptr, nelem, elsize, oldnelem)) == NULL)
00125     {
00126       fprintf (stderr, "\nOut of Memory on realloc.\n");
00127       G_sleep (2);
00128       exit (-1);
00129     }
00130   return (ret);
00131 }
00132 
00133 /*  these functions don't exit on "no more memory",  calling funtion should
00134    check the return value  */
00135 
00136 void *
00137 dig__falloc (int nelem, int elsize)
00138 {
00139   char *ptr;
00140 
00141   if (elsize == 0)
00142     {
00143       elsize = 4;
00144     }
00145   if (nelem == 0)
00146     {
00147       nelem = 1;
00148     }
00149 
00150   ptr = calloc ((unsigned) nelem, (unsigned) elsize);
00151   return (ptr);
00152 }
00153 
00154 void *
00155 dig__frealloc (
00156                 void *oldptr,
00157                 int nelem, int elsize,
00158                 int oldnelem)
00159 {
00160   char *ptr;
00161 
00162   if (elsize == 0)
00163     {
00164       elsize = 4;
00165     }
00166   if (nelem == 0)
00167     {
00168       nelem = 1;
00169     }
00170 
00171   ptr = calloc ((unsigned) nelem, (unsigned) elsize);
00172 
00173   /*  out of memory  */
00174   if (!ptr)
00175     return (ptr);
00176 
00177   {
00178     register char *a;
00179     register char *b;
00180     register long n;
00181 
00182     n = oldnelem * elsize;
00183     a = ptr;
00184     b = oldptr;
00185     while (n--)
00186       *a++ = *b++;
00187   }
00188 
00189   free (oldptr);
00190   return (ptr);
00191 }

Generated on Sun Apr 6 17:32:44 2008 for GRASS by  doxygen 1.5.5