我有这个 best_fit 内存分配程序,我不知道为什么当我的空闲列表(空闲节点列表)中有空间时,它在第一次分配时运行良好,而在第二次分配时运行良好。
//find best fit
117 list_t* find_bestfit(list_t* best_node, int sizeToAlloc){
118
119
120 best_node=NULL;
121 list_t * temp;
122 temp=freep;
123 while(temp!=NULL){
124
125 if (((temp->size) >= sizeToAlloc) && ((best_node == NULL) || ((temp->size) < (best_node->size)))){
126 best_node = temp;
127 printf("I am here line 127\n");
128 printf("best node address is : %llx\n", (long long unsigned) best_node);
129 }
130 if( (best_node->size) == sizeToAlloc){
131 printf("I am here line 130\n");
132 break;
133 }
134 temp = temp->next;
135 }
136 if(best_node == NULL){
137 printf("I am here line 137 \n");
138 m_error = E_NO_SPACE;
139 printf("NOT enough space\n");
140
141 // printf("No space available");
142 exit(-1);
143 }
144 return best_node;
145 }
这是我分配内存的方式(考虑到我有大块的可用空间):
ptr[0] = Mem_Alloc(360, BESTFIT);
64 assert(ptr[0] != NULL);
65 if (ptr[0] != NULL)
66 {
67 printf("pointer[0] worked line 65\n");
68 printf("360 is allocated at %p \n",ptr[0]);
69 }
70
71
72 printf("reached here \n");
73 printf("pointer [2] before is %p \n",ptr[2]);
74 ptr[2] = Mem_Alloc(360, BESTFIT);
75 printf("pointer [2] after is %p \n",ptr[2]);
76 assert(ptr[2] != NULL);
77 if (ptr[2] != NULL)
78 {
79 printf("pointer[2] worked \n");
80 printf("360 is allocated at %p \n",ptr[2]);
81 }
这是程序的输出:
I am here line 127
best node address is : 7f4932687000
header address is : 7f4932687000
pointer[0] worked line 65
360 is allocated at 0x7f4932687008
reached here
pointer [2] before is (nil)
1360 Segmentation fault (core dumped) myprogram
根据评论中的请求,这里是 Mem_Alloc 的代码:
void *Mem_Alloc(int size, int style){
if( size <= 0 ){
m_error = E_BAD_ARGS;
printf("Bad size args\n");
return NULL;
}
//check if size if valid/upper limit stuff
if(size % ALIGN_EIGHT != 0) {
size += ( ALIGN_EIGHT - (size % ALIGN_EIGHT) );
}
//local variables
list_t * newfreesp;
list_t *temp = NULL; //will return this as a ptr to the allocated spac
int adjusted_size;
adjusted_size = size + sizeof(header_t);
header_t *headptr; //declare pointer for the header space
printf("come to fill head \n");
//allocate new header
header_t fillhead;
fillhead.size = adjusted_size;
fillhead.magic = 123;
switch (style){
case BESTFIT:
temp = find_bestfit(temp,adjusted_size);
break;
case WORSTFIT:
temp = find_worstfit(temp,adjusted_size);
break;
case FIRSTFIT:
temp = find_firstfit(temp,adjusted_size);
break;
default:
m_error = E_BAD_ARGS;
printf("bad style args\n");
return NULL;
break;
}
if(temp != NULL){
////////////////only one node in free list////////////////////////
if(temp == freep && (freep->next == NULL)){
//all of memory requested
if(temp->size == adjusted_size){
freep = tail = NULL;
}
//only portion requested
else{
freep = (list_t*)((char*)temp + adjusted_size);
tail = freep;
//decrements freep size by (size requested + header size)
freep->size = temp->size - adjusted_size-sizeof(list_t);
}
}
////////////////more than one node in free list///////////////////////
else{
//size requested is the same size as free node-
//in an a free list with more than 1
if(temp->size == adjusted_size){
//temp is first node in free list
if(temp == freep && (freep->next != NULL)){
freep = temp->next;
}
//temp some middle node in the list
else if(temp != tail){
(temp->prev)->next = temp->next;
(temp->next)->prev = temp->prev;
}
//temp is the tail
else{
(temp->prev)->next = NULL;
tail = temp->prev;
}
//sizeToAlloc is only apart of the node in a list with
//more than one node
}else{
newfreesp =(list_t*) ((char*)temp + adjusted_size);
newfreesp->size = temp->size - adjusted_size- sizeof(list_t);
//temp is first node in free list w/ more than one node
if(temp == freep && (freep->next != NULL) ){
newfreesp->next = temp->next;
freep = newfreesp;
}
//temp some middle node in the list w/ more than one
else if(temp != tail){
newfreesp->prev = temp->prev;
newfreesp->next = temp->next;
(temp->prev)->next=newfreesp;
}
//temp is the tail
else{
//local variables
newfreesp->prev = temp->prev;
newfreesp->next = NULL;
(temp->prev)->next=newfreesp;
tail = newfreesp;
}
}
}//endelse for (more than more node in list)
//move temp ptr past the header (8bytes)
//so it points to the beginning of the allocated
//space
temp = (list_t*)((char*)temp + sizeof(header_t));
//initialise header pointer address
headptr = (header_t*)((char*)temp - sizeof(header_t));
printf("header address is : %llx\n", (long long unsigned) headptr);
//headerpointer: fill this space with the header
*headptr = fillhead;
//cast to void and return pointer to allocated space
return (void *) temp;
}
else{
printf("I am here in line 359\n");
m_error = E_NO_SPACE;
printf("NOT enough space\n");
return NULL;
}
// carve out 100 bytes
// change your freep list: sizeOfRegion - 100(approx)
// return ptr to 100 bytes
//return NULL;
}