Back to index

radiance  4R0+20100331
palloc.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: palloc.c,v 1.4 2003/11/15 02:13:37 schorsch Exp $";
00003 #endif
00004 /*
00005  *   Limited dynamic storage allocator for primitives
00006  */
00007 
00008 
00009 #define  FBSIZE  72         /* size of malloc call */
00010 
00011 
00012 #include  "meta.h"
00013 #include  "rtio.h"
00014 
00015 
00016 extern int  maxalloc;              /* number of prims to allocate */
00017 int  nalloc = 0;            /* number allocated so far */
00018 
00019 static PLIST  freelist = {NULL, NULL};
00020 
00021 
00022 static int morefree(void);
00023 
00024 
00025 PRIMITIVE *
00026 palloc(void)         /* allocate a primitive */
00027 
00028 {
00029     register PRIMITIVE  *p;
00030 
00031     if (maxalloc > 0 && nalloc >= maxalloc)
00032        return(NULL);
00033 
00034     if ((p = pop(&freelist)) == NULL) {
00035        if (morefree())
00036            p = pop(&freelist);
00037        else {
00038            sprintf(errmsg, "out of memory in palloc (nalloc = %d)", nalloc);
00039            error(SYSTEM, errmsg);
00040        }
00041     }
00042 
00043     nalloc++;
00044     return(p);
00045 }
00046 
00047 
00048 
00049 void
00050 pfree(        /* free a primitive */
00051 register PRIMITIVE  *p
00052 )
00053 {
00054 
00055  if (p->args != NULL) {
00056     freestr(p->args);
00057     p->args = NULL;
00058     } 
00059  push(p, &freelist);
00060  nalloc--;
00061 
00062  }
00063 
00064 
00065 
00066 void
00067 plfree(              /* free a primitive list */
00068 register PLIST  *pl
00069 )
00070 {
00071     register PRIMITIVE  *p;
00072     
00073     for (p = pl->ptop; p != NULL; p = p->pnext) {
00074         if (p->args != NULL) {
00075            freestr(p->args);
00076            p->args = NULL;
00077            }
00078         nalloc--;
00079         }
00080 
00081     append(pl, &freelist);
00082     pl->ptop = pl->pbot = NULL;
00083 
00084 }
00085     
00086 
00087 
00088 static int
00089 morefree(void)              /* get more free space */
00090 
00091 {
00092  register PRIMITIVE  *p;
00093  register int  i;
00094  int rnu;
00095 
00096  if (maxalloc > 0 && (i = maxalloc-nalloc) < FBSIZE)
00097     rnu = i;
00098  else
00099     rnu = i = FBSIZE;
00100 
00101  p = (PRIMITIVE *)malloc((unsigned)i * sizeof(PRIMITIVE));
00102 
00103  if (p == NULL)
00104     return(0);
00105 
00106  while (i--) {
00107     p->args = NULL;
00108     push(p, &freelist);
00109     p++;
00110     }
00111 
00112  return(rnu);
00113  }