Lions Unix Code Annotating
#include <iostream>
using namespace std;
struct map
{
unsigned int * m_size;//orignal @_ char * m_size;
unsigned int * m_addr;// orignal @_ char * m_addr;
}
int my_malloc(map * mp,unsigned int * size)
{
register unsigned int * a;
map * bp;
for(bp = mp;bp->m_size;bp++)
{
if(bp->m_size >= size)//if the block finded is bigger than we need
{
a = bp->m_addr;//"a" store the const address of the function malloc
bp->m_addr =+ size;//new pb update
}
if((bp->m_size -= size == 0))//if the area allocrated is just equal to the first blcok we find then
do{
bp++;//pointer point to the next block
(bp-1)->m_addr = bp->m_addr;//the next's addr is copied to the pre
}while((bp-1)->m_size =bp->m_size);//the next's m_size is copied to the pre and when the m_size is 0 loop is stopped;
return a;//return the addr we just find ;
}
}
void * mfree(map *mp,int size,void * aa)//free the block; which the begin addr is aa and the length is size and the allocate place is in the map which the begin addr is mp
{
register struct map * bp;
register int t;//temp value for Xchange
register int a;
a = aa;//a is a pointer whos orignal value is equal to aa
for(bp=mp;bp->m_addr <= a && bp->m_size ;bp++);//find the block's addr which is just bigger than aa
if((bp-1)->m_addr+(bp-1)->m_size == a && bp>mp)//if the bp-1 block is just next to the destinetion block and the destination block is not at the
{ //beginning of the map;this condition is queite exsit : the des block is just
(bp-1)->m_size = =+ size;
if(a+size == bp->m_addr)//if the destination block is next to the bp block ;at this condotion the destination block is between the two blocks the
//bp block the destination block has combined with the bp-1 blcock
// so we can only combine the bp block and the bp-1 blcock
{
(bp-1)->m_size =+ bp->m_size;
while(bp->m_size)
{
bp++;//copy the bp's block info to (bp-1)'s block;at this time the the num of the map is unchanged
(bp-1)->m_addr = bp->m_addr;
(bp-1)->m_size = bp->m_size;
}
}
else//when the destination block is not next to the bp-1 block;
{
if(a+size == bp->m_addr && bp->m_size)//when the destination block is just next to the bp block
{
bp->m_addr = a;//bp block's adddr is change into the a
bp->m_size =+ size;//bp block's size is bigger
}
else
if(size)
do
{
bp++;
t = bp->m_addr;
bp->m_addr = a;
a=t;// the Xchange between destination's a and bp->m_addr
t = bp->m_size;
bp->m_size = size//the Xchange between destiantion block's size and bp->size
}while(size=t);//the third Xchange is here!!!
}
}
}
int main()
{
cout<<"asdfasd"<<endl;
system("pause");
return 0;
}

浙公网安备 33010602011771号