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

Last change on this file since 777 was 777, checked in by meunier, 8 years ago
  • Ajout de quelques fonction dans la lib math
  • Déplacement de certaines fonctions de stdlib vers string
  • Property svn:executable set to *
File size: 15.0 KB
Line 
1////////////////////////////////////////////////////////////////////////////////
2// File     : malloc.c
3// Date     : 05/03/2013
4// Author   : Jean-Baptiste Bréjon / alain greiner
5// Copyright (c) UPMC-LIP6
6////////////////////////////////////////////////////////////////////////////////
7
8#include "malloc.h"
9#include "stdio.h"
10#include "stdlib.h"
11#include "giet_config.h"
12
13// Global variables defining the heap descriptors array (one heap per cluster)
14giet_heap_t     heap[X_SIZE][Y_SIZE];
15
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
50////////////////////////////////////////
51void display_free_array( unsigned int x,
52                         unsigned int y )
53{
54    unsigned int next;
55    unsigned int id;
56    unsigned int iter;
57
58    giet_tty_printf("\nUser Heap[%d][%d] base = %x / size = %x\n", x , y , 
59                   heap[x][y].heap_base, heap[x][y].heap_size );
60    for ( id = 0 ; id < 32 ; id++ )
61    { 
62        next = heap[x][y].free[id];
63        giet_tty_printf(" - free[%d] = " , id );
64        iter = 0;
65        while ( next != 0 )
66        {
67            giet_tty_printf("%x | ", next );
68            next = (*(unsigned int*)next);
69            iter++;
70        }
71        giet_tty_printf("0\n");
72    }
73}  // end display_free_array()
74
75
76
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]
84
85    unsigned int alloc_base;       // alloc[] array base
86    unsigned int alloc_size;       // alloc[] array size
87    unsigned int alloc_index;      // size_index in alloc[array]
88
89    unsigned int index;            // iterator
90
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 );
94
95#if GIET_DEBUG_USER_MALLOC
96giet_tty_printf("\n[DEBUG USER_MALLOC] Starting Heap[%d][%d] initialisation /"
97                " base = %x / size = %x\n", x, y, heap_base, heap_size );
98#endif
99
100    // checking heap segment constraints
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");
107
108    // compute size of block containin alloc[] array
109    alloc_size = heap_size / MIN_BLOCK_SIZE;
110    if ( alloc_size < MIN_BLOCK_SIZE) alloc_size = MIN_BLOCK_SIZE;
111
112    // get index for the corresponding block
113    alloc_index = GET_SIZE_INDEX( alloc_size );
114
115    // compute alloc[] array base address
116    alloc_base = heap_base + heap_size - alloc_size;
117
118    // reset the free[] array
119    for ( index = 0 ; index < 32 ; index++ )
120    {
121        heap[x][y].free[index] = 0;
122    }
123
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 
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;
134    for ( index = heap_index-1 ; index >= alloc_index ; index-- )
135    {
136        heap[x][y].free[index] = base;
137        ptr = (unsigned int*)base;
138        *ptr = 0;
139        base = base + (1<<index);
140    }
141
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;
149
150    lock_init( &heap[x][y].lock );
151
152#if GIET_DEBUG_USER_MALLOC
153giet_tty_printf("\n[DEBUG USER_MALLOC] Completing Heap[%d][%d] initialisation\n", x, y );
154display_free_array(x,y);
155#endif
156               
157}  // end heap_init()
158
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;
173    }
174    else            // non terminal case : lower half block must be split again
175    {                               
176        return split_block( heap, vaddr, searched_index-1, requested_index );
177    }
178} // end split_block()
179
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
187    {
188        return 0;
189    }
190    else                            // search a block in free[searched_index]
191    {
192        unsigned int vaddr = heap->free[searched_index];
193        if ( vaddr == 0 )     // block not found : search in free[searched_index+1]
194        {
195            return get_block( heap, searched_index+1, requested_index );
196        }
197        else                // block found : pop it from free[searched_index]
198        {
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;
207            }
208            else                                      // split is required
209            {
210                return split_block( heap, vaddr, searched_index, requested_index );
211            }
212        } 
213    }
214} // end get_block()
215
216////////////////////////////////////////
217void * remote_malloc( int size,
218                      unsigned int x,
219                      unsigned int y ) 
220{
221
222#if GIET_DEBUG_USER_MALLOC
223giet_tty_printf("\n[DEBUG USER_MALLOC] request for Heap[%d][%d] / size = %x\n", 
224                 x, y, size );
225#endif
226
227    // checking arguments
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");
236
237    // normalize size
238    if ( size < MIN_BLOCK_SIZE ) size = MIN_BLOCK_SIZE;
239
240    // compute requested_index for the free[] array
241    unsigned int requested_index = GET_SIZE_INDEX( size );
242
243    // take the lock protecting access to heap[x][y]
244    lock_acquire( &heap[x][y].lock );
245
246    // call the recursive function get_block
247    unsigned int base = get_block( &heap[x][y], 
248                                   requested_index, 
249                                   requested_index );
250
251    // check block found
252    if (base == 0)
253    {
254        lock_release( &heap[x][y].lock );
255        giet_pthread_assert( 0 , "error in remote_malloc() : no more space\n" );
256    }
257
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 );
266        giet_pthread_assert( 0 , "error in remote_malloc() : block already allocated");
267    }
268
269    // update alloc_array
270    *ptr = requested_index;
271
272    // release the lock
273    lock_release( &heap[x][y].lock );
274 
275#if GIET_DEBUG_USER_MALLOC
276giet_tty_printf("\n[DEBUG USER_MALLOC] allocated block from heap[%d][%d] : "
277                "base = %x / size = %x\n", x , y , base , size );
278display_free_array(x,y);
279#endif
280
281    return (void*) base;
282
283} // end remote_malloc()
284
285
286//////////////////////////////////
287void * malloc( int size )
288{
289    // get cluster coordinates
290    unsigned int x;
291    unsigned int y;
292    unsigned int lpid;
293    giet_proc_xyp( &x, &y, &lpid );
294
295    return remote_malloc( size, x, y );
296} 
297
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
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].
320
321
322    // compute released block size
323    unsigned int size = 1<<size_index;
324
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)
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];
345    while ( iter ) 
346    {
347        if ( iter == companion_base ) 
348        {
349            found = 1;
350            break;
351        }
352        prev = iter;
353        iter = *(unsigned int*)iter;
354    }
355
356    if ( found == 0 )  // Companion not found => push in free[size_index] 
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
371//////////////////////
372void free( void* ptr )
373{
374    // get the cluster coordinate from ptr value
375    unsigned int x;
376    unsigned int y;
377    giet_get_xy( ptr, &x, &y );
378
379#if GIET_DEBUG_USER_MALLOC
380giet_tty_printf("\n[DEBUG USER_MALLOC] Free for vaddr = %x / x = %d / y = %d\n",
381                 (unsigned int)ptr, x, y );
382#endif
383
384    // check ptr value
385    unsigned int base = (unsigned int)ptr;
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" );
389 
390    // get the lock protecting heap[x][y]
391    lock_acquire( &heap[x][y].lock );
392
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;
399
400    // check block allocation
401    if ( size_index == 0 )
402    {
403        lock_release( &heap[x][y].lock );
404        giet_pthread_assert( 0 , "error in free() : released block not allocated");
405    }
406
407    // check released block alignment
408    if ( base % (1 << size_index) )
409    {
410        lock_release( &heap[x][y].lock );
411        giet_pthread_assert( 0 , "error in free() : released block not aligned");
412    }
413
414    // call the recursive function update_free_array()
415    update_free_array( &heap[x][y], base, size_index ); 
416
417    // release the lock
418    lock_release( &heap[x][y].lock );
419
420#if GIET_DEBUG_USER_MALLOC
421display_free_array(x,y);
422#endif
423
424} // end free()
425
426// Local Variables:
427// tab-width: 4
428// c-basic-offset: 4
429// c-file-offsets:((innamespace . 0)(inline-open . 0))
430// indent-tabs-mode: nil
431// End:
432// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
433
434
435
Note: See TracBrowser for help on using the repository browser.