/*   Tree routines.   
     Start of tree routines that allocate memory in blocks.
     Until this is tested, we just use the routine in gram.y
     called make_root ().
*/


#include <stdio.h>
#include "tr.h"

#define TR_BLOCK_SIZE  (sizeof (TRnode) * 128)

extern char *progname;
TRnode *tr_block_array[1024];
TRnode *new_tr_node;
TRnode *tr_block = 0;
int tr_block_array_ind = 0;
int curr_tr_pos = 0;
static int count_me = 0;
#ifdef nada
TRnode *
make_root (pc, type)
     char *pc;
     int type;
{
  TRnode *node;

  /* if (curr_tr_pos + sizeof (TRnode) >= TR_BLOCK_SIZE) */
  if (++count_me == 2048)
    {
      count_me = 0;
      tr_block_array[tr_block_array_ind++] = tr_block;
      tr_block = (TRnode *) malloc (TR_BLOCK_SIZE);
      if (0 == tr_block)
	{
	  fprintf (stderr, "%s: memory problem in make_root\n", progname);
	  exit (1);
	}
      curr_tr_pos = 0;
      new_tr_node = tr_block;
    }
  /* node = (TRnode *) (tr_block + curr_tr_pos);

  curr_tr_pos += sizeof (TRnode); */
  node = new_tr_node;
  ++new_tr_node;
  /* node = (TRnode *) malloc (sizeof (TRnode)); */
  node->left = 0;
  node->right = 0;
  node->pc = make_string (pc);
  node->type = type;
  return node;
}
#endif

init_tree_nodes ()
{
  static int here = 0;
  if (here)
    return;
  ++here;
  
  count_me = 0;
  tr_block = (TRnode *) malloc (TR_BLOCK_SIZE);
  if (0 == tr_block)
    {
      fprintf (stderr, "%s: memory problem in init_tree_nodes\n", progname);
      exit (1);
    }
  new_tr_node = tr_block;
  return;
}

clean_tree_nodes ()
{
  int i;
  
  for (i = 0; i < tr_block_array_ind; ++i)
    if (tr_block_array[i])
      free (tr_block_array[i]);
  tr_block_array_ind = 0;
  curr_tr_pos = 0;
  count_me = 0;
  return;
}	    
	    
