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) |
---|
14 | giet_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 | //////////////////////////////////////// |
---|
51 | void 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 | //////////////////////////////// |
---|
78 | void 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 |
---|
96 | giet_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 |
---|
153 | giet_tty_printf("\n[DEBUG USER_MALLOC] Completing Heap[%d][%d] initialisation\n", x, y ); |
---|
154 | display_free_array(x,y); |
---|
155 | #endif |
---|
156 | |
---|
157 | } // end heap_init() |
---|
158 | |
---|
159 | //////////////////////////////////////////// |
---|
160 | unsigned 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 | ////////////////////////////////////////// |
---|
181 | unsigned 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 | //////////////////////////////////////// |
---|
217 | void * remote_malloc( int size, |
---|
218 | unsigned int x, |
---|
219 | unsigned int y ) |
---|
220 | { |
---|
221 | |
---|
222 | #if GIET_DEBUG_USER_MALLOC |
---|
223 | giet_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 |
---|
276 | giet_tty_printf("\n[DEBUG USER_MALLOC] allocated block from heap[%d][%d] : " |
---|
277 | "base = %x / size = %x\n", x , y , base , size ); |
---|
278 | display_free_array(x,y); |
---|
279 | #endif |
---|
280 | |
---|
281 | return (void*) base; |
---|
282 | |
---|
283 | } // end remote_malloc() |
---|
284 | |
---|
285 | |
---|
286 | ////////////////////////////////// |
---|
287 | void * 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 | //////////////////////////////////// |
---|
300 | void * calloc ( int nbmem, int size ) |
---|
301 | { |
---|
302 | void * a = malloc( nbmem * size ); |
---|
303 | memset( a, 0, nbmem * size ); |
---|
304 | return a; |
---|
305 | } |
---|
306 | |
---|
307 | |
---|
308 | ///////////////////////////////////////////////// |
---|
309 | // Added by QM |
---|
310 | // Warning, I can't have tested this function yet |
---|
311 | ///////////////////////////////////////////////// |
---|
312 | void * realloc ( void * ptr, int size ) |
---|
313 | { |
---|
314 | if ( ptr == NULL ) |
---|
315 | { |
---|
316 | return malloc( size ); |
---|
317 | } |
---|
318 | if ( size == 0 ) |
---|
319 | { |
---|
320 | free( ptr ); |
---|
321 | return NULL; |
---|
322 | } |
---|
323 | unsigned int x; |
---|
324 | unsigned int y; |
---|
325 | giet_get_xy( ptr, &x, &y ); |
---|
326 | unsigned int base = (unsigned int) ptr; |
---|
327 | int index = (base - heap[x][y].heap_base) / MIN_BLOCK_SIZE; |
---|
328 | |
---|
329 | char * pchar = (char *) (heap[x][y].alloc_base + index); |
---|
330 | int old_size = 1 << ((int) *pchar); |
---|
331 | |
---|
332 | void * new_ptr = malloc( size ); |
---|
333 | int min_size = (size < old_size) ? size : old_size; |
---|
334 | memcpy( new_ptr, ptr, min_size ); |
---|
335 | free( ptr ); |
---|
336 | return new_ptr; |
---|
337 | } |
---|
338 | |
---|
339 | |
---|
340 | |
---|
341 | /////////////////////////////////////////// |
---|
342 | void update_free_array( giet_heap_t* heap, |
---|
343 | unsigned int base, |
---|
344 | unsigned int size_index ) |
---|
345 | { |
---|
346 | // This recursive function try to merge the released block |
---|
347 | // with the companion block if this companion block is free. |
---|
348 | // This companion has the same size, and almost the same address |
---|
349 | // (only one address bit is different) |
---|
350 | // - If the companion is not in free[size_index], |
---|
351 | // the released block is pushed in free[size_index]. |
---|
352 | // - If the companion is found, it is evicted from free[size_index] |
---|
353 | // and the merged bloc is pushed in the free[size_index+1]. |
---|
354 | |
---|
355 | |
---|
356 | // compute released block size |
---|
357 | unsigned int size = 1<<size_index; |
---|
358 | |
---|
359 | // compute companion block and merged block base addresses |
---|
360 | unsigned int companion_base; |
---|
361 | unsigned int merged_base; |
---|
362 | |
---|
363 | if ( (base & size) == 0 ) // the released block is aligned on (2*size) |
---|
364 | { |
---|
365 | companion_base = base + size; |
---|
366 | merged_base = base; |
---|
367 | } |
---|
368 | else |
---|
369 | { |
---|
370 | companion_base = base - size; |
---|
371 | merged_base = base - size; |
---|
372 | } |
---|
373 | |
---|
374 | // scan all blocks in free[size_index] |
---|
375 | // the iter & prev variables are actually addresses |
---|
376 | unsigned int found = 0; |
---|
377 | unsigned int iter = heap->free[size_index]; |
---|
378 | unsigned int prev = (unsigned int)&heap->free[size_index]; |
---|
379 | while ( iter ) |
---|
380 | { |
---|
381 | if ( iter == companion_base ) |
---|
382 | { |
---|
383 | found = 1; |
---|
384 | break; |
---|
385 | } |
---|
386 | prev = iter; |
---|
387 | iter = *(unsigned int*)iter; |
---|
388 | } |
---|
389 | |
---|
390 | if ( found == 0 ) // Companion not found => push in free[size_index] |
---|
391 | { |
---|
392 | *(unsigned int*)base = heap->free[size_index]; |
---|
393 | heap->free[size_index] = base; |
---|
394 | } |
---|
395 | else // Companion found : merge |
---|
396 | { |
---|
397 | // evict the searched block from free[size_index] |
---|
398 | *(unsigned int*)prev = *(unsigned int*)iter; |
---|
399 | |
---|
400 | // call the update_free() function for free[size_index+1] |
---|
401 | update_free_array( heap, merged_base , size_index+1 ); |
---|
402 | } |
---|
403 | } |
---|
404 | |
---|
405 | ////////////////////// |
---|
406 | void free( void* ptr ) |
---|
407 | { |
---|
408 | // get the cluster coordinate from ptr value |
---|
409 | unsigned int x; |
---|
410 | unsigned int y; |
---|
411 | giet_get_xy( ptr, &x, &y ); |
---|
412 | |
---|
413 | #if GIET_DEBUG_USER_MALLOC |
---|
414 | giet_tty_printf("\n[DEBUG USER_MALLOC] Free for vaddr = %x / x = %d / y = %d\n", |
---|
415 | (unsigned int)ptr, x, y ); |
---|
416 | #endif |
---|
417 | |
---|
418 | // check ptr value |
---|
419 | unsigned int base = (unsigned int)ptr; |
---|
420 | giet_pthread_assert( (base >= heap[x][y].heap_base) && |
---|
421 | (base < (heap[x][y].heap_base + heap[x][y].heap_size)) , |
---|
422 | "error in free() : illegal pointer for released block" ); |
---|
423 | |
---|
424 | // get the lock protecting heap[x][y] |
---|
425 | lock_acquire( &heap[x][y].lock ); |
---|
426 | |
---|
427 | // compute released block index in alloc[] array |
---|
428 | unsigned index = (base - heap[x][y].heap_base ) / MIN_BLOCK_SIZE; |
---|
429 | |
---|
430 | // get the released block size_index |
---|
431 | unsigned char* pchar = (unsigned char*)(heap[x][y].alloc_base + index); |
---|
432 | unsigned int size_index = (unsigned int)*pchar; |
---|
433 | |
---|
434 | // check block allocation |
---|
435 | if ( size_index == 0 ) |
---|
436 | { |
---|
437 | lock_release( &heap[x][y].lock ); |
---|
438 | giet_pthread_assert( 0 , "error in free() : released block not allocated"); |
---|
439 | } |
---|
440 | |
---|
441 | // check released block alignment |
---|
442 | if ( base % (1 << size_index) ) |
---|
443 | { |
---|
444 | lock_release( &heap[x][y].lock ); |
---|
445 | giet_pthread_assert( 0 , "error in free() : released block not aligned"); |
---|
446 | } |
---|
447 | |
---|
448 | // reset the alloc[index] entry |
---|
449 | *pchar = 0; |
---|
450 | |
---|
451 | // call the recursive function update_free_array() |
---|
452 | update_free_array( &heap[x][y], base, size_index ); |
---|
453 | |
---|
454 | // release the lock |
---|
455 | lock_release( &heap[x][y].lock ); |
---|
456 | |
---|
457 | #if GIET_DEBUG_USER_MALLOC |
---|
458 | display_free_array(x,y); |
---|
459 | #endif |
---|
460 | |
---|
461 | } // end free() |
---|
462 | |
---|
463 | // Local Variables: |
---|
464 | // tab-width: 4 |
---|
465 | // c-basic-offset: 4 |
---|
466 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
467 | // indent-tabs-mode: nil |
---|
468 | // End: |
---|
469 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
470 | |
---|
471 | |
---|
472 | |
---|