Fixed "conditional expr is always true due to being unsigned < 0"
[wine/dcerpc.git] / dlls / ntdll / rtlbitmap.c
blobd04754a39dc7d2dfc3f51679cba3643041505447
1 /*
2 * NTDLL bitmap functions
4 * Copyright 2002 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * NOTES
21 * Bitmaps are an efficient type for manipulating large arrays of bits. They
22 * are commonly used by device drivers (e.g. For holding dirty/free blocks),
23 * but are also available to applications that need this functionality.
25 * Bits are set LSB to MSB in each consecutive byte, making this implementation
26 * binary compatable with Win32.
28 * Note that to avoid unexpected behaviour, the size of a bitmap should be set
29 * to a multiple of 32.
31 #include <stdlib.h>
32 #include <string.h>
33 #include "windef.h"
34 #include "winternl.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
39 /* Bits set from LSB to MSB; used as mask for runs < 8 bits */
40 static const BYTE NTDLL_maskBits[8] = { 0, 1, 3, 7, 15, 31, 63, 127 };
42 /* Number of set bits for each value of a nibble; used for counting */
43 static const BYTE NTDLL_nibbleBitCount[16] = {
44 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
47 /* First set bit in a nibble; used for determining least significant bit */
48 static const BYTE NTDLL_leastSignificant[16] = {
49 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
52 /* Last set bit in a nibble; used for determining most significant bit */
53 static const BYTE NTDLL_mostSignificant[16] = {
54 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3
57 /*************************************************************************
58 * RtlInitializeBitMap [NTDLL.@]
60 * Initialise a new bitmap.
62 * PARAMS
63 * lpBits [I] Bitmap pointer
64 * lpBuff [I] Memory for the bitmap
65 * ulSize [I] Size of lpBuff in bits
67 * RETURNS
68 * Nothing.
70 * NOTES
71 * lpBuff must be aligned on a ULONG suitable boundary, to a multiple of 32 bytes
72 * in size (irrespective of ulSize given).
74 #undef RtlInitializeBitMap
75 VOID WINAPI RtlInitializeBitMap(PRTL_BITMAP lpBits, LPBYTE lpBuff, ULONG ulSize)
77 TRACE("(%p,%p,%ld)\n", lpBits,lpBuff,ulSize);
78 lpBits->SizeOfBitMap = ulSize;
79 lpBits->BitMapBuffer = lpBuff;
82 /*************************************************************************
83 * RtlSetAllBits [NTDLL.@]
85 * Set all bits in a bitmap.
87 * PARAMS
88 * lpBits [I] Bitmap pointer
90 * RETURNS
91 * Nothing.
93 #undef RtlSetAllBits
94 VOID WINAPI RtlSetAllBits(PRTL_BITMAP lpBits)
96 TRACE("(%p)\n", lpBits);
97 memset(lpBits->BitMapBuffer, 0xff, ((lpBits->SizeOfBitMap + 31) & 0xffffffe0) >> 3);
100 /*************************************************************************
101 * RtlClearAllBits [NTDLL.@]
103 * Clear all bits in a bitmap.
105 * PARAMS
106 * lpBit [I] Bitmap pointer
108 * RETURNS
109 * Nothing.
111 #undef RtlClearAllBits
112 VOID WINAPI RtlClearAllBits(PRTL_BITMAP lpBits)
114 TRACE("(%p)\n", lpBits);
115 memset(lpBits->BitMapBuffer, 0, ((lpBits->SizeOfBitMap + 31) & 0xffffffe0) >> 3);
118 /*************************************************************************
119 * RtlSetBits [NTDLL.@]
121 * Set a range of bits in a bitmap.
123 * PARAMS
124 * lpBits [I] Bitmap pointer
125 * ulStart [I] First bit to set
126 * ulCount [I] Number of consecutive bits to set
128 * RETURNS
129 * Nothing.
131 VOID WINAPI RtlSetBits(PRTL_BITMAP lpBits, ULONG ulStart, ULONG ulCount)
133 LPBYTE lpOut;
135 TRACE("(%p,%ld,%ld)\n", lpBits, ulStart, ulCount);
137 if (!lpBits || !ulCount || ulStart + ulCount > lpBits->SizeOfBitMap)
138 return;
140 lpOut = lpBits->BitMapBuffer + (ulStart >> 3u);
142 /* Set bits in first byte, if ulStart isn't a byte boundary */
143 if (ulStart & 7)
145 if (ulCount > 7)
147 /* Set from start bit to the end of the byte */
148 *lpOut++ |= 0xff << (ulStart & 7);
149 ulCount -= (8 - (ulStart & 7));
151 else
153 /* Set from the start bit, possibly into the next byte also */
154 USHORT initialWord = NTDLL_maskBits[ulCount] << (ulStart & 7);
156 *lpOut++ |= (initialWord & 0xff);
157 *lpOut |= (initialWord >> 8);
158 return;
162 /* Set bits up to complete byte count */
163 if (ulCount >> 3)
165 memset(lpOut, 0xff, ulCount >> 3);
166 lpOut = lpOut + (ulCount >> 3);
169 /* Set remaining bits, if any */
170 *lpOut |= NTDLL_maskBits[ulCount & 0x7];
173 /*************************************************************************
174 * RtlClearBits [NTDLL.@]
176 * Clear bits in a bitmap.
178 * PARAMS
179 * lpBits [I] Bitmap pointer
180 * ulStart [I] First bit to set
181 * ulCount [I] Number of consecutive bits to clear
183 * RETURNS
184 * Nothing.
186 VOID WINAPI RtlClearBits(PRTL_BITMAP lpBits, ULONG ulStart, ULONG ulCount)
188 LPBYTE lpOut;
190 TRACE("(%p,%ld,%ld)\n", lpBits, ulStart, ulCount);
192 if (!lpBits || !ulCount || ulStart + ulCount > lpBits->SizeOfBitMap)
193 return;
195 lpOut = lpBits->BitMapBuffer + (ulStart >> 3u);
197 /* Clear bits in first byte, if ulStart isn't a byte boundary */
198 if (ulStart & 7)
200 if (ulCount > 7)
202 /* Clear from start bit to the end of the byte */
203 *lpOut++ &= ~(0xff << (ulStart & 7));
204 ulCount -= (8 - (ulStart & 7));
206 else
208 /* Clear from the start bit, possibly into the next byte also */
209 USHORT initialWord = ~(NTDLL_maskBits[ulCount] << (ulStart & 7));
211 *lpOut++ &= (initialWord & 0xff);
212 *lpOut &= (initialWord >> 8);
213 return;
217 /* Clear bits (in blocks of 8) on whole byte boundaries */
218 if (ulCount >> 3)
220 memset(lpOut, 0, ulCount >> 3);
221 lpOut = lpOut + (ulCount >> 3);
224 /* Clear remaining bits, if any */
225 if (ulCount & 0x7)
226 *lpOut &= ~NTDLL_maskBits[ulCount & 0x7];
229 /*************************************************************************
230 * RtlAreBitsSet [NTDLL.@]
232 * Determine if part of a bitmap is set.
234 * PARAMS
235 * lpBits [I] Bitmap pointer
236 * ulStart [I] First bit to check from
237 * ulCount [I] Number of consecutive bits to check
239 * RETURNS
240 * TRUE, If ulCount bits from ulStart are set.
241 * FALSE, Otherwise.
243 BOOLEAN WINAPI RtlAreBitsSet(PCRTL_BITMAP lpBits, ULONG ulStart, ULONG ulCount)
245 LPBYTE lpOut;
246 ULONG ulRemainder;
248 TRACE("(%p,%ld,%ld)\n", lpBits, ulStart, ulCount);
250 if (!lpBits || !ulCount || ulStart + ulCount > lpBits->SizeOfBitMap)
251 return FALSE;
253 lpOut = lpBits->BitMapBuffer + (ulStart >> 3u);
255 /* Check bits in first byte, if ulStart isn't a byte boundary */
256 if (ulStart & 7)
258 if (ulCount > 7)
260 /* Check from start bit to the end of the byte */
261 if ((*lpOut &
262 ((0xff << (ulStart & 7))) & 0xff) != ((0xff << (ulStart & 7) & 0xff)))
263 return FALSE;
264 lpOut++;
265 ulCount -= (8 - (ulStart & 7));
267 else
269 /* Check from the start bit, possibly into the next byte also */
270 USHORT initialWord = NTDLL_maskBits[ulCount] << (ulStart & 7);
272 if ((*lpOut & (initialWord & 0xff)) != (initialWord & 0xff))
273 return FALSE;
274 if ((initialWord & 0xff00) &&
275 ((lpOut[1] & (initialWord >> 8)) != (initialWord >> 8)))
276 return FALSE;
277 return TRUE;
281 /* Check bits in blocks of 8 bytes */
282 ulRemainder = ulCount & 7;
283 ulCount >>= 3;
284 while (ulCount--)
286 if (*lpOut++ != 0xff)
287 return FALSE;
290 /* Check remaining bits, if any */
291 if (ulRemainder &&
292 (*lpOut & NTDLL_maskBits[ulRemainder]) != NTDLL_maskBits[ulRemainder])
293 return FALSE;
294 return TRUE;
297 /*************************************************************************
298 * RtlAreBitsClear [NTDLL.@]
300 * Determine if part of a bitmap is clear.
302 * PARAMS
303 * lpBits [I] Bitmap pointer
304 * ulStart [I] First bit to check from
305 * ulCount [I] Number of consecutive bits to check
307 * RETURNS
308 * TRUE, If ulCount bits from ulStart are clear.
309 * FALSE, Otherwise.
311 BOOLEAN WINAPI RtlAreBitsClear(PCRTL_BITMAP lpBits, ULONG ulStart, ULONG ulCount)
313 LPBYTE lpOut;
314 ULONG ulRemainder;
316 TRACE("(%p,%ld,%ld)\n", lpBits, ulStart, ulCount);
318 if (!lpBits || !ulCount || ulStart + ulCount > lpBits->SizeOfBitMap)
319 return FALSE;
321 lpOut = lpBits->BitMapBuffer + (ulStart >> 3u);
323 /* Check bits in first byte, if ulStart isn't a byte boundary */
324 if (ulStart & 7)
326 if (ulCount > 7)
328 /* Check from start bit to the end of the byte */
329 if (*lpOut & ((0xff << (ulStart & 7)) & 0xff))
330 return FALSE;
331 lpOut++;
332 ulCount -= (8 - (ulStart & 7));
334 else
336 /* Check from the start bit, possibly into the next byte also */
337 USHORT initialWord = NTDLL_maskBits[ulCount] << (ulStart & 7);
339 if (*lpOut & (initialWord & 0xff))
340 return FALSE;
341 if ((initialWord & 0xff00) && (lpOut[1] & (initialWord >> 8)))
342 return FALSE;
343 return TRUE;
347 /* Check bits in blocks of 8 bytes */
348 ulRemainder = ulCount & 7;
349 ulCount >>= 3;
350 while (ulCount--)
352 if (*lpOut++)
353 return FALSE;
356 /* Check remaining bits, if any */
357 if (ulRemainder && *lpOut & NTDLL_maskBits[ulRemainder])
358 return FALSE;
359 return TRUE;
362 /*************************************************************************
363 * RtlFindSetBits [NTDLL.@]
365 * Find a block of set bits in a bitmap.
367 * PARAMS
368 * lpBits [I] Bitmap pointer
369 * ulCount [I] Number of consecutive set bits to find
370 * ulHint [I] Suggested starting position for set bits
372 * RETURNS
373 * The bit at which the match was found, or -1 if no match was found.
375 ULONG WINAPI RtlFindSetBits(PCRTL_BITMAP lpBits, ULONG ulCount, ULONG ulHint)
377 ULONG ulPos, ulEnd;
379 TRACE("(%p,%ld,%ld)\n", lpBits, ulCount, ulHint);
381 if (!lpBits || !ulCount || ulCount > lpBits->SizeOfBitMap)
382 return -1u;
384 ulEnd = lpBits->SizeOfBitMap;
386 if (ulHint + ulCount > lpBits->SizeOfBitMap)
387 ulHint = 0;
389 ulPos = ulHint;
391 while (ulPos < ulEnd)
393 /* FIXME: This could be made a _lot_ more efficient */
394 if (RtlAreBitsSet(lpBits, ulPos, ulCount))
395 return ulPos;
397 /* Start from the beginning if we hit the end and had a hint */
398 if (ulPos == ulEnd - 1 && ulHint)
400 ulEnd = ulHint;
401 ulPos = ulHint = 0;
403 else
404 ulPos++;
406 return -1u;
409 /*************************************************************************
410 * RtlFindClearBits [NTDLL.@]
412 * Find a block of clear bits in a bitmap.
414 * PARAMS
415 * lpBits [I] Bitmap pointer
416 * ulCount [I] Number of consecutive clear bits to find
417 * ulHint [I] Suggested starting position for clear bits
419 * RETURNS
420 * The bit at which the match was found, or -1 if no match was found.
422 ULONG WINAPI RtlFindClearBits(PCRTL_BITMAP lpBits, ULONG ulCount, ULONG ulHint)
424 ULONG ulPos, ulEnd;
426 TRACE("(%p,%ld,%ld)\n", lpBits, ulCount, ulHint);
428 if (!lpBits || !ulCount || ulCount > lpBits->SizeOfBitMap)
429 return -1u;
431 ulEnd = lpBits->SizeOfBitMap;
433 if (ulHint + ulCount > lpBits->SizeOfBitMap)
434 ulHint = 0;
436 ulPos = ulHint;
438 while (ulPos < ulEnd)
440 /* FIXME: This could be made a _lot_ more efficient */
441 if (RtlAreBitsClear(lpBits, ulPos, ulCount))
442 return ulPos;
444 /* Start from the beginning if we hit the end and started from ulHint */
445 if (ulPos == ulEnd - 1 && ulHint)
447 ulEnd = ulHint;
448 ulPos = ulHint = 0;
450 else
451 ulPos++;
453 return -1u;
456 /*************************************************************************
457 * RtlFindSetBitsAndClear [NTDLL.@]
459 * Find a block of set bits in a bitmap, and clear them if found.
461 * PARAMS
462 * lpBits [I] Bitmap pointer
463 * ulCount [I] Number of consecutive set bits to find
464 * ulHint [I] Suggested starting position for set bits
466 * RETURNS
467 * The bit at which the match was found, or -1 if no match was found.
469 ULONG WINAPI RtlFindSetBitsAndClear(PRTL_BITMAP lpBits, ULONG ulCount, ULONG ulHint)
471 ULONG ulPos;
473 TRACE("(%p,%ld,%ld)\n", lpBits, ulCount, ulHint);
475 ulPos = RtlFindSetBits(lpBits, ulCount, ulHint);
476 if (ulPos != -1u)
477 RtlClearBits(lpBits, ulPos, ulCount);
478 return ulPos;
481 /*************************************************************************
482 * RtlFindClearBitsAndSet [NTDLL.@]
484 * Find a block of clear bits in a bitmap, and set them if found.
486 * PARAMS
487 * lpBits [I] Bitmap pointer
488 * ulCount [I] Number of consecutive clear bits to find
489 * ulHint [I] Suggested starting position for clear bits
491 * RETURNS
492 * The bit at which the match was found, or -1 if no match was found.
494 ULONG WINAPI RtlFindClearBitsAndSet(PRTL_BITMAP lpBits, ULONG ulCount, ULONG ulHint)
496 ULONG ulPos;
498 TRACE("(%p,%ld,%ld)\n", lpBits, ulCount, ulHint);
500 ulPos = RtlFindClearBits(lpBits, ulCount, ulHint);
501 if (ulPos != -1u)
502 RtlSetBits(lpBits, ulPos, ulCount);
503 return ulPos;
506 /*************************************************************************
507 * RtlNumberOfSetBits [NTDLL.@]
509 * Find the number of set bits in a bitmap.
511 * PARAMS
512 * lpBits [I] Bitmap pointer
514 * RETURNS
515 * The number of set bits.
517 ULONG WINAPI RtlNumberOfSetBits(PCRTL_BITMAP lpBits)
519 ULONG ulSet = 0;
521 TRACE("(%p)\n", lpBits);
523 if (lpBits)
525 LPBYTE lpOut = lpBits->BitMapBuffer;
526 ULONG ulCount, ulRemainder;
527 BYTE bMasked;
529 ulCount = lpBits->SizeOfBitMap >> 3;
530 ulRemainder = lpBits->SizeOfBitMap & 0x7;
532 while (ulCount--)
534 ulSet += NTDLL_nibbleBitCount[*lpOut >> 4];
535 ulSet += NTDLL_nibbleBitCount[*lpOut & 0xf];
536 lpOut++;
539 bMasked = *lpOut & NTDLL_maskBits[ulRemainder];
540 ulSet += NTDLL_nibbleBitCount[bMasked >> 4];
541 ulSet += NTDLL_nibbleBitCount[bMasked & 0xf];
543 return ulSet;
546 /*************************************************************************
547 * RtlNumberOfClearBits [NTDLL.@]
549 * Find the number of clear bits in a bitmap.
551 * PARAMS
552 * lpBits [I] Bitmap pointer
554 * RETURNS
555 * The number of clear bits.
557 ULONG WINAPI RtlNumberOfClearBits(PCRTL_BITMAP lpBits)
559 TRACE("(%p)\n", lpBits);
561 if (lpBits)
562 return lpBits->SizeOfBitMap - RtlNumberOfSetBits(lpBits);
563 return 0;
566 /*************************************************************************
567 * RtlFindMostSignificantBit [NTDLL.@]
569 * Find the most significant bit in a 64 bit integer.
571 * PARAMS
572 * lpBits [I] Bitmap pointer
573 * ulStart [I] First bit to search from
575 * RETURNS
576 * The position of the most significant bit.
578 CCHAR WINAPI RtlFindMostSignificantBit(ULONGLONG ulLong)
580 LONG lCount = 64;
581 LPBYTE lpOut = (LPBYTE)&ulLong + 7;
583 TRACE("(%lld)\n", ulLong);
585 if (!(ulLong & 0xffffffff00000000ul))
587 lpOut -= 4;
588 lCount -= 32;
591 for (; lCount > 0; lCount -= 8)
593 if (*lpOut)
595 if (*lpOut & 0x0f)
596 return lCount - 8 + NTDLL_mostSignificant[*lpOut & 0x0f];
597 return lCount - 4 + NTDLL_mostSignificant[*lpOut >> 4];
599 lpOut--;
601 return -1;
604 /*************************************************************************
605 * RtlFindLeastSignificantBit [NTDLL.@]
607 * Find the least significant bit in a 64 bit integer.
609 * PARAMS
610 * lpBits [I] Bitmap pointer
611 * ulStart [I] First bit to search from
613 * RETURNS
614 * The position of the least significant bit.
616 CCHAR WINAPI RtlFindLeastSignificantBit(ULONGLONG ulLong)
618 ULONG ulCount = 0;
619 LPBYTE lpOut = (LPBYTE)&ulLong;
621 TRACE("(%lld)\n", ulLong);
623 if (!(ulLong & 0xffffffff))
625 lpOut += 4;
626 ulCount = 32;
629 for (; ulCount < 64; ulCount += 8)
631 if (*lpOut)
633 if (*lpOut & 0x0f)
634 return ulCount + NTDLL_leastSignificant[*lpOut & 0x0f];
635 return ulCount + 4 + NTDLL_leastSignificant[*lpOut >> 4];
637 lpOut++;
639 return -1;
642 /*************************************************************************
643 * NTDLL_RunSortFn
645 * Internal helper: qsort comparason function for RTL_BITMAP_RUN arrays
647 static int NTDLL_RunSortFn(const void *lhs, const void *rhs)
649 if (((PCRTL_BITMAP_RUN)lhs)->SizeOfRun > ((PRTL_BITMAP_RUN)rhs)->SizeOfRun)
650 return -1;
651 return 1;
654 /*************************************************************************
655 * NTDLL_FindSetRun
657 * Internal helper: Find the next run of set bits in a bitmap.
659 static ULONG NTDLL_FindSetRun(PCRTL_BITMAP lpBits, ULONG ulStart, PULONG lpSize)
661 LPBYTE lpOut;
662 ULONG ulFoundAt = 0, ulCount = 0;
664 lpOut = lpBits->BitMapBuffer + (ulStart >> 3u);
666 while (1)
668 /* Check bits in first byte */
669 const BYTE bMask = (0xff << (ulStart & 7)) & 0xff;
670 const BYTE bFirst = *lpOut & bMask;
672 if (bFirst)
674 /* Have a set bit in first byte */
675 if (bFirst != bMask)
677 /* Not every bit is set */
678 ULONG ulOffset;
680 if (bFirst & 0x0f)
681 ulOffset = NTDLL_leastSignificant[bFirst & 0x0f];
682 else
683 ulOffset = 4 + NTDLL_leastSignificant[bFirst >> 4];
684 ulStart += ulOffset;
685 ulFoundAt = ulStart;
686 for (;ulOffset < 8; ulOffset++)
688 if (!(bFirst & (1 << ulOffset)))
690 *lpSize = ulCount;
691 return ulFoundAt; /* Set from start, but not until the end */
693 ulCount++;
694 ulStart++;
696 /* Set to the end - go on to count further bits */
697 lpOut++;
698 break;
700 /* every bit from start until the end of the byte is set */
701 ulFoundAt = ulStart;
702 ulCount = 8 - (ulStart & 7);
703 ulStart = (ulStart & ~7u) + 8;
704 lpOut++;
705 break;
707 ulStart = (ulStart & ~7u) + 8;
708 lpOut++;
709 if (ulStart >= lpBits->SizeOfBitMap)
710 return -1u;
713 /* Count blocks of 8 set bits */
714 while (*lpOut == 0xff)
716 ulCount += 8;
717 ulStart += 8;
718 if (ulStart >= lpBits->SizeOfBitMap)
720 *lpSize = ulCount - (ulStart - lpBits->SizeOfBitMap);
721 return ulFoundAt;
723 lpOut++;
726 /* Count remaining contigious bits, if any */
727 if (*lpOut & 1)
729 ULONG ulOffset = 0;
731 for (;ulOffset < 7u; ulOffset++)
733 if (!(*lpOut & (1 << ulOffset)))
734 break;
735 ulCount++;
738 *lpSize = ulCount;
739 return ulFoundAt;
742 /*************************************************************************
743 * NTDLL_FindClearRun
745 * Internal helper: Find the next run of set bits in a bitmap.
747 static ULONG NTDLL_FindClearRun(PCRTL_BITMAP lpBits, ULONG ulStart, PULONG lpSize)
749 LPBYTE lpOut;
750 ULONG ulFoundAt = 0, ulCount = 0;
752 lpOut = lpBits->BitMapBuffer + (ulStart >> 3u);
754 while (1)
756 /* Check bits in first byte */
757 const BYTE bMask = (0xff << (ulStart & 7)) & 0xff;
758 const BYTE bFirst = (~*lpOut) & bMask;
760 if (bFirst)
762 /* Have a clear bit in first byte */
763 if (bFirst != bMask)
765 /* Not every bit is clear */
766 ULONG ulOffset;
768 if (bFirst & 0x0f)
769 ulOffset = NTDLL_leastSignificant[bFirst & 0x0f];
770 else
771 ulOffset = 4 + NTDLL_leastSignificant[bFirst >> 4];
772 ulStart += ulOffset;
773 ulFoundAt = ulStart;
774 for (;ulOffset < 8; ulOffset++)
776 if (!(bFirst & (1 << ulOffset)))
778 *lpSize = ulCount;
779 return ulFoundAt; /* Clear from start, but not until the end */
781 ulCount++;
782 ulStart++;
784 /* Clear to the end - go on to count further bits */
785 lpOut++;
786 break;
788 /* Every bit from start until the end of the byte is clear */
789 ulFoundAt = ulStart;
790 ulCount = 8 - (ulStart & 7);
791 ulStart = (ulStart & ~7u) + 8;
792 lpOut++;
793 break;
795 ulStart = (ulStart & ~7u) + 8;
796 lpOut++;
797 if (ulStart >= lpBits->SizeOfBitMap)
798 return -1u;
801 /* Count blocks of 8 clear bits */
802 while (!*lpOut)
804 ulCount += 8;
805 ulStart += 8;
806 if (ulStart >= lpBits->SizeOfBitMap)
808 *lpSize = ulCount - (ulStart - lpBits->SizeOfBitMap);
809 return ulFoundAt;
811 lpOut++;
814 /* Count remaining contigious bits, if any */
815 if (!(*lpOut & 1))
817 ULONG ulOffset = 0;
819 for (;ulOffset < 7u; ulOffset++)
821 if (*lpOut & (1 << ulOffset))
822 break;
823 ulCount++;
826 *lpSize = ulCount;
827 return ulFoundAt;
830 /*************************************************************************
831 * RtlFindNextForwardRunSet [NTDLL.@]
833 * Find the next run of set bits in a bitmap.
835 * PARAMS
836 * lpBits [I] Bitmap pointer
837 * ulStart [I] Bit position to start searching from
838 * lpPos [O] Start of run
840 * RETURNS
841 * Success: The length of the next set run in the bitmap. lpPos is set to
842 * the start of the run.
843 * Failure: 0, if no run is found or any parameters are invalid.
845 ULONG WINAPI RtlFindNextForwardRunSet(PCRTL_BITMAP lpBits, ULONG ulStart, PULONG lpPos)
847 ULONG ulSize = 0;
849 TRACE("(%p,%ld,%p)\n", lpBits, ulStart, lpPos);
851 if (lpBits && ulStart < lpBits->SizeOfBitMap && lpPos)
852 *lpPos = NTDLL_FindSetRun(lpBits, ulStart, &ulSize);
854 return ulSize;
857 /*************************************************************************
858 * RtlFindNextForwardRunClear [NTDLL.@]
860 * Find the next run of clear bits in a bitmap.
862 * PARAMS
863 * lpBits [I] Bitmap pointer
864 * ulStart [I] Bit position to start searching from
865 * lpPos [O] Start of run
867 * RETURNS
868 * Success: The length of the next clear run in the bitmap. lpPos is set to
869 * the start of the run.
870 * Failure: 0, if no run is found or any parameters are invalid.
872 ULONG WINAPI RtlFindNextForwardRunClear(PCRTL_BITMAP lpBits, ULONG ulStart, PULONG lpPos)
874 ULONG ulSize = 0;
876 TRACE("(%p,%ld,%p)\n", lpBits, ulStart, lpPos);
878 if (lpBits && ulStart < lpBits->SizeOfBitMap && lpPos)
879 *lpPos = NTDLL_FindClearRun(lpBits, ulStart, &ulSize);
881 return ulSize;
884 /*************************************************************************
885 * RtlFindLastBackwardRunSet [NTDLL.@]
887 * Find a previous run of set bits in a bitmap.
889 * PARAMS
890 * lpBits [I] Bitmap pointer
891 * ulStart [I] Bit position to start searching from
892 * lpPos [O] Start of run
894 * RETURNS
895 * Success: The length of the previous set run in the bitmap. lpPos is set to
896 * the start of the run.
897 * Failure: 0, if no run is found or any parameters are invalid.
899 ULONG WINAPI RtlFindLastBackwardRunSet(PCRTL_BITMAP lpBits, ULONG ulStart, PULONG lpPos)
901 FIXME("(%p,%ld,%p)-stub!\n", lpBits, ulStart, lpPos);
902 return 0;
905 /*************************************************************************
906 * RtlFindLastBackwardRunClear [NTDLL.@]
908 * Find a previous run of clear bits in a bitmap.
910 * PARAMS
911 * lpBits [I] Bitmap pointer
912 * ulStart [I] Bit position to start searching from
913 * lpPos [O] Start of run
915 * RETURNS
916 * Success: The length of the previous clear run in the bitmap. lpPos is set
917 * to the start of the run.
918 * Failure: 0, if no run is found or any parameters are invalid.
920 ULONG WINAPI RtlFindLastBackwardRunClear(PCRTL_BITMAP lpBits, ULONG ulStart, PULONG lpPos)
922 FIXME("(%p,%ld,%p)-stub!\n", lpBits, ulStart, lpPos);
923 return 0;
926 /*************************************************************************
927 * NTDLL_FindRuns
929 * Internal implementation of RtlFindSetRuns/RtlFindClearRuns.
931 static ULONG WINAPI NTDLL_FindRuns(PCRTL_BITMAP lpBits, PRTL_BITMAP_RUN lpSeries,
932 ULONG ulCount, BOOLEAN bLongest,
933 ULONG (*fn)(PCRTL_BITMAP,ULONG,PULONG))
935 BOOL bNeedSort = ulCount > 1 ? TRUE : FALSE;
936 ULONG ulPos = 0, ulRuns = 0;
938 TRACE("(%p,%p,%ld,%d)\n", lpBits, lpSeries, ulCount, bLongest);
940 if (!ulCount)
941 return -1u;
943 while (ulPos < lpBits->SizeOfBitMap)
945 /* Find next set/clear run */
946 ULONG ulSize, ulNextPos = fn(lpBits, ulPos, &ulSize);
948 if (ulNextPos == -1u)
949 break;
951 if (bLongest && ulRuns == ulCount)
953 /* Sort runs with shortest at end, if they are out of order */
954 if (bNeedSort)
955 qsort(lpSeries, ulRuns, sizeof(RTL_BITMAP_RUN), NTDLL_RunSortFn);
957 /* Replace last run if this one is bigger */
958 if (ulSize > lpSeries[ulRuns - 1].SizeOfRun)
960 lpSeries[ulRuns - 1].StartOfRun = ulNextPos;
961 lpSeries[ulRuns - 1].SizeOfRun = ulSize;
963 /* We need to re-sort the array, _if_ we didn't leave it sorted */
964 if (ulRuns > 1 && ulSize > lpSeries[ulRuns - 2].SizeOfRun)
965 bNeedSort = TRUE;
968 else
970 /* Append to found runs */
971 lpSeries[ulRuns].StartOfRun = ulNextPos;
972 lpSeries[ulRuns].SizeOfRun = ulSize;
973 ulRuns++;
975 if (!bLongest && ulRuns == ulCount)
976 break;
978 ulPos = ulNextPos + ulSize;
980 return ulRuns;
983 /*************************************************************************
984 * RtlFindSetRuns [NTDLL.@]
986 * Find a series of set runs in a bitmap.
988 * PARAMS
989 * lpBits [I] Bitmap pointer
990 * ulSeries [O] Array for each run found
991 * ulCount [I] Number of runs to find
992 * bLongest [I] Whether to find the very longest runs or not
994 * RETURNS
995 * The number of set runs found.
997 ULONG WINAPI RtlFindSetRuns(PCRTL_BITMAP lpBits, PRTL_BITMAP_RUN lpSeries,
998 ULONG ulCount, BOOLEAN bLongest)
1000 TRACE("(%p,%p,%ld,%d)\n", lpBits, lpSeries, ulCount, bLongest);
1002 return NTDLL_FindRuns(lpBits, lpSeries, ulCount, bLongest, NTDLL_FindSetRun);
1005 /*************************************************************************
1006 * RtlFindClearRuns [NTDLL.@]
1008 * Find a series of clear runs in a bitmap.
1010 * PARAMS
1011 * lpBits [I] Bitmap pointer
1012 * ulSeries [O] Array for each run found
1013 * ulCount [I] Number of runs to find
1014 * bLongest [I] Whether to find the very longest runs or not
1016 * RETURNS
1017 * The number of clear runs found.
1019 ULONG WINAPI RtlFindClearRuns(PCRTL_BITMAP lpBits, PRTL_BITMAP_RUN lpSeries,
1020 ULONG ulCount, BOOLEAN bLongest)
1022 TRACE("(%p,%p,%ld,%d)\n", lpBits, lpSeries, ulCount, bLongest);
1024 return NTDLL_FindRuns(lpBits, lpSeries, ulCount, bLongest, NTDLL_FindClearRun);
1027 /*************************************************************************
1028 * RtlFindLongestRunSet [NTDLL.@]
1030 * Find the longest set run in a bitmap.
1032 * PARAMS
1033 * lpBits [I] Bitmap pointer
1034 * pulStart [O] Destination for start of run
1036 * RETURNS
1037 * The length of the run found, or 0 if no run is found.
1039 ULONG WINAPI RtlFindLongestRunSet(PCRTL_BITMAP lpBits, PULONG pulStart)
1041 RTL_BITMAP_RUN br;
1043 TRACE("(%p,%p)\n", lpBits, pulStart);
1045 if (RtlFindSetRuns(lpBits, &br, 1, TRUE) == 1)
1047 if (pulStart)
1048 *pulStart = br.StartOfRun;
1049 return br.SizeOfRun;
1051 return 0;
1054 /*************************************************************************
1055 * RtlFindLongestRunClear [NTDLL.@]
1057 * Find the longest clear run in a bitmap.
1059 * PARAMS
1060 * lpBits [I] Bitmap pointer
1061 * pulStart [O] Destination for start of run
1063 * RETURNS
1064 * The length of the run found, or 0 if no run is found.
1066 ULONG WINAPI RtlFindLongestRunClear(PCRTL_BITMAP lpBits, PULONG pulStart)
1068 RTL_BITMAP_RUN br;
1070 TRACE("(%p,%p)\n", lpBits, pulStart);
1072 if (RtlFindClearRuns(lpBits, &br, 1, TRUE) == 1)
1074 if (pulStart)
1075 *pulStart = br.StartOfRun;
1076 return br.SizeOfRun;
1078 return 0;