bc_allocrus.c

Go to the documentation of this file.
00001 /****************************************************************************/
00002 /*                                                                          */
00003 /*  This file is part of CONCORDE                                           */
00004 /*                                                                          */
00005 /*  (c) Copyright 1995--1999 by David Applegate, Robert Bixby,              */
00006 /*  Vasek Chvatal, and William Cook                                         */
00007 /*                                                                          */
00008 /*  Permission is granted for academic research use.  For other uses,       */
00009 /*  contact the authors for licensing options.                              */
00010 /*                                                                          */
00011 /*  Use at your own risk.  We make no guarantees about the                  */
00012 /*  correctness or usefulness of this code.                                 */
00013 /*                                                                          */
00014 /****************************************************************************/
00015 
00016 /****************************************************************************/
00017 /*                                                                          */
00018 /*                   MEMORY ALLOCATION MACROS                               */
00019 /*                                                                          */
00020 /*                           TSP CODE                                       */
00021 /*                                                                          */
00022 /*                                                                          */
00023 /*  Written by:  Applegate, Bixby, Chvatal, and Cook                        */
00024 /*  Date: February 2, 1995 (cofeb16)                                        */
00025 /*                                                                          */
00026 /*                                                                          */
00027 /*    EXPORTED FUNCTIONS:                                                   */
00028 /*                                                                          */
00029 /*  void *CCutil_allocrus (size_t size)                                     */
00030 /*    RETURNS a pointer to an allocated block of "size" memory.             */
00031 /*                                                                          */
00032 /*  void CCutil_freerus (void *ptr)                                         */
00033 /*    FREES ptr.                                                            */
00034 /*                                                                          */
00035 /*  void *CCutil_reallocrus (void *ptr, size_t size)                        */
00036 /*    REALLOCS ptr to size bytes.                                           */
00037 /*                                                                          */
00038 /*  int CCutil_reallocrus_scale (void **pptr, int *pnnum, int count,        */
00039 /*      double scale, size_t size)                                          */
00040 /*    void **pptr (a reference to the pointer to the allocated space)       */
00041 /*    int *pnnum (a reference to the number of objects in the               */
00042 /*                allocated space)                                          */
00043 /*    int count (a minimum value for the new nnum)                          */
00044 /*    double scale (a scale factor to apply to nnum)                        */
00045 /*    int size (the size of objects to be realloced)                        */
00046 /*    RETURNS 0 if *pptr was successfully changed to point to at            */
00047 /*            least max(*pnnum*scale, *pnnum+1000, count) objects.          */
00048 /*            *pnnum is changed to the new object count.                    */
00049 /*            Otherwise, prints an error message, leaves *pptr and          */
00050 /*            *pnnum alone, and returns nonzero.                            */
00051 /*                                                                          */
00052 /*  int CCutil_reallocrus_count (void **pptr, int count,                    */
00053 /*      size_t size)                                                        */
00054 /*    void **pptr (a reference to the pointer to the allocated space)       */
00055 /*    int count (number of objects to be realloced)                         */
00056 /*    int size (the size of the objects to be realloced)                    */
00057 /*    RETURNS 0 is successful, and 1 if the realloc failed.                 */
00058 /*                                                                          */
00059 /*  CCbigchunkptr *CCutil_bigchunkalloc (void)                              */
00060 /*         RETURNS a CCbigchunkptr with the "this_one" field loaded with a  */
00061 /*                 a pointer to a bigchunk of memory.                       */
00062 /*    NOTES:                                                                */
00063 /*       The idea is to use bigchunks (the size of a bigchunk is defined    */
00064 /*       by CC_BIGCHUNK in util.h) to supply local routines with memory     */
00065 /*       for ptrs, so the memory can be shared with other                   */
00066 /*       local routines.                                                    */
00067 /*                                                                          */
00068 /*  CCutil_bigchunkfree (CCbigchunkptr *bp)                                 */
00069 /*    ACTION: Frees a CCbigchunkptr.                                        */
00070 /*                                                                          */
00071 /*  void CCptrworld_init (CCptrworld *world)                                */
00072 /*     initialize a CCptrworld with 1 reference                             */
00073 /*                                                                          */
00074 /*  void CCptrworld_add (CCptrworld *world)                                 */
00075 /*     add a reference to a CCptrworld                                      */
00076 /*                                                                          */
00077 /*  void CCptrworld_delete (CCptrworld *world)                              */
00078 /*     delete a reference to a ptrworld, and free if no more references     */
00079 /*                                                                          */
00080 /****************************************************************************/
00081 
00082 #include "bc_machdefs.h"
00083 #include "bc_util.h"
00084 
00085 typedef struct CCbigchunk
00086 {
00087   char space[CC_BIGCHUNK];
00088   CCbigchunkptr ptr;
00089 }
00090 CCbigchunk;
00091 
00092 void *CCutil_allocrus (size_t size)
00093 {
00094   void *mem = (void *) NULL;
00095 
00096   if (size == 0)
00097   {
00098     fprintf (stderr, "Warning: 0 bytes allocated\n");
00099   }
00100 
00101   mem = (void *) malloc (size);
00102   if (mem == (void *) NULL)
00103   {
00104     fprintf (stderr, "Out of memory. Asked for %d bytes\n", (int) size);
00105   }
00106   return mem;
00107 }
00108 
00109 void CCutil_freerus (void *p)
00110 {
00111   if (!p)
00112   {
00113     fprintf (stderr, "Warning: null pointer freed\n");
00114     return;
00115   }
00116 
00117   free (p);
00118 }
00119 
00120 void *CCutil_reallocrus (void *ptr,
00121                          size_t size)
00122 {
00123   void *newptr;
00124 
00125   if (!ptr)
00126   {
00127     return CCutil_allocrus (size);
00128   }
00129   else
00130   {
00131     newptr = (void *) realloc (ptr, size);
00132     if (!newptr)
00133     {
00134       fprintf (stderr, "Out of memory.  Tried to grow to %d bytes\n",
00135                (int) size);
00136     }
00137     return newptr;
00138   }
00139 }
00140 
00141 int CCutil_reallocrus_scale (void **pptr,
00142                              int *pnnum,
00143                              int count,
00144                              double scale,
00145                              size_t size)
00146 {
00147   int newsize = (int) (((double) *pnnum) * scale);
00148   void *p;
00149 
00150   if (newsize < *pnnum + 1000)
00151     newsize = *pnnum + 1000;
00152   if (newsize < count)
00153     newsize = count;
00154   p = CCutil_reallocrus (*pptr, newsize * size);
00155   if (!p)
00156   {
00157     return 1;
00158   }
00159   else
00160   {
00161     *pptr = p;
00162     *pnnum = newsize;
00163     return 0;
00164   }
00165 }
00166 
00167 int CCutil_reallocrus_count (void **pptr,
00168                              int count,
00169                              size_t size)
00170 {
00171   void *p = CCutil_reallocrus (*pptr, count * size);
00172 
00173   if (!p)
00174   {
00175     return 1;
00176   }
00177   else
00178   {
00179     *pptr = p;
00180     return 0;
00181   }
00182 }
00183 
00184 
00185 CCbigchunkptr *CCutil_bigchunkalloc (void)
00186 {
00187   CCbigchunk *p = CC_SAFE_MALLOC (1, CCbigchunk);
00188 
00189   if (p == (CCbigchunk *) NULL)
00190   {
00191     fprintf (stderr, "Out of memory in CCutil_bigchunkalloc\n");
00192     return (CCbigchunkptr *) NULL;
00193   }
00194   p->ptr.this_chunk = p;
00195   p->ptr.this_one = (void *) p->space;
00196   return &(p->ptr);
00197 }
00198 
00199 void CCutil_bigchunkfree (CCbigchunkptr * bp)
00200 {
00201   /* This copy is necessary since CC_FREE zeros its first argument */
00202   CCbigchunk *p = bp->this_chunk;
00203 
00204   CC_FREE (p, CCbigchunk);
00205 }
00206 
00207 void CCptrworld_init (CCptrworld * world)
00208 {
00209   world->refcount = 1;
00210   world->freelist = (void *) NULL;
00211   world->chunklist = (CCbigchunkptr *) NULL;
00212 }
00213 
00214 void CCptrworld_add (CCptrworld * world)
00215 {
00216   world->refcount++;
00217 }
00218 
00219 void CCptrworld_delete (CCptrworld * world)
00220 {
00221   world->refcount--;
00222   if (world->refcount <= 0)
00223   {
00224     CCbigchunkptr *bp,
00225      *bpnext;
00226 
00227     for (bp = world->chunklist; bp; bp = bpnext)
00228     {
00229       bpnext = bp->next;
00230       CCutil_bigchunkfree (bp);
00231     }
00232     world->chunklist = (CCbigchunkptr *) NULL;
00233     world->freelist = (void *) NULL;
00234     world->refcount = 0;
00235   }
00236 }

Generated on Thu Oct 20 14:58:39 2005 for DominoParitySeparator by  doxygen 1.4.5