#include <stdio.h>
#include <stdlib.h>
#include <stdbool.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
*/

/* This variable will simulate the allocatable memory */

static long memory[MEM_SIZE];

/* Data used by the scheduler */
struct sched_data
{
    int num_schedules;
    double avg_used_slice;
    long used_memory;
};

/* The actual CPU scheduler is implemented here */
static void cpu_scheduler()
{
    struct student_pcb *head_proc = ready_proc;

    assert(head_proc->userdata);

    if (head_proc)
    {
        set_slice(10);
        queue_remove(&ready_proc, head_proc);
        queue_append(&ready_proc, head_proc);
    }

    head_proc = ready_proc;

    struct sched_data *sched_data = head_proc->userdata;
    sched_data->num_schedules += 1;
}

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

/* Defined in simul2018.c */
extern int N_TRY;

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

    bool try_counter_started = false;
    int tries_left = N_TRY;

    if (!proc->userdata)
    {
        proc->userdata = calloc(1, sizeof (struct sched_data));
    }

    struct sched_data *sched_data;

    while ((proc = proc->next))
    {
        sched_data = (struct sched_data*)proc->userdata;
        index = mem_get(proc->mem_need);

        if (index >= 0)
        {
            // Allocation succeeded. Set the base address, and put the
            // process in the ready queue.
            proc->mem_base = index;

            sched_data->used_memory += proc->mem_need;

            queue_remove(&new_proc, proc);
            queue_append(&ready_proc, proc);
        } else {
            // Allocation failed. Try N_try more processes before exiting.
            try_counter_started = true;
        }

        if (try_counter_started && tries_left--)
            break;
    }
}

/* 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;
    }
}

/* You may want to have the last word... */

static void my_finale() { /* Your very own code goes here */ }

/* The main scheduling routine */

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

    if (first) {
        mem_init(memory);
        finale = my_finale;
        first = 0;
        /* Add your own initialisation code here
         */
    }

    switch (event) {
    /* You may want to do this differently
     */
    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;
    }
}
