4.16 實現自己的堆管理器

yzm65580952發表於2020-10-09

4.16 實現自己的堆管理器

功能要求

管理16個記憶體塊,大小範圍[8,128]
使用bitmap陣列對16個記憶體塊進行管理
可以申請的記憶體大小[1,128]
實現介面函式
pool_malloc:記憶體申請
pool_free: 釋放記憶體
pool_info: 查詢堆記憶體使用情況

程式碼

mempool.c


/************************************************
* Copyright(C) zhaixue.cc. All rights reserved
*
*      Filename: mempool.c
*        Author: litao.wang
*        E-mail: 3284757626@qq.com
*   Description: 
*        Create: 2017-11-08 18:55:16
* Last Modified: 2017-11-25 17:27:06
************************************************/
#include<stdio.h>

#define POOL_SIZE 1088
#define CHUNK_NUM 16

struct chunk{
   unsigned char *addr;
   char used;
   unsigned char size;
};

char mempool[POOL_SIZE];
struct chunk bitmap[CHUNK_NUM];

void pool_init(void)
{
   int i;
   char *p = &mempool[0];
   for(int i=0;i<CHUNK_NUM;i++)
   {
   	p = p + i*8;
   	bitmap[i].addr = p;
   	bitmap[i].size = 8 *(i+1);
   	bitmap[i].used = 0;
   }
}

int bitmap_index(int nbytes)
{
   if(nbytes%8==0)
   	return nbytes/8 -1;
   else
   	return nbytes/8;
}

void* pool_malloc(int nbytes)
{
   int i;
   int index;
   index = bitmap_index(nbytes);
   for( i=index;i<CHUNK_NUM;i++)
   {
   	if(bitmap[i].used == 0){
   		bitmap[i].used = 1;
   		return bitmap[i].addr;
   	}
   	else
   		continue;
   }
   return (void *)0;
}

void pool_free(void *p)
{
   int i;
   for(i=0;i<CHUNK_NUM;i++)
   {
   	if(bitmap[i].addr == p)
   		bitmap[i].used = 0;
   }
}

void pool_info(void)
{
   int frees = 0;
   int used_size  = 0;
   int i;
   for(i=0;i<CHUNK_NUM;i++)
   {
   	if(bitmap[i].used ==1)
   		used_size = used_size + bitmap[i].size;
   	else
   		frees++;
   }
   printf("------------------------------\n");
   printf("           memory info          \n\n");
   printf("Total size:  %d\tBytes\n",POOL_SIZE);
   printf("Used  size:  %d\tBytes\n",used_size);
   printf("Free  size:  %d\tBytes\n",POOL_SIZE-used_size);
   printf("Used Chunks: %d\n",CHUNK_NUM-frees);
   printf("Free Chunks: %d\n",frees);
   printf("Pool  usage: %d\%\n",(used_size*100/POOL_SIZE));
   printf("------------------------------\n");
}

宣告檔案


/************************************************
* Copyright(C) zhaixue.cc. All rights reserved
*
*      Filename: mempool.h
*        Author: litao.wang
*        E-mail: 3284757626@qq.com
*   Description: 
*        Create: 2017-11-25 16:36:59
* Last Modified: 2017-11-25 16:37:57
************************************************/
#ifndef __MEMPOOL_H
#define __MEMPOOL_H

void pool_init(void);
void *pool_malloc(int nbytes);
void pool_free(void *p);
void pool_info(void);

#endif

main.c


/************************************************
* Copyright(C) zhaixue.cc. All rights reserved
*
*      Filename: main.c
*        Author: litao.wang
*        E-mail: 3284757626@qq.com
*   Description: 
*        Create: 2017-11-08 22:23:16
* Last Modified: 2017-11-25 16:38:33
************************************************/
#include<stdio.h>
#include<string.h>
#include "mempool.h"

int main(void)
{
	pool_init();
	char *p=NULL;
	char *q=NULL;
	p =(char *)pool_malloc(100);
	q =(char *)pool_malloc(24);
	memcpy(p,"hello world\n",15);
	printf("%s\n",p);
	pool_info();
	pool_free(p);
	pool_free(q);
	pool_info();
	return 0;
}

相關文章