allocation.c

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

Generated on Thu Jul 16 13:21:17 2009 for GRASS Programmer's Manual by  doxygen 1.5.6