source: soft/giet_vm/giet_libs/malloc.c @ 781

Last change on this file since 781 was 781, checked in by alain, 8 years ago

Fixing two bugs:
1) In the string library: the strcpy() function must copy
the terminating NUL character in the destination buffer.
2) In the malloc library: the free() function must reset
the entry associated to the released buffer in the alloc[array].

  • Property svn:executable set to *
File size: 15.0 KB
RevLine 
[382]1////////////////////////////////////////////////////////////////////////////////
2// File     : malloc.c
3// Date     : 05/03/2013
4// Author   : Jean-Baptiste Bréjon / alain greiner
5// Copyright (c) UPMC-LIP6
6////////////////////////////////////////////////////////////////////////////////
[258]7
8#include "malloc.h"
9#include "stdio.h"
[777]10#include "stdlib.h"
[468]11#include "giet_config.h"
[258]12
[382]13// Global variables defining the heap descriptors array (one heap per cluster)
[588]14giet_heap_t     heap[X_SIZE][Y_SIZE];
[325]15
[382]16// Macro returning the smallest power of 2 larger or equal to size value
17#define GET_SIZE_INDEX(size)                (size <= 0x00000001) ? 0  :\
18                                            (size <= 0x00000002) ? 1  :\
19                                            (size <= 0x00000004) ? 2  :\
20                                            (size <= 0x00000008) ? 3  :\
21                                            (size <= 0x00000010) ? 4  :\
22                                            (size <= 0x00000020) ? 5  :\
23                                            (size <= 0x00000040) ? 6  :\
24                                            (size <= 0x00000080) ? 7  :\
25                                            (size <= 0x00000100) ? 8  :\
26                                            (size <= 0x00000200) ? 9  :\
27                                            (size <= 0x00000400) ? 10 :\
28                                            (size <= 0x00000800) ? 11 :\
29                                            (size <= 0x00001000) ? 12 :\
30                                            (size <= 0x00002000) ? 13 :\
31                                            (size <= 0x00004000) ? 14 :\
32                                            (size <= 0x00008000) ? 15 :\
33                                            (size <= 0x00010000) ? 16 :\
34                                            (size <= 0x00020000) ? 17 :\
35                                            (size <= 0x00040000) ? 18 :\
36                                            (size <= 0x00080000) ? 19 :\
37                                            (size <= 0x00100000) ? 20 :\
38                                            (size <= 0x00200000) ? 21 :\
39                                            (size <= 0x00400000) ? 22 :\
40                                            (size <= 0x00800000) ? 23 :\
41                                            (size <= 0x01000000) ? 24 :\
42                                            (size <= 0x02000000) ? 25 :\
43                                            (size <= 0x04000000) ? 26 :\
44                                            (size <= 0x08000000) ? 27 :\
45                                            (size <= 0x10000000) ? 28 :\
46                                            (size <= 0x20000000) ? 29 :\
47                                            (size <= 0x40000000) ? 30 :\
48                                            (size <= 0x80000000) ? 31 :\
49                                                                   32
[394]50////////////////////////////////////////
51void display_free_array( unsigned int x,
52                         unsigned int y )
53{
[588]54    unsigned int next;
55    unsigned int id;
56    unsigned int iter;
[394]57
[680]58    giet_tty_printf("\nUser Heap[%d][%d] base = %x / size = %x\n", x , y , 
[588]59                   heap[x][y].heap_base, heap[x][y].heap_size );
[680]60    for ( id = 0 ; id < 32 ; id++ )
[588]61    { 
62        next = heap[x][y].free[id];
[680]63        giet_tty_printf(" - free[%d] = " , id );
[588]64        iter = 0;
65        while ( next != 0 )
66        {
[680]67            giet_tty_printf("%x | ", next );
[588]68            next = (*(unsigned int*)next);
69            iter++;
70        }
[680]71        giet_tty_printf("0\n");
[588]72    }
73}  // end display_free_array()
74
75
76
[382]77////////////////////////////////
78void heap_init( unsigned int x,
79                unsigned int y )
80{
81    unsigned int heap_base;        // heap segment base
82    unsigned int heap_size;        // heap segment size
83    unsigned int heap_index;       // size_index in free[array]
[258]84
[382]85    unsigned int alloc_base;       // alloc[] array base
86    unsigned int alloc_size;       // alloc[] array size
[588]87    unsigned int alloc_index;      // size_index in alloc[array]
[258]88
[382]89    unsigned int index;            // iterator
[258]90
[382]91    // get heap_base, heap size, and heap index
92    giet_heap_info( &heap_base, &heap_size, x, y );
93    heap_index = GET_SIZE_INDEX( heap_size );
[258]94
[468]95#if GIET_DEBUG_USER_MALLOC
[680]96giet_tty_printf("\n[DEBUG USER_MALLOC] Starting Heap[%d][%d] initialisation /"
[468]97                " base = %x / size = %x\n", x, y, heap_base, heap_size );
98#endif
99
[382]100    // checking heap segment constraints
[709]101    giet_pthread_assert( (heap_size != 0) , 
102                         "error in heap_init() : heap not found"); 
103    giet_pthread_assert( (heap_size == (1<<heap_index)) ,
104                         "error in heap_init() : heap size must be power of 2");
105    giet_pthread_assert( (heap_base%heap_size == 0) ,
106                         "error in heap_init() : heap segment must be aligned\n");
[258]107
[390]108    // compute size of block containin alloc[] array
[382]109    alloc_size = heap_size / MIN_BLOCK_SIZE;
[390]110    if ( alloc_size < MIN_BLOCK_SIZE) alloc_size = MIN_BLOCK_SIZE;
[258]111
[382]112    // get index for the corresponding block
113    alloc_index = GET_SIZE_INDEX( alloc_size );
[258]114
[382]115    // compute alloc[] array base address
116    alloc_base = heap_base + heap_size - alloc_size;
[258]117
[382]118    // reset the free[] array
119    for ( index = 0 ; index < 32 ; index++ )
120    {
121        heap[x][y].free[index] = 0;
[258]122    }
123
[588]124    // reset the alloc_size array
125    unsigned int   word;
126    unsigned int*  tab = (unsigned int*)alloc_base;
127    for ( word = 0 ; word < (alloc_size>>2) ; word++ )  tab[word] = 0;
128 
[382]129    // split the heap into various sizes blocks,
130    // initializes the free[] array and NEXT pointers
131    // base is the block base address
132    unsigned int   base = heap_base;
133    unsigned int*  ptr;
[390]134    for ( index = heap_index-1 ; index >= alloc_index ; index-- )
[382]135    {
136        heap[x][y].free[index] = base;
137        ptr = (unsigned int*)base;
138        *ptr = 0;
139        base = base + (1<<index);
[258]140    }
141
[382]142    heap[x][y].init       = HEAP_INITIALIZED;
143    heap[x][y].x          = x;
144    heap[x][y].y          = y;
145    heap[x][y].heap_base  = heap_base;
146    heap[x][y].heap_size  = heap_size;
147    heap[x][y].alloc_size = alloc_size;
148    heap[x][y].alloc_base = alloc_base;
[258]149
[468]150    lock_init( &heap[x][y].lock );
[258]151
[468]152#if GIET_DEBUG_USER_MALLOC
[680]153giet_tty_printf("\n[DEBUG USER_MALLOC] Completing Heap[%d][%d] initialisation\n", x, y );
[394]154display_free_array(x,y);
[382]155#endif
156               
157}  // end heap_init()
[258]158
[382]159////////////////////////////////////////////
160unsigned int split_block( giet_heap_t* heap,
161                          unsigned int vaddr, 
162                          unsigned int searched_index,
163                          unsigned int requested_index )
164{
165    // push the upper half block into free[searched_index-1]
166    unsigned int* new            = (unsigned int*)(vaddr + (1<<(searched_index-1)));
167    *new                         = heap->free[searched_index-1]; 
168    heap->free[searched_index-1] = (unsigned int)new;
169       
170    if ( searched_index == requested_index + 1 )  // terminal case: return lower half block
171    {
172        return vaddr;
[258]173    }
[382]174    else            // non terminal case : lower half block must be split again
175    {                               
176        return split_block( heap, vaddr, searched_index-1, requested_index );
[258]177    }
[382]178} // end split_block()
[258]179
[382]180//////////////////////////////////////////
181unsigned int get_block( giet_heap_t* heap,
182                        unsigned int searched_index,
183                        unsigned int requested_index )
184{
185    // test terminal case
186    if ( (1<<searched_index) > heap->heap_size )  // failure : return a NULL value
[258]187    {
[382]188        return 0;
[258]189    }
[382]190    else                            // search a block in free[searched_index]
[258]191    {
[382]192        unsigned int vaddr = heap->free[searched_index];
193        if ( vaddr == 0 )     // block not found : search in free[searched_index+1]
[258]194        {
[382]195            return get_block( heap, searched_index+1, requested_index );
[258]196        }
[382]197        else                // block found : pop it from free[searched_index]
[258]198        {
[382]199            // pop the block from free[searched_index]
200            unsigned int next = *((unsigned int*)vaddr); 
201            heap->free[searched_index] = next;
202           
203            // test if the block must be split
204            if ( searched_index == requested_index )  // no split required
205            {
206                return vaddr;
[258]207            }
[382]208            else                                      // split is required
209            {
210                return split_block( heap, vaddr, searched_index, requested_index );
[258]211            }
[382]212        } 
[258]213    }
[382]214} // end get_block()
[258]215
[382]216////////////////////////////////////////
[777]217void * remote_malloc( int size,
[382]218                      unsigned int x,
219                      unsigned int y ) 
[258]220{
[397]221
[468]222#if GIET_DEBUG_USER_MALLOC
[680]223giet_tty_printf("\n[DEBUG USER_MALLOC] request for Heap[%d][%d] / size = %x\n", 
[397]224                 x, y, size );
225#endif
226
[382]227    // checking arguments
[709]228    giet_pthread_assert( (size != 0) ,
229                         "error in remote_malloc() : requested size = 0 \n");
230    giet_pthread_assert( (x < X_SIZE) ,
231                         "error in remote_malloc() : x coordinate too large\n");
232    giet_pthread_assert( (y < Y_SIZE) ,
233                         "error in remote_malloc() : y coordinate too large\n");
234    giet_pthread_assert( (heap[x][y].init == HEAP_INITIALIZED) ,
235                         "error in remote_malloc() : heap not initialized\n");
[258]236
[390]237    // normalize size
238    if ( size < MIN_BLOCK_SIZE ) size = MIN_BLOCK_SIZE;
239
[382]240    // compute requested_index for the free[] array
241    unsigned int requested_index = GET_SIZE_INDEX( size );
[258]242
[394]243    // take the lock protecting access to heap[x][y]
[382]244    lock_acquire( &heap[x][y].lock );
[258]245
[382]246    // call the recursive function get_block
247    unsigned int base = get_block( &heap[x][y], 
248                                   requested_index, 
249                                   requested_index );
[258]250
[588]251    // check block found
[709]252    if (base == 0)
[390]253    {
[588]254        lock_release( &heap[x][y].lock );
[709]255        giet_pthread_assert( 0 , "error in remote_malloc() : no more space\n" );
[390]256    }
[258]257
[588]258    // compute pointer in alloc[] array
259    unsigned offset    = (base - heap[x][y].heap_base) / MIN_BLOCK_SIZE;
260    unsigned char* ptr = (unsigned char*)(heap[x][y].alloc_base + offset);
261
262    // check the alloc[] array
263    if ( *ptr != 0 )
264    {
265        lock_release( &heap[x][y].lock );
[709]266        giet_pthread_assert( 0 , "error in remote_malloc() : block already allocated");
[588]267    }
268
269    // update alloc_array
270    *ptr = requested_index;
271
[382]272    // release the lock
273    lock_release( &heap[x][y].lock );
274 
[468]275#if GIET_DEBUG_USER_MALLOC
[680]276giet_tty_printf("\n[DEBUG USER_MALLOC] allocated block from heap[%d][%d] : "
[588]277                "base = %x / size = %x\n", x , y , base , size );
[394]278display_free_array(x,y);
279#endif
280
[777]281    return (void*) base;
[258]282
[382]283} // end remote_malloc()
[258]284
285
[382]286//////////////////////////////////
[777]287void * malloc( int size )
[382]288{
[431]289    // get cluster coordinates
[777]290    unsigned int x;
291    unsigned int y;
292    unsigned int lpid;
[431]293    giet_proc_xyp( &x, &y, &lpid );
[258]294
[382]295    return remote_malloc( size, x, y );
296} 
[258]297
[777]298
299////////////////////////////////////
300void * calloc ( int nbmem, int size )
301{
302    void * a = malloc( nbmem * size );
303    memset( a, 0, nbmem * size );
304    return a;
305}
306
[394]307///////////////////////////////////////////
308void update_free_array( giet_heap_t* heap,
309                        unsigned int base,
310                        unsigned int size_index )
311{
312    // This recursive function try to merge the released block
313    // with the companion block if this companion block is free.
314    // This companion has the same size, and almost the same address
315    // (only one address bit is different)
316    // - If the companion is not in free[size_index],
317    //   the released block is pushed in free[size_index].
318    // - If the companion is found, it is evicted from free[size_index]
319    //   and the merged bloc is pushed in the free[size_index+1].
[258]320
[394]321
322    // compute released block size
323    unsigned int size = 1<<size_index;
324
[588]325    // compute companion block and merged block base addresses
326    unsigned int companion_base; 
327    unsigned int merged_base; 
328
329    if ( (base & size) == 0 )   // the released block is aligned on (2*size)
[394]330    {
331        companion_base  = base + size;
332        merged_base     = base;
333    }
334    else
335    {
336        companion_base  = base - size;
337        merged_base     = base - size;
338    }
339
340    // scan all blocks in free[size_index]
341    // the iter & prev variables are actually addresses
342    unsigned int  found = 0;
343    unsigned int  iter  = heap->free[size_index];
344    unsigned int  prev  = (unsigned int)&heap->free[size_index];
[588]345    while ( iter ) 
[394]346    {
347        if ( iter == companion_base ) 
348        {
349            found = 1;
350            break;
351        }
[588]352        prev = iter;
[394]353        iter = *(unsigned int*)iter;
354    }
355
[588]356    if ( found == 0 )  // Companion not found => push in free[size_index] 
[394]357    {
358        *(unsigned int*)base   = heap->free[size_index];
359        heap->free[size_index] = base;
360    }
361    else               // Companion found : merge
362    {
363        // evict the searched block from free[size_index]
364        *(unsigned int*)prev = *(unsigned int*)iter;
365
366        // call the update_free() function for free[size_index+1]
367        update_free_array( heap, merged_base , size_index+1 );
368    }
369}
370
[382]371//////////////////////
372void free( void* ptr )
373{
[394]374    // get the cluster coordinate from ptr value
[390]375    unsigned int x;
376    unsigned int y;
377    giet_get_xy( ptr, &x, &y );
378
[468]379#if GIET_DEBUG_USER_MALLOC
[680]380giet_tty_printf("\n[DEBUG USER_MALLOC] Free for vaddr = %x / x = %d / y = %d\n",
[397]381                 (unsigned int)ptr, x, y );
382#endif
383
[394]384    // check ptr value
385    unsigned int base = (unsigned int)ptr;
[709]386    giet_pthread_assert( (base >= heap[x][y].heap_base) && 
387                         (base < (heap[x][y].heap_base + heap[x][y].heap_size)) ,
388                         "error in free() : illegal pointer for released block" );
[390]389 
[588]390    // get the lock protecting heap[x][y]
391    lock_acquire( &heap[x][y].lock );
392
[394]393    // compute released block index in alloc[] array
394    unsigned index = (base - heap[x][y].heap_base ) / MIN_BLOCK_SIZE;
395 
396    // get the released block size_index
397    unsigned char* pchar      = (unsigned char*)(heap[x][y].alloc_base + index);
398    unsigned int   size_index = (unsigned int)*pchar;
[390]399
[588]400    // check block allocation
401    if ( size_index == 0 )
402    {
403        lock_release( &heap[x][y].lock );
[709]404        giet_pthread_assert( 0 , "error in free() : released block not allocated");
[588]405    }
406
[394]407    // check released block alignment
408    if ( base % (1 << size_index) )
409    {
[709]410        lock_release( &heap[x][y].lock );
411        giet_pthread_assert( 0 , "error in free() : released block not aligned");
[394]412    }
[258]413
[781]414    // reset the alloc[index] entry
415    *pchar = 0;
416
[394]417    // call the recursive function update_free_array()
418    update_free_array( &heap[x][y], base, size_index ); 
[258]419
[394]420    // release the lock
421    lock_release( &heap[x][y].lock );
[258]422
[468]423#if GIET_DEBUG_USER_MALLOC
[394]424display_free_array(x,y);
425#endif
[258]426
[394]427} // end free()
428
[258]429// Local Variables:
430// tab-width: 4
431// c-basic-offset: 4
432// c-file-offsets:((innamespace . 0)(inline-open . 0))
433// indent-tabs-mode: nil
434// End:
435// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
436
437
438
Note: See TracBrowser for help on using the repository browser.