4.16 实现自己的堆管理器

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: [email protected]
*   Description: 
*        Create: 2017-11-08 18:55:16
* Last Modified: 2017-11-25 17:27:06
************************************************/
#include

#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: [email protected]
*   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: [email protected]
*   Description: 
*        Create: 2017-11-08 22:23:16
* Last Modified: 2017-11-25 16:38:33
************************************************/
#include
#include
#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;
}

你可能感兴趣的:(4.16 实现自己的堆管理器)