最近一直在学习实时操作系统内核,突然发觉自己对操作系统设计产生了浓厚的兴趣,打算这次项目做完有空余时间了自己也搞个实时内核吧。这里做了一个小型的内存管理包,如同malloc可以给指针分配空间,由于我的内存管理系统严格要求内存分布的连续性,所以不能分配很大的空间(即每次分配不能超过2kb,因为每个块我设定为2kb,内存池最大容量32kb)分配大空间的算法还在思考中,哎~总觉得有点遗憾,但还是贴出代码吧,大家多提意见。
/* common.h */
#ifndef __COMMON_H
#define __COMMON_H
#define _PER_KBYTE 1024
#define BLK_SIZE _PER_KBYTE * 2
#define MAX_BLK 16
#define NUL(datatype) (datatype)0
typedef unsigned char byte_t;
struct xcore_memoff {
void * addr; /* Begin address of a data block */
size_t size; /* Length of the data block */
xcore_memoff *next; /* Address of next data block */
};
struct xcore_memblk {
byte_t blk[BLK_SIZE]; /* A static memory block */
size_t free_size; /* Free size of the block */
xcore_memoff *last_offset;
};
xcore_memoff memoff_map[MAX_BLK]; /* A map for inquiring used address */
xcore_memblk mem_pool[MAX_BLK]; /* A memory pool */
#endif
/* memmgr.cpp */
#include <stdio.h>
#include <stdlib.h>
#include <mem.h>
#include <conio.h>
#include <string.h>
#include "common.h"
void
init_memmgr( void ) {
/* counter */
register int i;
/* initialize inquiring map */
for(i = 0; i < MAX_BLK; i++) {
memoff_map[i].addr = NUL(void *);
memoff_map[i].size = 0;
memoff_map[i].next = NUL(xcore_memoff *);
}
/* initialize memory pool */
for(i = 0; i < MAX_BLK; i++) {
mem_pool[i].free_size = BLK_SIZE;
mem_pool[i].last_offset = &memoff_map[i];
}
}
void *
getblock(size_t size) {
/* allocating size is too large */
if(size > BLK_SIZE)
return NUL(void *);
/* for returning usage */
void *blk_addr = NUL(void *);
/* for holding the pointer of mem offset node */
xcore_memoff *offnode = NUL(xcore_memoff *);
size_t remain;
/* counter */
register int i;
for(i = 0; i < MAX_BLK; i++) {
if((remain = mem_pool[i].free_size) >= size) {
/* get the begining address */
blk_addr = (void *)(&mem_pool[i].blk[remain - 1]);
/* create a new offset map node */
offnode = (xcore_memoff *)malloc(sizeof(xcore_memoff));
if(!offnode)
return NUL(void *);
/* configure node */
offnode->next = NUL(xcore_memoff *);
offnode->addr = blk_addr;
offnode->size = size;
/* link with new created node */
/* A new block detected */
if(!mem_pool[i].last_offset->size) {
mem_pool[i].last_offset->addr = offnode->addr;
mem_pool[i].last_offset->size = offnode->size;
/* throw the offnode */
free(offnode);
}
else {
/* The block has got one more sections allocated */
mem_pool[i].last_offset->next = offnode;
}
/* reset the last offset */
mem_pool[i].last_offset = offnode;
/* reset the block free size */
mem_pool[i].free_size -= size;
/* return the allocated address */
return blk_addr;
}
}
return NUL(void *);
}
void
freeblock( void *addr ) {
/* Cannot free NULL address */
if(!addr) return ;
/* counter */
register int i;
xcore_memoff *p = NUL(xcore_memoff *);
xcore_memoff *bak = NUL(xcore_memoff *);
/* find the address from map */
for(i = 0; i < MAX_BLK; i++) {
/* whether addr is in position i */
if((size_t)memoff_map[i].addr + BLK_SIZE < (size_t)addr)
continue;
if((size_t)memoff_map[i].addr + memoff_map[i].size >= (size_t)addr) {
/* address is within the section */
/* free memory */
memset(addr,'\0',memoff_map[i].size);
mem_pool[i].free_size += memoff_map[i].size;
memoff_map[i].size = 0;
addr = NUL(void *);
/* exit function */
return ;
}
else {
/* find the actural position */
p = memoff_map[i].next;
while(p) {
if((size_t)p->addr + p->size >= (size_t)addr) {
/* disconnected the item which will be destoried */
bak = p->next;
memset(addr,'\0',p->size);
mem_pool[i].free_size += p->size;
/* disconnected */
p->next = bak->next;
/* destory node */
free(p);
addr = NUL(void *);
return ;
}
else
p = p->next;
}
}
}
}
int main() {
char *str;
init_memmgr();
str = (char *)getblock(20*sizeof(char));
strcpy(str,"Hello,world!\n");
printf("%s",str);
freeblock((void *)str);
getch();
return 0;
}
不好意思,最近觉得自己英语越来越差了,所以不断练习英语,所以注释都用英语了,希望大家原谅!!
[此贴子已经被作者于2006-6-11 8:58:57编辑过]