📄 bfmap.c
字号:
/*************************************************************************** * Copyright (C) 2004 by root * * root@simplymepis * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <stdio.h>#include <stdlib.h>#define MAPSIZE 100struct map //存储资源表结构{ /* char *m_size; char *m_addr; */ int m_addr; int m_size;};struct map map[MAPSIZE]; //存储资源表int BF_malloc(struct map *mp,int size) //存储分配函数{ register int a,s; register struct map *bp,*bpp; for(bp = mp; bp->m_size; bp++) { if (bp->m_size >= size) { a = bp->m_addr; s = bp->m_size; for(bpp = bp; bpp->m_size; bpp++)//find the best fit { if(bpp->m_size >= size && bpp->m_size < s) { a = bpp->m_addr; s = bpp->m_size; bp = bpp; } } bp->m_addr += size; if ((bp->m_size -=size) == 0) do { bp++; (bp-1)->m_addr = bp->m_addr; } while((bp-1)->m_size = bp->m_size); return(a); } } return(-1);}void mfree(struct map *mp,int aa,int size) //存储释放函数{ register struct map *bp; register int t; register int a; a = aa; for(bp = mp; bp->m_addr<=a && bp->m_size != 0; bp++) ; if(bp>mp && (bp-1)->m_addr+(bp-1)->m_size==a) { //与前合并 (bp-1)->m_size += size; if (a+size == bp->m_addr) { //前后合并 (bp-1)->m_size += bp->m_size; while (bp->m_size) { bp++; (bp-1)->m_addr = bp->m_addr; (bp-1)->m_size = bp->m_size; } } } else { if (a+size == bp->m_addr && bp->m_size) { //与后合并 bp->m_addr -= size; bp->m_size += size; } else if (size) do { //无合并 t = bp->m_addr; bp->m_addr = a; a = t; t = bp->m_size; bp->m_size = size; bp++; } while (size = t); }}void init(){ struct map *bp; int addr,size; int i=0; bp=map; printf("Please input starting addr and total size:"); scanf("%d,%d",&addr,&size); bp->m_addr=addr; bp->m_size=size; (++bp)->m_size=0; //表尾}void show_map(){ int i=0; system("clear"); //调用清屏命令 struct map *bp; bp=map; printf("\nCurrent memory map...\n"); printf("Address\t\tSize\n"); while(bp->m_size!=0) { printf("<%d\t\t%d>\n",bp->m_addr,bp->m_size); bp++; } printf("\n");}main(){ int a,s; int c; int i; init(); do { //显示存储分配表 show_map(); printf("Please input, 1 for request,2 for release, 0 for exit:"); scanf("%d",&c); switch(c) { case 1: printf("Please input size:"); scanf("%d", &s); if((a=BF_malloc(map,s))==-1) //call malloc printf("request can't be satisfied\n"); else printf("alloc memory at address:%d,size:%d\n",a,s); break; case 2: printf("Please input addr and size:"); scanf("%d,%d",&a,&s); mfree(map,a,s); break; case 0: exit(0); } } while(1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -