Changeset 20 for trunk


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

cosmetic and improve a few comments

Location:
trunk/kernel/mm
Files:
2 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}
  • 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.