Ignore:
Timestamp:
Aug 7, 2014, 4:57:07 PM (10 years ago)
Author:
alain
Message:

1) Introducing the new system call giet_get_xy() in stdio.h,

required for the free() funcyion in malloc.h

2) Fixing bugs in malloc.c

File:
1 edited

Legend:

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

    r382 r390  
    7878    }
    7979
    80     // compute alloc[] minimal array size
     80    // compute size of block containin alloc[] array
    8181    alloc_size = heap_size / MIN_BLOCK_SIZE;
     82    if ( alloc_size < MIN_BLOCK_SIZE) alloc_size = MIN_BLOCK_SIZE;
    8283
    8384    // get index for the corresponding block
     
    9899    unsigned int   base = heap_base;
    99100    unsigned int*  ptr;
    100     for ( index = heap_index-1 ; index > alloc_index ; index-- )
     101    for ( index = heap_index-1 ; index >= alloc_index ; index-- )
    101102    {
    102103        heap[x][y].free[index] = base;
     
    247248    }
    248249
     250    // normalize size
     251    if ( size < MIN_BLOCK_SIZE ) size = MIN_BLOCK_SIZE;
     252
    249253    // compute requested_index for the free[] array
    250254    unsigned int requested_index = GET_SIZE_INDEX( size );
     
    258262                                   requested_index );
    259263
    260     // update the alloc[] array
    261     unsigned offset = (base - heap[x][y].heap_base) / MIN_BLOCK_SIZE;
    262     unsigned char* ptr = (unsigned char*)(heap[x][y].alloc_base + offset);
    263     *ptr = requested_index;
     264    // update the alloc[] array if block found
     265    if ( base != 0 )
     266    {
     267        unsigned offset = (base - heap[x][y].heap_base) / MIN_BLOCK_SIZE;
     268        unsigned char* ptr = (unsigned char*)(heap[x][y].alloc_base + offset);
     269        *ptr = requested_index;
     270    }
    264271
    265272    // release the lock
     
    286293void free( void* ptr )
    287294{
    288     // To be done
    289     // - first : to compute the cluster coordinate
    290     // - second : retrieve the block length from the
    291     // - third : try to concatenate with another free block of same size
    292     // - fourth : update the free[] array
     295    unsigned int vaddr = (unsigned int)ptr;
     296
     297    // get the cluster coordinate
     298    unsigned int x;
     299    unsigned int y;
     300    giet_get_xy( ptr, &x, &y );
     301
     302    // compute index in alloc[] array
     303    unsigned index = ( (unsigned int)ptr - heap[x][y].heap_base ) / MAX_BLOCK_SIZE;
     304 
     305    // get the freed block size_index
     306    unsigned char* p i        = (unsigned char*)(heap[x][y].alloc_base + index);
     307    unsigned int   size_index = (unsigned int)*p;
     308
     309    // update the free[] array and NEXT pointer
     310    *(unsigned int*)ptr         = heap[x][y].free[size_index];
     311    heap[x][y].free[size_index] = (unsigned int)ptr;
     312   
     313    // TODO try to concatenate with another free block of same size
     314    // this require probably to replace this simple push by a recursive function
    293315}
    294316
Note: See TracChangeset for help on using the changeset viewer.