// file: mm_new_7.cc
//
 
// isip include files
//
#include "memory_manager.h"
#include "memory_manager_constants.h"

#include <instance.h>

// method: new_instance_cc
//
// arguments: none
//
// return: a Instance* pointer to a new instance node
//
// this method outputs a new empty instance, and creates them whenever
// necessary
//
Instance* Memory_manager::new_instance_cc() {

  // if there are no free instances available, create a few
  //
  if (instance_count_d == (int_4)0) {
    grow_instance_cc();
  }

  // get a instance off the list
  //
  Link_node* lnd = instance_list_d;
  instance_list_d = instance_list_d->get_next_cc();

  // adjust params
  //
  lnd->set_next_cc((Link_node*)NULL);

  if (instance_list_d != (Link_node*)NULL) {
    instance_list_d->set_prev_cc((Link_node*)NULL);
  }

  // get the instance here
  //
  Instance* inst = (Instance*)(lnd->get_item_cc());

  // return the link node to the free node list
  //
  delete_cc(lnd);

  // decrement free instance count
  //
  instance_count_d--;
  
  // return the instance and exit gracefully
  //
  return inst;
}
