#include <stdio.h>
#include <stdlib.h>

#include "schedule.h"
#include "mem_alloc.h"

/* This file contains a bare bones skeleton for the scheduler function
   for the second assignment for the OSN course of the 2005 fall
   semester.
   Author:

   G.D. van Albada
   Section Computational Science
   Universiteit van Amsterdam

   Date:
   October 23, 2003
   Modified:
   September 29, 2005
*/

#define N_TRY 5
#define SLICE 10

/* This variable will simulate the allocatable memory */

static long memory[MEM_SIZE];

/* Returns the proces with the lowest memory need from the new queue */

student_pcb* low_mem_need() {
    student_pcb *proc;
    student_pcb *low_proc;

    proc = new_proc;
    low_proc = new_proc;

    while(proc) {
        if (proc->mem_need < low_proc->mem_need) {
            low_proc = proc;
        }
        proc = proc->next;
    }

    return low_proc;
}

/* Returns the proces with the highest memory need from the ready queue */

student_pcb* high_mem_need() {
    student_pcb *proc;
    student_pcb *high_proc;

    proc = ready_proc;
    high_proc = ready_proc;

    while(proc) {
        if (proc->mem_need > high_proc->mem_need) {
            high_proc = proc;
        }
        proc = proc->next;
    }

    return high_proc;
}

/* The actual CPU scheduler is implemented here */

static void cpu_scheduler() {
    student_pcb *proc;
    proc = high_mem_need();
    if (proc) {
        queue_remove(&ready_proc, proc);
        set_slice(SLICE);
        queue_append(&ready_proc, proc);
    }
}


/* The high-level memory allocation scheduler is implemented here */

static void give_memory() {
    int index;
    student_pcb *proc;

    //proc = low_mem_need();

    proc = low_mem_need();

    if (proc) {
        index = mem_get(proc->mem_need);
        if (index >= 0) {
            proc->mem_base = index;
            queue_remove(&new_proc, proc);
            queue_append(&ready_proc, proc);
        }
    }


}

/* Here we reclaim the memory of a process after it
  has finished */

static void reclaim_memory() {
    student_pcb *proc;

    proc = defunct_proc;
    while (proc) {
        /* Free your own administrative structure if it exists
         */
        if (proc->userdata) {
            free(proc->userdata);
        }
        /* Free the simulated allocated memory
         */
        mem_free(proc->mem_base);
        proc->mem_base = -1;

        /* Call the function that cleans up the simulated process
         */
        rm_process(&proc);

        /* See if there are more processes to be removed
         */
        proc = defunct_proc;
    }
}

/* The main scheduling routine */

void schedule(event_type event) {
    static int first = 1;

    if (first) {
        mem_init(memory);
        finale = my_finale;
        first = 0;
    }

    switch (event) {
    case NEW_PROCESS_EVENT:
        give_memory();
        break;
    case TIME_EVENT:
    case IO_EVENT:
        cpu_scheduler();
        break;
    case READY_EVENT:
        break;
    case FINISH_EVENT:
        reclaim_memory();
        give_memory();
        cpu_scheduler();
        break;
    default:
        printf("I cannot handle event nr. %d\n", event);
        break;
    }
}
