⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fatbuffer.cpp

📁 Jazmyn is a 32-bit, protected mode, multitasking OS which runs on i386 & above CPU`s. Its complete
💻 CPP
字号:
/*
 * Copyright (C) 2004, Thejesh AP. All rights reserved.
 */

#include <sys\types.h>
#include <null.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <mm\new.h>
#include <drivers\drvreq.h>
#include <fs\devmgr.h>
#include <fs\fatvol.h>
#include <semaphore.h>
#include <fs\fatdriver.h>
#include <fs\fatbuffer.h>
#include <drivers\console.h>
#include <drivers\keyboard.h>

extern device_manager _dev_mgr;
extern FATdriver _FAT_driver;

fatbuffer::fatbuffer()
{
        head = NULL;
        FATbuf = NULL;
        FATmemsz = 0;
}

fatbuffer::~fatbuffer()
{
        delete FATbuf;
        node *p = head;
        while(head!=NULL)
        {
                p = head;
                head = head->next;
                delete p;
        }
}

int fatbuffer::init_FATbuffer(uint totsize,uint memsize)
{
        int ret = 0;
        uint FATmem = totsize;
        cout<<"Initializing FAT buffer\n";
        cout<<"Total FAT memory required :"<<FATmem<<endl;
        cout<<"Total physical memory     :"<<memsize<<endl;
        if(FATmem <= 0.35 * memsize)
        {
                ret = 1;
                FATmemsz = FATmem;
        }
        else FATmemsz = 0.2 * (FATmem + memsize);
        FATbuf = new byte[FATmemsz];
        if(FATbuf)
        {
                next_free_base = FATbuf;
                next_free_size = FATmemsz;
                return ret;
        }
        return -1;
}


void* fatbuffer::read_FAT(FATvolume &fv)
{
        uint size = fv.FATsz * fv._bpb.bps;
        if (size > FATmemsz) return NULL;
        if(head == NULL)
        {
                head = new node;
                head->drive = fv.drive;
                head->base = next_free_base;
                head->size = size;
                head->next = NULL;

                cout<<"Reading "<<fv.drive<<": drive`s FAT..."<<endl;;
                cout<<"Number of FAT sectors :"<<fv.FATsz<<endl;

                disk_req req;
                uint start = fv.FATstart + fv._pi.start_sec;
                _CR(req,DISK_READ,fv.dev,start,fv.FATsz,head->base);
                if(_dev_mgr.driver_tab[fv.slot].main(&req)<0)
                {
                        delete head;
                        head = NULL;
                        return NULL;
                }
                next_free_base = (void*)((uint)head->base + head->size);
                next_free_size = next_free_size - head->size;
                return head->base;
        }
        else
        {
                node *t = NULL;
                node *p = head;
                while(p!=NULL)
                {
                        if(p->drive == fv.drive)
                        {
                                return p->base;
                        }
                        t = p;
                        p = p->next;
                }
                if(size <= next_free_size)
                {
                        node *temp = new node;
                        temp->drive = fv.drive;
                        temp->base = next_free_base;
                        temp->size = size;
                        temp->next = NULL;

                        disk_req req;
                        uint start = fv.FATstart + fv._pi.start_sec;
                        _CR(req,DISK_READ,fv.dev,start,fv.FATsz,temp->base);
                        if(_dev_mgr.driver_tab[fv.slot].main(&req)<0)
                        {
                                delete temp;
                                return NULL;
                        }
                        t->next = temp;
                        compact();
                        return temp->base;
                }
                else    //size  > next_free_size so swap FATbuffer
                {
                        uint asize = 0;
                        node *p;
                        while(head!=NULL)
                        {
                                asize += head->size;
                                p = head;
                                if(save_FAT(p)<0) return NULL;
                                head = head->next;
                                delete p;
                                if(size <= asize) break;
                        }
                        next_free_base = FATbuf;
                        next_free_size = asize;
                        return read_FAT(fv);
                }
        }
}

void fatbuffer::compact()
{
        int cnt = 0;
        int sz = 0;
        node *p = head;
        while(p!=NULL)
        {
                p=p->next;
                cnt++;
        }
        addrs *fataddrs = new addrs[cnt];
        p = head;
        cnt = 0;
        while(p!=NULL)
        {
                fataddrs[cnt++].n = p;
                p = p->next;
        }
        sort(fataddrs,cnt);
        sz += fataddrs[0].n->size;
        int i;
        for(i=0;i<cnt-1;i++)
        {
                uint dst = (uint)fataddrs[i].n->base + fataddrs[i].n->size;
                uint src = (uint)fataddrs[i+1].n->base;
                uint len = fataddrs[i+1].n->size;
                memmove((void*)dst,(void*)src,len);
                fataddrs[i+1].n->base = (void*)dst;
                sz += fataddrs[i+1].n->size;
        }
        next_free_base = (void*)((uint)fataddrs[i].n->base+fataddrs[i].n->size);
        next_free_size = FATmemsz - sz;
        delete fataddrs;
}


void fatbuffer::sort(addrs *fataddrs,int n)
{
        for(int i=0;i<n-1;i++)
        for(int j=i+1;j<n;j++)
        if(fataddrs[i].n->base > fataddrs[j].n->base)
        {
                addrs temp = fataddrs[i];
                fataddrs[i] = fataddrs[j];
                fataddrs[j] = temp;
        }
}                
                
int fatbuffer::save_FAT(node *p)
{
        FATvolume *fv = _FAT_driver.get_FATvolume(p->drive);
        if(fv)
        {
                disk_req req;
                uint start = fv->FATstart + fv->_pi.start_sec;
                _CR(req,DISK_WRITE,fv->dev,start,fv->FATsz,p->base);
                if(_dev_mgr.driver_tab[fv->slot].main(&req)<0) return -1;
                return 0;
        }
        return -1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -