Back to index

wims  3.65+svn20090927
memory.c
Go to the documentation of this file.
00001 
00002 /*** MEMORY.C ***/
00003 
00004 #include <stdio.h>
00005 #include <stdlib.h>  /* malloc(), exit() */
00006 
00007 #include "vdefs.h"
00008 
00009 extern int sqrt_nsites, siteidx ;
00010 
00011 void
00012 freeinit(Freelist * fl, int size)
00013     {
00014     fl->head = (Freenode *)NULL ;
00015     fl->nodesize = size ;
00016     }
00017 
00018 char *
00019 getfree(Freelist * fl)
00020     {
00021     int i ;
00022     Freenode * t ;
00023     if (fl->head == (Freenode *)NULL)
00024         {
00025         t =  (Freenode *) myalloc(sqrt_nsites * fl->nodesize) ;
00026         for(i = 0 ; i < sqrt_nsites ; i++)
00027             {
00028             makefree((Freenode *)((char *)t+i*fl->nodesize), fl) ;
00029             }
00030         }
00031     t = fl->head ;
00032     fl->head = (fl->head)->nextfree ;
00033     return ((char *)t) ;
00034     }
00035 
00036 void
00037 makefree(Freenode * curr, Freelist * fl)
00038     {
00039     curr->nextfree = fl->head ;
00040     fl->head = curr ;
00041     }
00042 
00043 int total_alloc ;
00044 
00045 char *
00046 myalloc(unsigned n)
00047     {
00048     char * t ;
00049     if ((t=malloc(n)) == (char *) 0)
00050         {
00051         fprintf(stderr,"Insufficient memory processing site %d (%d bytes in use)\n",
00052         siteidx, total_alloc) ;
00053         exit(0) ;
00054         }
00055     total_alloc += n ;
00056     return (t) ;
00057     }