xref: /onnv-gate/usr/src/lib/libntfs/common/include/ntfs/bitmap.h (revision 9663:ace9a2ac3683)
1*9663SMark.Logan@Sun.COM /*
2*9663SMark.Logan@Sun.COM  * bitmap.h - Exports for bitmap handling. Part of the Linux-NTFS project.
3*9663SMark.Logan@Sun.COM  *
4*9663SMark.Logan@Sun.COM  * Copyright (c) 2000-2004 Anton Altaparmakov
5*9663SMark.Logan@Sun.COM  * Copyright (c) 2004-2005 Richard Russon
6*9663SMark.Logan@Sun.COM  *
7*9663SMark.Logan@Sun.COM  * This program/include file is free software; you can redistribute it and/or
8*9663SMark.Logan@Sun.COM  * modify it under the terms of the GNU General Public License as published
9*9663SMark.Logan@Sun.COM  * by the Free Software Foundation; either version 2 of the License, or
10*9663SMark.Logan@Sun.COM  * (at your option) any later version.
11*9663SMark.Logan@Sun.COM  *
12*9663SMark.Logan@Sun.COM  * This program/include file is distributed in the hope that it will be
13*9663SMark.Logan@Sun.COM  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14*9663SMark.Logan@Sun.COM  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*9663SMark.Logan@Sun.COM  * GNU General Public License for more details.
16*9663SMark.Logan@Sun.COM  *
17*9663SMark.Logan@Sun.COM  * You should have received a copy of the GNU General Public License
18*9663SMark.Logan@Sun.COM  * along with this program (in the main directory of the Linux-NTFS
19*9663SMark.Logan@Sun.COM  * distribution in the file COPYING); if not, write to the Free Software
20*9663SMark.Logan@Sun.COM  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21*9663SMark.Logan@Sun.COM  */
22*9663SMark.Logan@Sun.COM 
23*9663SMark.Logan@Sun.COM #ifndef _NTFS_BITMAP_H
24*9663SMark.Logan@Sun.COM #define _NTFS_BITMAP_H
25*9663SMark.Logan@Sun.COM 
26*9663SMark.Logan@Sun.COM #include "types.h"
27*9663SMark.Logan@Sun.COM #include "attrib.h"
28*9663SMark.Logan@Sun.COM 
29*9663SMark.Logan@Sun.COM /*
30*9663SMark.Logan@Sun.COM  * NOTES:
31*9663SMark.Logan@Sun.COM  *
32*9663SMark.Logan@Sun.COM  * - Operations are 8-bit only to ensure the functions work both on little
33*9663SMark.Logan@Sun.COM  *   and big endian machines! So don't make them 32-bit ops!
34*9663SMark.Logan@Sun.COM  * - bitmap starts at bit = 0 and ends at bit = bitmap size - 1.
35*9663SMark.Logan@Sun.COM  * - _Caller_ has to make sure that the bit to operate on is less than the
36*9663SMark.Logan@Sun.COM  *   size of the bitmap.
37*9663SMark.Logan@Sun.COM  */
38*9663SMark.Logan@Sun.COM 
39*9663SMark.Logan@Sun.COM /**
40*9663SMark.Logan@Sun.COM  * ntfs_bit_set - set a bit in a field of bits
41*9663SMark.Logan@Sun.COM  * @bitmap:	field of bits
42*9663SMark.Logan@Sun.COM  * @bit:	bit to set
43*9663SMark.Logan@Sun.COM  * @new_value:	value to set bit to (0 or 1)
44*9663SMark.Logan@Sun.COM  *
45*9663SMark.Logan@Sun.COM  * Set the bit @bit in the @bitmap to @new_value. Ignore all errors.
46*9663SMark.Logan@Sun.COM  */
ntfs_bit_set(u8 * bitmap,const u64 bit,const u8 new_value)47*9663SMark.Logan@Sun.COM static __inline__ void ntfs_bit_set(u8 *bitmap, const u64 bit,
48*9663SMark.Logan@Sun.COM 		const u8 new_value)
49*9663SMark.Logan@Sun.COM {
50*9663SMark.Logan@Sun.COM 	if (!bitmap || new_value > 1)
51*9663SMark.Logan@Sun.COM 		return;
52*9663SMark.Logan@Sun.COM 	if (!new_value)
53*9663SMark.Logan@Sun.COM 		bitmap[bit >> 3] &= ~(1 << (bit & 7));
54*9663SMark.Logan@Sun.COM 	else
55*9663SMark.Logan@Sun.COM 		bitmap[bit >> 3] |= (1 << (bit & 7));
56*9663SMark.Logan@Sun.COM }
57*9663SMark.Logan@Sun.COM 
58*9663SMark.Logan@Sun.COM /**
59*9663SMark.Logan@Sun.COM  * ntfs_bit_get - get value of a bit in a field of bits
60*9663SMark.Logan@Sun.COM  * @bitmap:	field of bits
61*9663SMark.Logan@Sun.COM  * @bit:	bit to get
62*9663SMark.Logan@Sun.COM  *
63*9663SMark.Logan@Sun.COM  * Get and return the value of the bit @bit in @bitmap (0 or 1).
64*9663SMark.Logan@Sun.COM  * Return -1 on error.
65*9663SMark.Logan@Sun.COM  */
ntfs_bit_get(const u8 * bitmap,const u64 bit)66*9663SMark.Logan@Sun.COM static __inline__ char ntfs_bit_get(const u8 *bitmap, const u64 bit)
67*9663SMark.Logan@Sun.COM {
68*9663SMark.Logan@Sun.COM 	if (!bitmap)
69*9663SMark.Logan@Sun.COM 		return -1;
70*9663SMark.Logan@Sun.COM 	return (bitmap[bit >> 3] >> (bit & 7)) & 1;
71*9663SMark.Logan@Sun.COM }
72*9663SMark.Logan@Sun.COM 
ntfs_bit_change(u8 * bitmap,const u64 bit)73*9663SMark.Logan@Sun.COM static __inline__ void ntfs_bit_change(u8 *bitmap, const u64 bit)
74*9663SMark.Logan@Sun.COM {
75*9663SMark.Logan@Sun.COM 	if (!bitmap)
76*9663SMark.Logan@Sun.COM 		return;
77*9663SMark.Logan@Sun.COM 	bitmap[bit >> 3] ^= 1 << (bit & 7);
78*9663SMark.Logan@Sun.COM }
79*9663SMark.Logan@Sun.COM 
80*9663SMark.Logan@Sun.COM /**
81*9663SMark.Logan@Sun.COM  * ntfs_bit_get_and_set - get value of a bit in a field of bits and set it
82*9663SMark.Logan@Sun.COM  * @bitmap:	field of bits
83*9663SMark.Logan@Sun.COM  * @bit:	bit to get/set
84*9663SMark.Logan@Sun.COM  * @new_value:	value to set bit to (0 or 1)
85*9663SMark.Logan@Sun.COM  *
86*9663SMark.Logan@Sun.COM  * Return the value of the bit @bit and set it to @new_value (0 or 1).
87*9663SMark.Logan@Sun.COM  * Return -1 on error.
88*9663SMark.Logan@Sun.COM  */
ntfs_bit_get_and_set(u8 * bitmap,const u64 bit,const u8 new_value)89*9663SMark.Logan@Sun.COM static __inline__ char ntfs_bit_get_and_set(u8 *bitmap, const u64 bit,
90*9663SMark.Logan@Sun.COM 		const u8 new_value)
91*9663SMark.Logan@Sun.COM {
92*9663SMark.Logan@Sun.COM 	register u8 old_bit, shift;
93*9663SMark.Logan@Sun.COM 
94*9663SMark.Logan@Sun.COM 	if (!bitmap || new_value > 1)
95*9663SMark.Logan@Sun.COM 		return -1;
96*9663SMark.Logan@Sun.COM 	shift = bit & 7;
97*9663SMark.Logan@Sun.COM 	old_bit = (bitmap[bit >> 3] >> shift) & 1;
98*9663SMark.Logan@Sun.COM 	if (new_value != old_bit)
99*9663SMark.Logan@Sun.COM 		bitmap[bit >> 3] ^= 1 << shift;
100*9663SMark.Logan@Sun.COM 	return old_bit;
101*9663SMark.Logan@Sun.COM }
102*9663SMark.Logan@Sun.COM 
103*9663SMark.Logan@Sun.COM extern int ntfs_bitmap_set_run(ntfs_attr *na, s64 start_bit, s64 count);
104*9663SMark.Logan@Sun.COM extern int ntfs_bitmap_clear_run(ntfs_attr *na, s64 start_bit, s64 count);
105*9663SMark.Logan@Sun.COM 
106*9663SMark.Logan@Sun.COM /**
107*9663SMark.Logan@Sun.COM  * ntfs_bitmap_set_bit - set a bit in a bitmap
108*9663SMark.Logan@Sun.COM  * @na:		attribute containing the bitmap
109*9663SMark.Logan@Sun.COM  * @bit:	bit to set
110*9663SMark.Logan@Sun.COM  *
111*9663SMark.Logan@Sun.COM  * Set the @bit in the bitmap described by the attribute @na.
112*9663SMark.Logan@Sun.COM  *
113*9663SMark.Logan@Sun.COM  * On success return 0 and on error return -1 with errno set to the error code.
114*9663SMark.Logan@Sun.COM  */
ntfs_bitmap_set_bit(ntfs_attr * na,s64 bit)115*9663SMark.Logan@Sun.COM static __inline__ int ntfs_bitmap_set_bit(ntfs_attr *na, s64 bit)
116*9663SMark.Logan@Sun.COM {
117*9663SMark.Logan@Sun.COM 	return ntfs_bitmap_set_run(na, bit, 1);
118*9663SMark.Logan@Sun.COM }
119*9663SMark.Logan@Sun.COM 
120*9663SMark.Logan@Sun.COM /**
121*9663SMark.Logan@Sun.COM  * ntfs_bitmap_clear_bit - clear a bit in a bitmap
122*9663SMark.Logan@Sun.COM  * @na:		attribute containing the bitmap
123*9663SMark.Logan@Sun.COM  * @bit:	bit to clear
124*9663SMark.Logan@Sun.COM  *
125*9663SMark.Logan@Sun.COM  * Clear @bit in the bitmap described by the attribute @na.
126*9663SMark.Logan@Sun.COM  *
127*9663SMark.Logan@Sun.COM  * On success return 0 and on error return -1 with errno set to the error code.
128*9663SMark.Logan@Sun.COM  */
ntfs_bitmap_clear_bit(ntfs_attr * na,s64 bit)129*9663SMark.Logan@Sun.COM static __inline__ int ntfs_bitmap_clear_bit(ntfs_attr *na, s64 bit)
130*9663SMark.Logan@Sun.COM {
131*9663SMark.Logan@Sun.COM 	return ntfs_bitmap_clear_run(na, bit, 1);
132*9663SMark.Logan@Sun.COM }
133*9663SMark.Logan@Sun.COM 
134*9663SMark.Logan@Sun.COM #endif /* defined _NTFS_BITMAP_H */
135