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

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

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

  • Property svn:executable set to *
File size: 12.1 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 heap_init( unsigned int x,
50                unsigned int y )
51{
52    unsigned int heap_base;        // heap segment base
53    unsigned int heap_size;        // heap segment size
54    unsigned int heap_index;       // size_index in free[array]
55
56    unsigned int alloc_base;       // alloc[] array base
57    unsigned int alloc_size;       // alloc[] array size
58    unsigned int alloc_index;      // size_index in free[array]
59
60    unsigned int index;            // iterator
61
62    // get heap_base, heap size, and heap index
63    giet_heap_info( &heap_base, &heap_size, x, y );
64    heap_index = GET_SIZE_INDEX( heap_size );
65
66    // checking heap segment constraints
67    if ( heap_size == 0 )                                    // heap segment exist
68    {
69        giet_exit("ERROR in malloc() : heap not found \n");
70    }
71    if ( heap_size != (1<<heap_index) )                      // heap size power of 2
72    {
73        giet_exit("ERROR in malloc() : heap size must be power of 2\n");
74    }
75    if ( heap_base % heap_size )                             // heap segment aligned
76    {
77        giet_exit("ERROR in malloc() : heap segment must be aligned\n");
78    }
79
80    // compute size of block containin alloc[] array
81    alloc_size = heap_size / MIN_BLOCK_SIZE;
82    if ( alloc_size < MIN_BLOCK_SIZE) alloc_size = MIN_BLOCK_SIZE;
83
84    // get index for the corresponding block
85    alloc_index = GET_SIZE_INDEX( alloc_size );
86
87    // compute alloc[] array base address
88    alloc_base = heap_base + heap_size - alloc_size;
89
90    // reset the free[] array
91    for ( index = 0 ; index < 32 ; index++ )
92    {
93        heap[x][y].free[index] = 0;
94    }
95
96    // split the heap into various sizes blocks,
97    // initializes the free[] array and NEXT pointers
98    // base is the block base address
99    unsigned int   base = heap_base;
100    unsigned int*  ptr;
101    for ( index = heap_index-1 ; index >= alloc_index ; index-- )
102    {
103        heap[x][y].free[index] = base;
104        ptr = (unsigned int*)base;
105        *ptr = 0;
106        base = base + (1<<index);
107    }
108
109    heap[x][y].init       = HEAP_INITIALIZED;
110    heap[x][y].x          = x;
111    heap[x][y].y          = y;
112    heap[x][y].heap_base  = heap_base;
113    heap[x][y].heap_size  = heap_size;
114    heap[x][y].alloc_size = alloc_size;
115    heap[x][y].alloc_base = alloc_base;
116
117    lock_release( &heap[x][y].lock );
118
119#if GIET_DEBUG_MALLOC
120giet_shr_printf("\n[MALLOC DEBUG] Heap[%d][%d] initialisation\n"
121                " - heap_base  = %x\n"
122                " - heap_size  = %x\n"
123                " - alloc_base = %x\n"
124                " - alloc_size = %x\n"
125                " - free[0]    = %x\n"
126                " - free[1]    = %x\n"
127                " - free[2]    = %x\n"
128                " - free[3]    = %x\n"
129                " - free[4]    = %x\n"
130                " - free[5]    = %x\n"
131                " - free[6]    = %x\n"
132                " - free[7]    = %x\n"
133                " - free[8]    = %x\n"
134                " - free[9]    = %x\n"
135                " - free[10]   = %x\n"
136                " - free[11]   = %x\n"
137                " - free[12]   = %x\n"
138                " - free[13]   = %x\n"
139                " - free[14]   = %x\n"
140                " - free[15]   = %x\n"
141                " - free[16]   = %x\n"
142                " - free[17]   = %x\n"
143                " - free[18]   = %x\n"
144                " - free[19]   = %x\n"
145                " - free[20]   = %x\n"
146                " - free[21]   = %x\n"
147                " - free[22]   = %x\n"
148                " - free[23]   = %x\n",
149                heap[x][y].x, heap[x][y].y, 
150                heap[x][y].heap_base, heap[x][y].heap_size, 
151                heap[x][y].alloc_base, heap[x][y].alloc_size,
152                heap[x][y].free[0], heap[x][y].free[1], 
153                heap[x][y].free[2], heap[x][y].free[3],
154                heap[x][y].free[4], heap[x][y].free[5],
155                heap[x][y].free[6], heap[x][y].free[7],
156                heap[x][y].free[8], heap[x][y].free[9],
157                heap[x][y].free[10], heap[x][y].free[11],
158                heap[x][y].free[12], heap[x][y].free[13],
159                heap[x][y].free[14], heap[x][y].free[15],
160                heap[x][y].free[16], heap[x][y].free[17],
161                heap[x][y].free[18], heap[x][y].free[19],
162                heap[x][y].free[20], heap[x][y].free[21],
163                heap[x][y].free[22], heap[x][y].free[23] );
164#endif
165               
166}  // end heap_init()
167
168////////////////////////////////////////////
169unsigned int split_block( giet_heap_t* heap,
170                          unsigned int vaddr, 
171                          unsigned int searched_index,
172                          unsigned int requested_index )
173{
174    // push the upper half block into free[searched_index-1]
175    unsigned int* new            = (unsigned int*)(vaddr + (1<<(searched_index-1)));
176    *new                         = heap->free[searched_index-1]; 
177    heap->free[searched_index-1] = (unsigned int)new;
178       
179    if ( searched_index == requested_index + 1 )  // terminal case: return lower half block
180    {
181        return vaddr;
182    }
183    else            // non terminal case : lower half block must be split again
184    {                               
185        return split_block( heap, vaddr, searched_index-1, requested_index );
186    }
187} // end split_block()
188
189//////////////////////////////////////////
190unsigned int get_block( giet_heap_t* heap,
191                        unsigned int searched_index,
192                        unsigned int requested_index )
193{
194    // test terminal case
195    if ( (1<<searched_index) > heap->heap_size )  // failure : return a NULL value
196    {
197        return 0;
198    }
199    else                            // search a block in free[searched_index]
200    {
201        unsigned int vaddr = heap->free[searched_index];
202        if ( vaddr == 0 )     // block not found : search in free[searched_index+1]
203        {
204            return get_block( heap, searched_index+1, requested_index );
205        }
206        else                // block found : pop it from free[searched_index]
207        {
208            // pop the block from free[searched_index]
209            unsigned int next = *((unsigned int*)vaddr); 
210            heap->free[searched_index] = next;
211           
212            // test if the block must be split
213            if ( searched_index == requested_index )  // no split required
214            {
215                return vaddr;
216            }
217            else                                      // split is required
218            {
219                return split_block( heap, vaddr, searched_index, requested_index );
220            }
221        } 
222    }
223} // end get_block()
224
225////////////////////////////////////////
226void * remote_malloc( unsigned int size,
227                      unsigned int x,
228                      unsigned int y ) 
229{
230    // checking arguments
231    if (size == 0) 
232    {
233        giet_exit("ERROR in malloc() : requested size = 0 \n");
234    }
235    if ( x >= X_SIZE )
236    {
237        giet_exit("ERROR in malloc() : x coordinate too large\n");
238    }
239    if ( y >= Y_SIZE )
240    {
241        giet_exit("ERROR in malloc() : y coordinate too large\n");
242    }
243
244    // initializes the heap if first access
245    if ( heap[x][y].init != HEAP_INITIALIZED )
246    {
247        heap_init( x, y );
248    }
249
250    // normalize size
251    if ( size < MIN_BLOCK_SIZE ) size = MIN_BLOCK_SIZE;
252
253    // compute requested_index for the free[] array
254    unsigned int requested_index = GET_SIZE_INDEX( size );
255
256    // take the lock protecting access to heap(x,y)
257    lock_acquire( &heap[x][y].lock );
258
259    // call the recursive function get_block
260    unsigned int base = get_block( &heap[x][y], 
261                                   requested_index, 
262                                   requested_index );
263
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    }
271
272    // release the lock
273    lock_release( &heap[x][y].lock );
274 
275    return (void*)base;
276
277} // end remote_malloc()
278
279
280//////////////////////////////////
281void * malloc( unsigned int size )
282{
283    unsigned int proc_id    = giet_procid();
284    unsigned int cluster_xy = proc_id / NB_PROCS_MAX;
285    unsigned int x          = cluster_xy >> Y_WIDTH;
286    unsigned int y          = cluster_xy & ((1<<Y_WIDTH)-1);
287
288    return remote_malloc( size, x, y );
289} 
290
291
292//////////////////////
293void free( void* ptr )
294{
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
315}
316
317
318
319
320// Local Variables:
321// tab-width: 4
322// c-basic-offset: 4
323// c-file-offsets:((innamespace . 0)(inline-open . 0))
324// indent-tabs-mode: nil
325// End:
326// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
327
328
329
Note: See TracBrowser for help on using the repository browser.