Index: memory_manager.cpp =================================================================== --- lib/drivers/libusb/memory_manager.cpp (revision 70372) +++ lib/drivers/libusb/memory_manager.cpp (working copy) @@ -132,7 +132,7 @@ OUT PVOID *OutVirtualAddress, OUT PPHYSICAL_ADDRESS OutPhysicalAddress) { - ULONG Length, BlockCount, FreeIndex, StartPage, EndPage; + ULONG Length, BlockCount, FreeIndex, LastIndex, StartPage, EndPage; KIRQL OldLevel; ULONG BlocksPerPage; @@ -166,11 +166,13 @@ // helper variable // BlocksPerPage = PAGE_SIZE / m_BlockSize; + ASSERT(BlockCount <= BlocksPerPage); // // start search // FreeIndex = 0; + LastIndex = MAXULONG; do { // @@ -190,10 +192,21 @@ } // - // check that the allocation does not spawn over page boundaries + // did we wrap over to the start of the bitmap? // - StartPage = (FreeIndex * m_BlockSize); - StartPage = (StartPage != 0 ? StartPage / PAGE_SIZE : 0); + if (FreeIndex <= LastIndex && LastIndex != MAXULONG) + { + // + // no free block found; + // + FreeIndex = MAXULONG; + break; + } + + // + // check that the allocation does not span over page boundaries + // + StartPage = (FreeIndex * m_BlockSize) / PAGE_SIZE; EndPage = ((FreeIndex + BlockCount) * m_BlockSize) / PAGE_SIZE; // @@ -227,13 +240,13 @@ else { // - // request spawned over page boundary + // request spanned over page boundary // restart search on next page // - FreeIndex = (EndPage * PAGE_SIZE) / m_BlockSize; + LastIndex = FreeIndex; + FreeIndex = (EndPage * PAGE_SIZE) / m_BlockSize; } - } - while(TRUE); + } while (TRUE); // // release lock @@ -248,7 +261,7 @@ // // failed to allocate block, requestor must retry // - return STATUS_UNSUCCESSFUL; + return STATUS_INSUFFICIENT_RESOURCES; } //