Subject:
|
Re: My problems with adding a Best Fit algorithm for memory management to kernel/mm.c
|
Newsgroups:
|
lugnet.robotics.rcx.legos
|
Date:
|
Sun, 7 Dec 2003 04:15:43 GMT
|
Viewed:
|
4231 times
|
| |
| |
Well,
I have another problem - this time with Worst Fit. It may be becuase I have
been looking at this code for so much time over the past few months, but I have
encountered this problem with my implementation and my mind is boggled (as well
as other's).
Here is my code (it is of course very similar to Best Fit):
===============================================
void *malloc(size_t size)
{
size_t *ptr,*next, worstSize, *mm_worst_free;
size=(size+1)>>1;
#ifdef CONF_TM
ENTER_KERNEL_CRITICAL_SECTION();
#endif
ptr=mm_first_free;
worstSize=0;
while(ptr>=mm_first_free)
{
if(*(ptr++)==MM_FREE)
{
#ifdef CONF_TM
mm_try_join(ptr);
#endif
if(*ptr>=size)
{
if(worstSize<(*ptr))
{
mm_worst_free=ptr;
worstSize=*(ptr);
}
}
}
ptr+=(*ptr)+1;
}
if(worstSize!=0)
{
ptr=mm_worst_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_worst_free+1); //or (ptr+1)
}
#ifdef CONF_TM
LEAVE_KERNEL_CRITICAL_SECTION();
#endif
return NULL;
}
===============================================
The results I am getting a very odd.
If I load this version of the OS and then load my testing program (one which
mallocs a few blocks, then frees some, and then mallocs a few more and then
finally scans user memory and prints out the memory layout, i.e. the address,
PID, size of each block) then it seems to allocate everything fine, except for
one random block of memory which is size 7 in the after all the of OS tasks are
allocated.
But if I turn the RCX off then on either:
(a) after downloading the program or
(b)after just running the program
and then run the program again, all of the OS tasks that were allocated now
somehow must have their PID set to MM_FREE and then they are all joined together
as 1 free block of memory beginning at mm_start. And then after that the user
program is allocated (and is fine throughout the whole process, nothing happens
to it, its PID is fine) the OS tasks get allocated again, and then from any time
after that the memory layout remains the same (i.e. no other blocks get reset
and joined to create a giant free block of memory).
Does anyone have any idea what is going on? And why?
The rest of my mm.c file is the same as the original and the same as my Best Fit
version which worls perfectly.
Thanks,
Mike
|
|
Message has 2 Replies:
Message is in Reply To:
14 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
This Message and its Replies on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|