Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

bitmap.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <errno.h>
  25. #include <ipxe/bitmap.h>
  26. /** @file
  27. *
  28. * Bitmaps for multicast downloads
  29. *
  30. */
  31. /**
  32. * Resize bitmap
  33. *
  34. * @v bitmap Bitmap
  35. * @v new_length New length of bitmap, in bits
  36. * @ret rc Return status code
  37. */
  38. int bitmap_resize ( struct bitmap *bitmap, unsigned int new_length ) {
  39. unsigned int old_num_blocks;
  40. unsigned int new_num_blocks;
  41. size_t new_size;
  42. bitmap_block_t *new_blocks;
  43. old_num_blocks = BITMAP_INDEX ( bitmap->length + BITMAP_BLKSIZE - 1 );
  44. new_num_blocks = BITMAP_INDEX ( new_length + BITMAP_BLKSIZE - 1 );
  45. if ( old_num_blocks != new_num_blocks ) {
  46. new_size = ( new_num_blocks * sizeof ( bitmap->blocks[0] ) );
  47. new_blocks = realloc ( bitmap->blocks, new_size );
  48. if ( ! new_blocks ) {
  49. DBGC ( bitmap, "Bitmap %p could not resize to %d "
  50. "bits\n", bitmap, new_length );
  51. return -ENOMEM;
  52. }
  53. bitmap->blocks = new_blocks;
  54. }
  55. bitmap->length = new_length;
  56. while ( old_num_blocks < new_num_blocks ) {
  57. bitmap->blocks[old_num_blocks++] = 0;
  58. }
  59. DBGC ( bitmap, "Bitmap %p resized to %d bits\n", bitmap, new_length );
  60. return 0;
  61. }
  62. /**
  63. * Test bit in bitmap
  64. *
  65. * @v bitmap Bitmap
  66. * @v bit Bit index
  67. * @ret is_set Bit is set
  68. */
  69. int bitmap_test ( struct bitmap *bitmap, unsigned int bit ) {
  70. unsigned int index = BITMAP_INDEX ( bit );
  71. bitmap_block_t mask = BITMAP_MASK ( bit );
  72. if ( bit >= bitmap->length )
  73. return 0;
  74. return ( ( bitmap->blocks[index] & mask ) != 0 );
  75. }
  76. /**
  77. * Set bit in bitmap
  78. *
  79. * @v bitmap Bitmap
  80. * @v bit Bit index
  81. */
  82. void bitmap_set ( struct bitmap *bitmap, unsigned int bit ) {
  83. unsigned int index = BITMAP_INDEX ( bit );
  84. bitmap_block_t mask = BITMAP_MASK ( bit );
  85. DBGC ( bitmap, "Bitmap %p setting bit %d\n", bitmap, bit );
  86. /* Update bitmap */
  87. bitmap->blocks[index] |= mask;
  88. /* Update first gap counter */
  89. while ( bitmap_test ( bitmap, bitmap->first_gap ) ) {
  90. bitmap->first_gap++;
  91. }
  92. }