Subject:
|
My problems with adding a Best Fit algorithm for memory management to kernel/mm.c
|
Newsgroups:
|
lugnet.robotics.rcx.legos
|
Date:
|
Sat, 6 Dec 2003 02:35:52 GMT
|
Viewed:
|
4135 times
|
| |
| |
Well, my senior project is almost due (Monday) and I am having problems with one
of the last algorithms to be added.
My senior project is adding the 3 other algorithms to the memory management
facitlity (mm.c), the algorithsm are best, worst, and next fit (if you do not
know the algorithms reply and i will explain then to you).
here is my malloc() function for Best Fit:
==========================================
void *malloc(size_t size)
{
size_t *ptr,*next, bestSize, *mm_best_free;
size=(size+1)>>1;
#ifdef CONF_TM
ENTER_KERNEL_CRITICAL_SECTION();
#endif
ptr=mm_first_free;
mm_best_free=ptr;
bestSize=0xFFFF;
mm_best_free++;
while(ptr>=mm_first_free)
{
if(*(ptr++)==MM_FREE)
{
#ifdef CONF_TM
mm_try_join(ptr);
#endif
if(*ptr>=size)
{
if((*mm_best_free)>(*ptr)) <------- look here
{
mm_best_free=ptr;
bestSize=*(ptr);
}
}
}
ptr+=(*ptr)+1;
}
if(bestSize!=0xFFFF)
{
ptr=mm_best_free;
*(ptr-1)=(size_t)ctid;
if((*ptr-size)>=MM_SPLIT_THRESH)
{
next=ptr+size+1;
*(next++)=MM_FREE;
*(next)=*ptr-size-MM_HEADER_SIZE;
mm_try_join(next);
*ptr=size;
}
if(ptr==mm_first_free+1)
mm_update_first_free(ptr+*ptr+1);
#ifdef CONF_TM
LEAVE_KERNEL_CRITICAL_SECTION();
#endif
return (void*) (mm_best_free+1); //or (ptr+1)
}
#ifdef CONF_TM
LEAVE_KERNEL_CRITICAL_SECTION();
#endif
return NULL;
}
==========================================
Mistakenly, I forgot the put the '*' before mm_best_free on the noted line above
(marked with '<-------'), and the OS compiled and ran fine, but did not provide
the 100% correct results, that is becuase that line compared the address of the
location pointed by mm_best_free to the value in the location pointed to by ptr
(which is always true becuase the HEX address are always going to be greater
than the size requested). When I noticed the problem I added the '*' and now
for some reason the OS freezes after it downloads (it compiles fine).
Does anyone have any idea what is going wrong? and what can be done to maybe
solve the problem? I have reviewed the code with 2 fellow classmates (who are
also using BrickOS) and they agree with me when I say the algorithm is correctly
written.
Thanks for your help.
|
|
Message has 2 Replies:
14 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|