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

Last change on this file since 397 was 397, checked in by alain, 10 years ago

Bug fix in malloc.c.

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