2009-12-08 01:02:36 +00:00
|
|
|
/*
|
2005-09-08 00:09:32 +00:00
|
|
|
* PROJECT: ReactOS system libraries
|
2012-06-24 11:32:11 +00:00
|
|
|
* LICENSE: GNU GPL - See COPYING in the top level directory
|
|
|
|
* BSD - See COPYING.ARM in the top level directory
|
2004-08-10 12:00:09 +00:00
|
|
|
* FILE: lib/rtl/bitmap.c
|
|
|
|
* PURPOSE: Bitmap functions
|
2009-12-08 01:02:36 +00:00
|
|
|
* PROGRAMMER: Timo Kreuzer (timo.kreuzer@reactos.org)
|
2004-08-10 12:00:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
|
2005-07-26 08:39:07 +00:00
|
|
|
#include <rtl.h>
|
2004-08-10 12:00:09 +00:00
|
|
|
|
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
2012-06-24 11:32:11 +00:00
|
|
|
// FIXME: hack
|
|
|
|
#undef ASSERT
|
|
|
|
#define ASSERT(...)
|
2006-06-28 20:51:51 +00:00
|
|
|
|
2013-04-14 18:49:20 +00:00
|
|
|
#ifdef USE_RTL_BITMAP64
|
|
|
|
#define _BITCOUNT 64
|
2013-05-05 09:02:36 +00:00
|
|
|
#define MAXINDEX 0xFFFFFFFFFFFFFFFF
|
2013-04-14 18:49:20 +00:00
|
|
|
typedef ULONG64 BITMAP_INDEX, *PBITMAP_INDEX;
|
|
|
|
typedef ULONG64 BITMAP_BUFFER, *PBITMAP_BUFFER;
|
|
|
|
#define RTL_BITMAP RTL_BITMAP64
|
|
|
|
#define PRTL_BITMAP PRTL_BITMAP64
|
|
|
|
#define RTL_BITMAP_RUN RTL_BITMAP_RUN64
|
|
|
|
#define PRTL_BITMAP_RUN PRTL_BITMAP_RUN64
|
|
|
|
#undef BitScanForward
|
2013-05-27 20:20:33 +00:00
|
|
|
#define BitScanForward(Index, Mask) \
|
|
|
|
do { unsigned long tmp; BitScanForward64(&tmp, Mask); *Index = tmp; } while (0)
|
2014-05-25 10:36:47 +00:00
|
|
|
#undef BitScanReverse
|
|
|
|
#define BitScanReverse(Index, Mask) \
|
|
|
|
do { unsigned long tmp; BitScanReverse64(&tmp, Mask); *Index = tmp; } while (0)
|
2013-04-14 18:49:20 +00:00
|
|
|
#define RtlFillMemoryUlong RtlFillMemoryUlonglong
|
|
|
|
|
|
|
|
#define RtlInitializeBitMap RtlInitializeBitMap64
|
|
|
|
#define RtlClearAllBits RtlClearAllBits64
|
|
|
|
#define RtlSetAllBits RtlSetAllBits64
|
|
|
|
#define RtlClearBit RtlClearBit64
|
|
|
|
#define RtlSetBit RtlSetBit64
|
|
|
|
#define RtlClearBits RtlClearBits64
|
|
|
|
#define RtlSetBits RtlSetBits64
|
|
|
|
#define RtlTestBit RtlTestBit64
|
|
|
|
#define RtlAreBitsClear RtlAreBitsClear64
|
|
|
|
#define RtlAreBitsSet RtlAreBitsSet64
|
|
|
|
#define RtlNumberOfSetBits RtlNumberOfSetBits64
|
|
|
|
#define RtlNumberOfClearBits RtlNumberOfClearBits64
|
|
|
|
#define RtlFindClearBits RtlFindClearBits64
|
|
|
|
#define RtlFindSetBits RtlFindSetBits64
|
|
|
|
#define RtlFindClearBitsAndSet RtlFindClearBitsAndSet64
|
|
|
|
#define RtlFindSetBitsAndClear RtlFindSetBitsAndClear64
|
|
|
|
#define RtlFindNextForwardRunClear RtlFindNextForwardRunClear64
|
|
|
|
#define RtlFindNextForwardRunSet RtlFindNextForwardRunSet64
|
|
|
|
#define RtlFindFirstRunClear RtlFindFirstRunClear64
|
|
|
|
#define RtlFindLastBackwardRunClear RtlFindLastBackwardRunClear64
|
|
|
|
#define RtlFindClearRuns RtlFindClearRuns64
|
|
|
|
#define RtlFindLongestRunClear RtlFindLongestRunClear64
|
|
|
|
#define RtlFindLongestRunSet RtlFindLongestRunSet64
|
|
|
|
#else
|
|
|
|
#define _BITCOUNT 32
|
2013-05-05 09:02:36 +00:00
|
|
|
#define MAXINDEX 0xFFFFFFFF
|
2013-04-14 18:49:20 +00:00
|
|
|
typedef ULONG BITMAP_INDEX, *PBITMAP_INDEX;
|
|
|
|
typedef ULONG BITMAP_BUFFER, *PBITMAP_BUFFER;
|
|
|
|
#endif
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* DATA *********************************************************************/
|
2006-06-28 20:51:51 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Number of set bits per byte value */
|
|
|
|
static const
|
|
|
|
UCHAR
|
|
|
|
BitCountTable[256] =
|
|
|
|
{
|
|
|
|
/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
|
|
|
|
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, /* 0x */
|
|
|
|
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, /* 1x */
|
|
|
|
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, /* 2x */
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* 3x */
|
|
|
|
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, /* 4x */
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* 5x */
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* 6c */
|
|
|
|
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, /* 7x */
|
|
|
|
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, /* 8x */
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* 9x */
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* Ax */
|
|
|
|
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, /* Bx */
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* Cx */
|
|
|
|
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, /* Dx */
|
|
|
|
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, /* Ex */
|
|
|
|
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 /* Fx */
|
2006-06-28 20:51:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* PRIVATE FUNCTIONS ********************************************************/
|
2006-06-28 20:51:51 +00:00
|
|
|
|
2010-08-01 16:34:22 +00:00
|
|
|
static __inline
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX
|
2009-12-08 01:02:36 +00:00
|
|
|
RtlpGetLengthOfRunClear(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader,
|
|
|
|
_In_ BITMAP_INDEX StartingIndex,
|
|
|
|
_In_ BITMAP_INDEX MaxLength)
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX Value, BitPos, Length;
|
|
|
|
PBITMAP_BUFFER Buffer, MaxBuffer;
|
2006-06-28 20:51:51 +00:00
|
|
|
|
2014-04-08 21:04:47 +00:00
|
|
|
/* If we are already at the end, the length of the run is zero */
|
|
|
|
ASSERT(StartingIndex <= BitMapHeader->SizeOfBitMap);
|
|
|
|
if (StartingIndex >= BitMapHeader->SizeOfBitMap)
|
|
|
|
return 0;
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Calculate positions */
|
2013-04-14 18:49:20 +00:00
|
|
|
Buffer = BitMapHeader->Buffer + StartingIndex / _BITCOUNT;
|
|
|
|
BitPos = StartingIndex & (_BITCOUNT - 1);
|
2006-06-28 20:51:51 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Calculate the maximum length */
|
|
|
|
MaxLength = min(MaxLength, BitMapHeader->SizeOfBitMap - StartingIndex);
|
2013-04-14 18:49:20 +00:00
|
|
|
MaxBuffer = Buffer + (BitPos + MaxLength + _BITCOUNT - 1) / _BITCOUNT;
|
2006-06-28 20:51:51 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Clear the bits that don't belong to this run */
|
|
|
|
Value = *Buffer++ >> BitPos << BitPos;
|
2006-06-28 20:51:51 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Skip all clear ULONGs */
|
|
|
|
while (Value == 0 && Buffer < MaxBuffer)
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2009-12-08 01:02:36 +00:00
|
|
|
Value = *Buffer++;
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Did we reach the end? */
|
|
|
|
if (Value == 0)
|
|
|
|
{
|
|
|
|
/* Return maximum length */
|
|
|
|
return MaxLength;
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
2009-12-08 01:02:36 +00:00
|
|
|
|
|
|
|
/* We hit a set bit, check how many clear bits are left */
|
|
|
|
BitScanForward(&BitPos, Value);
|
|
|
|
|
|
|
|
/* Calculate length up to where we read */
|
2013-04-14 18:49:20 +00:00
|
|
|
Length = (BITMAP_INDEX)(Buffer - BitMapHeader->Buffer) * _BITCOUNT - StartingIndex;
|
|
|
|
Length += BitPos - _BITCOUNT;
|
2009-12-08 02:18:42 +00:00
|
|
|
|
|
|
|
/* Make sure we don't go past the last bit */
|
2009-12-08 01:02:36 +00:00
|
|
|
if (Length > BitMapHeader->SizeOfBitMap - StartingIndex)
|
|
|
|
Length = BitMapHeader->SizeOfBitMap - StartingIndex;
|
|
|
|
|
2009-12-08 02:18:42 +00:00
|
|
|
/* Return the result */
|
2009-12-08 01:02:36 +00:00
|
|
|
return Length;
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 16:34:22 +00:00
|
|
|
static __inline
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX
|
2009-12-08 01:02:36 +00:00
|
|
|
RtlpGetLengthOfRunSet(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader,
|
|
|
|
_In_ BITMAP_INDEX StartingIndex,
|
|
|
|
_In_ BITMAP_INDEX MaxLength)
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX InvValue, BitPos, Length;
|
|
|
|
PBITMAP_BUFFER Buffer, MaxBuffer;
|
2006-06-28 20:51:51 +00:00
|
|
|
|
2014-04-08 21:04:47 +00:00
|
|
|
/* If we are already at the end, the length of the run is zero */
|
|
|
|
ASSERT(StartingIndex <= BitMapHeader->SizeOfBitMap);
|
|
|
|
if (StartingIndex >= BitMapHeader->SizeOfBitMap)
|
|
|
|
return 0;
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Calculate positions */
|
2013-04-14 18:49:20 +00:00
|
|
|
Buffer = BitMapHeader->Buffer + StartingIndex / _BITCOUNT;
|
|
|
|
BitPos = StartingIndex & (_BITCOUNT - 1);
|
2006-06-28 20:51:51 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Calculate the maximum length */
|
|
|
|
MaxLength = min(MaxLength, BitMapHeader->SizeOfBitMap - StartingIndex);
|
2013-04-14 18:49:20 +00:00
|
|
|
MaxBuffer = Buffer + (BitPos + MaxLength + _BITCOUNT - 1) / _BITCOUNT;
|
2006-06-28 20:51:51 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Get the inversed value, clear bits that don't belong to the run */
|
|
|
|
InvValue = ~(*Buffer++) >> BitPos << BitPos;
|
|
|
|
|
|
|
|
/* Skip all set ULONGs */
|
|
|
|
while (InvValue == 0 && Buffer < MaxBuffer)
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2009-12-08 01:02:36 +00:00
|
|
|
InvValue = ~(*Buffer++);
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
2009-12-08 01:02:36 +00:00
|
|
|
|
|
|
|
/* Did we reach the end? */
|
|
|
|
if (InvValue == 0)
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Yes, return maximum */
|
|
|
|
return MaxLength;
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* We hit a clear bit, check how many set bits are left */
|
|
|
|
BitScanForward(&BitPos, InvValue);
|
2006-06-28 20:51:51 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Calculate length up to where we read */
|
2013-04-14 18:49:20 +00:00
|
|
|
Length = (ULONG)(Buffer - BitMapHeader->Buffer) * _BITCOUNT - StartingIndex;
|
|
|
|
Length += BitPos - _BITCOUNT;
|
2009-12-08 01:02:36 +00:00
|
|
|
|
2009-12-08 02:18:42 +00:00
|
|
|
/* Make sure we don't go past the last bit */
|
2009-12-08 01:02:36 +00:00
|
|
|
if (Length > BitMapHeader->SizeOfBitMap - StartingIndex)
|
|
|
|
Length = BitMapHeader->SizeOfBitMap - StartingIndex;
|
|
|
|
|
2009-12-08 02:18:42 +00:00
|
|
|
/* Return the result */
|
2009-12-08 01:02:36 +00:00
|
|
|
return Length;
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* PUBLIC FUNCTIONS **********************************************************/
|
2006-06-28 20:51:51 +00:00
|
|
|
|
2013-04-14 18:49:20 +00:00
|
|
|
#ifndef USE_RTL_BITMAP64
|
2009-12-08 01:02:36 +00:00
|
|
|
CCHAR
|
|
|
|
NTAPI
|
|
|
|
RtlFindMostSignificantBit(ULONGLONG Value)
|
|
|
|
{
|
|
|
|
ULONG Position;
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2012-06-24 11:32:11 +00:00
|
|
|
#ifdef _M_AMD64
|
|
|
|
if (BitScanReverse64(&Position, Value))
|
|
|
|
{
|
|
|
|
return (CCHAR)Position;
|
|
|
|
}
|
|
|
|
#else
|
2013-04-14 18:49:20 +00:00
|
|
|
if (BitScanReverse(&Position, Value >> _BITCOUNT))
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2013-04-14 18:49:20 +00:00
|
|
|
return (CCHAR)(Position + _BITCOUNT);
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
2009-12-08 01:02:36 +00:00
|
|
|
else if (BitScanReverse(&Position, (ULONG)Value))
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2011-07-04 16:30:45 +00:00
|
|
|
return (CCHAR)Position;
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
2012-06-24 11:32:11 +00:00
|
|
|
#endif
|
2009-12-08 01:02:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCHAR
|
|
|
|
NTAPI
|
|
|
|
RtlFindLeastSignificantBit(ULONGLONG Value)
|
|
|
|
{
|
|
|
|
ULONG Position;
|
2006-06-28 20:51:51 +00:00
|
|
|
|
2012-06-24 11:32:11 +00:00
|
|
|
#ifdef _M_AMD64
|
|
|
|
if (BitScanForward64(&Position, Value))
|
|
|
|
{
|
|
|
|
return (CCHAR)Position;
|
|
|
|
}
|
|
|
|
#else
|
2009-12-08 01:02:36 +00:00
|
|
|
if (BitScanForward(&Position, (ULONG)Value))
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2011-07-04 16:30:45 +00:00
|
|
|
return (CCHAR)Position;
|
2009-12-08 01:02:36 +00:00
|
|
|
}
|
2013-04-14 18:49:20 +00:00
|
|
|
else if (BitScanForward(&Position, Value >> _BITCOUNT))
|
2009-12-08 01:02:36 +00:00
|
|
|
{
|
2013-04-14 18:49:20 +00:00
|
|
|
return (CCHAR)(Position + _BITCOUNT);
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
2012-06-24 11:32:11 +00:00
|
|
|
#endif
|
2009-12-08 01:02:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2013-04-14 18:49:20 +00:00
|
|
|
#endif /* !USE_RTL_BITMAP64 */
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2006-06-28 20:51:51 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
2009-12-08 01:02:36 +00:00
|
|
|
RtlInitializeBitMap(
|
2013-04-14 18:49:20 +00:00
|
|
|
_Out_ PRTL_BITMAP BitMapHeader,
|
2013-05-27 20:20:33 +00:00
|
|
|
_In_opt_ __drv_aliasesMem PBITMAP_BUFFER BitMapBuffer,
|
|
|
|
_In_opt_ ULONG SizeOfBitMap)
|
2004-08-10 12:00:09 +00:00
|
|
|
{
|
2006-06-28 20:51:51 +00:00
|
|
|
/* Setup the bitmap header */
|
|
|
|
BitMapHeader->SizeOfBitMap = SizeOfBitMap;
|
|
|
|
BitMapHeader->Buffer = BitMapBuffer;
|
2004-08-10 12:00:09 +00:00
|
|
|
}
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
VOID
|
2006-06-28 20:51:51 +00:00
|
|
|
NTAPI
|
2009-12-08 01:02:36 +00:00
|
|
|
RtlClearAllBits(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader)
|
2004-08-10 12:00:09 +00:00
|
|
|
{
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX LengthInUlongs;
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2013-04-14 18:49:20 +00:00
|
|
|
LengthInUlongs = (BitMapHeader->SizeOfBitMap + _BITCOUNT - 1) / _BITCOUNT;
|
|
|
|
RtlFillMemoryUlong(BitMapHeader->Buffer, LengthInUlongs * sizeof(BITMAP_INDEX), 0);
|
2009-12-08 01:02:36 +00:00
|
|
|
}
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
RtlSetAllBits(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader)
|
2004-08-10 12:00:09 +00:00
|
|
|
{
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX LengthInUlongs;
|
2011-07-04 16:30:45 +00:00
|
|
|
|
2013-04-14 18:49:20 +00:00
|
|
|
LengthInUlongs = (BitMapHeader->SizeOfBitMap + _BITCOUNT - 1) / _BITCOUNT;
|
|
|
|
RtlFillMemoryUlong(BitMapHeader->Buffer, LengthInUlongs * sizeof(BITMAP_INDEX), ~0);
|
2004-08-10 12:00:09 +00:00
|
|
|
}
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
RtlClearBit(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader,
|
|
|
|
_In_ BITMAP_INDEX BitNumber)
|
2004-08-10 12:00:09 +00:00
|
|
|
{
|
2009-12-08 01:02:36 +00:00
|
|
|
ASSERT(BitNumber <= BitMapHeader->SizeOfBitMap);
|
2013-04-14 18:49:20 +00:00
|
|
|
BitMapHeader->Buffer[BitNumber / _BITCOUNT] &= ~(1 << (BitNumber & (_BITCOUNT - 1)));
|
2004-08-10 12:00:09 +00:00
|
|
|
}
|
|
|
|
|
2006-06-28 20:51:51 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
2009-12-08 01:02:36 +00:00
|
|
|
RtlSetBit(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader,
|
2013-05-27 20:20:33 +00:00
|
|
|
_In_range_(<, BitMapHeader->SizeOfBitMap) BITMAP_INDEX BitNumber)
|
2004-08-10 12:00:09 +00:00
|
|
|
{
|
2009-12-08 01:02:36 +00:00
|
|
|
ASSERT(BitNumber <= BitMapHeader->SizeOfBitMap);
|
2013-04-14 18:49:20 +00:00
|
|
|
BitMapHeader->Buffer[BitNumber / _BITCOUNT] |= ((BITMAP_INDEX)1 << (BitNumber & (_BITCOUNT - 1)));
|
2004-08-10 12:00:09 +00:00
|
|
|
}
|
|
|
|
|
2006-06-28 20:51:51 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
2009-12-08 01:02:36 +00:00
|
|
|
RtlClearBits(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader,
|
2013-05-27 20:20:33 +00:00
|
|
|
_In_range_(0, BitMapHeader->SizeOfBitMap - NumberToClear) BITMAP_INDEX StartingIndex,
|
|
|
|
_In_range_(0, BitMapHeader->SizeOfBitMap - StartingIndex) BITMAP_INDEX NumberToClear)
|
2004-08-10 12:00:09 +00:00
|
|
|
{
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX Bits, Mask;
|
|
|
|
PBITMAP_BUFFER Buffer;
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
ASSERT(StartingIndex + NumberToClear <= BitMapHeader->SizeOfBitMap);
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Calculate buffer start and first bit index */
|
2013-04-14 18:49:20 +00:00
|
|
|
Buffer = &BitMapHeader->Buffer[StartingIndex / _BITCOUNT];
|
|
|
|
Bits = StartingIndex & (_BITCOUNT - 1);
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Are we unaligned? */
|
|
|
|
if (Bits)
|
|
|
|
{
|
2013-05-05 09:02:36 +00:00
|
|
|
/* Create an inverse mask by shifting MAXINDEX */
|
|
|
|
Mask = MAXINDEX << Bits;
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* This is what's left in the first ULONG */
|
2013-04-14 18:49:20 +00:00
|
|
|
Bits = _BITCOUNT - Bits;
|
2006-06-28 20:51:51 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Even less bits to clear? */
|
|
|
|
if (NumberToClear < Bits)
|
|
|
|
{
|
|
|
|
/* Calculate how many bits are left */
|
|
|
|
Bits -= NumberToClear;
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Fixup the mask on the high side */
|
|
|
|
Mask = Mask << Bits >> Bits;
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Clear bits and return */
|
|
|
|
*Buffer &= ~Mask;
|
|
|
|
return;
|
|
|
|
}
|
2004-09-25 22:59:17 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Clear bits */
|
|
|
|
*Buffer &= ~Mask;
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Update buffer and left bits */
|
|
|
|
Buffer++;
|
|
|
|
NumberToClear -= Bits;
|
2004-08-10 12:00:09 +00:00
|
|
|
}
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Clear all full ULONGs */
|
|
|
|
RtlFillMemoryUlong(Buffer, NumberToClear >> 3, 0);
|
2013-04-14 18:49:20 +00:00
|
|
|
Buffer += NumberToClear / _BITCOUNT;
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Clear what's left */
|
2013-04-14 18:49:20 +00:00
|
|
|
NumberToClear &= (_BITCOUNT - 1);
|
2014-10-22 19:40:13 +00:00
|
|
|
if (NumberToClear != 0)
|
2014-10-21 14:22:28 +00:00
|
|
|
{
|
|
|
|
Mask = MAXINDEX << NumberToClear;
|
|
|
|
*Buffer &= Mask;
|
|
|
|
}
|
2004-08-10 12:00:09 +00:00
|
|
|
}
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
RtlSetBits(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader,
|
2013-05-27 20:20:33 +00:00
|
|
|
_In_range_(0, BitMapHeader->SizeOfBitMap - NumberToSet) BITMAP_INDEX StartingIndex,
|
|
|
|
_In_range_(0, BitMapHeader->SizeOfBitMap - StartingIndex) BITMAP_INDEX NumberToSet)
|
2004-08-10 12:00:09 +00:00
|
|
|
{
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX Bits, Mask;
|
|
|
|
PBITMAP_BUFFER Buffer;
|
2006-06-28 20:51:51 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
ASSERT(StartingIndex + NumberToSet <= BitMapHeader->SizeOfBitMap);
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Calculate buffer start and first bit index */
|
2013-04-14 18:49:20 +00:00
|
|
|
Buffer = &BitMapHeader->Buffer[StartingIndex / _BITCOUNT];
|
|
|
|
Bits = StartingIndex & (_BITCOUNT - 1);
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Are we unaligned? */
|
|
|
|
if (Bits)
|
|
|
|
{
|
2013-05-05 09:02:36 +00:00
|
|
|
/* Create a mask by shifting MAXINDEX */
|
|
|
|
Mask = MAXINDEX << Bits;
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* This is what's left in the first ULONG */
|
2013-04-14 18:49:20 +00:00
|
|
|
Bits = _BITCOUNT - Bits;
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Even less bits to clear? */
|
|
|
|
if (NumberToSet < Bits)
|
|
|
|
{
|
|
|
|
/* Calculate how many bits are left */
|
|
|
|
Bits -= NumberToSet;
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Fixup the mask on the high side */
|
|
|
|
Mask = Mask << Bits >> Bits;
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Set bits and return */
|
|
|
|
*Buffer |= Mask;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set bits */
|
|
|
|
*Buffer |= Mask;
|
|
|
|
|
|
|
|
/* Update buffer and left bits */
|
|
|
|
Buffer++;
|
|
|
|
NumberToSet -= Bits;
|
2004-08-10 12:00:09 +00:00
|
|
|
}
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Set all full ULONGs */
|
2013-05-05 09:02:36 +00:00
|
|
|
RtlFillMemoryUlong(Buffer, NumberToSet >> 3, MAXINDEX);
|
2013-04-14 18:49:20 +00:00
|
|
|
Buffer += NumberToSet / _BITCOUNT;
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Set what's left */
|
2013-04-14 18:49:20 +00:00
|
|
|
NumberToSet &= (_BITCOUNT - 1);
|
2014-10-22 19:40:13 +00:00
|
|
|
if (NumberToSet != 0)
|
2014-10-21 14:22:28 +00:00
|
|
|
{
|
|
|
|
Mask = MAXINDEX << NumberToSet;
|
|
|
|
*Buffer |= ~Mask;
|
|
|
|
}
|
2009-12-08 01:02:36 +00:00
|
|
|
}
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
RtlTestBit(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader,
|
2013-05-27 20:20:33 +00:00
|
|
|
_In_range_(<, BitMapHeader->SizeOfBitMap) BITMAP_INDEX BitNumber)
|
2004-08-10 12:00:09 +00:00
|
|
|
{
|
2009-12-08 01:02:36 +00:00
|
|
|
ASSERT(BitNumber < BitMapHeader->SizeOfBitMap);
|
2013-04-14 18:49:20 +00:00
|
|
|
return (BitMapHeader->Buffer[BitNumber / _BITCOUNT] >> (BitNumber & (_BITCOUNT - 1))) & 1;
|
2004-08-10 12:00:09 +00:00
|
|
|
}
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
RtlAreBitsClear(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader,
|
|
|
|
_In_ BITMAP_INDEX StartingIndex,
|
|
|
|
_In_ BITMAP_INDEX Length)
|
2004-08-10 12:00:09 +00:00
|
|
|
{
|
2012-02-13 14:35:26 +00:00
|
|
|
/* Verify parameters */
|
|
|
|
if ((StartingIndex + Length > BitMapHeader->SizeOfBitMap) ||
|
|
|
|
(StartingIndex + Length <= StartingIndex))
|
|
|
|
return FALSE;
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
return RtlpGetLengthOfRunClear(BitMapHeader, StartingIndex, Length) >= Length;
|
2004-08-10 12:00:09 +00:00
|
|
|
}
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
RtlAreBitsSet(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader,
|
|
|
|
_In_ BITMAP_INDEX StartingIndex,
|
|
|
|
_In_ BITMAP_INDEX Length)
|
2004-08-10 12:00:09 +00:00
|
|
|
{
|
2012-02-13 14:35:26 +00:00
|
|
|
/* Verify parameters */
|
|
|
|
if ((StartingIndex + Length > BitMapHeader->SizeOfBitMap) ||
|
|
|
|
(StartingIndex + Length <= StartingIndex))
|
|
|
|
return FALSE;
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
return RtlpGetLengthOfRunSet(BitMapHeader, StartingIndex, Length) >= Length;
|
|
|
|
}
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX
|
2009-12-08 01:02:36 +00:00
|
|
|
NTAPI
|
|
|
|
RtlNumberOfSetBits(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader)
|
2009-12-08 01:02:36 +00:00
|
|
|
{
|
|
|
|
PUCHAR Byte, MaxByte;
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX BitCount = 0;
|
|
|
|
ULONG Shift;
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
Byte = (PUCHAR)BitMapHeader->Buffer;
|
2012-06-24 12:51:27 +00:00
|
|
|
MaxByte = Byte + BitMapHeader->SizeOfBitMap / 8;
|
2004-09-25 20:49:57 +00:00
|
|
|
|
2012-06-24 17:50:46 +00:00
|
|
|
while (Byte < MaxByte)
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2009-12-08 01:02:36 +00:00
|
|
|
BitCount += BitCountTable[*Byte++];
|
2004-08-10 12:00:09 +00:00
|
|
|
}
|
2012-06-24 12:51:27 +00:00
|
|
|
|
2014-03-31 19:57:42 +00:00
|
|
|
if (BitMapHeader->SizeOfBitMap & 7)
|
|
|
|
{
|
|
|
|
Shift = 8 - (BitMapHeader->SizeOfBitMap & 7);
|
|
|
|
BitCount += BitCountTable[((*Byte) << Shift) & 0xFF];
|
|
|
|
}
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
return BitCount;
|
|
|
|
}
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX
|
2009-12-08 01:02:36 +00:00
|
|
|
NTAPI
|
|
|
|
RtlNumberOfClearBits(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader)
|
2004-08-10 12:00:09 +00:00
|
|
|
{
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Do some math */
|
|
|
|
return BitMapHeader->SizeOfBitMap - RtlNumberOfSetBits(BitMapHeader);
|
2004-08-10 12:00:09 +00:00
|
|
|
}
|
|
|
|
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX
|
2009-12-08 01:02:36 +00:00
|
|
|
NTAPI
|
|
|
|
RtlFindClearBits(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader,
|
|
|
|
_In_ BITMAP_INDEX NumberToFind,
|
|
|
|
_In_ BITMAP_INDEX HintIndex)
|
2004-08-10 12:00:09 +00:00
|
|
|
{
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX CurrentBit, Margin, CurrentLength;
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Check for valid parameters */
|
|
|
|
if (!BitMapHeader || NumberToFind > BitMapHeader->SizeOfBitMap)
|
|
|
|
{
|
2013-05-05 09:02:36 +00:00
|
|
|
return MAXINDEX;
|
2009-12-08 01:02:36 +00:00
|
|
|
}
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2012-06-24 18:33:08 +00:00
|
|
|
/* Check if the hint is outside the bitmap */
|
|
|
|
if (HintIndex >= BitMapHeader->SizeOfBitMap) HintIndex = 0;
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Check for trivial case */
|
|
|
|
if (NumberToFind == 0)
|
2004-08-10 12:00:09 +00:00
|
|
|
{
|
2012-06-24 18:33:08 +00:00
|
|
|
/* Return hint rounded down to byte margin */
|
|
|
|
return HintIndex & ~7;
|
2004-08-10 12:00:09 +00:00
|
|
|
}
|
|
|
|
|
2009-12-08 03:06:40 +00:00
|
|
|
/* First margin is end of bitmap */
|
|
|
|
Margin = BitMapHeader->SizeOfBitMap;
|
|
|
|
|
|
|
|
retry:
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Start with hint index, length is 0 */
|
|
|
|
CurrentBit = HintIndex;
|
|
|
|
|
|
|
|
/* Loop until something is found or the end is reached */
|
2009-12-08 03:06:40 +00:00
|
|
|
while (CurrentBit + NumberToFind < Margin)
|
2008-12-24 10:12:01 +00:00
|
|
|
{
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Search for the next clear run, by skipping a set run */
|
2009-12-08 02:18:42 +00:00
|
|
|
CurrentBit += RtlpGetLengthOfRunSet(BitMapHeader,
|
2009-12-08 01:02:36 +00:00
|
|
|
CurrentBit,
|
2013-05-05 09:02:36 +00:00
|
|
|
MAXINDEX);
|
2009-12-08 01:02:36 +00:00
|
|
|
|
|
|
|
/* Get length of the clear bit run */
|
|
|
|
CurrentLength = RtlpGetLengthOfRunClear(BitMapHeader,
|
|
|
|
CurrentBit,
|
|
|
|
NumberToFind);
|
|
|
|
|
|
|
|
/* Is this long enough? */
|
|
|
|
if (CurrentLength >= NumberToFind)
|
|
|
|
{
|
|
|
|
/* It is */
|
|
|
|
return CurrentBit;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentBit += CurrentLength;
|
2008-12-24 10:12:01 +00:00
|
|
|
}
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 03:06:40 +00:00
|
|
|
/* Did we start at a hint? */
|
|
|
|
if (HintIndex)
|
|
|
|
{
|
|
|
|
/* Retry at the start */
|
|
|
|
Margin = min(HintIndex + NumberToFind, BitMapHeader->SizeOfBitMap);
|
|
|
|
HintIndex = 0;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Nothing found */
|
2013-05-05 09:02:36 +00:00
|
|
|
return MAXINDEX;
|
2009-12-08 01:02:36 +00:00
|
|
|
}
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX
|
2009-12-08 01:02:36 +00:00
|
|
|
NTAPI
|
|
|
|
RtlFindSetBits(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader,
|
|
|
|
_In_ BITMAP_INDEX NumberToFind,
|
|
|
|
_In_ BITMAP_INDEX HintIndex)
|
2004-08-10 12:00:09 +00:00
|
|
|
{
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX CurrentBit, Margin, CurrentLength;
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Check for valid parameters */
|
|
|
|
if (!BitMapHeader || NumberToFind > BitMapHeader->SizeOfBitMap)
|
|
|
|
{
|
2013-05-05 09:02:36 +00:00
|
|
|
return MAXINDEX;
|
2009-12-08 01:02:36 +00:00
|
|
|
}
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2012-06-24 18:33:08 +00:00
|
|
|
/* Check if the hint is outside the bitmap */
|
|
|
|
if (HintIndex >= BitMapHeader->SizeOfBitMap) HintIndex = 0;
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Check for trivial case */
|
|
|
|
if (NumberToFind == 0)
|
|
|
|
{
|
2012-06-24 18:33:08 +00:00
|
|
|
/* Return hint rounded down to byte margin */
|
|
|
|
return HintIndex & ~7;
|
2009-12-08 01:02:36 +00:00
|
|
|
}
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2011-07-04 16:30:45 +00:00
|
|
|
/* First margin is end of bitmap */
|
|
|
|
Margin = BitMapHeader->SizeOfBitMap;
|
|
|
|
|
|
|
|
retry:
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Start with hint index, length is 0 */
|
|
|
|
CurrentBit = HintIndex;
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Loop until something is found or the end is reached */
|
2011-07-04 16:30:45 +00:00
|
|
|
while (CurrentBit + NumberToFind <= Margin)
|
2009-12-08 01:02:36 +00:00
|
|
|
{
|
|
|
|
/* Search for the next set run, by skipping a clear run */
|
|
|
|
CurrentBit += RtlpGetLengthOfRunClear(BitMapHeader,
|
|
|
|
CurrentBit,
|
2013-05-05 09:02:36 +00:00
|
|
|
MAXINDEX);
|
2009-12-08 01:02:36 +00:00
|
|
|
|
|
|
|
/* Get length of the set bit run */
|
|
|
|
CurrentLength = RtlpGetLengthOfRunSet(BitMapHeader,
|
|
|
|
CurrentBit,
|
|
|
|
NumberToFind);
|
|
|
|
|
|
|
|
/* Is this long enough? */
|
|
|
|
if (CurrentLength >= NumberToFind)
|
|
|
|
{
|
|
|
|
/* It is */
|
|
|
|
return CurrentBit;
|
|
|
|
}
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
CurrentBit += CurrentLength;
|
|
|
|
}
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2011-07-04 16:30:45 +00:00
|
|
|
/* Did we start at a hint? */
|
|
|
|
if (HintIndex)
|
|
|
|
{
|
|
|
|
/* Retry at the start */
|
|
|
|
Margin = min(HintIndex + NumberToFind, BitMapHeader->SizeOfBitMap);
|
|
|
|
HintIndex = 0;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Nothing found */
|
2013-05-05 09:02:36 +00:00
|
|
|
return MAXINDEX;
|
2009-12-08 01:02:36 +00:00
|
|
|
}
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX
|
2009-12-08 01:02:36 +00:00
|
|
|
NTAPI
|
|
|
|
RtlFindClearBitsAndSet(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader,
|
|
|
|
_In_ BITMAP_INDEX NumberToFind,
|
|
|
|
_In_ BITMAP_INDEX HintIndex)
|
2004-08-10 12:00:09 +00:00
|
|
|
{
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX Position;
|
2009-12-08 01:02:36 +00:00
|
|
|
|
|
|
|
/* Try to find clear bits */
|
|
|
|
Position = RtlFindClearBits(BitMapHeader, NumberToFind, HintIndex);
|
|
|
|
|
|
|
|
/* Did we get something? */
|
2013-05-05 09:02:36 +00:00
|
|
|
if (Position != MAXINDEX)
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Yes, set the bits */
|
|
|
|
RtlSetBits(BitMapHeader, Position, NumberToFind);
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Return what we found */
|
|
|
|
return Position;
|
2004-08-10 12:00:09 +00:00
|
|
|
}
|
|
|
|
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX
|
2009-12-08 01:02:36 +00:00
|
|
|
NTAPI
|
|
|
|
RtlFindSetBitsAndClear(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader,
|
|
|
|
_In_ BITMAP_INDEX NumberToFind,
|
|
|
|
_In_ BITMAP_INDEX HintIndex)
|
2004-08-10 12:00:09 +00:00
|
|
|
{
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX Position;
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Try to find set bits */
|
|
|
|
Position = RtlFindSetBits(BitMapHeader, NumberToFind, HintIndex);
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Did we get something? */
|
2013-05-05 09:02:36 +00:00
|
|
|
if (Position != MAXINDEX)
|
2009-12-08 01:02:36 +00:00
|
|
|
{
|
|
|
|
/* Yes, clear the bits */
|
|
|
|
RtlClearBits(BitMapHeader, Position, NumberToFind);
|
|
|
|
}
|
2004-08-10 12:00:09 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Return what we found */
|
|
|
|
return Position;
|
2004-08-10 12:00:09 +00:00
|
|
|
}
|
|
|
|
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX
|
2009-12-08 01:02:36 +00:00
|
|
|
NTAPI
|
|
|
|
RtlFindNextForwardRunClear(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader,
|
|
|
|
_In_ BITMAP_INDEX FromIndex,
|
|
|
|
_Out_ PBITMAP_INDEX StartingRunIndex)
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX Length;
|
2009-12-08 01:02:36 +00:00
|
|
|
|
2012-06-24 13:39:32 +00:00
|
|
|
/* Check for buffer overrun */
|
|
|
|
if (FromIndex >= BitMapHeader->SizeOfBitMap)
|
|
|
|
{
|
|
|
|
*StartingRunIndex = FromIndex;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Assume a set run first, count it's length */
|
2013-05-05 09:02:36 +00:00
|
|
|
Length = RtlpGetLengthOfRunSet(BitMapHeader, FromIndex, MAXINDEX);
|
2009-12-08 03:06:40 +00:00
|
|
|
*StartingRunIndex = FromIndex + Length;
|
2009-12-08 01:02:36 +00:00
|
|
|
|
|
|
|
/* Now return the length of the run */
|
2013-05-05 09:02:36 +00:00
|
|
|
return RtlpGetLengthOfRunClear(BitMapHeader, FromIndex + Length, MAXINDEX);
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
|
|
|
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX
|
2009-12-08 01:02:36 +00:00
|
|
|
NTAPI
|
|
|
|
RtlFindNextForwardRunSet(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader,
|
|
|
|
_In_ BITMAP_INDEX FromIndex,
|
|
|
|
_Out_ PBITMAP_INDEX StartingRunIndex)
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX Length;
|
2006-06-28 20:51:51 +00:00
|
|
|
|
2012-06-24 13:39:32 +00:00
|
|
|
/* Check for buffer overrun */
|
|
|
|
if (FromIndex >= BitMapHeader->SizeOfBitMap)
|
|
|
|
{
|
|
|
|
*StartingRunIndex = FromIndex;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Assume a clear run first, count it's length */
|
2013-05-05 09:02:36 +00:00
|
|
|
Length = RtlpGetLengthOfRunClear(BitMapHeader, FromIndex, MAXINDEX);
|
2010-01-20 23:34:21 +00:00
|
|
|
*StartingRunIndex = FromIndex + Length;
|
2009-12-08 01:02:36 +00:00
|
|
|
|
|
|
|
/* Now return the length of the run */
|
2014-01-28 21:55:30 +00:00
|
|
|
return RtlpGetLengthOfRunSet(BitMapHeader, FromIndex + Length, MAXINDEX);
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
|
|
|
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX
|
2009-12-08 01:02:36 +00:00
|
|
|
NTAPI
|
|
|
|
RtlFindFirstRunClear(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader,
|
|
|
|
_Out_ PBITMAP_INDEX StartingIndex)
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2009-12-08 01:02:36 +00:00
|
|
|
return RtlFindNextForwardRunClear(BitMapHeader, 0, StartingIndex);
|
|
|
|
}
|
2006-06-28 20:51:51 +00:00
|
|
|
|
2014-05-25 10:36:47 +00:00
|
|
|
BITMAP_INDEX
|
2009-12-08 01:02:36 +00:00
|
|
|
NTAPI
|
|
|
|
RtlFindLastBackwardRunClear(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader,
|
|
|
|
_In_ BITMAP_INDEX FromIndex,
|
|
|
|
_Out_ PBITMAP_INDEX StartingRunIndex)
|
2009-12-08 01:02:36 +00:00
|
|
|
{
|
2014-01-28 21:55:30 +00:00
|
|
|
BITMAP_INDEX Value, InvValue, BitPos;
|
|
|
|
PBITMAP_BUFFER Buffer;
|
|
|
|
|
|
|
|
/* Make sure we don't go past the end */
|
|
|
|
FromIndex = min(FromIndex, BitMapHeader->SizeOfBitMap - 1);
|
|
|
|
|
|
|
|
/* Calculate positions */
|
|
|
|
Buffer = BitMapHeader->Buffer + FromIndex / _BITCOUNT;
|
|
|
|
BitPos = (_BITCOUNT - 1) - (FromIndex & (_BITCOUNT - 1));
|
|
|
|
|
|
|
|
/* Get the inversed value, clear bits that don't belong to the run */
|
|
|
|
InvValue = ~(*Buffer--) << BitPos >> BitPos;
|
|
|
|
|
|
|
|
/* Skip all set ULONGs */
|
|
|
|
while (InvValue == 0)
|
|
|
|
{
|
|
|
|
/* Did we already reach past the first ULONG? */
|
|
|
|
if (Buffer < BitMapHeader->Buffer)
|
|
|
|
{
|
|
|
|
/* Yes, nothing found */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
InvValue = ~(*Buffer--);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We hit a clear bit, check how many set bits are left */
|
|
|
|
BitScanReverse(&BitPos, InvValue);
|
|
|
|
|
|
|
|
/* Calculate last bit position */
|
2014-05-25 10:36:47 +00:00
|
|
|
FromIndex = (BITMAP_INDEX)((Buffer + 1 - BitMapHeader->Buffer) * _BITCOUNT + BitPos);
|
2014-01-28 21:55:30 +00:00
|
|
|
|
|
|
|
Value = ~InvValue << ((_BITCOUNT - 1) - BitPos) >> ((_BITCOUNT - 1) - BitPos);
|
|
|
|
|
|
|
|
/* Skip all clear ULONGs */
|
|
|
|
while (Value == 0 && Buffer >= BitMapHeader->Buffer)
|
|
|
|
{
|
|
|
|
Value = *Buffer--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Value != 0)
|
|
|
|
{
|
|
|
|
/* We hit a set bit, check how many clear bits are left */
|
|
|
|
BitScanReverse(&BitPos, Value);
|
|
|
|
|
|
|
|
/* Calculate Starting Index */
|
2014-05-25 10:36:47 +00:00
|
|
|
*StartingRunIndex = (BITMAP_INDEX)((Buffer + 1 - BitMapHeader->Buffer) * _BITCOUNT + BitPos + 1);
|
2014-01-28 21:55:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We reached the start of the bitmap */
|
|
|
|
*StartingRunIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return length of the run */
|
2014-05-25 10:36:47 +00:00
|
|
|
return (FromIndex - *StartingRunIndex);
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
|
|
|
|
ULONG
|
|
|
|
NTAPI
|
|
|
|
RtlFindClearRuns(
|
2013-04-14 18:49:20 +00:00
|
|
|
_In_ PRTL_BITMAP BitMapHeader,
|
|
|
|
_In_ PRTL_BITMAP_RUN RunArray,
|
|
|
|
_In_ ULONG SizeOfRunArray,
|
|
|
|
_In_ BOOLEAN LocateLongestRuns)
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX StartingIndex, NumberOfBits, FromIndex = 0, SmallestRun = 0;
|
|
|
|
ULONG Run;
|
2006-06-28 20:51:51 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Loop the runs */
|
|
|
|
for (Run = 0; Run < SizeOfRunArray; Run++)
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Look for a run */
|
2011-07-04 16:30:45 +00:00
|
|
|
NumberOfBits = RtlFindNextForwardRunClear(BitMapHeader,
|
2009-12-08 01:02:36 +00:00
|
|
|
FromIndex,
|
|
|
|
&StartingIndex);
|
|
|
|
|
|
|
|
/* Nothing more found? Quit looping. */
|
|
|
|
if (NumberOfBits == 0) break;
|
|
|
|
|
|
|
|
/* Add another run */
|
|
|
|
RunArray[Run].StartingIndex = StartingIndex;
|
|
|
|
RunArray[Run].NumberOfBits = NumberOfBits;
|
|
|
|
|
|
|
|
/* Update smallest run */
|
|
|
|
if (NumberOfBits < RunArray[SmallestRun].NumberOfBits)
|
|
|
|
{
|
|
|
|
SmallestRun = Run;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance bits */
|
2009-12-08 03:06:40 +00:00
|
|
|
FromIndex = StartingIndex + NumberOfBits;
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
2009-12-08 01:02:36 +00:00
|
|
|
|
|
|
|
/* Check if we are finished */
|
|
|
|
if (Run < SizeOfRunArray || !LocateLongestRuns)
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Return the number of found runs */
|
|
|
|
return Run;
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
2009-12-08 01:02:36 +00:00
|
|
|
|
|
|
|
while (1)
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Look for a run */
|
2011-07-04 16:30:45 +00:00
|
|
|
NumberOfBits = RtlFindNextForwardRunClear(BitMapHeader,
|
2009-12-08 01:02:36 +00:00
|
|
|
FromIndex,
|
|
|
|
&StartingIndex);
|
|
|
|
|
|
|
|
/* Nothing more found? Quit looping. */
|
|
|
|
if (NumberOfBits == 0) break;
|
|
|
|
|
|
|
|
/* Check if we have something to update */
|
|
|
|
if (NumberOfBits > RunArray[SmallestRun].NumberOfBits)
|
|
|
|
{
|
|
|
|
/* Update smallest run */
|
|
|
|
RunArray[SmallestRun].StartingIndex = StartingIndex;
|
|
|
|
RunArray[SmallestRun].NumberOfBits = NumberOfBits;
|
|
|
|
|
|
|
|
/* Loop all runs */
|
|
|
|
for (Run = 0; Run < SizeOfRunArray; Run++)
|
|
|
|
{
|
|
|
|
/*Is this the new smallest run? */
|
|
|
|
if (NumberOfBits < RunArray[SmallestRun].NumberOfBits)
|
|
|
|
{
|
|
|
|
/* Set it as new smallest run */
|
|
|
|
SmallestRun = Run;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance bits */
|
|
|
|
FromIndex += NumberOfBits;
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
2009-12-08 01:02:36 +00:00
|
|
|
|
|
|
|
return Run;
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
|
|
|
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX
|
2009-12-08 01:02:36 +00:00
|
|
|
NTAPI
|
|
|
|
RtlFindLongestRunClear(
|
|
|
|
IN PRTL_BITMAP BitMapHeader,
|
2013-04-14 18:49:20 +00:00
|
|
|
IN PBITMAP_INDEX StartingIndex)
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX NumberOfBits, Index, MaxNumberOfBits = 0, FromIndex = 0;
|
2006-06-28 20:51:51 +00:00
|
|
|
|
2009-12-08 01:02:36 +00:00
|
|
|
while (1)
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Look for a run */
|
2011-07-04 16:30:45 +00:00
|
|
|
NumberOfBits = RtlFindNextForwardRunClear(BitMapHeader,
|
2009-12-08 01:02:36 +00:00
|
|
|
FromIndex,
|
|
|
|
&Index);
|
|
|
|
|
|
|
|
/* Nothing more found? Quit looping. */
|
|
|
|
if (NumberOfBits == 0) break;
|
|
|
|
|
|
|
|
/* Was that the longest run? */
|
|
|
|
if (NumberOfBits > MaxNumberOfBits)
|
|
|
|
{
|
|
|
|
/* Update values */
|
|
|
|
MaxNumberOfBits = NumberOfBits;
|
|
|
|
*StartingIndex = Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance bits */
|
|
|
|
FromIndex += NumberOfBits;
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
2009-12-08 01:02:36 +00:00
|
|
|
|
|
|
|
return MaxNumberOfBits;
|
|
|
|
}
|
|
|
|
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX
|
2009-12-08 01:02:36 +00:00
|
|
|
NTAPI
|
|
|
|
RtlFindLongestRunSet(
|
|
|
|
IN PRTL_BITMAP BitMapHeader,
|
2013-04-14 18:49:20 +00:00
|
|
|
IN PBITMAP_INDEX StartingIndex)
|
2009-12-08 01:02:36 +00:00
|
|
|
{
|
2013-04-14 18:49:20 +00:00
|
|
|
BITMAP_INDEX NumberOfBits, Index, MaxNumberOfBits = 0, FromIndex = 0;
|
2009-12-08 01:02:36 +00:00
|
|
|
|
|
|
|
while (1)
|
2006-06-28 20:51:51 +00:00
|
|
|
{
|
2009-12-08 01:02:36 +00:00
|
|
|
/* Look for a run */
|
2011-07-04 16:30:45 +00:00
|
|
|
NumberOfBits = RtlFindNextForwardRunSet(BitMapHeader,
|
2009-12-08 01:02:36 +00:00
|
|
|
FromIndex,
|
|
|
|
&Index);
|
|
|
|
|
|
|
|
/* Nothing more found? Quit looping. */
|
|
|
|
if (NumberOfBits == 0) break;
|
|
|
|
|
|
|
|
/* Was that the longest run? */
|
|
|
|
if (NumberOfBits > MaxNumberOfBits)
|
|
|
|
{
|
|
|
|
/* Update values */
|
|
|
|
MaxNumberOfBits = NumberOfBits;
|
|
|
|
*StartingIndex = Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance bits */
|
|
|
|
FromIndex += NumberOfBits;
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
2009-12-08 01:02:36 +00:00
|
|
|
|
|
|
|
return MaxNumberOfBits;
|
2006-06-28 20:51:51 +00:00
|
|
|
}
|
|
|
|
|