Subject:
|
Re: 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 18:47:40 GMT
|
Viewed:
|
4275 times
|
| |
| |
In lugnet.robotics.rcx.legos, Michael Martelli wrote:
> 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++; //this must point to the size portion of the header
>
> while(ptr>=mm_first_free) //&mm_start gives the same results
> {
> if(*(ptr++)==MM_FREE)
> {
> #ifdef CONF_TM
> mm_try_join(ptr);
> #endif
>
> if(*ptr>=size)
> {
> if(bestSize>(*ptr)) //THIS LINE!!!!
> {
> 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;
> }
BTW, you can delete these two lines of intialization (i.e. lines 12 & 15 in the
above function) as the intialized value gets clobbered on finding the first
suitable sized block:
12) mm_best_free=ptr;
15) mm_best_free++; //this must point to the size portion of the
If you never find a suitable block, mm_best_free isn't used for anything either.
Mark
|
|
Message has 1 Reply:
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
|
|
|
|