allocrus.c

Go to the documentation of this file.
00001 /****************************************************************************/
00002 /*                                                                          */
00003 /*  This file is part of QSopt_ex.                                          */
00004 /*                                                                          */
00005 /*  (c) Copyright 2006 by David Applegate, William Cook, Sanjeeb Dash,      */
00006 /*  and Daniel Espinoza                                                     */
00007 /*                                                                          */
00008 /*  Sanjeeb Dash ownership of copyright in QSopt_ex is derived from his     */
00009 /*  copyright in QSopt.                                                     */
00010 /*                                                                          */
00011 /*  This code may be used under the terms of the GNU General Public License */
00012 /*  (Version 2.1 or later) as published by the Free Software Foundation.    */
00013 /*                                                                          */
00014 /*  Alternatively, use is granted for research purposes only.               */
00015 /*                                                                          */
00016 /*  It is your choice of which of these two licenses you are operating      */
00017 /*  under.                                                                  */
00018 /*                                                                          */
00019 /*  We make no guarantees about the correctness or usefulness of this code. */
00020 /*                                                                          */
00021 /****************************************************************************/
00022 
00023 /* RCSINFO $Id: allocrus.c,v 1.2 2003/11/05 16:47:22 meven Exp $ */
00024 /****************************************************************************/
00025 /*                                                                          */
00026 /*  This file is part of CONCORDE                                           */
00027 /*                                                                          */
00028 /*  (c) Copyright 1995--1999 by David Applegate, Robert Bixby,              */
00029 /*  Vasek Chvatal, and William Cook                                         */
00030 /*                                                                          */
00031 /*  Permission is granted for academic research use.  For other uses,       */
00032 /*  contact the authors for licensing options.                              */
00033 /*                                                                          */
00034 /*  Use at your own risk.  We make no guarantees about the                  */
00035 /*  correctness or usefulness of this code.                                 */
00036 /*                                                                          */
00037 /****************************************************************************/
00038 
00039 /****************************************************************************/
00040 /*                                                                          */
00041 /*                   MEMORY ALLOCATION MACROS                               */
00042 /*                                                                          */
00043 /*                           TSP CODE                                       */
00044 /*                                                                          */
00045 /*                                                                          */
00046 /*  Written by:  Applegate, Bixby, Chvatal, and Cook                        */
00047 /*  Date: February 2, 1995 (cofeb16)                                        */
00048 /*                                                                          */
00049 /*                                                                          */
00050 /*    EXPORTED FUNCTIONS:                                                   */
00051 /*                                                                          */
00052 /*  void *ILLutil_allocrus (size_t size)                                    */
00053 /*    RETURNS a pointer to an allocated block of "size" memory.             */
00054 /*                                                                          */
00055 /*  void ILLutil_freerus (void *ptr)                                        */
00056 /*    FREES ptr.                                                            */
00057 /*                                                                          */
00058 /*  void *ILLutil_reallocrus (void *ptr, size_t size)                       */
00059 /*    REALLOCS ptr to size bytes.                                           */
00060 /*                                                                          */
00061 /*  int ILLutil_reallocrus_scale (void **pptr, int *pnnum, int count,       */
00062 /*      double scale, size_t size)                                          */
00063 /*    void **pptr (a reference to the pointer to the allocated space)       */
00064 /*    int *pnnum (a reference to the number of objects in the               */
00065 /*                allocated space)                                          */
00066 /*    int count (a minimum value for the new nnum)                          */
00067 /*    double scale (a scale factor to apply to nnum)                        */
00068 /*    int size (the size of objects to be realloced)                        */
00069 /*    RETURNS 0 if *pptr was successfully changed to point to at            */
00070 /*            least max(*pnnum*scale, *pnnum+1000, count) objects.          */
00071 /*            *pnnum is changed to the new object count.                    */
00072 /*            Otherwise, prints an error message, leaves *pptr and          */
00073 /*            *pnnum alone, and returns nonzero.                            */
00074 /*                                                                          */
00075 /*  int ILLutil_reallocrus_count (void **pptr, int count,                   */
00076 /*      size_t size)                                                        */
00077 /*    void **pptr (a reference to the pointer to the allocated space)       */
00078 /*    int count (number of objects to be realloced)                         */
00079 /*    int size (the size of the objects to be realloced)                    */
00080 /*    RETURNS 0 is successful, and 1 if the realloc failed.                 */
00081 /*                                                                          */
00082 /*  ILLbigchunkptr *ILLutil_bigchunkalloc (void)                            */
00083 /*         RETURNS a ILLbigchunkptr with the "this_one" field loaded with a */
00084 /*                 a pointer to a bigchunk of memory.                       */
00085 /*    NOTES:                                                                */
00086 /*       The idea is to use bigchunks (the size of a bigchunk is defined    */
00087 /*       by ILL_BIGCHUNK in util.h) to supply local routines with memory    */
00088 /*       for ptrs, so the memory can be shared with other                   */
00089 /*       local routines.                                                    */
00090 /*                                                                          */
00091 /*  ILLutil_bigchunkfree (ILLbigchunkptr *bp)                               */
00092 /*    ACTION: Frees a ILLbigchunkptr.                                       */
00093 /*                                                                          */
00094 /*  void ILLptrworld_init (ILLptrworld *world)                              */
00095 /*     initialize a ILLptrworld with 1 reference                            */
00096 /*                                                                          */
00097 /*  void ILLptrworld_add (ILLptrworld *world)                               */
00098 /*     add a reference to a ILLptrworld                                     */
00099 /*                                                                          */
00100 /*  void ILLptrworld_delete (ILLptrworld *world)                            */
00101 /*     delete a reference to a ptrworld, and free if no more references     */
00102 /*                                                                          */
00103 /****************************************************************************/
00104 
00105 #include "machdefs.h"
00106 #include "except.h"
00107 #include "util.h"
00108 #ifdef USEDMALLOC
00109 #include "dmalloc.h"
00110 #endif
00111 
00112 int ILLTRACE_MALLOC = 0;
00113 
00114 typedef struct ILLbigchunk
00115 {
00116   char space[ILL_BIGCHUNK];
00117   ILLbigchunkptr ptr;
00118 }
00119 ILLbigchunk;
00120 
00121 void *ILLutil_allocrus (
00122   size_t size)
00123 {
00124   void *mem = (void *) NULL;
00125 
00126   if (size == 0)
00127   {
00128     //fprintf (stderr, "Warning: 0 bytes allocated\n");
00129   }
00130 
00131   mem = (void *) malloc (size);
00132   if (mem == (void *) NULL)
00133   {
00134     fprintf (stderr, "Out of memory. Asked for %d bytes\n", (int) size);
00135   }
00136   return mem;
00137 }
00138 
00139 void ILLutil_freerus (
00140   void *p)
00141 {
00142   if (!p)
00143   {
00144     //fprintf (stderr, "Warning: null pointer freed\n");
00145     return;
00146   }
00147 
00148   free (p);
00149 }
00150 
00151 void *ILLutil_reallocrus (
00152   void *ptr,
00153   size_t size)
00154 {
00155   void *newptr;
00156 
00157   if (!ptr)
00158   {
00159     return ILLutil_allocrus (size);
00160   }
00161   else
00162   {
00163     newptr = (void *) realloc (ptr, size);
00164     if (!newptr)
00165     {
00166       fprintf (stderr, "Out of memory.  Tried to grow to %d bytes\n",
00167                (int) size);
00168     }
00169     return newptr;
00170   }
00171 }
00172 
00173 int ILLutil_reallocrus_scale (
00174   void **pptr,
00175   int *pnnum,
00176   int count,
00177   double scale,
00178   size_t size)
00179 {
00180   int rval = 0;
00181   int newsize = (int) (((double) *pnnum) * scale);
00182   void *p;
00183 
00184   if (newsize < *pnnum + 1000)
00185     newsize = *pnnum + 1000;
00186   if (newsize < count)
00187     newsize = count;
00188   p = ILLutil_reallocrus (*pptr, newsize * size);
00189   if (!p)
00190   {
00191     rval = ILL_GENERAL_ERROR;
00192     ILL_REPRT ("ILLutil_reallocrus_scale failed\n");
00193     ILL_CLEANUP;
00194   }
00195   else
00196   {
00197     *pptr = p;
00198     *pnnum = newsize;
00199   }
00200 CLEANUP:
00201   return rval;
00202 }
00203 
00204 int ILLutil_reallocrus_count (
00205   void **pptr,
00206   int count,
00207   size_t size)
00208 {
00209   int rval = 0;
00210   void *p = ILLutil_reallocrus (*pptr, count * size);
00211 
00212   if (!p)
00213   {
00214     rval = ILL_GENERAL_ERROR;
00215     ILL_REPRT ("ILLutil_reallocrus_count failed\n");
00216     ILL_CLEANUP;
00217   }
00218   else
00219   {
00220     *pptr = p;
00221   }
00222 CLEANUP:
00223   return rval;
00224 }
00225 
00226 
00227 ILLbigchunkptr *ILLutil_bigchunkalloc (
00228   void)
00229 {
00230   ILLbigchunk *p;
00231 
00232   ILL_NEW_no_rval (p, ILLbigchunk);
00233 
00234   p->ptr.this_chunk = p;
00235   p->ptr.this_one = (void *) p->space;
00236 CLEANUP:
00237   if (p == (ILLbigchunk *) NULL)
00238   {
00239     return (ILLbigchunkptr *) NULL;
00240   }
00241   return &(p->ptr);
00242 }
00243 
00244 void ILLutil_bigchunkfree (
00245   ILLbigchunkptr * bp)
00246 {
00247   /* This copy is necessary since ILL_FREE zeros its first argument */
00248   ILLbigchunk *p = bp->this_chunk;
00249 
00250   ILL_IFFREE (p, ILLbigchunk);
00251 }
00252 
00253 void ILLptrworld_init (
00254   ILLptrworld * world)
00255 {
00256   world->refcount = 1;
00257   world->freelist = (void *) NULL;
00258   world->chunklist = (ILLbigchunkptr *) NULL;
00259 }
00260 
00261 void ILLptrworld_add (
00262   ILLptrworld * world)
00263 {
00264   world->refcount++;
00265 }
00266 
00267 void ILLptrworld_delete (
00268   ILLptrworld * world)
00269 {
00270   world->refcount--;
00271   if (world->refcount <= 0)
00272   {
00273     ILLbigchunkptr *bp, *bpnext;
00274 
00275     for (bp = world->chunklist; bp; bp = bpnext)
00276     {
00277       bpnext = bp->next;
00278       ILLutil_bigchunkfree (bp);
00279     }
00280     world->chunklist = (ILLbigchunkptr *) NULL;
00281     world->freelist = (void *) NULL;
00282     world->refcount = 0;
00283   }
00284 }

Generated on Wed Apr 22 09:16:09 2009 for QSopt_ex by  doxygen 1.5.2