https://github.com/joelpet/malloc
我想学习如何创建更高效的malloc,我找到了这个项目。我可以运行make来编译它:
$ make tstmalloc
make: `tstmalloc' is up to date.
但我该如何运行并测试呢?我阅读了自述文件,但不够详细。我想编译所有程序,测试所有程序并了解程序的功能。但是,如果我只是运行,那么它会以我不理解的方式抱怨:
$ make
gcc -g -Wall -ansi -DSTRATEGY=2 -c -o malloc.o malloc.c
malloc.c: In function ‘morecore’:
malloc.c:77:3: warning: implicit declaration of function ‘getpagesize’ [-Wimplicit-function-declaration]
noPages = ((nu*sizeof(Header))-1)/getpagesize() + 1;
^
malloc.c:78:84: error: ‘MAP_ANONYMOUS’ undeclared (first use in this function)
cp = mmap(__endHeap, noPages*getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
^
malloc.c:78:84: note: each undeclared identifier is reported only once for each function it appears in
malloc.c:85:5: warning: implicit declaration of function ‘perror’ [-Wimplicit-function-declaration]
perror("failed to get more memory");
^
make: *** [malloc.o] Error 1
如果我尝试单独编译程序,我会收到另一个错误消息,我不明白:
$ gcc malloc.c
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
我malloc.c
的内容是:
#include "brk.h"
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/mman.h>
#define NALLOC 1024 /* minimum #units to request */
typedef long Align; /* for alignment to long boundary */
union header { /* block header */
struct {
union header *ptr; /* next block if on free list */
unsigned size; /* size of this block - what unit? */
} s;
Align x; /* force alignment of blocks */
};
typedef union header Header;
static Header base; /* empty list to get started */
static Header *freep = NULL; /* start of free list */
/* free: put block ap in the free list */
void free(void * ap)
{
Header *bp, *p;
if(ap == NULL) return; /* Nothing to do */
bp = (Header *) ap - 1; /* point to block header */
for(p = freep; !(bp > p && bp < p->s.ptr); p = p->s.ptr)
if(p >= p->s.ptr && (bp > p || bp < p->s.ptr))
break; /* freed block at atrt or end of arena */
if(bp + bp->s.size == p->s.ptr) { /* join to upper nb */
bp->s.size += p->s.ptr->s.size;
bp->s.ptr = p->s.ptr->s.ptr;
}
else
bp->s.ptr = p->s.ptr;
if(p + p->s.size == bp) { /* join to lower nbr */
p->s.size += bp->s.size;
p->s.ptr = bp->s.ptr;
} else
p->s.ptr = bp;
freep = p;
}
/* morecore: ask system for more memory */
#ifdef MMAP
static void * __endHeap = 0;
void * endHeap(void)
{
if(__endHeap == 0) __endHeap = sbrk(0);
return __endHeap;
}
#endif
static Header *morecore(unsigned nu)
{
void *cp;
Header *up;
#ifdef MMAP
unsigned noPages;
if(__endHeap == 0) __endHeap = sbrk(0);
#endif
if(nu < NALLOC)
nu = NALLOC;
#ifdef MMAP
noPages = ((nu*sizeof(Header))-1)/getpagesize() + 1;
cp = mmap(__endHeap, noPages*getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
nu = (noPages*getpagesize())/sizeof(Header);
__endHeap += noPages*getpagesize();
#else
cp = sbrk(nu*sizeof(Header));
#endif
if(cp == (void *) -1){ /* no space at all */
perror("failed to get more memory");
return NULL;
}
up = (Header *) cp;
up->s.size = nu;
free((void *)(up+1));
return freep;
}
void * malloc(size_t nbytes)
{
Header *p, *prevp;
Header * morecore(unsigned);
unsigned nunits;
if(nbytes == 0) return NULL;
nunits = (nbytes+sizeof(Header)-1)/sizeof(Header) +1;
if((prevp = freep) == NULL) {
base.s.ptr = freep = prevp = &base;
base.s.size = 0;
}
for(p= prevp->s.ptr; ; prevp = p, p = p->s.ptr) {
if(p->s.size >= nunits) { /* big enough */
if (p->s.size == nunits) /* exactly */
prevp->s.ptr = p->s.ptr;
else { /* allocate tail end */
p->s.size -= nunits;
p += p->s.size;
p->s.size = nunits;
}
freep = prevp;
return (void *)(p+1);
}
if(p == freep) /* wrapped around free list */
if((p = morecore(nunits)) == NULL)
return NULL; /* none left */
}
}
答案 0 :(得分:2)
66 static Header *morecore(
67 unsigned nu) /* the amount of memory to ask for (in Header-sized units) */
68 {
69 char *cp;
70 Header *up;
71 if (nu < NALLOC)
72 nu = NALLOC;
73 cp = sbrk(nu * sizeof(Header));
74 if (cp == (char *) -1) /* no space at all */
75 return NULL;
76 up = (Header *) cp;
77 up->s.size = nu;
78 free((void *)(up+1));
79 return freep;
80 }
您的错误是指malloc.c
的第77行为no_pages = ...
,但GitHub上的当前代码不是 <div ng-app="routerApp" ng-controller="ctrl">
<div class="form-group" ng-style="{'background-color': color}">
<label>Background color for views</label>
<input type="color" name="background_color" id="background_color" ng-model="color" class="form-control" />
</div>
</div>
。所以在malloc.c中发布你的内容,或者从GitHub获取最新的代码。