Changeset 588


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

Location:
soft/giet_vm/giet_libs
Files:
7 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
  • soft/giet_vm/giet_libs/malloc.h

    r550 r588  
    1414//   to MIN_BLOCK_SIZE (typically 128 bytes).
    1515// - All free blocks are aligned.
    16 // - They are pre-classed in NB_SIZES linked lists, where all blocks in the
    17 //   same list have the same size.
     16// - They are pre-classed in NB_SIZES linked lists, where all blocks in a
     17//   given list have the same size.
    1818// - The NEXT pointer implementing those linked lists is written
    1919//   in the 4 first bytes of the block itself, using the unsigned int type.
     
    4040// - This allocation policy has the nice following property:
    4141//   If the heap segment is aligned (the heap_base is a multiple of the
    42 //   heap_base), all allocated blocks are aligned on the actual_size.
     42//   heap_size), all allocated blocks are aligned on the actual_size.
    4343////////////////////////////////////////////////////////////////////////////////
    4444// Free policy:
     
    5252// - The index in this array is computed from the allocated block base address:
    5353//      index = (block_base - heap_base) / MIN_BLOCK_SIZE
    54 // - The allocated[] array is stored at the end of heap segment. This consume
     54// - The alloc[] array is stored at the end of heap segment. This consume
    5555//   (1 / MIN_BLOCK_SIZE) of the total heap storage capacity.
    5656////////////////////////////////////////////////////////////////////////////////
     
    7373// heap(x,y) descriptor (one per cluster)
    7474////////////////////////////////////////////////////////////////////////////////
     75
    7576typedef struct giet_heap_s
    7677{
     78    user_lock_t    lock;            // lock protecting exclusive access
    7779    unsigned int   init;            // initialised <=> value == HEAP_INITIALIZED
    7880    unsigned int   x;               // cluster X coordinate
     
    8284    unsigned int   alloc_base;      // alloc[] array base address
    8385    unsigned int   alloc_size;      // alloc[] array size (bytes)
    84     user_lock_t    lock;            // lock protecting exclusive access
    8586    unsigned int   free[32];        // array of base addresses of free blocks
    8687                                    // (address of first block of a given size)
  • soft/giet_vm/giet_libs/math/math_private.h

    r581 r588  
    1616#define _MATH_PRIVATE_H_
    1717
    18 #include <stdint.h>
     18typedef unsigned int u_int32_t;
    1919
    20 typedef uint32_t u_int32_t;
    21 
     20typedef int          int32_t;
    2221
    2322/* The original fdlibm code used statements like:
  • soft/giet_vm/giet_libs/stdio.c

    r581 r588  
    190190            {
    191191                int val = va_arg( *args, int );
    192                 if (modifiers != NO_MOD) goto return_error; //Modifiers have no meaning
     192                if (modifiers != NO_MOD) goto return_error; // Modifiers have no meaning
    193193               
    194194                len = 1;
     
    201201                int val = va_arg( *args, int );
    202202               
    203                 if (modifiers == LL_MOD) goto return_error; //64 bits not supported
     203                if (modifiers == LL_MOD) goto return_error; // 64 bits not supported
    204204               
    205205                if (val < 0)
     
    290290                char* str = va_arg( *args, char* );
    291291               
    292                 if (modifiers != NO_MOD) goto return_error; //Modifiers have no meaning
     292                if (modifiers != NO_MOD) goto return_error; // Modifiers have no meaning
    293293               
    294294                while (str[len])
     
    879879///////////////////////////////////////////////////////////////////////////////////
    880880
    881 ///////////////////////////////////////////
    882 int giet_fat_open( const char*   pathname,
    883                    unsigned int  flags )
    884 {
    885     int ret = sys_call( SYSCALL_FAT_OPEN,
    886                         (unsigned int)pathname,
    887                         flags,
    888                         0, 0 );
    889     if ( ret < 0 ) giet_exit("error in giet_fat_open()");
    890     return ret;
    891 }
    892 
    893 ////////////////////////////////////
    894 void giet_fat_read( unsigned int fd,     
     881/////////////////////////////////////////
     882int giet_fat_open( char*        pathname,
     883                   unsigned int flags )
     884{
     885    return  sys_call( SYSCALL_FAT_OPEN,
     886                      (unsigned int)pathname,
     887                      flags,
     888                      0, 0 );
     889}
     890
     891/////////////////////////////////////////
     892int giet_fat_close( unsigned int fd_id )
     893{
     894    return  sys_call( SYSCALL_FAT_CLOSE,
     895                      fd_id,
     896                      0, 0, 0 );
     897}
     898
     899/////////////////////////////////////////////
     900int giet_fat_file_info( unsigned int  fd_id,
     901                        unsigned int* size,
     902                        unsigned int* offset )
     903{
     904    return sys_call( SYSCALL_FAT_FINFO,
     905                     fd_id,
     906                     (unsigned int)size,
     907                     (unsigned int)offset,
     908                     0 );
     909}
     910
     911///////////////////////////////////////
     912int giet_fat_read( unsigned int fd_id,     
     913                   void*        buffer,
     914                   unsigned int count ) 
     915{
     916    return sys_call( SYSCALL_FAT_READ,
     917                     fd_id,
     918                     (unsigned int)buffer,
     919                     count,
     920                     0 );
     921}
     922
     923////////////////////////////////////////
     924int giet_fat_write( unsigned int fd_id,
    895925                    void*        buffer,
    896                     unsigned int count, 
    897                     unsigned int offset )
    898 {
    899     if ( sys_call( SYSCALL_FAT_READ,
    900                    fd,
    901                    (unsigned int)buffer,
    902                    count,
    903                    offset ) != count ) giet_exit("ERROR in giet_fat_read()");
    904 }
    905 
    906 /////////////////////////////////////
    907 void giet_fat_write( unsigned int fd,
    908                      void*        buffer,
    909                      unsigned int count,
    910                      unsigned int offset )
    911 {
    912     if ( sys_call( SYSCALL_FAT_WRITE,
    913                    fd,
    914                    (unsigned int)buffer,
    915                    count,
    916                    offset ) != count ) giet_exit("ERROR in giet_fat_write()");
    917 }
    918 
    919 /* variant implementing the UNIX spec
    920 ///////////////////////////////////////////////////////////////////////////////////
    921 // This system call writes to a file identified by the "fd" file descriptor.
    922 // - "buffer" is the source buffer virtual address (must be word aligned).
    923 // - "count" is a number of bytes (must be multiple of 4).
    924 // It uses the implicit "lseek" pointer in file descriptor.
    925 ///////////////////////////////////////////////////////////////////////////////////
    926 void giet_fat_write( unsigned int fd,
    927                     void*        buffer,
    928                     unsigned int count )  // number of bytes
     926                    unsigned int count )
    929927{
    930928    return sys_call( SYSCALL_FAT_WRITE,
    931                      fd,
     929                     fd_id,
    932930                     (unsigned int)buffer,
    933                      count, 0 );
    934 }
    935 */
    936 
    937 /////////////////////////////////////
    938 void giet_fat_lseek( unsigned int fd,
     931                     count,
     932                     0 );
     933}
     934
     935////////////////////////////////////////
     936int giet_fat_lseek( unsigned int fd_id,
    939937                    unsigned int offset,
    940938                    unsigned int whence )
    941939{
    942     if ( sys_call( SYSCALL_FAT_LSEEK,
    943                    fd,
    944                    offset,
    945                    whence,
    946                    0 ) ) giet_exit("ERROR in giet_fat_lseek()");
    947 }
    948 
    949 //////////////////////////////////////
    950 void giet_fat_fstat( unsigned int fd )
    951 {
    952     if ( sys_call( SYSCALL_FAT_FSTAT,
    953                    fd,
    954                    0, 0, 0 ) )  giet_exit("ERROR in giet_fat_lseek()");
     940    return sys_call( SYSCALL_FAT_LSEEK,
     941                     fd_id,
     942                     offset,
     943                     whence,
     944                     0 );
     945}
     946
     947////////////////////////////////////////////
     948int giet_fat_remove( char*         pathname,
     949                     unsigned int  should_be_dir )
     950{
     951    return sys_call( SYSCALL_FAT_REMOVE,
     952                     (unsigned int)pathname,
     953                      should_be_dir,
     954                      0, 0 );
    955955}
    956956
    957957/////////////////////////////////////
    958 void giet_fat_close( unsigned int fd )
    959 {
    960     if ( sys_call( SYSCALL_FAT_CLOSE,
    961                    fd,
    962                    0, 0, 0 ) )  giet_exit("ERROR in giet_fat_close()");
    963 }
    964 
     958int giet_fat_rename( char*  old_path,
     959                     char*  new_path )
     960{
     961    return sys_call( SYSCALL_FAT_RENAME,
     962                     (unsigned int)old_path,
     963                     (unsigned int)new_path,
     964                      0, 0 );
     965}
     966
     967////////////////////////////////////
     968int giet_fat_mkdir( char* pathname )
     969{
     970    return sys_call( SYSCALL_FAT_MKDIR,
     971                     (unsigned int)pathname,
     972                      0, 0, 0 );
     973}
     974
     975/////////////////////////////////////
     976int giet_fat_list( char* pathname )
     977{
     978    return sys_call( SYSCALL_FAT_LIST,
     979                     (unsigned int)pathname,
     980                     0, 0, 0 );
     981}
    965982
    966983
  • soft/giet_vm/giet_libs/stdio.h

    r558 r588  
    5454#define SYSCALL_FAT_WRITE            0x22
    5555#define SYSCALL_FAT_LSEEK            0x23
    56 #define SYSCALL_FAT_FSTAT            0x24
     56#define SYSCALL_FAT_FINFO            0x24
    5757#define SYSCALL_FAT_CLOSE            0x25
    58 //                                   0x26
    59 //                                   0x27
    60 //                                   0x28
    61 //                                   0x29
     58#define SYSCALL_FAT_REMOVE           0x26
     59#define SYSCALL_FAT_RENAME           0x27
     60#define SYSCALL_FAT_MKDIR            0x28
     61#define SYSCALL_FAT_LIST             0x29
    6262//                                   0x2A
    6363//                                   0x2B
     
    8383#define SYSCALL_COPROC_RUN           0x3E
    8484#define SYSCALL_COPROC_RELEASE       0x3F
     85
     86////////////////////////////////////////////////////////////////////////////
     87// These define must be synchronised vith values defined in fat32.h
     88////////////////////////////////////////////////////////////////////////////
     89
     90#define SEEK_SET            0          // argument for giet_fat_lseek()
     91#define SEEK_CUR            1          // argument for giet_fat_lseek
     92
     93#define O_CREATE            0x20       // argument for giet_fat_open()
     94#define O_RDONLY            0x01       // argument for giet_fat_open()
    8595
    8696////////////////////////////////////////////////////////////////////////////
     
    266276//////////////////////////////////////////////////////////////////////////
    267277
    268 extern int  giet_fat_open(  const char*  pathname,
    269                             unsigned int flags );
    270 
    271 extern void giet_fat_read(  unsigned int fd,
    272                             void*        buffer,
    273                             unsigned int count,
    274                             unsigned int offset );
    275 
    276 extern void giet_fat_write( unsigned int fd,
    277                             void*        buffer,
    278                             unsigned int count,
    279                             unsigned int offset );
    280 
    281 extern void giet_fat_lseek( unsigned int fd,
    282                             unsigned int offset,
    283                             unsigned int whence );
    284 
    285 extern void giet_fat_fstat( unsigned int fd );
    286 
    287 extern void giet_fat_close( unsigned int fd );
     278extern int giet_fat_open( char*        pathname,
     279                          unsigned int flags );
     280
     281extern int giet_fat_close( unsigned int fd_id );
     282
     283extern int giet_fat_file_info( unsigned int  fd_id,
     284                               unsigned int* size,
     285                               unsigned int* offset );
     286
     287extern int giet_fat_read( unsigned int fd_id,
     288                          void*        buffer,
     289                          unsigned int count );
     290
     291extern int giet_fat_write( unsigned int fd,
     292                           void*        buffer,
     293                           unsigned int count );
     294
     295extern int giet_fat_lseek( unsigned int fd,
     296                           unsigned int offset,
     297                           unsigned int whence );
     298
     299extern int giet_fat_remove( char*        pathname,
     300                            unsigned int should_be_dir );
     301
     302extern int giet_fat_rename( char*  old_path,
     303                            char*  new_path );
     304
     305extern int giet_fat_mkdir( char* pathname );
     306
     307extern int giet_fat_list( char* pathname );
    288308
    289309//////////////////////////////////////////////////////////////////////////
  • soft/giet_vm/giet_libs/user_barrier.c

    r580 r588  
    3535unsigned int p;
    3636giet_proc_xyp( &x, &y, &p );
    37 giet_shr_printf("[DEBUG USER BARRIER] proc[%d,%d,%d] enters barrier_wait()\n",
     37giet_shr_printf("\n[DEBUG USER BARRIER] proc[%d,%d,%d] enters barrier_wait()\n",
    3838                x, y, p );
    3939#endif
     
    8585
    8686#if GIET_DEBUG_USER_BARRIER
    87 giet_shr_printf("[DEBUG USER BARRIER] proc[%d,%d,%d] exit barrier_wait()\n",
     87giet_shr_printf("\n[DEBUG USER BARRIER] proc[%d,%d,%d] exit barrier_wait()\n",
    8888                x, y, p );
    8989#endif
     
    126126
    127127#if GIET_DEBUG_USER_BARRIER
    128 giet_shr_printf("[DEBUG USER BARRIER] initialize sqt_node[%d][%d][%d] : arity = %d\n"
     128giet_shr_printf("\n[DEBUG USER BARRIER] initialize sqt_node[%d][%d][%d] : arity = %d\n"
    129129                " parent = %x / child0 = %x / child1 = %x / child2 = %x / child3 = %x\n",
    130130                x, y, level, node->arity,
     
    175175
    176176#if GIET_DEBUG_USER_BARRIER
    177 giet_shr_printf("[DEBUG USER BARRIER] initialize sqt_node[%d][%d][%d] : arity = %d\n"
     177giet_shr_printf("\n[DEBUG USER BARRIER] initialize sqt_node[%d][%d][%d] : arity = %d\n"
    178178                " parent = %x / child0 = %x / child1 = %x / child0 = %x / child1 = %x\n",
    179179                x, y, level, node->arity,
     
    248248
    249249#if GIET_DEBUG_USER_BARRIER
    250 giet_shr_printf("[DEBUG USER BARRIER] SQT node[%d][%d][%d] : vaddr = %x\n",
     250giet_shr_printf("\n[DEBUG USER BARRIER] SQT node[%d][%d][%d] : vaddr = %x\n",
    251251                x, y, l, (unsigned int)barrier->node[x][y][l] );
    252252#endif
  • soft/giet_vm/giet_libs/user_barrier.h

    r501 r588  
    6565typedef struct giet_sqt_barrier_s
    6666{
    67     sqt_node_t*     node[16][16][5];    // array of pointers on SBT nodes
     67    sqt_node_t*     node[16][16][5];    // array of pointers on SQT nodes
    6868} giet_sqt_barrier_t;
    6969
Note: See TracChangeset for help on using the changeset viewer.