home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / compcomp / cgrammer / tr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-19  |  1.8 KB  |  87 lines

  1. /*   Tree routines.   
  2.      Start of tree routines that allocate memory in blocks.
  3.      Until this is tested, we just use the routine in gram.y
  4.      called make_root ().
  5. */
  6.  
  7.  
  8. #include <stdio.h>
  9. #include "tr.h"
  10.  
  11. #define TR_BLOCK_SIZE  (sizeof (TRnode) * 128)
  12.  
  13. extern char *progname;
  14. TRnode *tr_block_array[1024];
  15. TRnode *new_tr_node;
  16. TRnode *tr_block = 0;
  17. int tr_block_array_ind = 0;
  18. int curr_tr_pos = 0;
  19. static int count_me = 0;
  20. #ifdef nada
  21. TRnode *
  22. make_root (pc, type)
  23.      char *pc;
  24.      int type;
  25. {
  26.   TRnode *node;
  27.  
  28.   /* if (curr_tr_pos + sizeof (TRnode) >= TR_BLOCK_SIZE) */
  29.   if (++count_me == 2048)
  30.     {
  31.       count_me = 0;
  32.       tr_block_array[tr_block_array_ind++] = tr_block;
  33.       tr_block = (TRnode *) malloc (TR_BLOCK_SIZE);
  34.       if (0 == tr_block)
  35.     {
  36.       fprintf (stderr, "%s: memory problem in make_root\n", progname);
  37.       exit (1);
  38.     }
  39.       curr_tr_pos = 0;
  40.       new_tr_node = tr_block;
  41.     }
  42.   /* node = (TRnode *) (tr_block + curr_tr_pos);
  43.  
  44.   curr_tr_pos += sizeof (TRnode); */
  45.   node = new_tr_node;
  46.   ++new_tr_node;
  47.   /* node = (TRnode *) malloc (sizeof (TRnode)); */
  48.   node->left = 0;
  49.   node->right = 0;
  50.   node->pc = make_string (pc);
  51.   node->type = type;
  52.   return node;
  53. }
  54. #endif
  55.  
  56. init_tree_nodes ()
  57. {
  58.   static int here = 0;
  59.   if (here)
  60.     return;
  61.   ++here;
  62.   
  63.   count_me = 0;
  64.   tr_block = (TRnode *) malloc (TR_BLOCK_SIZE);
  65.   if (0 == tr_block)
  66.     {
  67.       fprintf (stderr, "%s: memory problem in init_tree_nodes\n", progname);
  68.       exit (1);
  69.     }
  70.   new_tr_node = tr_block;
  71.   return;
  72. }
  73.  
  74. clean_tree_nodes ()
  75. {
  76.   int i;
  77.   
  78.   for (i = 0; i < tr_block_array_ind; ++i)
  79.     if (tr_block_array[i])
  80.       free (tr_block_array[i]);
  81.   tr_block_array_ind = 0;
  82.   curr_tr_pos = 0;
  83.   count_me = 0;
  84.   return;
  85. }        
  86.         
  87.