#include "global.h"
extern unsigned long get_free_page(void);
extern void free_page(unsigned long addr);
struct bucket_desc {
/* 16 bytes */
void
*page;
struct bucket_desc
*next;
void
*freeptr;
unsigned short
refcnt;
unsigned short
bucket_size;
};
struct _bucket_dir {
/* 8 bytes */
int
size;
struct bucket_desc
*chain;
};
struct _bucket_dir bucket_dir[] = {
{ 16,
(struct bucket_desc *) 0},
{ 32,
(struct bucket_desc *) 0},
{ 64,
(struct bucket_desc *) 0},
{ 128,
(struct bucket_desc *) 0},
{ 256,
(struct bucket_desc *) 0},
{ 512,
(struct bucket_desc *) 0},
{ 1024,
(struct bucket_desc *) 0},
{ 2048, (struct bucket_desc *) 0},
{ 4096, (struct bucket_desc *) 0},
{ 0, (struct bucket_desc *) 0}}; /* End of list marker */
struct bucket_desc *free_bucket_desc = (struct bucket_desc *) 0;
static inline void init_bucket_desc()
{//只是初始化,size没确定
struct bucket_desc *bdesc, *first;
int
i;
first = bdesc = (struct bucket_desc *) get_free_page();//申请一页内存
if (!bdesc)
panic("Out of memory in init_bucket_desc()");
for (i = PAGE_SIZE/sizeof(struct bucket_desc); i > 1; i--) {
bdesc->next = bdesc+1;
bdesc++;
}
/*
* This is done last, to avoid race conditions in case
* get_free_page() sleeps and this routine gets called again....
*/
bdesc->next = free_bucket_desc;
free_bucket_desc = first;
}
//分配物理内存
void *malloc(unsigned int len)
{
struct _bucket_dir
*bdir;
struct bucket_desc
*bdesc;
void
*retval;
/*
* First we search the bucket_dir to find the right bucket change
* for this request.
*/
for (bdir = bucket_dir; bdir->size; bdir++)//取合适存储桶
if (bdir->size >= len)
break;
if (!bdir->size) {
//没有合适存储桶 则输出fail
//printk("malloc called with impossibly large argument (%d)\n",len);
//panic("malloc: bad arg");
}
/*
* Now we search for a bucket descriptor which has free space
*/
cli();
/* Avoid race conditions */
for (bdesc = bdir->chain; bdesc; bdesc = bdesc->next)//某个存储桶没初始化 则会进入下面if ,有初始化 寻找未使用的内存页
if (bdesc->freeptr)//判断是否有空某页内存是否有空
break;
/*
* If we didn't find a bucket with free space, then we'll
* allocate a new one.
*/
if (!bdesc) {
//未始化, chain 即 bdesc都为0 所以未初始化会执行到这里
char
*cp;
int
i;
if (!free_bucket_desc)
init_bucket_desc();
bdesc = free_bucket_desc;
free_bucket_desc = bdesc->next;
bdesc->refcnt = 0;
bdesc->bucket_size = bdir->size;
cp = (char*)get_free_page();
bdesc->page = bdesc->freeptr = (void*) cp;
if (!cp)
panic("Out of memory in kernel malloc()");
/* Set up the chain of free objects */
//{*//这里最巧妙的地方
for (i=PAGE_SIZE/bdir->size; i > 1; i--) {
*((char **) cp) = cp + bdir->size;
cp += bdir->size;
}
*((char **) cp) = 0;
bdesc->next = bdir->chain; /* OK, link it in! */
bdir->chain = bdesc;
}
retval = (void *) bdesc->freeptr;
bdesc->freeptr = *((void **) retval);
bdesc->refcnt++;
//*}//这里最巧妙的地方
sti();
/* OK, we're safe again */
return(retval);
}
void free_s(void *obj, int size)
{
void
*page;
struct _bucket_dir
*bdir;
struct bucket_desc
*bdesc, *prev;
/* Calculate what page this object lives in */
page = (void *) ((unsigned long) obj & 0xfffff000);
/* Now search the buckets looking for that page */
for (bdir = bucket_dir; bdir->size; bdir++) {
prev = 0;
/* If size is zero then this conditional is always false */
if (bdir->size < size)
continue;
for (bdesc = bdir->chain; bdesc; bdesc = bdesc->next) {
//根据页起始地址判断 该内存在桶目录的哪个项里
if (bdesc->page == page)
goto found;
prev = bdesc;
}
}
panic("Bad address passed to kernel free_s()");
found:
cli(); /* To avoid race conditions */
*((void **)obj) = bdesc->freeptr;
bdesc->freeptr = obj;
bdesc->refcnt--;
if (bdesc->refcnt == 0) {
/*
* We need to make sure that prev is still accurate. It
* may not be, if someone rudely interrupted us....
*/
if ((prev && (prev->next != bdesc)) ||
(!prev && (bdir->chain != bdesc)))
for (prev = bdir->chain; prev; prev = prev->next)
if (prev->next == bdesc)
break;
if (prev)
prev->next = bdesc->next;
else {
if (bdir->chain != bdesc)
panic("malloc bucket chains corrupted");
bdir->chain = bdesc->next;
}
free_page((unsigned long) bdesc->page);
bdesc->next = free_bucket_desc;
free_bucket_desc = bdesc;
}
sti();
return;
}
分享到:
相关推荐
电子-malloc.rar,单片机/嵌入式STM32-F3/F4/F7/H7
echo "ac_cv_func_malloc_0_nonnull=yes">arm-hisiv400-linux.cache #./configure --host=arm-hisiv400-linux --cache-file=arm-hisiv400-linux.cache --enable-inputapi=no --prefix=$(pwd)/arm-hisiv400-linux-...
http轻量级服务器,Monkey框架,最新1.6.1版本 $ wget ...$ tar zxfv monkey-1.6.9.tar.gz $ cd monkey-1.6.9 $ ./configure --malloc-libc --local $ make $ build/monkey
从uclibc的malloc和glib的glist、timeout提取出来的代码,用于嵌入式开发。分配一块内存用于动态内存,有定时功能,提供双向链表这种容器。用于没有操作系统和c标准库支持的环境。
glibc2.16源码下载 glibc-2.16.0.tar.bz2 malloc strcpy strncpy 等标准函数的实现,提高嵌入式编程的神器。
Using this marvelous tool, you can uncloak any imbalance in malloc/free calls (double free's or leaks), operations with uninitialized memory, access outside of properly allocated memory and oh so ...
电子-fMalloc.h,操作系统/图形界面FreeRTOS实时系统
Valgrind 是一款 Linux下(支持 x86、x86_64和ppc32)程序的内存调试工具,它可以对编译后的二进制程序进行内存使用监测(C语言中的malloc和free,以及C++中的new和delete),找出内存泄漏问题。
由于CSAPP:3e Malloc Lab的Traces不完整 所以这个是完整版的Traces 里面包含了amptjp-bal.rep等等10个完整Traces 希望对你有所帮助 免费下载嗷 希望CSDN别给我设定积分下载了
动态存储分配函数头文件,当对内存区进行操作是,调用相关函数.ANSI标准建议使用stdlib.h头文件,但许多C编译要求用malloc.h,使用时应查阅有关手册.
C语言中的头文件,下面复制用,完全针对和TUBER C 2.0
gperftools是google开发的一款非常实用的工具集,主要包括:性能优异的malloc free内存分配器tcmalloc;基于tcmalloc的堆内存检测和内存泄漏分析工具heap-profiler,heap-checker;基于tcmalloc实现的程序CPU性能监测...
linux .....\boot .....\....\bootsect.s .....\....\head.s .....\....\setup.s .....\fs .....\..\bitmap.c .....\..\block_dev.c .....\..\buffer.c ... .....\...\malloc.c .....\...\open.c
5malloc.c
CSAPP(深入理解操作系统)的倒数第二个实验源代码,总共有三种方法
malloc函数的内部实现以及源代码分析,这里主要上传的是glibc库里面的malloc.c源代码,供读者分析使用。
test-malloc-eg:测试程序的 GNU/Linux x86_64 可执行版本 运行测试文件的示例 下面给出了一个执行示例: $ test-malloc 10 100 ********** *** INITIAL # allocs = 0 - # deallocs = 0 - # sbrk = 0 Block @ 0x...
cssapp的倒数第二个lab,在这里只上传handout的原始文档,答案之后会上传。
csapp 实验 这是csapp实验中的第三个系列,有关malloc 的改写,就发了有用的