Changeset 20 for trunk/kernel/mm/kcm.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/kcm.c

    r18 r20  
    4747                             kcm_page_t * page )
    4848{
    49     assert( page->active , __FUNCTION__ , "kcm page should be active" );
    50 
    51     // get first block available
     49        assert( page->active , __FUNCTION__ , "kcm page should be active" );
     50
     51        // get first block available
    5252        int32_t index = bitmap_ffs( page->bitmap , kcm->blocks_nr );
    5353
    54     assert( (index != -1) , __FUNCTION__ , "kcm page should not be full" );
    55 
    56     // allocate block
     54        assert( (index != -1) , __FUNCTION__ , "kcm page should not be full" );
     55
     56        // allocate block
    5757        bitmap_clear( page->bitmap , index );
    5858
    59     // increase page refcount
     59        // increase page refcount
    6060        page->refcount ++;
    6161
    62     // change the page to busy no more free block in page
    63     if( page->refcount >= kcm->blocks_nr )
    64     {
    65         page->active = 0;
     62        // change the page to busy no more free block in page
     63        if( page->refcount >= kcm->blocks_nr )
     64        {
     65                page->active = 0;
    6666                list_unlink( &page->list);
    6767                kcm->active_pages_nr --;
     
    6969                list_add_first( &kcm->busy_root , &page->list);
    7070                kcm->busy_pages_nr ++;
    71         page->busy   = 1;
    72     }
     71                page->busy   = 1;
     72        }
    7373
    7474        return (page->base + index * kcm->block_size );
     
    8787{
    8888        kcm_page_t * page;
    89     uint32_t     index;
     89        uint32_t     index;
    9090
    9191        page = (kcm_page_t*)((intptr_t)ptr & CONFIG_PPM_PAGE_MASK);
     
    9595        page->refcount --;
    9696
    97     // change the page to active if it was busy
     97        // change the page to active if it was busy
    9898        if( page->busy )
    9999        {
     
    104104                list_add_last( &kcm->active_root, &page->list );
    105105                kcm->active_pages_nr ++;
    106         page->active = 1;
    107         }
    108 
    109     // change the page to free if last block in active page
     106                page->active = 1;
     107        }
     108
     109        // change the page to free if last block in active page
    110110        if( (page->active) && (page->refcount == 0) )
    111111        {
    112         page->active = 0;
     112                page->active = 0;
    113113                list_unlink( &page->list);
    114114                kcm->active_pages_nr --;
     
    127127        page_t     * page;
    128128        kcm_page_t * ptr;
    129     kmem_req_t   req;
    130 
    131     // get one page from local PPM
    132     req.type  = KMEM_PAGE;
    133     req.size  = 0;
    134     req.flags = AF_KERNEL;
    135     page = kmem_alloc( &req );
     129        kmem_req_t   req;
     130
     131        // get one page from local PPM
     132        req.type  = KMEM_PAGE;
     133        req.size  = 0;
     134        req.flags = AF_KERNEL;
     135        page = kmem_alloc( &req );
    136136
    137137        if( page == NULL )
    138138        {
    139139                printk("\n[ERROR] in %s : failed to allocate page in cluster %d\n",
    140                __FUNCTION__ , local_cxy );
    141         return ENOMEM;
    142         }
    143 
    144     // get page base address
     140                       __FUNCTION__ , local_cxy );
     141                return ENOMEM;
     142        }
     143
     144        // get page base address
    145145        ptr = ppm_page2base( page );
    146146
    147     // initialize KCM-page descriptor
     147        // initialize KCM-page descriptor
    148148        bitmap_set_range( ptr->bitmap , 0 , kcm->blocks_nr );
    149149
     
    155155        ptr->page          = page;
    156156
    157     // introduce new page in free-list
     157        // introduce new page in free-list
    158158        list_add_first( &kcm->free_root , &ptr->list );
    159159        kcm->free_pages_nr ++;
     
    164164
    165165/////////////////////////////////////////////////////////////////////////////////////
    166 // This private function get one KCM page from the KCM freelist.
     166// This private function gets one KCM page from the KCM freelist.
    167167// It populates the freelist if required.
    168168/////////////////////////////////////////////////////////////////////////////////////
     
    172172        kcm_page_t * page;
    173173
    174     // get a new page from PPM if freelist empty
     174        // get a new page from PPM if freelist empty
    175175        if( kcm->free_pages_nr == 0 )
    176176        {
    177         error = freelist_populate( kcm );
    178         if( error       ) return NULL;
    179         }
    180 
    181     // get first KCM page from freelist and change its status to active
     177                error = freelist_populate( kcm );
     178                if( error ) return NULL;
     179        }
     180
     181        // get first KCM page from freelist and change its status to active
    182182        page = LIST_FIRST( &kcm->free_root, kcm_page_t , list );
    183183        list_unlink( &page->list );
     
    197197        uint32_t     remaining;
    198198
    199     // initialize lock
     199        // initialize lock
    200200        spinlock_init( &kcm->lock );
    201201
    202     // initialize KCM type
     202        // initialize KCM type
    203203        kcm->type = type;
    204204
    205     // initialise KCM page lists
     205        // initialize KCM page lists
    206206        kcm->free_pages_nr   = 0;
    207207        kcm->busy_pages_nr   = 0;
     
    211211        list_root_init( &kcm->active_root );
    212212
    213     // initialize block size and number of blocks per page
     213        // initialize block size and number of blocks per page
    214214        block_size      = ARROUND_UP( kmem_type_size( type ) , 64 );
    215215        blocks_nr       = CONFIG_PPM_PAGE_SIZE / block_size;
     
    220220        kcm->block_size = block_size;
    221221
    222     kcm_dmsg("\n[INFO] %s : KCM %s initialised / block_size = %d / blocks_nr = %d\n",
    223              __FUNCTION__ , kmem_type_str( type ) , block_size , blocks_nr );
     222        kcm_dmsg("\n[INFO] %s : KCM %s initialised / block_size = %d / blocks_nr = %d\n",
     223                 __FUNCTION__ , kmem_type_str( type ) , block_size , blocks_nr );
    224224
    225225}  // kcm_init()
     
    231231        list_entry_t * iter;
    232232
    233     // get KCM lock
     233        // get KCM lock
    234234        spinlock_lock( &kcm->lock );
    235235
    236     // release all free pages
     236        // release all free pages
    237237        LIST_FOREACH( &kcm->free_root , iter )
    238238        {
     
    243243        }
    244244
    245     // release all active pages
     245        // release all active pages
    246246        LIST_FOREACH( &kcm->active_root , iter )
    247247        {
     
    252252        }
    253253
    254     // release all busy pages
     254        // release all busy pages
    255255        LIST_FOREACH( &kcm->busy_root , iter )
    256256        {
     
    261261        }
    262262
    263     // release KCM lock
    264     spinlock_unlock( &kcm->lock );
     263        // release KCM lock
     264        spinlock_unlock( &kcm->lock );
    265265
    266266}  // kcm_destroy()
     
    272272        void       * ptr = NULL;   // pointer on block
    273273
    274     // get lock
     274        // get lock
    275275        spinlock_lock( &kcm->lock );
    276276
    277     // get an active page
    278     if( list_is_empty( &kcm->active_root ) )  // no active page => get one
    279     {
    280         kcm_dmsg("\n[INFO] %s : enters for type %s but no active page => get one\n",
    281                  __FUNCTION__ , kmem_type_str( kcm->type ) );
    282 
    283         // get a page from free list
     277        // get an active page
     278        if( list_is_empty( &kcm->active_root ) )  // no active page => get one
     279        {
     280                kcm_dmsg("\n[INFO] %s : enters for type %s but no active page => get one\n",
     281                         __FUNCTION__ , kmem_type_str( kcm->type ) );
     282
     283                // get a page from free list
    284284                page = freelist_get( kcm );
    285             if( page == NULL ) return NULL;
    286 
    287         // insert page in active list
    288         list_add_first( &kcm->active_root , &page->list );
    289             kcm->active_pages_nr ++;
    290         page->active = 1;
    291     }
    292     else                     // get first page from active list
    293     {
    294         kcm_dmsg("\n[INFO] %s : enters for type %s with an active page\n",
    295                  __FUNCTION__ , kmem_type_str( kcm->type ) );
    296 
    297         // get page pointer from active list
     285                if( page == NULL ) return NULL;
     286
     287                // insert page in active list
     288                list_add_first( &kcm->active_root , &page->list );
     289                kcm->active_pages_nr ++;
     290                page->active = 1;
     291        }
     292        else                     // get first page from active list
     293        {
     294                kcm_dmsg("\n[INFO] %s : enters for type %s with an active page\n",
     295                         __FUNCTION__ , kmem_type_str( kcm->type ) );
     296
     297                // get page pointer from active list
    298298                page = (kcm_page_t *)LIST_FIRST( &kcm->active_root , kcm_page_t , list );
    299     }
    300 
    301     // get a block from selected active page
    302     // cannot fail, as an active page cannot be full...
    303     ptr  = kcm_get_block( kcm , page );
    304 
    305     // release lock
     299        }
     300
     301        // get a block from selected active page
     302        // cannot fail, as an active page cannot be full...
     303        ptr  = kcm_get_block( kcm , page );
     304
     305        // release lock
    306306        spinlock_unlock(&kcm->lock);
    307307
    308     kcm_dmsg("\n[INFO] %s : allocated one block of type %s / ptr = %x\n",
    309              __FUNCTION__ , kmem_type_str( kcm->type ) , (uint32_t)ptr );
     308        kcm_dmsg("\n[INFO] %s : allocated one block of type %s / ptr = %x\n",
     309                 __FUNCTION__ , kmem_type_str( kcm->type ) , (uint32_t)ptr );
    310310
    311311        return ptr;
     
    324324        kcm  = page->kcm;
    325325
    326     // get lock
     326        // get lock
    327327        spinlock_lock( &kcm->lock );
    328328
    329     // release block
     329        // release block
    330330        kcm_put_block( kcm , ptr );
    331331
    332     // release lock
     332        // release lock
    333333        spinlock_unlock( &kcm->lock );
    334334}
     
    338338{
    339339        printk("*** KCM type = %s / free_pages = %d / busy_pages = %d / active_pages = %d\n",
    340            kmem_type_str( kcm->type ) ,
    341            kcm->free_pages_nr ,
    342            kcm->busy_pages_nr ,
    343            kcm->active_pages_nr );
     340               kmem_type_str( kcm->type ) ,
     341               kcm->free_pages_nr ,
     342               kcm->busy_pages_nr ,
     343               kcm->active_pages_nr );
    344344}
Note: See TracChangeset for help on using the changeset viewer.