Changeset 588 for soft/giet_vm/giet_libs/malloc.c
- Timestamp:
- Jul 8, 2015, 3:40:18 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
soft/giet_vm/giet_libs/malloc.c
r541 r588 11 11 12 12 // Global variables defining the heap descriptors array (one heap per cluster) 13 giet_heap_t heap[X_SIZE][Y_SIZE];13 giet_heap_t heap[X_SIZE][Y_SIZE]; 14 14 15 15 // Macro returning the smallest power of 2 larger or equal to size value … … 51 51 unsigned int y ) 52 52 { 53 giet_shr_printf( 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].heap_base, heap[x][y].heap_size, 83 heap[x][y].alloc_base, heap[x][y].alloc_size, 84 heap[x][y].free[0], heap[x][y].free[1], 85 heap[x][y].free[2], heap[x][y].free[3], 86 heap[x][y].free[4], heap[x][y].free[5], 87 heap[x][y].free[6], heap[x][y].free[7], 88 heap[x][y].free[8], heap[x][y].free[9], 89 heap[x][y].free[10], heap[x][y].free[11], 90 heap[x][y].free[12], heap[x][y].free[13], 91 heap[x][y].free[14], heap[x][y].free[15], 92 heap[x][y].free[16], heap[x][y].free[17], 93 heap[x][y].free[18], heap[x][y].free[19], 94 heap[x][y].free[20], heap[x][y].free[21], 95 heap[x][y].free[22], heap[x][y].free[23] ); 96 } // end display_free array() 53 unsigned int next; 54 unsigned int id; 55 unsigned int iter; 56 57 giet_shr_printf("\nUser Heap[%d][%d] base = %x / size = %x\n", x , y , 58 heap[x][y].heap_base, heap[x][y].heap_size ); 59 for ( id = 6 ; id < 28 ; id++ ) 60 { 61 next = heap[x][y].free[id]; 62 giet_shr_printf(" - free[%d] = " , id ); 63 iter = 0; 64 while ( next != 0 ) 65 { 66 giet_shr_printf("%x | ", next ); 67 next = (*(unsigned int*)next); 68 iter++; 69 } 70 giet_shr_printf("0\n"); 71 } 72 } // end display_free_array() 73 74 97 75 98 76 //////////////////////////////// … … 106 84 unsigned int alloc_base; // alloc[] array base 107 85 unsigned int alloc_size; // alloc[] array size 108 unsigned int alloc_index; // size_index in free[array]86 unsigned int alloc_index; // size_index in alloc[array] 109 87 110 88 unsigned int index; // iterator … … 149 127 } 150 128 129 // reset the alloc_size array 130 unsigned int word; 131 unsigned int* tab = (unsigned int*)alloc_base; 132 for ( word = 0 ; word < (alloc_size>>2) ; word++ ) tab[word] = 0; 133 151 134 // split the heap into various sizes blocks, 152 135 // initializes the free[] array and NEXT pointers … … 243 226 244 227 #if GIET_DEBUG_USER_MALLOC 245 giet_shr_printf("\n[DEBUG USER_MALLOC] Mallocrequest for Heap[%d][%d] / size = %x\n",228 giet_shr_printf("\n[DEBUG USER_MALLOC] request for Heap[%d][%d] / size = %x\n", 246 229 x, y, size ); 247 230 #endif … … 250 233 if (size == 0) 251 234 { 252 giet_exit(" ERROR inmalloc() : requested size = 0 \n");235 giet_exit("\nERROR in remote_malloc() : requested size = 0 \n"); 253 236 } 254 237 if ( x >= X_SIZE ) 255 238 { 256 giet_exit(" ERROR inmalloc() : x coordinate too large\n");239 giet_exit("\nERROR in remote_malloc() : x coordinate too large\n"); 257 240 } 258 241 if ( y >= Y_SIZE ) 259 242 { 260 giet_exit(" ERROR inmalloc() : y coordinate too large\n");243 giet_exit("\nERROR in remote_malloc() : y coordinate too large\n"); 261 244 } 262 245 … … 264 247 if ( heap[x][y].init != HEAP_INITIALIZED ) 265 248 { 266 giet_exit(" ERROR inmalloc() : heap not initialized\n");249 giet_exit("\nERROR in remote_malloc() : heap not initialized\n"); 267 250 } 268 251 … … 281 264 requested_index ); 282 265 283 // update the alloc[] array if block found 284 if ( base != 0 ) 285 { 286 unsigned offset = (base - heap[x][y].heap_base) / MIN_BLOCK_SIZE; 287 unsigned char* ptr = (unsigned char*)(heap[x][y].alloc_base + offset); 288 *ptr = requested_index; 289 } 266 // check block found 267 if ( base == 0 ) 268 { 269 lock_release( &heap[x][y].lock ); 270 giet_exit("\nERROR in remote_malloc() : no more space\n"); 271 } 272 273 // compute pointer in alloc[] array 274 unsigned offset = (base - heap[x][y].heap_base) / MIN_BLOCK_SIZE; 275 unsigned char* ptr = (unsigned char*)(heap[x][y].alloc_base + offset); 276 277 // check the alloc[] array 278 if ( *ptr != 0 ) 279 { 280 lock_release( &heap[x][y].lock ); 281 giet_exit("\nERROR in remote_malloc() : block already allocated ???\n"); 282 } 283 284 // update alloc_array 285 *ptr = requested_index; 290 286 291 287 // release the lock … … 293 289 294 290 #if GIET_DEBUG_USER_MALLOC 295 giet_shr_printf("\n[DEBUG USER_MALLOC] Malloc vaddr from Heap[%d][%d] = %x\n",296 x, y, base );291 giet_shr_printf("\n[DEBUG USER_MALLOC] allocated block from heap[%d][%d] : " 292 "base = %x / size = %x\n", x , y , base , size ); 297 293 display_free_array(x,y); 298 294 #endif … … 333 329 unsigned int size = 1<<size_index; 334 330 335 // compute companion_base and merged_base 336 unsigned int companion_base; // companion block base address 337 unsigned int merged_base; // merged block base address 338 if ( base % (size<<1) ) 331 // compute companion block and merged block base addresses 332 unsigned int companion_base; 333 unsigned int merged_base; 334 335 if ( (base & size) == 0 ) // the released block is aligned on (2*size) 339 336 { 340 337 companion_base = base + size; … … 352 349 unsigned int iter = heap->free[size_index]; 353 350 unsigned int prev = (unsigned int)&heap->free[size_index]; 354 while ( iter != 0)351 while ( iter ) 355 352 { 356 353 if ( iter == companion_base ) … … 359 356 break; 360 357 } 358 prev = iter; 361 359 iter = *(unsigned int*)iter; 362 prev = iter; 363 } 364 365 if ( found == 0 ) // Companion not found 366 { 367 // push the block in free[size_index] 360 } 361 362 if ( found == 0 ) // Companion not found => push in free[size_index] 363 { 368 364 *(unsigned int*)base = heap->free[size_index]; 369 365 heap->free[size_index] = base; … … 392 388 #endif 393 389 394 // get the lock protecting heap[x][y]395 lock_acquire( &heap[x][y].lock );396 397 390 // check ptr value 398 391 unsigned int base = (unsigned int)ptr; … … 403 396 } 404 397 398 // get the lock protecting heap[x][y] 399 lock_acquire( &heap[x][y].lock ); 400 405 401 // compute released block index in alloc[] array 406 402 unsigned index = (base - heap[x][y].heap_base ) / MIN_BLOCK_SIZE; … … 410 406 unsigned int size_index = (unsigned int)*pchar; 411 407 408 // check block allocation 409 if ( size_index == 0 ) 410 { 411 lock_release( &heap[x][y].lock ); 412 giet_exit("\nERROR in free() : released block not allocated ???\n"); 413 } 414 412 415 // check released block alignment 413 416 if ( base % (1 << size_index) ) 414 417 { 415 giet_exit(" ERROR in free() : released block not aligned");418 giet_exit("\nERROR in free() : released block not aligned\n"); 416 419 } 417 420
Note: See TracChangeset
for help on using the changeset viewer.