1N/A/*
1N/A * bitmap.h - Exports for bitmap handling. Part of the Linux-NTFS project.
1N/A *
1N/A * Copyright (c) 2000-2004 Anton Altaparmakov
1N/A * Copyright (c) 2004-2005 Richard Russon
1N/A *
1N/A * This program/include file is free software; you can redistribute it and/or
1N/A * modify it under the terms of the GNU General Public License as published
1N/A * by the Free Software Foundation; either version 2 of the License, or
1N/A * (at your option) any later version.
1N/A *
1N/A * This program/include file is distributed in the hope that it will be
1N/A * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
1N/A * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A * GNU General Public License for more details.
1N/A *
1N/A * You should have received a copy of the GNU General Public License
1N/A * along with this program (in the main directory of the Linux-NTFS
1N/A * distribution in the file COPYING); if not, write to the Free Software
1N/A * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1N/A */
1N/A
1N/A#ifndef _NTFS_BITMAP_H
1N/A#define _NTFS_BITMAP_H
1N/A
1N/A#include "types.h"
1N/A#include "attrib.h"
1N/A
1N/A/*
1N/A * NOTES:
1N/A *
1N/A * - Operations are 8-bit only to ensure the functions work both on little
1N/A * and big endian machines! So don't make them 32-bit ops!
1N/A * - bitmap starts at bit = 0 and ends at bit = bitmap size - 1.
1N/A * - _Caller_ has to make sure that the bit to operate on is less than the
1N/A * size of the bitmap.
1N/A */
1N/A
1N/A/**
1N/A * ntfs_bit_set - set a bit in a field of bits
1N/A * @bitmap: field of bits
1N/A * @bit: bit to set
1N/A * @new_value: value to set bit to (0 or 1)
1N/A *
1N/A * Set the bit @bit in the @bitmap to @new_value. Ignore all errors.
1N/A */
1N/Astatic __inline__ void ntfs_bit_set(u8 *bitmap, const u64 bit,
1N/A const u8 new_value)
1N/A{
1N/A if (!bitmap || new_value > 1)
1N/A return;
1N/A if (!new_value)
1N/A bitmap[bit >> 3] &= ~(1 << (bit & 7));
1N/A else
1N/A bitmap[bit >> 3] |= (1 << (bit & 7));
1N/A}
1N/A
1N/A/**
1N/A * ntfs_bit_get - get value of a bit in a field of bits
1N/A * @bitmap: field of bits
1N/A * @bit: bit to get
1N/A *
1N/A * Get and return the value of the bit @bit in @bitmap (0 or 1).
1N/A * Return -1 on error.
1N/A */
1N/Astatic __inline__ char ntfs_bit_get(const u8 *bitmap, const u64 bit)
1N/A{
1N/A if (!bitmap)
1N/A return -1;
1N/A return (bitmap[bit >> 3] >> (bit & 7)) & 1;
1N/A}
1N/A
1N/Astatic __inline__ void ntfs_bit_change(u8 *bitmap, const u64 bit)
1N/A{
1N/A if (!bitmap)
1N/A return;
1N/A bitmap[bit >> 3] ^= 1 << (bit & 7);
1N/A}
1N/A
1N/A/**
1N/A * ntfs_bit_get_and_set - get value of a bit in a field of bits and set it
1N/A * @bitmap: field of bits
1N/A * @bit: bit to get/set
1N/A * @new_value: value to set bit to (0 or 1)
1N/A *
1N/A * Return the value of the bit @bit and set it to @new_value (0 or 1).
1N/A * Return -1 on error.
1N/A */
1N/Astatic __inline__ char ntfs_bit_get_and_set(u8 *bitmap, const u64 bit,
1N/A const u8 new_value)
1N/A{
1N/A register u8 old_bit, shift;
1N/A
1N/A if (!bitmap || new_value > 1)
1N/A return -1;
1N/A shift = bit & 7;
1N/A old_bit = (bitmap[bit >> 3] >> shift) & 1;
1N/A if (new_value != old_bit)
1N/A bitmap[bit >> 3] ^= 1 << shift;
1N/A return old_bit;
1N/A}
1N/A
1N/Aextern int ntfs_bitmap_set_run(ntfs_attr *na, s64 start_bit, s64 count);
1N/Aextern int ntfs_bitmap_clear_run(ntfs_attr *na, s64 start_bit, s64 count);
1N/A
1N/A/**
1N/A * ntfs_bitmap_set_bit - set a bit in a bitmap
1N/A * @na: attribute containing the bitmap
1N/A * @bit: bit to set
1N/A *
1N/A * Set the @bit in the bitmap described by the attribute @na.
1N/A *
1N/A * On success return 0 and on error return -1 with errno set to the error code.
1N/A */
1N/Astatic __inline__ int ntfs_bitmap_set_bit(ntfs_attr *na, s64 bit)
1N/A{
1N/A return ntfs_bitmap_set_run(na, bit, 1);
1N/A}
1N/A
1N/A/**
1N/A * ntfs_bitmap_clear_bit - clear a bit in a bitmap
1N/A * @na: attribute containing the bitmap
1N/A * @bit: bit to clear
1N/A *
1N/A * Clear @bit in the bitmap described by the attribute @na.
1N/A *
1N/A * On success return 0 and on error return -1 with errno set to the error code.
1N/A */
1N/Astatic __inline__ int ntfs_bitmap_clear_bit(ntfs_attr *na, s64 bit)
1N/A{
1N/A return ntfs_bitmap_clear_run(na, bit, 1);
1N/A}
1N/A
1N/A#endif /* defined _NTFS_BITMAP_H */