// file: mm_cstr_0.cc
//

// isip include files
//
#include "memory_manager.h"
#include "memory_manager_constants.h"
#include <trace.h>
#include <lex_node.h>
#include <lattice_node.h>
#include <lattice_path.h>
#include <ngram_node.h>
#include <hash_cell.h>
#include <history.h>
#include <instance.h>

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

  // dummy variables
  //
  Trace* tr = (Trace*)NULL;  
  Lex_node* lxn = (Lex_node*)NULL;  
  Lattice_node* ltn = (Lattice_node*)NULL;  
  Lattice_path* lpth = (Lattice_path*)NULL;  
  Ngram_node* ngn = (Ngram_node*)NULL;  
  Hash_cell* hcell = (Hash_cell*)NULL;
  History* hist = (History*)NULL;
  Instance* inst = (Instance*)NULL;  
  Link_node* tmp_node = (Link_node*)NULL;

  // delete the memory of the free lex nodes
  //
  for (Link_node* node = lex_list_d; node != (Link_node*)NULL;
       node = tmp_node) {
    tmp_node = node->get_next_cc();
    lxn = (Lex_node*)(node->get_item_cc());
    if (lxn != (Lex_node*)NULL) {
      delete lxn;
      lex_count_d--;
    }
    delete_cc(node);
  }
  lex_list_d = (Link_node*)NULL;
  
  // delete the memory of the free lattice nodes
  //
  for (Link_node* node = lat_list_d; node != (Link_node*)NULL;
       node = tmp_node) {
    tmp_node = node->get_next_cc();
    ltn = (Lattice_node*)(node->get_item_cc());
    if (ltn != (Lattice_node*)NULL) {
      delete ltn;
      lat_count_d--;
    }
    delete_cc(node);
  }
  lat_list_d = (Link_node*)NULL;
  
  // delete the memory of the free lattice paths
  //
  for (Link_node* node = lpath_list_d; node != (Link_node*)NULL;
       node = tmp_node) {
    tmp_node = node->get_next_cc();
    lpth = (Lattice_path*)(node->get_item_cc());
    if (lpth != (Lattice_path*)NULL) {
      delete lpth;
      lpath_count_d--;
    }
    delete_cc(node);
  }
  lpath_list_d = (Link_node*)NULL;
  
  // delete the memory of the free history nodes
  //
  for (Link_node* node = hist_list_d; node != (Link_node*)NULL;
       node = tmp_node) {
    tmp_node = node->get_next_cc();
    hist = (History*)(node->get_item_cc());
    if (hist != (History*)NULL) {
      delete hist;
      hist_count_d--;
    }
    delete_cc(node);
  }
  hist_list_d = (Link_node*)NULL;

  // delete the memory of the free instances
  //
  for (Link_node* node = instance_list_d; node != (Link_node*)NULL;
       node = tmp_node) {
    tmp_node = node->get_next_cc();
    inst = (Instance*)(node->get_item_cc());
    if (inst != (Instance*)NULL) {
      delete inst;
      instance_count_d--;
    }
    delete_cc(node);
  }
  instance_list_d = (Link_node*)NULL;
  
  // delete the memory of the free hash table cells
  //
  for (Link_node* node = hash_list_d; node != (Link_node*)NULL;
       node = tmp_node) {
    tmp_node = node->get_next_cc();
    hcell = (Hash_cell*)(node->get_item_cc());
    if (hcell != (Hash_cell*)NULL) {
      delete hcell;
      hash_count_d--;
    }
    delete_cc(node);
  }
  hash_list_d = (Link_node*)NULL;
  
  // delete memory of the free traces
  //
  for (Link_node* node = trace_list_d; node != (Link_node*)NULL;
       node = tmp_node) {
    tmp_node = node->get_next_cc();
    tr = (Trace*)(node->get_item_cc());
    if (tr != (Trace*)NULL) {
      delete tr;
      trace_count_d--;
    }
    delete_cc(node);
  }
  trace_list_d = (Link_node*)NULL;
  
  // delete the memory of the free ngram nodes
  //
  for (Link_node* node = ngram_list_d; node != (Link_node*)NULL;
       node = tmp_node) {
    tmp_node = node->get_next_cc();
    ngn = (Ngram_node*)(node->get_item_cc());
    if (ngn != (Ngram_node*)NULL) {
      delete ngn;
      ngram_count_d--;
    }
    delete_cc(node);
  }
  ngram_list_d = (Link_node*)NULL;

  // delete memory of the free nodes
  //  
  for (Link_node* node = node_list_d; node != (Link_node*)NULL;
       node = tmp_node) {
    tmp_node = node->get_next_cc();
    delete node;
    node_count_d--;    
  }
  node_list_d = (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;
  lpath_count_d = (int_4)0;
  ngram_count_d = (int_4)0;
  hash_count_d = (int_4)0;
  hist_count_d = (int_4)0;
  instance_count_d = (int_4)0;

  // reset the total generation counts
  //
  num_node_d = (int_4)0;
  num_trace_d = (int_4)0;
  num_lex_d = (int_4)0;
  num_lat_d = (int_4)0;
  num_lpath_d = (int_4)0;
  num_ngram_d = (int_4)0;
  num_hash_d = (int_4)0;
  num_hist_d = (int_4)0;
  num_instance_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;
  lpath_grow_size_d = (int_4)0;
  ngram_grow_size_d = (int_4)0;
  hash_grow_size_d = (int_4)0;
  hist_grow_size_d = (int_4)0;
  instance_grow_size_d = (int_4)0;
  
  // exit gracefully
  //
}

