↑ Back to main site.

EPOS

Experimental Protected-mode Operating System

memory.c
/*
EPOS
http://www.atanaslaskov.com/epos/

FILE:        memory.c
DESCRIPTION: Dynamic memory managment
 
BSD LICENSE
Copyright (c) 2006, Atanas Laskov
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
   1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "memory.h"

#include "..\gdt\gdt.h"
#include "..\access\access.h"

// Data structures
//
struct MemBlock_internal{
    unsigned long dwStart;
    unsigned long dwEnd;
    unsigned char bValid;
};

static struct MemBlock_internal g_arBlocks[MEM_MAX_BLOCK_COUNT];
unsigned long g_dwMemSize = 0;
unsigned long g_dwFreeMem = 0;
unsigned long g_dwFreeBlock = MEM_MAX_BLOCK_COUNT;

// Initialize memory manager
//
void memInit() {
    g_dwMemSize = 0;
    g_dwFreeMem = 0;
    g_dwFreeBlock = MEM_MAX_BLOCK_COUNT;

    // Detect the amount of available memory
    //
    unsigned char bFound;
    HMEMORY i;
   
    while(1) {
        bFound = 1;
       
        gdt_linear_setb(g_dwMemSize+MEM_START, 0);
        if(gdt_linear_getb(g_dwMemSize+MEM_START)!=0) bFound=0;
       
        gdt_linear_setb(g_dwMemSize+MEM_START, 0xFA);
        if(gdt_linear_getb(g_dwMemSize+MEM_START)!=0xFA) bFound=0;
       
        if(bFound) g_dwMemSize += 0x100000;
        else break;
    }
   
    g_dwFreeMem = g_dwMemSize;
   
    //  Print out the detected amount
    puts("\nKRN: "); putd((g_dwMemSize+MEM_START)/0x100000);
    puts("MB memory found");

    //Invalidate all memory blocks
    for(i=0; i<MEM_MAX_BLOCK_COUNT; i++) g_arBlocks[i].bValid = 0;
}

// Allocate a block of memory
//
MEMRET memAlloc(HMEMORY *ph, unsigned long dwBytes)
{
    HMEMORY i,j;
    unsigned long dwStart=0;
    unsigned long dwEnd=0;
    unsigned short bAcceptible;

    // Round up size to 4K pages
    //
    dwBytes = dwBytes/(PSTEP)+1; //4k page count
    dwBytes = dwBytes*(PSTEP);   //byte count

    if(dwBytes==0) {
        /*puts("Invalid Block");*/
        return MEMRET_INVBLOCK;
    }
   
    if(dwBytes>=g_dwMemSize) {
        /*puts("Out of Memory");*/
        return MEMRET_OUTOFMEM;
    }

    // Find a free block descriptor
    //
    for(i=0; i<MEM_MAX_BLOCK_COUNT; i++) if(!g_arBlocks[i].bValid) break;
    if(i>=MEM_MAX_BLOCK_COUNT) {
        /*puts("Out of Block Descriptors");*/
        return MEMRET_OUTOFBLOCKS;
    }

    // Find base address
    //
    dwEnd = MEM_START + g_dwMemSize - dwBytes;
   
    for(dwStart=MEM_START; dwStart<dwEnd; dwStart+PSTEP)
    {
        //Is this base free
        bAcceptible = 1;
        for(j=0; j<MEM_MAX_BLOCK_COUNT; j++) if(g_arBlocks[j].bValid)
        {
            if( ((dwStart+dwBytes)<g_arBlocks[j].dwStart) ||
                (dwStart>g_arBlocks[j].dwEnd) ) continue;
            bAcceptible = 0;
            break;
        }
        if(bAcceptible) break;
    }
   
    if(dwStart>=(MEM_START+g_dwMemSize-dwBytes)) {
        /*puts("Out of Memory");*/
        return MEMRET_OUTOFMEM;
    }

    // Register the block as allocated
    //
    g_arBlocks[i].bValid  = 1;
    g_arBlocks[i].dwStart = dwStart;
    g_arBlocks[i].dwEnd   = dwStart+dwBytes;
    g_dwFreeMem-=dwBytes;
    *ph = i;
   
    g_dwFreeMem -= dwBytes;
    g_dwFreeBlock--;

    ((char*)memFormPointer(i))[dwBytes-1] = 5;
   
    return MEMRET_OK;
}

// Free a block of memory
//
MEMRET memFree(HMEMORY h)
{
    if(h>=MEM_MAX_BLOCK_COUNT) return MEMRET_INVBLOCK;
    if(!g_arBlocks[h].bValid)  return MEMRET_INVBLOCK;
   
    g_arBlocks[h].bValid = 0;
    g_dwFreeMem += (g_arBlocks[h].dwEnd-g_arBlocks[h].dwStart);
    g_dwFreeBlock++;
   
    return MEMRET_OK;
}

// Get information about an existing block
//
MEMRET memBlockInfo(HMEMORY h, struct MemBlock *pInfo)
{
    if(h>=MEM_MAX_BLOCK_COUNT) return MEMRET_INVBLOCK;
    if(!g_arBlocks[h].bValid)  return MEMRET_INVBLOCK;
   
    //kMemCpy(pInfo, g_arBlocks+h, sizeof(struct MemBlock));
    pInfo->dwStart = g_arBlocks[h].dwStart;
    pInfo->dwEnd = g_arBlocks[h].dwEnd;

    return MEMRET_OK;
}

// Convert memory handler to a C pointer
//
void*  memFormPointer(HMEMORY h)
{
    if(h>=MEM_MAX_BLOCK_COUNT) return 0;
    if(!g_arBlocks[h].bValid)  return 0;
   
    return (void*)((char*)(g_arBlocks[h].dwStart)-0x600);
}

// Get memory status
//
unsigned long memGetPhysicalMemorySize() {
    return g_dwMemSize+MEM_START;
}

unsigned long memGetFreePhysicalMemorySize() {
    return g_dwFreeMem;
}

unsigned long memGetFreeBlockCnt() {
    return g_dwFreeBlock;
}