果然不到50行,果然可以在2小时上机写完,管然面试让15分钟写出来不过分
const int SEG_LEN = 128-4;
struct Mem_seg
{
char mem [SEG_LEN];
Mem_seg * next;
};
struct Mem_list
{
Mem_list(Mem_seg *h, Mem_seg *e)
:head(h),end(e)
{
}
Mem_seg * head;
Mem_seg * end;
};
Mem_list mem_init(char * all_mem, int free_size)
{
int seg_num = free_size / SEG_LEN;
for(int i=0;i
((Mem_seg *)&all_mem[i * SEG_LEN])->next=(Mem_seg *)&all_mem[(i+1)*SEG_LEN];
return Mem_list((Mem_seg *)all_mem,(Mem_seg *)&all_mem[seg_num * SEG_LEN]);
}
Mem_list mem_alloc(Mem_list &free_list, int size)
{
int n=size/SEG_LEN;
if(size > n * SEG_LEN)
++n;
Mem_seg *phead=free_list.head;
Mem_seg *pend=free_list.head;
for(int i =0;i pend=pend->next;
free_list.head=pend;
return Mem_list(phead,pend);
}
int mem_free(Mem_list &free_list, Mem_list recycle_list)
{
Mem_seg *ptemp=recycle_list.head;
while(ptemp->next != recycle_list.end)
ptemp=ptemp->next;
ptemp->next=free_list.head;
free_list.head=recycle_list.head;
}
void test_list(Mem_list test_list)
{
std::ofstream outfile;
outfile.open("test.txt", std::ios::app);
Mem_seg * phead = test_list.head;
Mem_seg * pend = test_list.end;
for(int i=0; phead != pend;phead=phead->next,i++)
outfile<mem)<<",\t";
outfile< outfile.close();
}
阅读(1320) | 评论(0) | 转发(0) |