home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume26 / unproto / part01 / tok_pool.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-07  |  2.1 KB  |  105 lines

  1. /*++
  2. /* NAME
  3. /*    tok_pool 3
  4. /* SUMMARY
  5. /*    maintain pool of unused token structures
  6. /* PACKAGE
  7. /*    unproto
  8. /* SYNOPSIS
  9. /*    #include "token.h"
  10. /*
  11. /*    struct token *tok_alloc()
  12. /*
  13. /*    void tok_free(t)
  14. /*    struct token *t;
  15. /* DESCRIPTION
  16. /*    tok_alloc() and tok_free() maintain a pool of unused token
  17. /*    structures.
  18. /*
  19. /*    tok_alloc() takes the first free token structure from the pool
  20. /*    or allocates a new one if the pool is empty.
  21. /*
  22. /*    tok_free() adds a (possibly composite) token structure to the pool.
  23. /* BUGS
  24. /*    The pool never shrinks.
  25. /* AUTHOR(S)
  26. /*    Wietse Venema
  27. /*    Eindhoven University of Technology
  28. /*    Department of Mathematics and Computer Science
  29. /*    Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  30. /* LAST MODIFICATION
  31. /*    91/09/01 23:08:36
  32. /* VERSION/RELEASE
  33. /*    1.1
  34. /*--*/
  35.  
  36. static char pool_sccsid[] = "@(#) tok_pool.c 1.1 91/09/01 23:08:36";
  37.  
  38. /* C library */
  39.  
  40. extern char *malloc();
  41.  
  42. /* Application-specific stuff */
  43.  
  44. #include "token.h"
  45. #include "vstring.h"
  46.  
  47. extern void error();
  48.  
  49. #define    TOKLEN    5            /* initial string buffer length */
  50.  
  51. struct token *tok_pool = 0;        /* free token pool */
  52.  
  53. /* tok_alloc - allocate token structure from pool or heap */
  54.  
  55. struct token *tok_alloc()
  56. {
  57.     register struct token *t;
  58.  
  59.     if (tok_pool) {                /* re-use an old one */
  60.     t = tok_pool;
  61.     tok_pool = t->next;
  62.     } else {                    /* create a new one */
  63.     if ((t = (struct token *) malloc(sizeof(struct token))) == 0
  64.         || (t->vstr = vs_alloc(TOKLEN)) == 0)
  65.         error(1, "out of memory");
  66.     }
  67.     t->next = t->head = t->tail = 0;
  68. #ifdef    DEBUG
  69.     strcpy(t->vstr->str, "BUSY");
  70. #endif
  71.     return (t);
  72. }
  73.  
  74. /* tok_free - return (possibly composite) token to pool of free tokens */
  75.  
  76. void    tok_free(t)
  77. register struct token *t;
  78. {
  79. #ifdef DEBUG
  80.     /* Check if we are freeing free token */
  81.  
  82.     register struct token *p;
  83.  
  84.     for (p = tok_pool; p; p = p->next)
  85.     if (p == t)
  86.         error(1, "freeing free token");
  87. #endif
  88.  
  89.     /* Free neighbours and subordinates first */
  90.  
  91.     if (t->next)
  92.     tok_free(t->next);
  93.     if (t->head)
  94.     tok_free(t->head);
  95.  
  96.     /* Free self */
  97.  
  98.     t->next = tok_pool;
  99.     t->head = t->tail = 0;
  100.     tok_pool = t;
  101. #ifdef    DEBUG
  102.     strcpy(t->vstr->str, "FREE");
  103. #endif
  104. }
  105.