Ignore:
Timestamp:
Jul 8, 2015, 3:40:18 PM (9 years ago)
Author:
alain
Message:

1) Fix a bug in the free() function in the malloc.c
2) Introduce new syscalls to access the FAT in stdio.c

File:
1 edited

Legend:

Unmodified
Added
Removed
  • soft/giet_vm/giet_libs/malloc.c

    r541 r588  
    1111
    1212// Global variables defining the heap descriptors array (one heap per cluster)
    13 giet_heap_t heap[X_SIZE][Y_SIZE];
     13giet_heap_t     heap[X_SIZE][Y_SIZE];
    1414
    1515// Macro returning the smallest power of 2 larger or equal to size value
     
    5151                         unsigned int y )
    5252{
    53     giet_shr_printf(
    54                 " - heap_base   = %x\n"
    55                 " - heap_size   = %x\n"
    56                 " - alloc_base  = %x\n"
    57                 " - alloc_size  = %x\n"
    58                 " - free[0]     = %x\n"
    59                 " - free[1]     = %x\n"
    60                 " - free[2]     = %x\n"
    61                 " - free[3]     = %x\n"
    62                 " - free[4]     = %x\n"
    63                 " - free[5]     = %x\n"
    64                 " - free[6]     = %x\n"
    65                 " - free[7]     = %x\n"
    66                 " - free[8]     = %x\n"
    67                 " - free[9]     = %x\n"
    68                 " - free[10]    = %x\n"
    69                 " - free[11]    = %x\n"
    70                 " - free[12]    = %x\n"
    71                 " - free[13]    = %x\n"
    72                 " - free[14]    = %x\n"
    73                 " - free[15]    = %x\n"
    74                 " - free[16]    = %x\n"
    75                 " - free[17]    = %x\n"
    76                 " - free[18]    = %x\n"
    77                 " - free[19]    = %x\n"
    78                 " - free[20]    = %x\n"
    79                 " - free[21]    = %x\n"
    80                 " - free[22]    = %x\n"
    81                 " - free[23]    = %x\n",
    82                 heap[x][y].heap_base, heap[x][y].heap_size,
    83                 heap[x][y].alloc_base, heap[x][y].alloc_size,
    84                 heap[x][y].free[0], heap[x][y].free[1],
    85                 heap[x][y].free[2], heap[x][y].free[3],
    86                 heap[x][y].free[4], heap[x][y].free[5],
    87                 heap[x][y].free[6], heap[x][y].free[7],
    88                 heap[x][y].free[8], heap[x][y].free[9],
    89                 heap[x][y].free[10], heap[x][y].free[11],
    90                 heap[x][y].free[12], heap[x][y].free[13],
    91                 heap[x][y].free[14], heap[x][y].free[15],
    92                 heap[x][y].free[16], heap[x][y].free[17],
    93                 heap[x][y].free[18], heap[x][y].free[19],
    94                 heap[x][y].free[20], heap[x][y].free[21],
    95                 heap[x][y].free[22], heap[x][y].free[23] );
    96 }  // end display_free array()
     53    unsigned int next;
     54    unsigned int id;
     55    unsigned int iter;
     56
     57    giet_shr_printf("\nUser Heap[%d][%d] base = %x / size = %x\n", x , y ,
     58                   heap[x][y].heap_base, heap[x][y].heap_size );
     59    for ( id = 6 ; id < 28 ; id++ )
     60    {
     61        next = heap[x][y].free[id];
     62        giet_shr_printf(" - free[%d] = " , id );
     63        iter = 0;
     64        while ( next != 0 )
     65        {
     66            giet_shr_printf("%x | ", next );
     67            next = (*(unsigned int*)next);
     68            iter++;
     69        }
     70        giet_shr_printf("0\n");
     71    }
     72}  // end display_free_array()
     73
     74
    9775
    9876////////////////////////////////
     
    10684    unsigned int alloc_base;       // alloc[] array base
    10785    unsigned int alloc_size;       // alloc[] array size
    108     unsigned int alloc_index;      // size_index in free[array]
     86    unsigned int alloc_index;      // size_index in alloc[array]
    10987
    11088    unsigned int index;            // iterator
     
    149127    }
    150128
     129    // reset the alloc_size array
     130    unsigned int   word;
     131    unsigned int*  tab = (unsigned int*)alloc_base;
     132    for ( word = 0 ; word < (alloc_size>>2) ; word++ )  tab[word] = 0;
     133 
    151134    // split the heap into various sizes blocks,
    152135    // initializes the free[] array and NEXT pointers
     
    243226
    244227#if GIET_DEBUG_USER_MALLOC
    245 giet_shr_printf("\n[DEBUG USER_MALLOC] Malloc request for Heap[%d][%d] / size = %x\n",
     228giet_shr_printf("\n[DEBUG USER_MALLOC] request for Heap[%d][%d] / size = %x\n",
    246229                 x, y, size );
    247230#endif
     
    250233    if (size == 0)
    251234    {
    252         giet_exit("ERROR in malloc() : requested size = 0 \n");
     235        giet_exit("\nERROR in remote_malloc() : requested size = 0 \n");
    253236    }
    254237    if ( x >= X_SIZE )
    255238    {
    256         giet_exit("ERROR in malloc() : x coordinate too large\n");
     239        giet_exit("\nERROR in remote_malloc() : x coordinate too large\n");
    257240    }
    258241    if ( y >= Y_SIZE )
    259242    {
    260         giet_exit("ERROR in malloc() : y coordinate too large\n");
     243        giet_exit("\nERROR in remote_malloc() : y coordinate too large\n");
    261244    }
    262245
     
    264247    if ( heap[x][y].init != HEAP_INITIALIZED )
    265248    {
    266         giet_exit("ERROR in malloc() : heap not initialized\n");
     249        giet_exit("\nERROR in remote_malloc() : heap not initialized\n");
    267250    }
    268251
     
    281264                                   requested_index );
    282265
    283     // update the alloc[] array if block found
    284     if ( base != 0 )
    285     {
    286         unsigned offset = (base - heap[x][y].heap_base) / MIN_BLOCK_SIZE;
    287         unsigned char* ptr = (unsigned char*)(heap[x][y].alloc_base + offset);
    288         *ptr = requested_index;
    289     }
     266    // check block found
     267    if ( base == 0 )
     268    {
     269        lock_release( &heap[x][y].lock );
     270        giet_exit("\nERROR in remote_malloc() : no more space\n");
     271    }
     272
     273    // compute pointer in alloc[] array
     274    unsigned offset    = (base - heap[x][y].heap_base) / MIN_BLOCK_SIZE;
     275    unsigned char* ptr = (unsigned char*)(heap[x][y].alloc_base + offset);
     276
     277    // check the alloc[] array
     278    if ( *ptr != 0 )
     279    {
     280        lock_release( &heap[x][y].lock );
     281        giet_exit("\nERROR in remote_malloc() : block already allocated ???\n");
     282    }
     283
     284    // update alloc_array
     285    *ptr = requested_index;
    290286
    291287    // release the lock
     
    293289 
    294290#if GIET_DEBUG_USER_MALLOC
    295 giet_shr_printf("\n[DEBUG USER_MALLOC] Malloc vaddr from Heap[%d][%d] = %x\n",
    296                 x, y, base );
     291giet_shr_printf("\n[DEBUG USER_MALLOC] allocated block from heap[%d][%d] : "
     292                "base = %x / size = %x\n", x , y , base , size );
    297293display_free_array(x,y);
    298294#endif
     
    333329    unsigned int size = 1<<size_index;
    334330
    335     // compute companion_base and merged_base
    336     unsigned int companion_base;   // companion block base address
    337     unsigned int merged_base;      // merged block base address
    338     if ( base % (size<<1) )
     331    // compute companion block and merged block base addresses
     332    unsigned int companion_base; 
     333    unsigned int merged_base; 
     334
     335    if ( (base & size) == 0 )   // the released block is aligned on (2*size)
    339336    {
    340337        companion_base  = base + size;
     
    352349    unsigned int  iter  = heap->free[size_index];
    353350    unsigned int  prev  = (unsigned int)&heap->free[size_index];
    354     while ( iter != 0 )
     351    while ( iter )
    355352    {
    356353        if ( iter == companion_base )
     
    359356            break;
    360357        }
     358        prev = iter;
    361359        iter = *(unsigned int*)iter;
    362         prev = iter;
    363     }
    364 
    365     if ( found == 0 )  // Companion not found
    366     {
    367         // push the block in free[size_index] 
     360    }
     361
     362    if ( found == 0 )  // Companion not found => push in free[size_index] 
     363    {
    368364        *(unsigned int*)base   = heap->free[size_index];
    369365        heap->free[size_index] = base;
     
    392388#endif
    393389
    394     // get the lock protecting heap[x][y]
    395     lock_acquire( &heap[x][y].lock );
    396 
    397390    // check ptr value
    398391    unsigned int base = (unsigned int)ptr;
     
    403396    }
    404397 
     398    // get the lock protecting heap[x][y]
     399    lock_acquire( &heap[x][y].lock );
     400
    405401    // compute released block index in alloc[] array
    406402    unsigned index = (base - heap[x][y].heap_base ) / MIN_BLOCK_SIZE;
     
    410406    unsigned int   size_index = (unsigned int)*pchar;
    411407
     408    // check block allocation
     409    if ( size_index == 0 )
     410    {
     411        lock_release( &heap[x][y].lock );
     412        giet_exit("\nERROR in free() : released block not allocated ???\n");
     413    }
     414
    412415    // check released block alignment
    413416    if ( base % (1 << size_index) )
    414417    {
    415         giet_exit("ERROR in free() : released block not aligned");
     418        giet_exit("\nERROR in free() : released block not aligned\n");
    416419    }
    417420
Note: See TracChangeset for help on using the changeset viewer.