Changeset 18 for trunk/kernel/mm/khm.c


Ignore:
Timestamp:
Jun 3, 2017, 4:42:49 PM (7 years ago)
Author:
max@…
Message:

cosmetic, and a few typos

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/mm/khm.c

    r14 r18  
    11/*
    22 * khm.c - kernel heap manager implementation.
    3  * 
     3 *
    44 * Authors  Ghassan Almaless (2008,2009,2010,2011,2012)
    55 *          Alain Greiner (2016)
     
    4444
    4545    // initialize lock
    46         spinlock_init( &khm->lock ); 
    47    
    48     //  compute kernel heap size
     46        spinlock_init( &khm->lock );
     47
     48    // compute kernel heap size
    4949    intptr_t heap_size = (1 << CONFIG_PPM_HEAP_ORDER) << CONFIG_PPM_PAGE_SHIFT;
    5050
    51     // get kernel heap base from PPM 
     51    // get kernel heap base from PPM
    5252    page_t * page      = ppm_alloc_pages( CONFIG_PPM_HEAP_ORDER );
    5353    void   * heap_base = ppm_page2base( page );
     
    6565
    6666/////////////////////////////////
    67 void * khm_alloc( khm_t    * khm, 
     67void * khm_alloc( khm_t    * khm,
    6868                  uint32_t   size )
    6969{
    70         khm_block_t  * current;       
     70        khm_block_t  * current;
    7171        khm_block_t  * next;
    7272        uint32_t       effective_size;
     
    7878    // get lock protecting heap
    7979        spinlock_lock( &khm->lock );
    80  
    81     // define a starting block to scan existing blocks 
     80
     81    // define a starting block to scan existing blocks
    8282    if( ((khm_block_t*)khm->next)->size < effective_size ) current = (khm_block_t*)khm->base;
    8383    else                                                   current = (khm_block_t*)khm->next;
    8484
    8585    // scan all existing blocks to find a large enough free block
    86         while( current->busy || (current->size < effective_size)) 
     86        while( current->busy || (current->size < effective_size))
    8787        {
    8888        // get next block pointer
    8989                current = (khm_block_t*)((char*)current + current->size);
    90    
     90
    9191                if( (intptr_t)current >= (khm->base + khm->size) )  // heap full
    9292                {
    9393                        spinlock_unlock(&khm->lock);
    9494
    95                         printk("\n[ERROR] in %s : failed to allocate block of size %d\n", 
     95                        printk("\n[ERROR] in %s : failed to allocate block of size %d\n",
    9696                               __FUNCTION__ , effective_size );
    9797                        return NULL;
     
    133133        khm_block_t * current;
    134134        khm_block_t * next;
    135  
     135
    136136        if(ptr == NULL) return;
    137  
     137
    138138        current = (khm_block_t *)((char*)ptr - sizeof(khm_block_t));
    139  
     139
    140140    // get lock protecting heap
    141141        spinlock_lock(&khm->lock);
    142142
    143     // release block 
     143    // release block
    144144        current->busy = 0;
    145  
     145
    146146    // try to merge released block with the next
    147147        while ( 1 )
    148         { 
     148        {
    149149        next = (khm_block_t*)((char*)current + current->size);
    150150                if ( ((intptr_t)next >= (khm->base + khm->size)) || (next->busy == 1) ) break;
     
    153153
    154154        if( (intptr_t)current < khm->next ) khm->next = (intptr_t)current;
    155  
     155
    156156    // release lock protecting heap
    157157        spinlock_unlock( &khm->lock );
Note: See TracChangeset for help on using the changeset viewer.