// file: tr_mm_cstr_0.cc
//

// isip include files
//
#include "train_memory_manager.h"
#include "train_memory_manager_constants.h"
#include <train_trace.h>
#include <train_lex_node.h>
#include <train_lattice_node.h>
#include <train_hash_cell.h>

// method: destructor
//
// arguments: none
//
// return: none
//
// this is the default destructor
//
Train_Memory_manager::~Train_Memory_manager() {

  // dummy variables
  //
  Train_Trace* tr = (Train_Trace*)NULL;  
  Train_Lex_node* lxn = (Train_Lex_node*)NULL;  
  Train_Lattice_node* ltn = (Train_Lattice_node*)NULL;  
  Train_Hash_cell* hcell = (Train_Hash_cell*)NULL;  
  Train_Link_node* tmp_node = (Train_Link_node*)NULL;

  // delete the memory of the free lex nodes
  //
  for (Train_Link_node* node = lex_list_d; node != (Train_Link_node*)NULL;
       node = tmp_node) {
    tmp_node = node->get_next_cc();
    lxn = (Train_Lex_node*)(node->get_item_cc());
    if (lxn != (Train_Lex_node*)NULL) {
      delete lxn;
      lex_count_d--;
    }
    delete_cc(node);
  }
  lex_list_d = (Train_Link_node*)NULL;
  
  // delete the memory of the free lattice nodes
  //
  for (Train_Link_node* node = lat_list_d; node != (Train_Link_node*)NULL;
       node = tmp_node) {
    tmp_node = node->get_next_cc();
    ltn = (Train_Lattice_node*)(node->get_item_cc());
    if (ltn != (Train_Lattice_node*)NULL) {
      delete ltn;
      lat_count_d--;
    }
    delete_cc(node);
  }
  lat_list_d = (Train_Link_node*)NULL;
  
  // delete the memory of the free hash table cells
  //
  for (Train_Link_node* node = hash_list_d; node != (Train_Link_node*)NULL;
       node = tmp_node) {
    tmp_node = node->get_next_cc();
    hcell = (Train_Hash_cell*)(node->get_item_cc());
    if (hcell != (Train_Hash_cell*)NULL) {
      delete hcell;
      hash_count_d--;
    }
    delete_cc(node);
  }
  hash_list_d = (Train_Link_node*)NULL;
  
  // delete memory of the free traces
  //
  for (Train_Link_node* node = trace_list_d; node != (Train_Link_node*)NULL;
       node = tmp_node) {
    tmp_node = node->get_next_cc();
    tr = (Train_Trace*)(node->get_item_cc());
    if (tr != (Train_Trace*)NULL) {
      delete tr;
      trace_count_d--;
    }
    delete_cc(node);
  }
  trace_list_d = (Train_Link_node*)NULL;
  
  // delete memory of the free nodes
  //  
  for (Train_Link_node* node = node_list_d; node != (Train_Link_node*)NULL;
       node = tmp_node) {
    tmp_node = node->get_next_cc();
    delete node;
    node_count_d--;
    if(node_count_d == 0) {
      break;
    }
  }
  node_list_d = (Train_Link_node*)NULL;

  // reset the counts
  //
  node_count_d = (int_4)0;
  trace_count_d = (int_4)0;
  lex_count_d = (int_4)0;
  lat_count_d = (int_4)0;
  hash_count_d = (int_4)0;

  // reset the block sizes
  //
  node_grow_size_d = (int_4)0;
  trace_grow_size_d = (int_4)0;
  lex_grow_size_d = (int_4)0;
  lat_grow_size_d = (int_4)0;
  hash_grow_size_d = (int_4)0;
  
  // exit gracefully
  //
}
