[382] | 1 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 2 | // File : malloc.c |
---|
| 3 | // Date : 05/03/2013 |
---|
| 4 | // Author : Jean-Baptiste Bréjon / alain greiner |
---|
| 5 | // Copyright (c) UPMC-LIP6 |
---|
| 6 | //////////////////////////////////////////////////////////////////////////////// |
---|
[258] | 7 | |
---|
| 8 | #include "malloc.h" |
---|
| 9 | #include "stdio.h" |
---|
| 10 | |
---|
[382] | 11 | // Global variables defining the heap descriptors array (one heap per cluster) |
---|
| 12 | giet_heap_t heap[X_SIZE][Y_SIZE]; |
---|
[325] | 13 | |
---|
[382] | 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 |
---|
[394] | 48 | //////////////////////////////////////// |
---|
| 49 | void 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 | |
---|
[382] | 99 | //////////////////////////////// |
---|
| 100 | void 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] |
---|
[258] | 106 | |
---|
[382] | 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] |
---|
[258] | 110 | |
---|
[382] | 111 | unsigned int index; // iterator |
---|
[258] | 112 | |
---|
[382] | 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 ); |
---|
[258] | 116 | |
---|
[382] | 117 | // checking heap segment constraints |
---|
| 118 | if ( heap_size == 0 ) // heap segment exist |
---|
| 119 | { |
---|
| 120 | giet_exit("ERROR in malloc() : heap not found \n"); |
---|
[258] | 121 | } |
---|
[382] | 122 | if ( heap_size != (1<<heap_index) ) // heap size power of 2 |
---|
[258] | 123 | { |
---|
[382] | 124 | giet_exit("ERROR in malloc() : heap size must be power of 2\n"); |
---|
[258] | 125 | } |
---|
[382] | 126 | if ( heap_base % heap_size ) // heap segment aligned |
---|
[258] | 127 | { |
---|
[382] | 128 | giet_exit("ERROR in malloc() : heap segment must be aligned\n"); |
---|
[258] | 129 | } |
---|
| 130 | |
---|
[390] | 131 | // compute size of block containin alloc[] array |
---|
[382] | 132 | alloc_size = heap_size / MIN_BLOCK_SIZE; |
---|
[390] | 133 | if ( alloc_size < MIN_BLOCK_SIZE) alloc_size = MIN_BLOCK_SIZE; |
---|
[258] | 134 | |
---|
[382] | 135 | // get index for the corresponding block |
---|
| 136 | alloc_index = GET_SIZE_INDEX( alloc_size ); |
---|
[258] | 137 | |
---|
[382] | 138 | // compute alloc[] array base address |
---|
| 139 | alloc_base = heap_base + heap_size - alloc_size; |
---|
[258] | 140 | |
---|
[382] | 141 | // reset the free[] array |
---|
| 142 | for ( index = 0 ; index < 32 ; index++ ) |
---|
| 143 | { |
---|
| 144 | heap[x][y].free[index] = 0; |
---|
[258] | 145 | } |
---|
| 146 | |
---|
[382] | 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; |
---|
[390] | 152 | for ( index = heap_index-1 ; index >= alloc_index ; index-- ) |
---|
[382] | 153 | { |
---|
| 154 | heap[x][y].free[index] = base; |
---|
| 155 | ptr = (unsigned int*)base; |
---|
| 156 | *ptr = 0; |
---|
| 157 | base = base + (1<<index); |
---|
[258] | 158 | } |
---|
| 159 | |
---|
[382] | 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; |
---|
[258] | 167 | |
---|
[382] | 168 | lock_release( &heap[x][y].lock ); |
---|
[258] | 169 | |
---|
[382] | 170 | #if GIET_DEBUG_MALLOC |
---|
[394] | 171 | giet_shr_printf("\n[MALLOC DEBUG] Completing Heap[%d][%d] initialisation\n", x, y ); |
---|
| 172 | display_free_array(x,y); |
---|
[382] | 173 | #endif |
---|
| 174 | |
---|
| 175 | } // end heap_init() |
---|
[258] | 176 | |
---|
[382] | 177 | //////////////////////////////////////////// |
---|
| 178 | unsigned 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; |
---|
[258] | 191 | } |
---|
[382] | 192 | else // non terminal case : lower half block must be split again |
---|
| 193 | { |
---|
| 194 | return split_block( heap, vaddr, searched_index-1, requested_index ); |
---|
[258] | 195 | } |
---|
[382] | 196 | } // end split_block() |
---|
[258] | 197 | |
---|
[382] | 198 | ////////////////////////////////////////// |
---|
| 199 | unsigned 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 |
---|
[258] | 205 | { |
---|
[382] | 206 | return 0; |
---|
[258] | 207 | } |
---|
[382] | 208 | else // search a block in free[searched_index] |
---|
[258] | 209 | { |
---|
[382] | 210 | unsigned int vaddr = heap->free[searched_index]; |
---|
| 211 | if ( vaddr == 0 ) // block not found : search in free[searched_index+1] |
---|
[258] | 212 | { |
---|
[382] | 213 | return get_block( heap, searched_index+1, requested_index ); |
---|
[258] | 214 | } |
---|
[382] | 215 | else // block found : pop it from free[searched_index] |
---|
[258] | 216 | { |
---|
[382] | 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; |
---|
[258] | 225 | } |
---|
[382] | 226 | else // split is required |
---|
| 227 | { |
---|
| 228 | return split_block( heap, vaddr, searched_index, requested_index ); |
---|
[258] | 229 | } |
---|
[382] | 230 | } |
---|
[258] | 231 | } |
---|
[382] | 232 | } // end get_block() |
---|
[258] | 233 | |
---|
[382] | 234 | //////////////////////////////////////// |
---|
| 235 | void * remote_malloc( unsigned int size, |
---|
| 236 | unsigned int x, |
---|
| 237 | unsigned int y ) |
---|
[258] | 238 | { |
---|
[397] | 239 | |
---|
| 240 | #if GIET_DEBUG_MALLOC |
---|
| 241 | giet_shr_printf("\n[MALLOC DEBUG] Malloc request for Heap[%d][%d] / size = %x\n", |
---|
| 242 | x, y, size ); |
---|
| 243 | #endif |
---|
| 244 | |
---|
[382] | 245 | // checking arguments |
---|
| 246 | if (size == 0) |
---|
[258] | 247 | { |
---|
[382] | 248 | giet_exit("ERROR in malloc() : requested size = 0 \n"); |
---|
[258] | 249 | } |
---|
[382] | 250 | if ( x >= X_SIZE ) |
---|
[258] | 251 | { |
---|
[382] | 252 | giet_exit("ERROR in malloc() : x coordinate too large\n"); |
---|
[258] | 253 | } |
---|
[382] | 254 | if ( y >= Y_SIZE ) |
---|
[258] | 255 | { |
---|
[382] | 256 | giet_exit("ERROR in malloc() : y coordinate too large\n"); |
---|
[258] | 257 | } |
---|
| 258 | |
---|
[382] | 259 | // initializes the heap if first access |
---|
| 260 | if ( heap[x][y].init != HEAP_INITIALIZED ) |
---|
[258] | 261 | { |
---|
[382] | 262 | heap_init( x, y ); |
---|
[258] | 263 | } |
---|
| 264 | |
---|
[390] | 265 | // normalize size |
---|
| 266 | if ( size < MIN_BLOCK_SIZE ) size = MIN_BLOCK_SIZE; |
---|
| 267 | |
---|
[382] | 268 | // compute requested_index for the free[] array |
---|
| 269 | unsigned int requested_index = GET_SIZE_INDEX( size ); |
---|
[258] | 270 | |
---|
[394] | 271 | // take the lock protecting access to heap[x][y] |
---|
[382] | 272 | lock_acquire( &heap[x][y].lock ); |
---|
[258] | 273 | |
---|
[382] | 274 | // call the recursive function get_block |
---|
| 275 | unsigned int base = get_block( &heap[x][y], |
---|
| 276 | requested_index, |
---|
| 277 | requested_index ); |
---|
[258] | 278 | |
---|
[390] | 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 | } |
---|
[258] | 286 | |
---|
[382] | 287 | // release the lock |
---|
| 288 | lock_release( &heap[x][y].lock ); |
---|
| 289 | |
---|
[394] | 290 | #if GIET_DEBUG_MALLOC |
---|
[397] | 291 | giet_shr_printf("\n[MALLOC DEBUG] Malloc vaddr from Heap[%d][%d] = %x\n", |
---|
| 292 | x, y, base ); |
---|
[394] | 293 | display_free_array(x,y); |
---|
| 294 | #endif |
---|
| 295 | |
---|
[382] | 296 | return (void*)base; |
---|
[258] | 297 | |
---|
[382] | 298 | } // end remote_malloc() |
---|
[258] | 299 | |
---|
| 300 | |
---|
[382] | 301 | ////////////////////////////////// |
---|
| 302 | void * malloc( unsigned int size ) |
---|
| 303 | { |
---|
[431] | 304 | // get cluster coordinates |
---|
| 305 | unsigned int x; |
---|
| 306 | unsigned int y; |
---|
| 307 | unsigned int lpid; |
---|
| 308 | giet_proc_xyp( &x, &y, &lpid ); |
---|
[258] | 309 | |
---|
[382] | 310 | return remote_malloc( size, x, y ); |
---|
| 311 | } |
---|
[258] | 312 | |
---|
[394] | 313 | /////////////////////////////////////////// |
---|
| 314 | void update_free_array( giet_heap_t* heap, |
---|
| 315 | unsigned int base, |
---|
| 316 | unsigned int size_index ) |
---|
| 317 | { |
---|
| 318 | // This recursive function try to merge the released block |
---|
| 319 | // with the companion block if this companion block is free. |
---|
| 320 | // This companion has the same size, and almost the same address |
---|
| 321 | // (only one address bit is different) |
---|
| 322 | // - If the companion is not in free[size_index], |
---|
| 323 | // the released block is pushed in free[size_index]. |
---|
| 324 | // - If the companion is found, it is evicted from free[size_index] |
---|
| 325 | // and the merged bloc is pushed in the free[size_index+1]. |
---|
[258] | 326 | |
---|
[394] | 327 | |
---|
| 328 | // compute released block size |
---|
| 329 | unsigned int size = 1<<size_index; |
---|
| 330 | |
---|
| 331 | // compute companion_base and merged_base |
---|
| 332 | unsigned int companion_base; // companion block base address |
---|
| 333 | unsigned int merged_base; // merged block base address |
---|
| 334 | if ( base % (size<<1) ) |
---|
| 335 | { |
---|
| 336 | companion_base = base + size; |
---|
| 337 | merged_base = base; |
---|
| 338 | } |
---|
| 339 | else |
---|
| 340 | { |
---|
| 341 | companion_base = base - size; |
---|
| 342 | merged_base = base - size; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | // scan all blocks in free[size_index] |
---|
| 346 | // the iter & prev variables are actually addresses |
---|
| 347 | unsigned int found = 0; |
---|
| 348 | unsigned int iter = heap->free[size_index]; |
---|
| 349 | unsigned int prev = (unsigned int)&heap->free[size_index]; |
---|
| 350 | while ( iter != 0 ) |
---|
| 351 | { |
---|
| 352 | if ( iter == companion_base ) |
---|
| 353 | { |
---|
| 354 | found = 1; |
---|
| 355 | break; |
---|
| 356 | } |
---|
| 357 | iter = *(unsigned int*)iter; |
---|
| 358 | prev = iter; |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | if ( found == 0 ) // Companion not found |
---|
| 362 | { |
---|
| 363 | // push the block in free[size_index] |
---|
| 364 | *(unsigned int*)base = heap->free[size_index]; |
---|
| 365 | heap->free[size_index] = base; |
---|
| 366 | } |
---|
| 367 | else // Companion found : merge |
---|
| 368 | { |
---|
| 369 | // evict the searched block from free[size_index] |
---|
| 370 | *(unsigned int*)prev = *(unsigned int*)iter; |
---|
| 371 | |
---|
| 372 | // call the update_free() function for free[size_index+1] |
---|
| 373 | update_free_array( heap, merged_base , size_index+1 ); |
---|
| 374 | } |
---|
| 375 | } |
---|
| 376 | |
---|
[382] | 377 | ////////////////////// |
---|
| 378 | void free( void* ptr ) |
---|
| 379 | { |
---|
[394] | 380 | // get the cluster coordinate from ptr value |
---|
[390] | 381 | unsigned int x; |
---|
| 382 | unsigned int y; |
---|
| 383 | giet_get_xy( ptr, &x, &y ); |
---|
| 384 | |
---|
[397] | 385 | #if GIET_DEBUG_MALLOC |
---|
| 386 | giet_shr_printf("\n[MALLOC DEBUG] Free for vaddr = %x / x = %d / y = %d\n", |
---|
| 387 | (unsigned int)ptr, x, y ); |
---|
| 388 | #endif |
---|
| 389 | |
---|
[394] | 390 | // get the lock protecting heap[x][y] |
---|
| 391 | lock_acquire( &heap[x][y].lock ); |
---|
| 392 | |
---|
| 393 | // check ptr value |
---|
| 394 | unsigned int base = (unsigned int)ptr; |
---|
| 395 | if ( (base < heap[x][y].heap_base) || |
---|
| 396 | (base >= (heap[x][y].heap_base + heap[x][y].heap_size)) ) |
---|
| 397 | { |
---|
| 398 | giet_exit("ERROR in free() : illegal pointer for released block"); |
---|
| 399 | } |
---|
[390] | 400 | |
---|
[394] | 401 | // compute released block index in alloc[] array |
---|
| 402 | unsigned index = (base - heap[x][y].heap_base ) / MIN_BLOCK_SIZE; |
---|
| 403 | |
---|
| 404 | // get the released block size_index |
---|
| 405 | unsigned char* pchar = (unsigned char*)(heap[x][y].alloc_base + index); |
---|
| 406 | unsigned int size_index = (unsigned int)*pchar; |
---|
[390] | 407 | |
---|
[394] | 408 | // check released block alignment |
---|
| 409 | if ( base % (1 << size_index) ) |
---|
| 410 | { |
---|
| 411 | giet_exit("ERROR in free() : released block not aligned"); |
---|
| 412 | } |
---|
[258] | 413 | |
---|
[394] | 414 | // call the recursive function update_free_array() |
---|
| 415 | update_free_array( &heap[x][y], base, size_index ); |
---|
[258] | 416 | |
---|
[394] | 417 | // release the lock |
---|
| 418 | lock_release( &heap[x][y].lock ); |
---|
[258] | 419 | |
---|
[394] | 420 | #if GIET_DEBUG_MALLOC |
---|
| 421 | display_free_array(x,y); |
---|
| 422 | #endif |
---|
[258] | 423 | |
---|
[394] | 424 | } // end free() |
---|
| 425 | |
---|
| 426 | |
---|
| 427 | |
---|
| 428 | |
---|
[258] | 429 | // Local Variables: |
---|
| 430 | // tab-width: 4 |
---|
| 431 | // c-basic-offset: 4 |
---|
| 432 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 433 | // indent-tabs-mode: nil |
---|
| 434 | // End: |
---|
| 435 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 436 | |
---|
| 437 | |
---|
| 438 | |
---|