Changeset 657 for trunk/kernel/libk/bits.c
- Timestamp:
- Mar 18, 2020, 11:16:59 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kernel/libk/bits.c
r635 r657 1 1 /* 2 * bits.c - bit s manipulation functionsimplementation2 * bits.c - bitmap API implementation 3 3 * 4 4 * Author Ghassan Almaless (2008,2009,2010,2011,2012) 5 * Alain Greiner (2016,2017,2018,2019 )5 * Alain Greiner (2016,2017,2018,2019,2020) 6 6 * 7 7 * Copyright (c) UPMC Sorbonne Universites … … 26 26 #include <bits.h> 27 27 28 ////////////////////////////////////////////////////////////////////////////// 29 ////////////// local access functions /////////////////////////////// 30 ////////////////////////////////////////////////////////////////////////////// 31 28 32 //////////////////////////////////// 29 33 void bitmap_init( bitmap_t * bitmap, … … 42 46 uint32_t index ) 43 47 { 44 uint32_t word = index / 32;45 uint32_t bit = index % 32;48 uint32_t word = index >> 5; 49 uint32_t bit = index & 0x1F; 46 50 47 51 bitmap[word] |= ( 1 << bit ); … … 52 56 uint32_t index ) 53 57 { 54 uint32_t word = index / 32;55 uint32_t bit = index % 32;58 uint32_t word = index >> 5; 59 uint32_t bit = index & 0x1F; 56 60 57 61 bitmap[word] &= ~( 1 << bit ); … … 62 66 uint32_t index ) 63 67 { 64 uint32_t word = index / 32;65 uint32_t bit = index % 32;68 uint32_t word = index >> 5; 69 uint32_t bit = index & 0x1F; 66 70 67 71 return (bitmap[word] & ( 1 << bit )) != 0; 68 72 } 73 74 ///////////////////////////////////////// 75 uint32_t bitmap_alloc( bitmap_t * bitmap, 76 uint32_t size ) 77 { 78 uint32_t max_word; 79 uint32_t max_bit; 80 uint32_t word; 81 uint32_t bit; 82 83 if( size ) 84 { 85 max_word = ( (size-1) >>5 ) + 1; 86 87 for( word = 0 ; word < max_word ; word++ ) 88 { 89 max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32; 90 91 if(bitmap[word] != 0XFFFFFFFF) 92 { 93 for(bit = 0 ; bit < max_bit ; bit++) 94 { 95 if( (bitmap[word] & (1 << bit)) == 0 ) 96 { 97 bitmap[word] |= (1 << bit); 98 return (word*32 + bit); 99 } 100 } 101 } 102 } 103 } 104 105 return -1; 106 107 } // end bitmap_alloc() 69 108 70 109 ////////////////////////////////////////// … … 207 246 { 208 247 uint32_t max_word; 248 uint32_t max_bit; 209 249 uint32_t word; 210 250 uint32_t bit; … … 216 256 for( word = 0 ; word < max_word ; word++ ) 217 257 { 258 max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32; 259 218 260 if(bitmap[word] != 0) 219 261 { 220 for(bit = 0 ; bit < 32; bit++)262 for(bit = 0 ; bit < max_bit ; bit++) 221 263 { 222 264 if( bitmap[word] & (1 << bit) ) return (word*32 + bit); … … 235 277 { 236 278 uint32_t max_word; 279 uint32_t max_bit; 237 280 uint32_t word; 238 281 uint32_t bit; … … 244 287 for( word = 0 ; word < max_word ; word++ ) 245 288 { 289 max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32; 290 246 291 if(bitmap[word] != 0XFFFFFFFF) 247 292 { 248 for(bit = 0 ; bit < 32; bit++)293 for(bit = 0 ; bit < max_bit ; bit++) 249 294 { 250 295 if( (bitmap[word] & (1 << bit)) == 0 ) return (word*32 + bit); … … 258 303 } // bitmap_ffc() 259 304 305 306 ////////////////////////////////////////////////////////////////////////////// 307 ////////////// remote access functions /////////////////////////////// 308 ////////////////////////////////////////////////////////////////////////////// 309 310 //////////////////////////////////////////// 311 void bitmap_remote_init( xptr_t bitmap_xp, 312 uint32_t len ) 313 { 314 bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp ); 315 cxy_t bitmap_cxy = GET_CXY( bitmap_xp ); 316 317 uint32_t word; 318 uint32_t nwords = BITMAP_SIZE( len ); 319 320 for( word = 0 ; word < nwords ; word++ ) 321 { 322 hal_remote_s32( XPTR( bitmap_cxy , &bitmap_ptr[word] ) , 0 ); 323 } 324 } 325 326 //////////////////////////////////////////////////// 327 inline void bitmap_remote_set( xptr_t bitmap_xp, 328 uint32_t index ) 329 { 330 bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp ); 331 cxy_t bitmap_cxy = GET_CXY( bitmap_xp ); 332 333 uint32_t word = index / 32; 334 uint32_t bit = index % 32; 335 336 hal_remote_atomic_or( XPTR( bitmap_cxy , &bitmap_ptr[word] ) , (1 <<bit) ); 337 } 338 339 ////////////////////////////////////////////////////// 340 inline void bitmap_remote_clear( xptr_t bitmap_xp, 341 uint32_t index ) 342 { 343 bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp ); 344 cxy_t bitmap_cxy = GET_CXY( bitmap_xp ); 345 346 uint32_t word = index / 32; 347 uint32_t bit = index % 32; 348 349 hal_remote_atomic_and( XPTR( bitmap_cxy , &bitmap_ptr[word] ) , ~(1 <<bit) ); 350 } 351 352 /////////////////////////////////////////////////// 353 uint32_t bitmap_remote_alloc( xptr_t bitmap_xp, 354 uint32_t size ) 355 { 356 uint32_t max_word; 357 uint32_t max_bit; 358 uint32_t word; 359 uint32_t bit; 360 xptr_t word_xp; 361 uint32_t value; 362 363 bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp ); 364 cxy_t bitmap_cxy = GET_CXY( bitmap_xp ); 365 366 if( size ) 367 { 368 max_word = ( (size-1) >>5 ) + 1; 369 370 for( word = 0 ; word < max_word ; word++ ) 371 { 372 max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32; 373 374 word_xp = XPTR( bitmap_cxy , &bitmap_ptr[word] ); 375 376 value = hal_remote_l32( word_xp ); 377 378 if( value != 0XFFFFFFFF ) 379 { 380 for(bit = 0 ; bit < max_bit ; bit++) 381 { 382 if( (value & (1 << bit)) == 0 ) 383 { 384 hal_remote_s32( word_xp , value | (1 << bit) ); 385 return (word*32 + bit); 386 } 387 } 388 } 389 } 390 } 391 392 return -1; 393 394 } // end bitmap_alloc() 395 396 /////////////////////////////////////////////// 397 uint32_t bitmap_remote_ffc( xptr_t bitmap_xp, 398 uint32_t size ) 399 { 400 uint32_t max_word; 401 uint32_t max_bit; 402 uint32_t word; 403 uint32_t bit; 404 uint32_t value; 405 406 bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp ); 407 cxy_t bitmap_cxy = GET_CXY( bitmap_xp ); 408 409 if( size ) 410 { 411 max_word = ( (size-1) >>5 ) + 1; 412 413 for( word = 0 ; word < max_word ; word++ ) 414 { 415 max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32; 416 417 value = hal_remote_l32( XPTR( bitmap_cxy , &bitmap_ptr[word] ) ); 418 419 if( value != 0xFFFFFFFF ) 420 { 421 for(bit = 0 ; bit < max_bit ; bit++) 422 { 423 if( (value & (1 << bit)) == 0 ) return (word*32 + bit); 424 } 425 } 426 } 427 } 428 429 return -1; 430 431 } // bitmap_remote_ffc() 432 433
Note: See TracChangeset
for help on using the changeset viewer.