Ignore:
Timestamp:
Feb 8, 2015, 12:55:35 PM (9 years ago)
Author:
alain
Message:

Introduce quad tree for distributed locks and barriers.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • soft/giet_vm/giet_common/kernel_malloc.c

    r466 r495  
    66////////////////////////////////////////////////////////////////////////////////
    77//   Implementation note:
    8 // - As this code is used to implement the SBT lock ptotecting TTY0,
     8// - As this code is used to implement the SQT lock ptotecting TTY0,
    99//   all functions here use the kernel _nolock_printf() function.
    1010// - It must exist one vseg with the HEAP type in each cluster. The length
     
    3737#include "mapping_info.h"
    3838#include "kernel_malloc.h"
    39 #include "locks.h"
     39#include "kernel_locks.h"
    4040#include "tty0.h"
    4141#include "utils.h"
     
    4545///////////////////////////////////////////////////////////////////////////////
    4646
    47 extern kernel_heap_t kernel_heap[X_SIZE][Y_SIZE];
     47__attribute__((section(".kdata")))
     48kernel_heap_t kernel_heap[X_SIZE][Y_SIZE];
    4849
    4950///////////////////////////////////////////////////////////////////////////////
     
    8384                                            (size <= 0x80000000) ? 31 :\
    8485                                                                   32
     86
    8587#if GIET_DEBUG_SYS_MALLOC
    8688////////////////////////////////////////////////
     
    115117                   " - free[22]    = %x\n"
    116118                   " - free[23]    = %x\n",
    117                    kernel_heap[x][y].x, kernel_heap[x][y].y,
     119                   x, y,
    118120                   kernel_heap[x][y].heap_base, kernel_heap[x][y].heap_size,
    119121                   kernel_heap[x][y].free[0] , kernel_heap[x][y].free[1],
     
    135137
    136138
    137 /////////////////////////////////////////////
    138 void _get_heap_info( unsigned int* heap_base,
    139                      unsigned int* heap_size,
    140                      unsigned int  x,
    141                      unsigned int  y )
     139/////////////////////////////////////////////////////
     140unsigned int _get_heap_info( unsigned int* heap_base,
     141                             unsigned int* heap_size,
     142                             unsigned int  x,
     143                             unsigned int  y )
    142144{
    143145    mapping_header_t  * header   = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
     
    155157    if ( (x >= X_SIZE) || (y >= Y_SIZE) )
    156158    {
    157         _nolock_printf("[GIET ERROR] _get_heap_info() illegal (%d,%d) coordinates\n",
    158                        x , y );
     159        _nolock_printf("\n[GIET ERROR] _get_heap_info()"
     160                       " illegal (%d,%d) coordinates\n", x , y );
    159161        _exit();
    160162    }
     
    172174            *heap_base = vsegs[vseg_id].vbase;
    173175            *heap_size = vobjs[vobj_id].length;
    174             return;
     176            return 0;
    175177        }
    176178    }
    177179
    178     // exit if not found
    179     _nolock_printf("[GIET ERROR] _get_heap_info() heap[%d][%d] vseg not found\n",
    180                    x , y );
    181     _exit();
    182 
     180    return 1;
    183181} // end _get_heap_info()
    184182
     
    191189    unsigned int heap_size;
    192190    unsigned int heap_index;
    193 
    194191    unsigned int index;
    195192    unsigned int x;
    196193    unsigned int y;
     194    unsigned int ko;
    197195
    198196    for ( x = 0 ; x < X_SIZE ; x++ )
     
    201199        {
    202200            // get heap_base, heap size, and heap index
    203             _get_heap_info( &heap_base, &heap_size, x, y );
    204             heap_index = GET_SIZE_INDEX( heap_size );
    205 
    206             // checking heap segment constraints
    207             if ( heap_size != (1<<heap_index) )
     201            ko = _get_heap_info( &heap_base, &heap_size, x, y );
     202       
     203            if ( ko )  // no kernel heap found in cluster[x][y]
    208204            {
    209                 _nolock_printf("[GIET ERROR] in _heap_init()"
    210                         " kernel_heap[‰d,‰d] not power of 2\n", x , y );
    211                 _exit();
     205                // initialise kernel_heap[x][y] descriptor
     206                kernel_heap[x][y].heap_base  = 0;
     207                kernel_heap[x][y].heap_size  = 0;
     208                _spin_lock_init( &kernel_heap[x][y].lock );
    212209            }
    213             if ( heap_base % heap_size )
     210            else       // kernel heap found in cluster[x][y]
    214211            {
    215                 _nolock_printf("[GIET ERROR] in _heap_init()"
    216                         " kernel_heap[‰d,‰d] not aligned\n", x , y );
    217                 _exit();
     212                heap_index = GET_SIZE_INDEX( heap_size );
     213
     214                // check heap[x][y] constraints
     215                if ( heap_size != (1<<heap_index) )
     216                {
     217                    _nolock_printf("\n[GIET ERROR] in _heap_init()"
     218                                   " kernel_heap[‰d,‰d] not power of 2\n", x , y );
     219                    _exit();
     220                }
     221                if ( heap_base % heap_size )
     222                {
     223                    _nolock_printf("\n[GIET ERROR] in _heap_init()"
     224                                   " kernel_heap[‰d,‰d] not aligned\n", x , y );
     225                    _exit();
     226                }
     227
     228                // initialise the free[] array
     229                for ( index = 0 ; index < 32 ; index++ )
     230                {
     231                    if (index == heap_index) kernel_heap[x][y].free[index] = heap_base;
     232                    else                     kernel_heap[x][y].free[index] = 0;
     233                }
     234
     235                // initialise kernel_heap[x][y] descriptor
     236                kernel_heap[x][y].heap_base  = heap_base;
     237                kernel_heap[x][y].heap_size  = heap_size;
     238                _spin_lock_init( &kernel_heap[x][y].lock );
    218239            }
    219240
    220             // initialise the free[] array
    221             for ( index = 0 ; index < 32 ; index++ )
    222             {
    223                 if (index == heap_index) kernel_heap[x][y].free[index] = heap_base;
    224                 else                     kernel_heap[x][y].free[index] = 0;
    225             }
    226             unsigned int* ptr = (unsigned int*)heap_base;
    227             *ptr = 0;
    228 
    229             // initialise kernel_heap[x][y] descriptor
    230             kernel_heap[x][y].x          = x;
    231             kernel_heap[x][y].y          = y;
    232             kernel_heap[x][y].heap_base  = heap_base;
    233             kernel_heap[x][y].heap_size  = heap_size;
    234 
    235             _spin_lock_init( &kernel_heap[x][y].lock );
    236 
    237241#if GIET_DEBUG_SYS_MALLOC
    238 _nolock_printf("\n[DEBUG KERNEL_MALLOC] Completing kernel_heap[%d][%d] initialisation\n",
    239                x, y );
     242_nolock_printf("\n[DEBUG KERNEL_MALLOC] Completing kernel_heap[%d][%d]"
     243               " initialisation\n", x, y );
    240244_display_free_array(x,y);
    241245#endif
     
    248252
    249253//////////////////////////////////////////////
    250 unsigned int split_block( kernel_heap_t* heap,
    251                           unsigned int   vaddr,
    252                           unsigned int   searched_index,
    253                           unsigned int   requested_index )
     254unsigned int _split_block( kernel_heap_t* heap,
     255                           unsigned int   vaddr,
     256                           unsigned int   searched_index,
     257                           unsigned int   requested_index )
    254258{
    255259    // push the upper half block into free[searched_index-1]
     
    258262    heap->free[searched_index-1] = (unsigned int)new;
    259263       
    260     if ( searched_index == requested_index + 1 )  // terminal case: return lower half block
     264    if ( searched_index == requested_index + 1 )  // return lower half block
    261265    {
    262266        return vaddr;
     
    264268    else            // non terminal case : lower half block must be split again
    265269    {                               
    266         return split_block( heap, vaddr, searched_index-1, requested_index );
    267     }
    268 } // end split_block()
     270        return _split_block( heap, vaddr, searched_index-1, requested_index );
     271    }
     272} // end _split_block()
    269273
    270274
    271275
    272276/////////////////////////////////////////////
    273 unsigned int get_block( kernel_heap_t* heap,
    274                         unsigned int   searched_index,
    275                         unsigned int   requested_index )
     277unsigned int _get_block( kernel_heap_t* heap,
     278                         unsigned int   searched_index,
     279                         unsigned int   requested_index )
    276280{
    277281    // test terminal case
     
    285289        if ( vaddr == 0 )     // block not found : search in free[searched_index+1]
    286290        {
    287             return get_block( heap, searched_index+1, requested_index );
     291            return _get_block( heap, searched_index+1, requested_index );
    288292        }
    289293        else                // block found : pop it from free[searched_index]
     
    300304            else                                      // split is required
    301305            {
    302                 return split_block( heap, vaddr, searched_index, requested_index );
     306                return _split_block( heap, vaddr, searched_index, requested_index );
    303307            }
    304308        }
    305309    }
    306 } // end get_block()
     310} // end _get_block()
    307311
    308312
     
    314318{
    315319    // checking arguments
    316     if (size == 0)
    317     {
    318         _nolock_printf("[GIET ERROR] _remote_malloc() : requested size = 0 \n");
     320    if ( x >= X_SIZE )
     321    {
     322        _nolock_printf("\n[GIET ERROR] _remote_malloc() : x coordinate too large\n");
    319323        _exit();
    320324    }
    321     if ( x >= X_SIZE )
    322     {
    323         _nolock_printf("[GIET ERROR] _remote_malloc() : x coordinate too large\n");
     325    if ( y >= Y_SIZE )
     326    {
     327        _nolock_printf("\n[GIET ERROR] _remote_malloc() : y coordinate too large\n");
    324328        _exit();
    325329    }
    326     if ( y >= Y_SIZE )
    327     {
    328         _nolock_printf("[GIET ERROR] _remote_malloc() : y coordinate too large\n");
     330    if ( kernel_heap[x][y].heap_size == 0 )
     331    {
     332        _nolock_printf("\n[GIET ERROR] _remote_malloc() : No heap[%d][%d]\n", x, y );
    329333        _exit();
     334     
    330335    }
    331336
     
    340345
    341346    // call the recursive function get_block
    342     unsigned int base = get_block( &kernel_heap[x][y],
    343                                    requested_index,
    344                                    requested_index );
     347    unsigned int base = _get_block( &kernel_heap[x][y],
     348                                    requested_index,
     349                                    requested_index );
    345350    // release the lock
    346351    _spin_lock_release( &kernel_heap[x][y].lock );
    347352 
     353    if ( base == 0 )
     354    {
     355        _nolock_printf("\n[GIET ERROR] _remote_malloc() : no more space "
     356                       "in heap[%d][%d]", x, y );
     357    }
     358
    348359#if GIET_DEBUG_SYS_MALLOC
    349360_nolock_printf("\n[DEBUG KERNEL_MALLOC] malloc vaddr %x from kernel_heap[%d][%d]\n",
     
    354365    return (void*)base;
    355366
    356 } // end remote_malloc()
     367} // end _remote_malloc()
    357368
    358369
Note: See TracChangeset for help on using the changeset viewer.