我正在尝试在C中实现自己的malloc()
函数,但我遇到了这个问题。前两个分配的地址是正确的,但在此之后,它不会显示其他地址。我的意思是,它陷入无限循环。但是,如果我删除条件&& ptr->size > BLK_SIZE
,它似乎有效。所以,问题是,为什么这个条件破坏了代码?如何通过除去条件之外的其他方式解决这个问题?
这是我的代码......
/* Author : Singh*/
typedef struct block_header {
unsigned int size : 29,
zero : 2,
alloc : 1;
} block_header;
//macros
#define HEADER_BLK_SIZE sizeof(block_header) // 4 bytes header
#define ALIGNED_PAYLOAD_SIZE (((((size)-1)>>2)<<2)+4) //to align the payload
#define BLK_SIZE HEADER_BLK_SIZE + ALIGNED_PAYLOAD_SIZE //total size of a blk
#define HEAP_EXTEND_SIZE ((BLK_SIZE)*1024) //the total heap size
static void *base_heap = NULL; //base of the heap, starting point
static void *end_heap = NULL;
static block_header *freeblk = NULL;
void *mymalloc(size_t size) {
size_t remainder_heap = (HEAP_EXTEND_SIZE) - (BLK_SIZE);
// first time init the heap and allocate the first block
if (!base_heap) {
base_heap = sbrk(0);
end_heap = sbrk(HEAP_EXTEND_SIZE);
if (base_heap == (void*)-1 || end_heap == (void*)-1)
return NULL;
block_header *blk = (block_header*)base_heap;
blk->size = BLK_SIZE;
blk->zero = 2;
blk->alloc = 1;
freeblk = ((void*)(base_heap)) + BLK_SIZE;
freeblk->size = remainder_heap;
freeblk->zero = 2;
freeblk->alloc = 0;
return ((void*)blk) + HEADER_BLK_SIZE;
} else
if (size >= HEAP_EXTEND_SIZE) {
return NULL;
} else {
//second time and the others
block_header *ptr = (block_header*)base_heap;
size_t i;
i = 0;
while (i < (HEAP_EXTEND_SIZE)) { //travel the heap
if ((ptr->alloc) ==1 ) { //if it's allocate we go to the nxt block
ptr = ((void*)ptr) + ((size_t)(ptr->size));
i += ((size_t)(ptr->size));
} else
if ((ptr->alloc) == 0 && ptr->size > BLK_SIZE) { /*if it's free and
big enough */
ptr->size = BLK_SIZE;
ptr->zero = 2;
ptr->alloc = 1;
return ((void*)ptr) + (HEADER_BLK_SIZE);
} else { //not big enough so we go to the next block
ptr = ((void*)ptr) + ((size_t)(ptr->size));
i += ((size_t)(ptr->size));
}
}
return NULL; //if it does not wok
}
}
//for testing my code
void main() {
int *i =(int*)mymalloc(12);
printf("pointeur i : %p\n", i);
int *ii = (int*)mymalloc(16);
printf("pointeur ii : %p\n", ii);
int *iii = (int*)mymalloc(20);
printf("pointeur iii : %p\n", iii);
int *iiii = (int*)mymalloc(24);
printf("pointeur iiii : %p\n", iiii);
}
答案 0 :(得分:1)
您的代码存在一个主要问题:
#define ALIGNED_PAYLOAD_SIZE (((((size)-1)>>2)<<2)+4) //to align the payload
#define BLK_SIZE HEADER_BLK_SIZE + ALIGNED_PAYLOAD_SIZE //total size of a blk
#define HEAP_EXTEND_SIZE ((BLK_SIZE)*1024) //the total heap size
所有这些宏都引用了一些当前的size
变量。 size
是mymalloc
的参数名称,使所有这些标识符不一致。这可能不是你想要的......
例如,看起来无辜的测试:
if(size >= HEAP_EXTEND_SIZE)
实际上扩展为
if(size >= ((HEADER_BLK_SIZE + (((((size)-1)>>2)<<2)+4))*1024))
除非发生无符号算术溢出,否则这是假的,并且巧合地计算为真。
您必须先清理代码,然后才能进行有效调试。
答案 1 :(得分:1)
在我看来,根本原因是你在分配一个块后忘记处理freeblock点。你需要处理freeblock点的原因是你需要freeblock来做出判断。 以下是我的修改:
else if((ptr->alloc)==0 && ptr->size > BLK_SIZE){ /*if it's free and
big enough */
ptr->size = BLK_SIZE;
ptr->zero = 2;
ptr->alloc = 1;
freeblk = ((void *)ptr) + (size_t)(ptr->size);
freeblk->size = freeblk->size - ptr->size;
freeblk->zero = 2;
freeblk->alloc = 0;
return ((void*)ptr) + (size_t)(ptr->size);
}
测试结束后,我发现它有效。
enter code here
[zzhen201 @〜] $ ./test pointeur i:0x2097004 pointeur ii:0x2097024 pointeur iii:0x209703c pointeur iiii:0x2097058
但是,我没有添加更多测试来测试是否还有其他错误。 所以也许你需要做更多的工作来测试它。
答案 2 :(得分:0)
如果我改变这一部分:
else{
block_header* ptr = (block_header*) base_heap;
size_t i;
i = 0;
while(i<(HEAP_EXTEND_SIZE)){ //travel the heap
if((ptr->alloc)==1){ //if it's allocate we go to the nxt block
ptr = ((void*)ptr) + ((size_t)(ptr->size));
i += ((size_t)(ptr->size));
}
else if((ptr->alloc)==0 && ptr->size > BLK_SIZE){ /*if it's free and
big enough */
ptr->size = BLK_SIZE;
ptr->zero = 2;
ptr->alloc = 1;
return ((void*)ptr) + (HEADER_BLK_SIZE);
}
else{ //not big enough so we go to the next block
ptr = ((void*)ptr) + ((size_t)(ptr->size));
i += ((size_t)(ptr->size));
}
}
return NULL; //if it does not wok
}
这一部分(如果空闲块足够大,它就不知道了)它的工作原理是&#34;我想..
else{
block_header* ptr = (block_header*) base_heap;
size_t i;
i = 0;
while(i<(HEAP_EXTEND_SIZE)){
if((ptr->alloc)==1){
ptr = ((void*)ptr) + ((size_t)(ptr->size));
i += ((size_t)(ptr->size));
}
else{
ptr->size = BLK_SIZE;
ptr->zero = 2;
ptr->alloc = 1;
return ((void*)ptr) + (HEADER_BLK_SIZE);
}
}
return NULL;
}
return NULL;
}