view cos/kernel/mm.c @ 26:dcce92b1efbc

Added mm.c
author windel
date Tue, 27 Dec 2011 17:36:52 +0100
parents
children 47b7df514243
line wrap: on
line source


/* Memory manager */

#include "kernel.h"

// BITMAP functions:
#define NFRAMES 1000
// Bit mask to check what frames are in use and which are free:
uint64_t frames[NFRAMES]; // NFRAMES*64*4kB frames.

void set_frame(uint64_t frame)
{
   uint64_t idx = frame / 64;
   uint64_t off = frame % 64;
   frames[idx] |= (0x1 << off);
}

void clear_frame(uint64_t frame)
{
   uint64_t idx = frame / 64;
   uint64_t off = frame % 64;
   frames[idx] &= ~(0x1 << off);
}

uint64_t test_frame(uint64_t frame)
{
   uint64_t idx = frame / 64;
   uint64_t off = frame % 64;
   return (frames[idx] & (0x1 << off));
}

uint64_t first_frame()
{
   uint64_t i, j;
   for (i = 0; i < NFRAMES / 64; i++)
   {
      if (frames[i] != 0xFFFFFFFFFFFFFFFF)
      {
         for (j = 0; j < 64; j++)
         {
            uint64_t dut = 0x1 << j;
            if ((frames[i] & dut) != dut)
            {
               return i*64+j;
            }
         }
      }
   }
   return 0xFFFFFFFFFFFFFFFF;
}

// Memory manager functions:
void alloc_frame()
{
   uint64_t idx = first_frame();
   if (idx == (uint64_t) -1)
   {
      panic("No more memory!");
   }
   set_frame(idx);
}