home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / CH_6.1 / XVOR / MEMORY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  1.1 KB  |  73 lines

  1. /* 
  2.  * memory.c
  3.  * 
  4.  * Practical Algorithms for Image Analysis
  5.  * 
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9. /*
  10.  * MEMORY.C
  11.  *
  12.  */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include "vor.h"
  16.  
  17.  
  18. void
  19. freeinit (fl, size)
  20.      struct Freelist *fl;
  21.      int size;
  22. {
  23.   fl->head = (struct Freenode *) NULL;
  24.   fl->nodesize = size;
  25. }
  26.  
  27. char *
  28. getfree (fl)
  29.      struct Freelist *fl;
  30. {
  31.   int i;
  32.   struct Freenode *t;
  33.  
  34.   if (fl->head == (struct Freenode *) NULL) {
  35.     t = (struct Freenode *) myalloc (sqrt_nsites * fl->nodesize);
  36.     for (i = 0; i < sqrt_nsites; i += 1)
  37.       makefree ((struct Freenode *) ((char *) t + i * fl->nodesize), fl);
  38.   }
  39.  
  40.   t = fl->head;
  41.   fl->head = (fl->head)->nextfree;
  42.  
  43.   return ((char *) t);
  44. }
  45.  
  46.  
  47. void
  48. makefree (curr, fl)
  49.      struct Freenode *curr;
  50.      struct Freelist *fl;
  51. {
  52.   curr->nextfree = fl->head;
  53.   fl->head = curr;
  54. }
  55.  
  56.  
  57. int total_alloc;
  58. char *
  59. myalloc (n)
  60.      unsigned n;
  61. {
  62.   char *t;
  63.  
  64.   if ((t = malloc (n)) == (char *) 0) {
  65.     fprintf (stderr, "Insufficient memory processing site %d (%d bytes in use)\n",
  66.              siteidx, total_alloc);
  67.     exit (1);
  68.   }
  69.   total_alloc += n;
  70.  
  71.   return (t);
  72. }
  73.