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


Ignore:
Timestamp:
Jun 3, 2017, 6:34:20 PM (7 years ago)
Author:
max@…
Message:

cosmetic and improve a few comments

File:
1 edited

Legend:

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

    r18 r20  
    3939void khm_init( khm_t * khm )
    4040{
    41     // check config parameters
    42     assert( ((CONFIG_PPM_PAGE_SHIFT + CONFIG_PPM_HEAP_ORDER) < 32 ) , __FUNCTION__ ,
    43              "CONFIG_PPM_HEAP_ORDER too large" );
     41        // check config parameters
     42        assert( ((CONFIG_PPM_PAGE_SHIFT + CONFIG_PPM_HEAP_ORDER) < 32 ) , __FUNCTION__ ,
     43                 "CONFIG_PPM_HEAP_ORDER too large" );
    4444
    45     // initialize lock
     45        // initialize lock
    4646        spinlock_init( &khm->lock );
    4747
    48     // compute kernel heap size
    49     intptr_t heap_size = (1 << CONFIG_PPM_HEAP_ORDER) << CONFIG_PPM_PAGE_SHIFT;
     48        // compute kernel heap size
     49        intptr_t heap_size = (1 << CONFIG_PPM_HEAP_ORDER) << CONFIG_PPM_PAGE_SHIFT;
    5050
    51     // get kernel heap base from PPM
    52     page_t * page      = ppm_alloc_pages( CONFIG_PPM_HEAP_ORDER );
    53     void   * heap_base = ppm_page2base( page );
     51        // get kernel heap base from PPM
     52        page_t * page      = ppm_alloc_pages( CONFIG_PPM_HEAP_ORDER );
     53        void   * heap_base = ppm_page2base( page );
    5454
    55     // initializes first block == complete heap
     55        // initialize first block (complete heap)
    5656        khm_block_t * block = (khm_block_t *)heap_base;
    5757        block->size = heap_size;
    5858        block->busy = 0;
    5959
    60     // initializes KHM fields
     60        // initialize KHM fields
    6161        khm->base    = (intptr_t)heap_base;
    6262        khm->size    = heap_size;
     
    7272        uint32_t       effective_size;
    7373
    74     // compute actual block size
     74        // compute actual block size
    7575        effective_size = size + sizeof(khm_block_t);
    7676        effective_size = ARROUND_UP( effective_size, CONFIG_CACHE_LINE_SIZE );
    7777
    78     // get lock protecting heap
     78        // get lock protecting heap
    7979        spinlock_lock( &khm->lock );
    8080
    81     // define a starting block to scan existing blocks
    82     if( ((khm_block_t*)khm->next)->size < effective_size ) current = (khm_block_t*)khm->base;
    83     else                                                   current = (khm_block_t*)khm->next;
     81        // define a starting block to scan existing blocks
     82        if( ((khm_block_t*)khm->next)->size < effective_size ) current = (khm_block_t*)khm->base;
     83        else                                                   current = (khm_block_t*)khm->next;
    8484
    85     // scan all existing blocks to find a large enough free block
     85        // scan all existing blocks to find a free block large enough
    8686        while( current->busy || (current->size < effective_size))
    8787        {
    88         // get next block pointer
     88                // get next block pointer
    8989                current = (khm_block_t*)((char*)current + current->size);
    9090
     
    9999        }
    100100
    101     // split the current block if current block is too large
     101        // split the current block if it is too large
    102102        if( (current->size - effective_size) >= CONFIG_CACHE_LINE_SIZE )
    103103        {
    104         // update new free block features
     104                // update new free block features
    105105                next           = (khm_block_t *)((char*)current + effective_size);
    106106                next->size     = current->size - effective_size;
    107107                next->busy     = 0;
    108108
    109         // register new free block
     109                // register new free block
    110110                khm->next = (intptr_t)next;
    111111
    112         // update allocated block features
     112                // update allocated block features
    113113                current->size  = effective_size;
    114114                current->busy  = 1;
    115115        }
    116116        else
    117     {
    118         // change block state
     117        {
     118                // change block state
    119119                current->busy  = 1;
    120     }
     120        }
    121121
    122     // release lock protecting heap
     122        // release lock protecting heap
    123123        spinlock_unlock( &khm->lock );
    124124
     
    138138        current = (khm_block_t *)((char*)ptr - sizeof(khm_block_t));
    139139
    140     // get lock protecting heap
     140        // get lock protecting heap
    141141        spinlock_lock(&khm->lock);
    142142
    143     // release block
     143        // release block
    144144        current->busy = 0;
    145145
    146     // try to merge released block with the next
     146        // try to merge released block with the next
    147147        while ( 1 )
    148148        {
    149         next = (khm_block_t*)((char*)current + current->size);
     149                next = (khm_block_t*)((char*)current + current->size);
    150150                if ( ((intptr_t)next >= (khm->base + khm->size)) || (next->busy == 1) ) break;
    151151                current->size += next->size;
     
    154154        if( (intptr_t)current < khm->next ) khm->next = (intptr_t)current;
    155155
    156     // release lock protecting heap
     156        // release lock protecting heap
    157157        spinlock_unlock( &khm->lock );
    158158}
Note: See TracChangeset for help on using the changeset viewer.