1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
| #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <stdio.h> #include <stdlib.h> #define MAPSIZE 100 struct map //存储资源表结构 { 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++) { 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); }
int WF_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++) { if(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); getchar(); bp->m_addr=addr; bp->m_size=size; (++bp)->m_size=0; } void show_map() { int i=0; 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; char c; int i; init(); printf("please input, b for BF, w for WF:"); scanf("%c",&c); getchar(); do { show_map(); printf("Please input,1 for request,2 for release,0 for exit:"); scanf("%d",&i); getchar(); switch(i) { case 1: printf("Please input size:"); scanf("%d", &s); getchar(); if(c=='b') a=BF_malloc(map,s); else a=WF_malloc(map,s); if(a==-1) 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); getchar(); mfree(map,a,s); break; case 0: exit(0); } } while(1); }
|