#includeusing namespace std;
#define MEM_SIZE 200
static char mem[MEM_SIZE];
#define MINBLK 16
struct block {
struct block * next;
size_t length;
char data[0];
};
static struct block * free_list = NULL;
//free_list为空
void malloc_init(void)
{
free_list = (struct block *)mem;
free_list->next = NULL;//next指向空
free_list->length = MEM_SIZE - sizeof(struct block);//data段的长度
}
//字节对齐
void * malloc(size_t size)
{
size = (size + 7) & ~7;
struct block * prev = (struct block *) &free_list;//block首段
struct block * cur = free_list;//cur 指向空闲的块
while (cur != NULL) {
if (cur->length >= size) //判断cur指向的块是否满足所要求的大小
break;
prev = cur;
cur = cur->next;
}
if (cur == NULL)
return NULL;
//cur->length(data段的长度)-size后的大小可不可以放下block
if ((cur->length - size) >= MINBLK) {
struct block * temp = (struct block *)(cur->data + size);//temp指向新block的头
temp->next = cur->next;//新block指向下一个空白
temp->length = cur->length - size - sizeof(struct block);
prev->next = temp;
cur->length = size;
}
else {
prev->next = cur->next;
}
return cur->data;
}
void free(void * ptr)
{
if (ptr == NULL)
return;
struct block * temp = (struct block *)((char *)ptr - sizeof(struct block));
char * tempn = (char *)ptr + temp->length; // 和temp物理上相邻的下一个block的地址
struct block * prev = (struct block *) &free_list;
struct block * cur = free_list;
char * curn; // 和cur物理上相邻的下一个block的地址
// 考虑多种情况,完成free函数
// ... ...
if (free_list > temp)//空块地址大于要释放的块的地址,直接将释放的块放入空白块头部(第一种情况)
{
if (((char*)temp + sizeof(block)+temp->length) == (char*)free_list) {
temp->length += sizeof(block)+free_list->length;
free_list = temp;
}
else {
temp->next = free_list;
free_list = temp;
}
ptr = NULL;
return;
}
//若不一定插入头部,寻找插入块
while (cur != NULL && (char*)cur < (char*)temp)
{
prev = cur;
cur = cur->next;
}
//temp与前段free_list相邻
if (((char *)prev + sizeof(struct block) + prev->length) == (char*)temp)
{
prev->length =prev->length + sizeof(struct block) + temp->length;
temp = prev;
ptr = NULL;
}
else
{
prev->next = temp;
ptr = NULL;
}
//free_list在比temp地址大的地方且两者相邻
if ((char *)tempn == (char *)cur || cur == NULL)
{
temp->next = cur->next;
temp->length = sizeof(struct block) + cur->length + temp->length;
ptr = NULL;
}
else
{
temp->next = cur;
ptr = NULL;
}
}
void malloc_state(void)
{
printf("free blocks:\n");
struct block * ptr;
for (ptr = free_list; ptr != NULL; ptr = ptr->next) {
int i;
printf("%p:", ptr);
//printf("%d", ptr->length);
for (int i = 0; i < ptr->length; i++) {
printf("=");
}
printf(" ");
}
printf("\n");
}
void malloc_test(void)
{//定义abcdef为分配状态
char* a = (char*)malloc(sizeof(char));
malloc_state();
char* b = (char*)malloc(sizeof(char));
malloc_state();
char* c = (char*)malloc(sizeof(char));
malloc_state();
char* d = (char*)malloc(sizeof(char));
malloc_state();
char* e = (char*)malloc(sizeof(char));
malloc_state();
char* f = (char*)malloc(sizeof(char));
//按照adbfec的顺序进行释放会将所有的情况包含进去
malloc_state();
free(a);
malloc_state();
free(d);
malloc_state();
free(b);
malloc_state();
free(f);
malloc_state();
free(e);
malloc_state();
free(c);
malloc_state();
}
int main(int argc, const char *argv[])
{
malloc_init();
malloc_test();
system("pause");
return 0;
}