1 /*             ----> DO NOT REMOVE THE FOLLOWING NOTICE <----
2  *
3  *                 Copyright (c) 2014-2015 Datalight, Inc.
4  *                     All Rights Reserved Worldwide.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; use version 2 of the License.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but "AS-IS," WITHOUT ANY WARRANTY; without even the implied warranty
12  *  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License along
16  *  with this program; if not, write to the Free Software Foundation, Inc.,
17  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 /*  Businesses and individuals that for commercial or other reasons cannot
21  *  comply with the terms of the GPLv2 license may obtain a commercial license
22  *  before incorporating Reliance Edge into proprietary software for
23  *  distribution in any form.  Visit http://www.datalight.com/reliance-edge for
24  *  more information.
25  */
26 
27 /** @file
28  *  @brief Implements utilities for working with bitmaps.
29  */
30 #include <redfs.h>
31 
32 
33 /** @brief Query the state of a bit in a bitmap.
34  *
35  *  Bits are counted from most significant to least significant.  Thus, the mask
36  *  for bit zero is 0x80 applied to the first byte in the bitmap.
37  *
38  *  @param pbBitmap Pointer to the bitmap.
39  *  @param ulBit    The bit to query.
40  *
41  *  @retval Whether the bit is set (true) or clear (false.
42  */
RedBitGet(const uint8_t * pbBitmap,uint32_t ulBit)43 bool RedBitGet( const uint8_t * pbBitmap,
44                 uint32_t ulBit )
45 {
46     bool fRet;
47 
48     if( pbBitmap == NULL )
49     {
50         REDERROR();
51         fRet = false;
52     }
53     else
54     {
55         fRet = ( pbBitmap[ ulBit >> 3U ] & ( 0x80U >> ( ulBit & 7U ) ) ) != 0U;
56     }
57 
58     return fRet;
59 }
60 
61 
62 /** @brief Set a bit in a bitmap to one.
63  *
64  *  Bits are counted from most significant to least significant.  Thus, the mask
65  *  for bit zero is 0x80 applied to the first byte in the bitmap.
66  *
67  *  @param pbBitmap Pointer to the bitmap.
68  *  @param ulBit    The bit to set.
69  */
RedBitSet(uint8_t * pbBitmap,uint32_t ulBit)70 void RedBitSet( uint8_t * pbBitmap,
71                 uint32_t ulBit )
72 {
73     REDASSERT( pbBitmap != NULL );
74 
75     if( pbBitmap != NULL )
76     {
77         pbBitmap[ ulBit >> 3U ] |= ( 0x80U >> ( ulBit & 7U ) );
78     }
79 }
80 
81 
82 /** @brief Clear a bit in a bitmap to zero.
83  *
84  *  Bits are counted from most significant to least significant.  Thus, the mask
85  *  for bit zero is 0x80 applied to the first byte in the bitmap.
86  *
87  *  @param pbBitmap Pointer to the bitmap.
88  *  @param ulBit    The bit to clear.
89  */
RedBitClear(uint8_t * pbBitmap,uint32_t ulBit)90 void RedBitClear( uint8_t * pbBitmap,
91                   uint32_t ulBit )
92 {
93     REDASSERT( pbBitmap != NULL );
94 
95     if( pbBitmap != NULL )
96     {
97         pbBitmap[ ulBit >> 3U ] &= ~( 0x80U >> ( ulBit & 7U ) );
98     }
99 }
100