diff --git a/reactos/include/reactos/helper.h b/reactos/include/reactos/helper.h index ea6b90a0ff9..940a31980ba 100644 --- a/reactos/include/reactos/helper.h +++ b/reactos/include/reactos/helper.h @@ -18,17 +18,21 @@ #define EXPORTED __declspec(dllexport) #define IMPORTED __declspec(dllimport) -/* iterate through the list using a list entry */ +/* iterate through the list using a list entry. + * elem is set to NULL if the list is run thru without breaking out or if list is empty. + */ #define LIST_FOR_EACH(elem, list, type, field) \ for ((elem) = CONTAINING_RECORD((list)->Flink, type, field); \ - &(elem)->field != (list); \ + &(elem)->field != (list) || (elem = NULL); \ (elem) = CONTAINING_RECORD((elem)->field.Flink, type, field)) -/* iterate through the list using a list entry, with safety against removal */ +/* iterate through the list using a list entry, with safety against removal + * elem is set to NULL if the list is run thru without breaking out or if list is empty. + */ #define LIST_FOR_EACH_SAFE(cursor, cursor2, list, type, field) \ for ((cursor) = CONTAINING_RECORD((list)->Flink, type, field), \ (cursor2) = CONTAINING_RECORD((cursor)->field.Flink, type, field); \ - &(cursor)->field != (list); \ + &(cursor)->field != (list) || (cursor = NULL); \ (cursor) = (cursor2), \ (cursor2) = CONTAINING_RECORD((cursor)->field.Flink, type, field)) @@ -55,7 +59,7 @@ #define HOURS_TO_100NS(hours) (((LONGLONG)(hours)) * MINUTES_TO_100NS(60)) #define UNICODIZE1(x) L##x #define UNICODIZE(x) UNICODIZE1(x) -#define InsertAscendingListFIFO(ListHead, Type, ListEntryField, NewEntry, SortField)\ +#define InsertAscendingListFIFO(ListHead, NewEntry, Type, ListEntryField, SortField)\ {\ PLIST_ENTRY current;\ \ @@ -73,7 +77,7 @@ InsertTailList(current, &((NewEntry)->ListEntryField));\ } -#define InsertDescendingListFIFO(ListHead, Type, ListEntryField, NewEntry, SortField)\ +#define InsertDescendingListFIFO(ListHead, NewEntry, Type, ListEntryField, SortField)\ {\ PLIST_ENTRY current;\ \ @@ -91,7 +95,7 @@ InsertTailList(current, &((NewEntry)->ListEntryField));\ } -#define InsertAscendingList(ListHead, Type, ListEntryField, NewEntry, SortField)\ +#define InsertAscendingList(ListHead, NewEntry, Type, ListEntryField, SortField)\ {\ PLIST_ENTRY current;\ \ @@ -109,7 +113,7 @@ InsertTailList(current, &((NewEntry)->ListEntryField));\ } -#define InsertDescendingList(ListHead, Type, ListEntryField, NewEntry, SortField)\ +#define InsertDescendingList(ListHead, NewEntry, Type, ListEntryField, SortField)\ {\ PLIST_ENTRY current;\ \ diff --git a/reactos/ntoskrnl/io/device.c b/reactos/ntoskrnl/io/device.c index 16f25126397..2e1cdfcfdb6 100644 --- a/reactos/ntoskrnl/io/device.c +++ b/reactos/ntoskrnl/io/device.c @@ -33,17 +33,13 @@ VOID IoShutdownRegisteredDevices(VOID) { PSHUTDOWN_ENTRY ShutdownEntry; - PLIST_ENTRY Entry; IO_STATUS_BLOCK StatusBlock; PIRP Irp; KEVENT Event; NTSTATUS Status; - Entry = ShutdownListHead.Flink; - while (Entry != &ShutdownListHead) + LIST_FOR_EACH(ShutdownEntry, &ShutdownListHead, SHUTDOWN_ENTRY, ShutdownList) { - ShutdownEntry = CONTAINING_RECORD(Entry, SHUTDOWN_ENTRY, ShutdownList); - KeInitializeEvent (&Event, NotificationEvent, FALSE); @@ -66,8 +62,6 @@ IoShutdownRegisteredDevices(VOID) FALSE, NULL); } - - Entry = Entry->Flink; } } @@ -1082,27 +1076,24 @@ VOID STDCALL IoUnregisterShutdownNotification(PDEVICE_OBJECT DeviceObject) { - PSHUTDOWN_ENTRY ShutdownEntry; - PLIST_ENTRY Entry; + PSHUTDOWN_ENTRY ShutdownEntry, tmp; KIRQL oldlvl; - Entry = ShutdownListHead.Flink; - while (Entry != &ShutdownListHead) + LIST_FOR_EACH_SAFE(ShutdownEntry, tmp, &ShutdownListHead, SHUTDOWN_ENTRY, ShutdownList) { - ShutdownEntry = CONTAINING_RECORD(Entry, SHUTDOWN_ENTRY, ShutdownList); + if (ShutdownEntry->DeviceObject == DeviceObject) { DeviceObject->Flags &= ~DO_SHUTDOWN_REGISTERED; KeAcquireSpinLock(&ShutdownListLock,&oldlvl); - RemoveEntryList(Entry); + RemoveEntryList(&ShutdownEntry->ShutdownList); KeReleaseSpinLock(&ShutdownListLock,oldlvl); - ExFreePool(Entry); + ExFreePool(ShutdownEntry); return; } - Entry = Entry->Flink; } } diff --git a/reactos/ntoskrnl/io/driver.c b/reactos/ntoskrnl/io/driver.c index 33fd8082b27..a2e1f5aa92b 100644 --- a/reactos/ntoskrnl/io/driver.c +++ b/reactos/ntoskrnl/io/driver.c @@ -1118,44 +1118,32 @@ IoCreateDriverList(VOID) NTSTATUS INIT_FUNCTION IoDestroyDriverList(VOID) { - PLIST_ENTRY GroupEntry; - PLIST_ENTRY ServiceEntry; - PSERVICE_GROUP CurrentGroup; - PSERVICE CurrentService; + PSERVICE_GROUP CurrentGroup, tmp1; + PSERVICE CurrentService, tmp2; DPRINT("IoDestroyDriverList() called\n"); /* Destroy group list */ - GroupEntry = GroupListHead.Flink; - while (GroupEntry != &GroupListHead) + LIST_FOR_EACH_SAFE(CurrentGroup, tmp1, &GroupListHead, SERVICE_GROUP, GroupListEntry) { - CurrentGroup = CONTAINING_RECORD(GroupEntry, SERVICE_GROUP, GroupListEntry); - ExFreePool(CurrentGroup->GroupName.Buffer); - RemoveEntryList(GroupEntry); + RemoveEntryList(&CurrentGroup->GroupListEntry); if (CurrentGroup->TagArray) { ExFreePool(CurrentGroup->TagArray); } ExFreePool(CurrentGroup); - - GroupEntry = GroupListHead.Flink; } /* Destroy service list */ - ServiceEntry = ServiceListHead.Flink; - while (ServiceEntry != &ServiceListHead) + LIST_FOR_EACH_SAFE(CurrentService, tmp2, &ServiceListHead, SERVICE, ServiceListEntry) { - CurrentService = CONTAINING_RECORD(ServiceEntry, SERVICE, ServiceListEntry); - ExFreePool(CurrentService->ServiceName.Buffer); ExFreePool(CurrentService->RegistryPath.Buffer); ExFreePool(CurrentService->ServiceGroup.Buffer); ExFreePool(CurrentService->ImagePath.Buffer); - RemoveEntryList(ServiceEntry); + RemoveEntryList(&CurrentService->ServiceListEntry); ExFreePool(CurrentService); - - ServiceEntry = ServiceListHead.Flink; } DPRINT("IoDestroyDriverList() done\n"); @@ -1423,8 +1411,6 @@ IopLoadDriver(PSERVICE Service) VOID FASTCALL IopInitializeSystemDrivers(VOID) { - PLIST_ENTRY GroupEntry; - PLIST_ENTRY ServiceEntry; PSERVICE_GROUP CurrentGroup; PSERVICE CurrentService; NTSTATUS Status; @@ -1432,21 +1418,15 @@ IopInitializeSystemDrivers(VOID) DPRINT("IopInitializeSystemDrivers()\n"); - GroupEntry = GroupListHead.Flink; - while (GroupEntry != &GroupListHead) + LIST_FOR_EACH(CurrentGroup, &GroupListHead, SERVICE_GROUP, GroupListEntry) { - CurrentGroup = CONTAINING_RECORD(GroupEntry, SERVICE_GROUP, GroupListEntry); - DPRINT("Group: %wZ\n", &CurrentGroup->GroupName); /* Load all drivers with a valid tag */ for (i = 0; i < CurrentGroup->TagCount; i++) { - ServiceEntry = ServiceListHead.Flink; - while (ServiceEntry != &ServiceListHead) + LIST_FOR_EACH(CurrentService, &ServiceListHead, SERVICE, ServiceListEntry) { - CurrentService = CONTAINING_RECORD(ServiceEntry, SERVICE, ServiceListEntry); - if ((RtlCompareUnicodeString(&CurrentGroup->GroupName, &CurrentService->ServiceGroup, TRUE) == 0) && (CurrentService->Start == 1 /*SERVICE_SYSTEM_START*/) && @@ -1455,15 +1435,12 @@ IopInitializeSystemDrivers(VOID) DPRINT(" Path: %wZ\n", &CurrentService->RegistryPath); Status = IopLoadDriver(CurrentService); } - ServiceEntry = ServiceEntry->Flink; } } /* Load all drivers without a tag or with an invalid tag */ - ServiceEntry = ServiceListHead.Flink; - while (ServiceEntry != &ServiceListHead) + LIST_FOR_EACH(CurrentService, &ServiceListHead, SERVICE, ServiceListEntry) { - CurrentService = CONTAINING_RECORD(ServiceEntry, SERVICE, ServiceListEntry); if ((RtlCompareUnicodeString(&CurrentGroup->GroupName, &CurrentService->ServiceGroup, TRUE) == 0) && (CurrentService->Start == 1 /*SERVICE_SYSTEM_START*/)) @@ -1481,10 +1458,8 @@ IopInitializeSystemDrivers(VOID) Status = IopLoadDriver(CurrentService); } } - ServiceEntry = ServiceEntry->Flink; } - GroupEntry = GroupEntry->Flink; } DPRINT("IopInitializeSystemDrivers() done\n"); diff --git a/reactos/ntoskrnl/io/file.c b/reactos/ntoskrnl/io/file.c index 6eec07c6179..e91ef44ffdb 100644 --- a/reactos/ntoskrnl/io/file.c +++ b/reactos/ntoskrnl/io/file.c @@ -1383,7 +1383,6 @@ NtCancelIoFile(IN HANDLE FileHandle, NTSTATUS Status; PFILE_OBJECT FileObject; PETHREAD Thread; - PLIST_ENTRY IrpEntry; PIRP Irp; KIRQL OldIrql; BOOLEAN OurIrpsInList = FALSE; @@ -1408,11 +1407,9 @@ NtCancelIoFile(IN HANDLE FileHandle, */ Thread = PsGetCurrentThread(); - for (IrpEntry = Thread->IrpList.Flink; - IrpEntry != &Thread->IrpList; - IrpEntry = IrpEntry->Flink) + + LIST_FOR_EACH(Irp, &Thread->IrpList, IRP, ThreadListEntry) { - Irp = CONTAINING_RECORD(IrpEntry, IRP, ThreadListEntry); if (Irp->Tail.Overlay.OriginalFileObject == FileObject) { IoCancelIrp(Irp); @@ -1440,11 +1437,8 @@ NtCancelIoFile(IN HANDLE FileHandle, * forever. */ - for (IrpEntry = Thread->IrpList.Flink; - IrpEntry != &Thread->IrpList; - IrpEntry = IrpEntry->Flink) + LIST_FOR_EACH(Irp, &Thread->IrpList, IRP, ThreadListEntry) { - Irp = CONTAINING_RECORD(IrpEntry, IRP, ThreadListEntry); if (Irp->Tail.Overlay.OriginalFileObject == FileObject) { OurIrpsInList = TRUE; diff --git a/reactos/ntoskrnl/io/fs.c b/reactos/ntoskrnl/io/fs.c index dddbb355240..935f2fc7943 100644 --- a/reactos/ntoskrnl/io/fs.c +++ b/reactos/ntoskrnl/io/fs.c @@ -72,7 +72,6 @@ IoInitFileSystemImplementation(VOID) VOID IoShutdownRegisteredFileSystems(VOID) { - PLIST_ENTRY current_entry; FILE_SYSTEM_OBJECT* current; PIRP Irp; KEVENT Event; @@ -87,11 +86,8 @@ IoShutdownRegisteredFileSystems(VOID) NotificationEvent, FALSE); - current_entry = FileSystemListHead.Flink; - while (current_entry!=(&FileSystemListHead)) + LIST_FOR_EACH(current, &FileSystemListHead, FILE_SYSTEM_OBJECT,Entry) { - current = CONTAINING_RECORD(current_entry,FILE_SYSTEM_OBJECT,Entry); - /* send IRP_MJ_SHUTDOWN */ Irp = IoBuildSynchronousFsdRequest(IRP_MJ_SHUTDOWN, current->DeviceObject, @@ -110,8 +106,6 @@ IoShutdownRegisteredFileSystems(VOID) FALSE, NULL); } - - current_entry = current_entry->Flink; } ExReleaseResourceLite(&FileSystemListLock); @@ -224,8 +218,7 @@ IoMountVolume(IN PDEVICE_OBJECT DeviceObject, * RETURNS: Status */ { - PLIST_ENTRY current_entry; - FILE_SYSTEM_OBJECT* current; + PFILE_SYSTEM_OBJECT current; NTSTATUS Status; DEVICE_TYPE MatchingDeviceType; PDEVICE_OBJECT DevObject; @@ -262,13 +255,12 @@ IoMountVolume(IN PDEVICE_OBJECT DeviceObject, KeEnterCriticalRegion(); ExAcquireResourceSharedLite(&FileSystemListLock,TRUE); - current_entry = FileSystemListHead.Flink; - while (current_entry!=(&FileSystemListHead)) + +restart: + LIST_FOR_EACH(current,&FileSystemListHead, FILE_SYSTEM_OBJECT, Entry) { - current = CONTAINING_RECORD(current_entry,FILE_SYSTEM_OBJECT,Entry); if (current->DeviceObject->DeviceType != MatchingDeviceType) { - current_entry = current_entry->Flink; continue; } /* If we are not allowed to mount this volume as a raw filesystem volume @@ -294,8 +286,7 @@ IoMountVolume(IN PDEVICE_OBJECT DeviceObject, return(Status); } ExAcquireResourceSharedLite(&FileSystemListLock,TRUE); - current_entry = FileSystemListHead.Flink; - continue; + goto restart; case STATUS_SUCCESS: DeviceObject->Vpb->Flags = DeviceObject->Vpb->Flags | @@ -306,7 +297,8 @@ IoMountVolume(IN PDEVICE_OBJECT DeviceObject, case STATUS_UNRECOGNIZED_VOLUME: default: - current_entry = current_entry->Flink; + /* do nothing */ + break; } } ExReleaseResourceLite(&FileSystemListLock); @@ -504,28 +496,26 @@ IoRegisterFileSystem(IN PDEVICE_OBJECT DeviceObject) VOID STDCALL IoUnregisterFileSystem(IN PDEVICE_OBJECT DeviceObject) { - PLIST_ENTRY current_entry; PFILE_SYSTEM_OBJECT current; DPRINT("IoUnregisterFileSystem(DeviceObject 0x%p)\n", DeviceObject); KeEnterCriticalRegion(); ExAcquireResourceExclusiveLite(&FileSystemListLock, TRUE); - current_entry = FileSystemListHead.Flink; - while (current_entry!=(&FileSystemListHead)) + + LIST_FOR_EACH(current,&FileSystemListHead, FILE_SYSTEM_OBJECT,Entry) { - current = CONTAINING_RECORD(current_entry,FILE_SYSTEM_OBJECT,Entry); if (current->DeviceObject == DeviceObject) { - RemoveEntryList(current_entry); + RemoveEntryList(¤t->Entry); ExFreePoolWithTag(current, TAG_FILE_SYSTEM); ExReleaseResourceLite(&FileSystemListLock); KeLeaveCriticalRegion(); IopNotifyFileSystemChange(DeviceObject, FALSE); return; } - current_entry = current_entry->Flink; } + ExReleaseResourceLite(&FileSystemListLock); KeLeaveCriticalRegion(); } @@ -595,17 +585,11 @@ IopNotifyFileSystemChange(PDEVICE_OBJECT DeviceObject, BOOLEAN DriverActive) { PFS_CHANGE_NOTIFY_ENTRY ChangeEntry; - PLIST_ENTRY Entry; KeAcquireGuardedMutex(&FsChangeNotifyListLock); - Entry = FsChangeNotifyListHead.Flink; - while (Entry != &FsChangeNotifyListHead) + LIST_FOR_EACH(ChangeEntry, &FsChangeNotifyListHead,FS_CHANGE_NOTIFY_ENTRY, FsChangeNotifyList) { - ChangeEntry = CONTAINING_RECORD(Entry, FS_CHANGE_NOTIFY_ENTRY, FsChangeNotifyList); - (ChangeEntry->FSDNotificationProc)(DeviceObject, DriverActive); - - Entry = Entry->Flink; } KeReleaseGuardedMutex(&FsChangeNotifyListLock); } @@ -646,24 +630,20 @@ IoUnregisterFsRegistrationChange(IN PDRIVER_OBJECT DriverObject, IN PDRIVER_FS_NOTIFICATION FSDNotificationProc) { PFS_CHANGE_NOTIFY_ENTRY ChangeEntry; - PLIST_ENTRY Entry; - Entry = FsChangeNotifyListHead.Flink; - while (Entry != &FsChangeNotifyListHead) + LIST_FOR_EACH(ChangeEntry, &FsChangeNotifyListHead, FS_CHANGE_NOTIFY_ENTRY, FsChangeNotifyList) { - ChangeEntry = CONTAINING_RECORD(Entry, FS_CHANGE_NOTIFY_ENTRY, FsChangeNotifyList); if (ChangeEntry->DriverObject == DriverObject && ChangeEntry->FSDNotificationProc == FSDNotificationProc) { KeAcquireGuardedMutex(&FsChangeNotifyListLock); - RemoveEntryList(Entry); + RemoveEntryList(&ChangeEntry->FsChangeNotifyList); KeReleaseGuardedMutex(&FsChangeNotifyListLock); - ExFreePoolWithTag(Entry, TAG_FS_CHANGE_NOTIFY); + ExFreePoolWithTag(ChangeEntry, TAG_FS_CHANGE_NOTIFY); return; } - Entry = Entry->Flink; } } diff --git a/reactos/ntoskrnl/io/irp.c b/reactos/ntoskrnl/io/irp.c index 82f8bfb2f80..b0dc4e022c2 100644 --- a/reactos/ntoskrnl/io/irp.c +++ b/reactos/ntoskrnl/io/irp.c @@ -962,7 +962,6 @@ VOID STDCALL IoCancelThreadIo(PETHREAD Thread) { - PLIST_ENTRY IrpEntry; PIRP Irp; KIRQL OldIrql; ULONG Retries = 3000; @@ -972,13 +971,8 @@ IoCancelThreadIo(PETHREAD Thread) OldIrql = KfRaiseIrql(APC_LEVEL); /* Start by cancelling all the IRPs in the current thread queue. */ - for (IrpEntry = Thread->IrpList.Flink; - IrpEntry != &Thread->IrpList; - IrpEntry = IrpEntry->Flink) + LIST_FOR_EACH(Irp, &Thread->IrpList, IRP, ThreadListEntry) { - /* Get the IRP */ - Irp = CONTAINING_RECORD(IrpEntry, IRP, ThreadListEntry); - /* Cancel it */ IoCancelIrp(Irp); } diff --git a/reactos/ntoskrnl/io/pnpnotify.c b/reactos/ntoskrnl/io/pnpnotify.c index e92e5cc578b..bf8deb6569a 100644 --- a/reactos/ntoskrnl/io/pnpnotify.c +++ b/reactos/ntoskrnl/io/pnpnotify.c @@ -184,7 +184,6 @@ IopNotifyPlugPlayNotification( IN PVOID EventCategoryData2) { PPNP_NOTIFY_ENTRY ChangeEntry; - PLIST_ENTRY Entry; PVOID NotificationStructure; BOOLEAN CallCurrentEntry; @@ -249,10 +248,8 @@ IopNotifyPlugPlayNotification( * list to find those that meet some criteria. */ - Entry = PnpNotifyListHead.Flink; - while (Entry != &PnpNotifyListHead) + LIST_FOR_EACH(ChangeEntry,&PnpNotifyListHead, PNP_NOTIFY_ENTRY, PnpNotifyList) { - ChangeEntry = CONTAINING_RECORD(Entry, PNP_NOTIFY_ENTRY, PnpNotifyList); CallCurrentEntry = FALSE; switch (EventCategory) @@ -294,7 +291,6 @@ IopNotifyPlugPlayNotification( ChangeEntry->Context); } - Entry = Entry->Flink; } KeReleaseGuardedMutex(&PnpNotifyListLock); ExFreePoolWithTag(NotificationStructure, TAG_PNP_NOTIFY); diff --git a/reactos/ntoskrnl/io/pnproot.c b/reactos/ntoskrnl/io/pnproot.c index b77e6bbdbd7..f525353b42c 100644 --- a/reactos/ntoskrnl/io/pnproot.c +++ b/reactos/ntoskrnl/io/pnproot.c @@ -766,7 +766,6 @@ PnpRootQueryBusRelations( PPNPROOT_PDO_DEVICE_EXTENSION PdoDeviceExtension; PPNPROOT_FDO_DEVICE_EXTENSION DeviceExtension; PDEVICE_RELATIONS Relations; - PLIST_ENTRY CurrentEntry; PPNPROOT_DEVICE Device; NTSTATUS Status; ULONG Size; @@ -796,11 +795,8 @@ PnpRootQueryBusRelations( Relations->Count = DeviceExtension->DeviceListCount; i = 0; - CurrentEntry = DeviceExtension->DeviceListHead.Flink; - while (CurrentEntry != &DeviceExtension->DeviceListHead) + LIST_FOR_EACH(Device,&DeviceExtension->DeviceListHead,PNPROOT_DEVICE, ListEntry) { - Device = CONTAINING_RECORD(CurrentEntry, PNPROOT_DEVICE, ListEntry); - if (!Device->Pdo) { /* Create a physical device object for the @@ -894,8 +890,6 @@ PnpRootQueryBusRelations( Relations->Objects[i] = Device->Pdo; i++; - - CurrentEntry = CurrentEntry->Flink; } if (NT_SUCCESS(Status)) diff --git a/reactos/ntoskrnl/ke/apc.c b/reactos/ntoskrnl/ke/apc.c index 50245aad6ef..92831f3c62d 100644 --- a/reactos/ntoskrnl/ke/apc.c +++ b/reactos/ntoskrnl/ke/apc.c @@ -254,8 +254,6 @@ KiInsertQueueApc(PKAPC Apc, KPRIORITY PriorityBoost) { PKTHREAD Thread = Apc->Thread; - PLIST_ENTRY ApcListEntry; - PKAPC QueuedApc; KeAcquireSpinLockAtDpcLevel(&Thread->ApcQueueLock); @@ -282,17 +280,14 @@ KiInsertQueueApc(PKAPC Apc, DPRINT("Inserting Special APC %x for '%.16s' into the Queue\n", Apc, ((PETHREAD)Thread)->ThreadsProcess->ImageFileName); - for (ApcListEntry = Thread->ApcStatePointer[(int)Apc->ApcStateIndex]->ApcListHead[(int)Apc->ApcMode].Flink; - ApcListEntry != &Thread->ApcStatePointer[(int)Apc->ApcStateIndex]->ApcListHead[(int)Apc->ApcMode]; - ApcListEntry = ApcListEntry->Flink) { - - QueuedApc = CONTAINING_RECORD(ApcListEntry, KAPC, ApcListEntry); - if (Apc->NormalRoutine != NULL) break; - } - - /* We found the first "Normal" APC, so write right before it */ - ApcListEntry = ApcListEntry->Blink; - InsertHeadList(ApcListEntry, &Apc->ApcListEntry); + /* insert special apc before normal apcs (if any) but after the last special apc (fifo) */ + InsertAscendingListFIFO( + &Thread->ApcStatePointer[(int)Apc->ApcStateIndex]->ApcListHead[(int)Apc->ApcMode], + Apc, + KAPC, + ApcListEntry, + NormalRoutine /* sort field */ + ); } else { @@ -469,25 +464,23 @@ KeFlushQueueApc(IN PKTHREAD Thread, { KIRQL OldIrql; PKAPC Apc; - PLIST_ENTRY FirstEntry, CurrentEntry; + PLIST_ENTRY FirstEntry = NULL; /* Lock the Dispatcher Database and APC Queue */ OldIrql = KeAcquireDispatcherDatabaseLock(); KeAcquireSpinLockAtDpcLevel(&Thread->ApcQueueLock); - if (IsListEmpty(&Thread->ApcState.ApcListHead[PreviousMode])) { - FirstEntry = NULL; - } else { - FirstEntry = Thread->ApcState.ApcListHead[PreviousMode].Flink; - RemoveEntryList(&Thread->ApcState.ApcListHead[PreviousMode]); - CurrentEntry = FirstEntry; - do { - Apc = CONTAINING_RECORD(CurrentEntry, KAPC, ApcListEntry); - Apc->Inserted = FALSE; - CurrentEntry = CurrentEntry->Flink; - } while (CurrentEntry != FirstEntry); + /* mark all apcs as not-inserted */ + LIST_FOR_EACH(Apc, &Thread->ApcState.ApcListHead[PreviousMode], KAPC, ApcListEntry) { + Apc->Inserted = FALSE; } - + + if (!IsListEmpty(&Thread->ApcState.ApcListHead[PreviousMode])) { + FirstEntry = Thread->ApcState.ApcListHead[PreviousMode].Flink; + /* unlink list head from the rest of the list */ + RemoveEntryList(&Thread->ApcState.ApcListHead[PreviousMode]); + } + /* Release the locks */ KeReleaseSpinLockFromDpcLevel(&Thread->ApcQueueLock); KeReleaseDispatcherDatabaseLock(OldIrql); diff --git a/reactos/ntoskrnl/ke/bug.c b/reactos/ntoskrnl/ke/bug.c index 2e4994c9cf7..97a8456cba5 100644 --- a/reactos/ntoskrnl/ke/bug.c +++ b/reactos/ntoskrnl/ke/bug.c @@ -252,7 +252,6 @@ KiDoBugCheckCallbacks(VOID) { PKBUGCHECK_CALLBACK_RECORD CurrentRecord; PLIST_ENTRY ListHead; - PLIST_ENTRY NextEntry; /* FIXME: Check Checksum and add support for WithReason Callbacks */ @@ -261,14 +260,8 @@ KiDoBugCheckCallbacks(VOID) if (ListHead->Flink && ListHead->Blink) { /* Loop the list */ - NextEntry = ListHead->Flink; - while (NextEntry != ListHead) { - - /* Get the Callback Record */ - CurrentRecord = CONTAINING_RECORD(NextEntry, - KBUGCHECK_CALLBACK_RECORD, - Entry); - + LIST_FOR_EACH(CurrentRecord, ListHead, KBUGCHECK_CALLBACK_RECORD, Entry) + { /* Make sure it's inserted */ if (CurrentRecord->State == BufferInserted) { @@ -278,9 +271,6 @@ KiDoBugCheckCallbacks(VOID) CurrentRecord->Length); CurrentRecord->State = BufferFinished; } - - /* Move to next Entry */ - NextEntry = NextEntry->Flink; } } } @@ -297,7 +287,6 @@ KeBugCheckWithTf(ULONG BugCheckCode, KIRQL OldIrql; BOOLEAN GotExtendedCrashInfo = FALSE; PVOID Address = 0; - PLIST_ENTRY CurrentEntry; PLDR_DATA_TABLE_ENTRY CurrentModule = NULL; extern LIST_ENTRY ModuleListHead; #if 0 @@ -322,14 +311,8 @@ KeBugCheckWithTf(ULONG BugCheckCode, Address = (PVOID)Tf->Eip; /* Try to get information on the module */ - CurrentEntry = ModuleListHead.Flink; - while (CurrentEntry != &ModuleListHead) + LIST_FOR_EACH(CurrentModule, &ModuleListHead, LDR_DATA_TABLE_ENTRY, InLoadOrderModuleList) { - /* Get the current Section */ - CurrentModule = CONTAINING_RECORD(CurrentEntry, - LDR_DATA_TABLE_ENTRY, - InLoadOrderModuleList); - /* Check if this is the right one */ if ((Address != NULL && (Address >= (PVOID)CurrentModule->DllBase && Address < (PVOID)((ULONG_PTR)CurrentModule->DllBase + CurrentModule->SizeOfImage)))) @@ -338,9 +321,6 @@ KeBugCheckWithTf(ULONG BugCheckCode, GotExtendedCrashInfo = TRUE; break; } - - /* Loop again */ - CurrentEntry = CurrentEntry->Flink; } } diff --git a/reactos/ntoskrnl/ke/kqueue.c b/reactos/ntoskrnl/ke/kqueue.c index 168f465828c..954f3811a09 100644 --- a/reactos/ntoskrnl/ke/kqueue.c +++ b/reactos/ntoskrnl/ke/kqueue.c @@ -118,9 +118,9 @@ KeInsertByKeyDeviceQueue(IN PKDEVICE_QUEUE DeviceQueue, /* Insert new entry after the last entry with SortKey less or equal to passed-in SortKey */ InsertAscendingListFIFO(&DeviceQueue->DeviceListHead, + DeviceQueueEntry, KDEVICE_QUEUE_ENTRY, DeviceListEntry, - DeviceQueueEntry, SortKey); Inserted = TRUE; } @@ -204,34 +204,25 @@ KeRemoveByKeyDeviceQueue (IN PKDEVICE_QUEUE DeviceQueue, } else { /* Find entry with SortKey greater than or equal to the passed-in SortKey */ - ListEntry = DeviceQueue->DeviceListHead.Flink; - while (ListEntry != &DeviceQueue->DeviceListHead) { - - /* Get Entry */ - ReturnEntry = CONTAINING_RECORD(ListEntry, - KDEVICE_QUEUE_ENTRY, - DeviceListEntry); - + LIST_FOR_EACH(ReturnEntry, &DeviceQueue->DeviceListHead, KDEVICE_QUEUE_ENTRY, DeviceListEntry) + { /* Check if keys match */ - if (ReturnEntry->SortKey >= SortKey) break; - - /* Move to next item */ - ListEntry = ListEntry->Flink; + if (ReturnEntry->SortKey >= SortKey) { + /* We found it, so just remove it */ + RemoveEntryList(&ReturnEntry->DeviceListEntry); + break; + } } /* Check if we found something */ - if (ListEntry == &DeviceQueue->DeviceListHead) { + if (!ReturnEntry) { /* Not found, return the first entry */ ListEntry = RemoveHeadList(&DeviceQueue->DeviceListHead); ReturnEntry = CONTAINING_RECORD(ListEntry, KDEVICE_QUEUE_ENTRY, DeviceListEntry); - } else { - - /* We found it, so just remove it */ - RemoveEntryList(&ReturnEntry->DeviceListEntry); - } + } /* Set it as non-inserted */ ReturnEntry->Inserted = FALSE; diff --git a/reactos/ntoskrnl/ke/kthread.c b/reactos/ntoskrnl/ke/kthread.c index bbb96d8992b..57e25846e55 100644 --- a/reactos/ntoskrnl/ke/kthread.c +++ b/reactos/ntoskrnl/ke/kthread.c @@ -76,7 +76,6 @@ PKTHREAD KiScanThreadList(KPRIORITY Priority, KAFFINITY Affinity) { - PLIST_ENTRY current_entry; PKTHREAD current; ULONG Mask; @@ -84,11 +83,7 @@ KiScanThreadList(KPRIORITY Priority, if (PriorityListMask & Mask) { - current_entry = PriorityListHead[Priority].Flink; - - while (current_entry != &PriorityListHead[Priority]) { - - current = CONTAINING_RECORD(current_entry, KTHREAD, WaitListEntry); + LIST_FOR_EACH(current, &PriorityListHead[Priority], KTHREAD, WaitListEntry) { if (current->State != Ready) { @@ -102,8 +97,6 @@ KiScanThreadList(KPRIORITY Priority, KiRemoveFromThreadList(current); return(current); } - - current_entry = current_entry->Flink; } } @@ -548,7 +541,6 @@ STDCALL KeFreezeAllThreads(PKPROCESS Process) { KIRQL OldIrql; - PLIST_ENTRY CurrentEntry; PKTHREAD Current; PKTHREAD CurrentThread = KeGetCurrentThread(); @@ -556,12 +548,8 @@ KeFreezeAllThreads(PKPROCESS Process) OldIrql = KeAcquireDispatcherDatabaseLock(); /* Loop the Process's Threads */ - CurrentEntry = Process->ThreadListHead.Flink; - while (CurrentEntry != &Process->ThreadListHead) + LIST_FOR_EACH(Current, &Process->ThreadListHead, KTHREAD, ThreadListEntry) { - /* Get the Thread */ - Current = CONTAINING_RECORD(CurrentEntry, KTHREAD, ThreadListEntry); - /* Make sure it's not ours */ if (Current == CurrentThread) continue; @@ -575,8 +563,6 @@ KeFreezeAllThreads(PKPROCESS Process) Current->SuspendSemaphore.Header.SignalState--; } } - - CurrentEntry = CurrentEntry->Flink; } /* Release the lock */ @@ -1456,8 +1442,8 @@ KiServiceCheck (VOID) if (Thread->ServiceTable != KeServiceDescriptorTableShadow) { /* We do. Initialize it and save the new table */ - PsInitWin32Thread((PETHREAD)Thread); Thread->ServiceTable = KeServiceDescriptorTableShadow; + PsInitWin32Thread((PETHREAD)Thread); } } diff --git a/reactos/ntoskrnl/ke/profile.c b/reactos/ntoskrnl/ke/profile.c index ca79123fe2c..224f5ac7b3d 100644 --- a/reactos/ntoskrnl/ke/profile.c +++ b/reactos/ntoskrnl/ke/profile.c @@ -53,17 +53,15 @@ KeStartProfile(PKPROFILE Profile, { KIRQL OldIrql; PKPROFILE_SOURCE_OBJECT SourceBuffer; - PKPROFILE_SOURCE_OBJECT Source = NULL; PKPROFILE_SOURCE_OBJECT CurrentSource; BOOLEAN FreeBuffer = TRUE; PKPROCESS ProfileProcess; - PLIST_ENTRY ListEntry; /* Allocate a buffer first, before we raise IRQL */ SourceBuffer = ExAllocatePoolWithTag(NonPagedPool, sizeof(KPROFILE_SOURCE_OBJECT), TAG('P', 'r', 'o', 'f')); - RtlZeroMemory(Source, sizeof(KPROFILE_SOURCE_OBJECT)); + RtlZeroMemory(SourceBuffer, sizeof(KPROFILE_SOURCE_OBJECT)); /* Raise to PROFILE_LEVEL */ KeRaiseIrql(PROFILE_LEVEL, &OldIrql); @@ -90,32 +88,23 @@ KeStartProfile(PKPROFILE Profile, } /* Check if this type of profile (source) is already running */ - for (ListEntry = KiProfileSourceListHead.Flink; - ListEntry != &KiProfileSourceListHead; - ListEntry = ListEntry->Flink) { - - /* Get the Source Object */ - CurrentSource = CONTAINING_RECORD(ListEntry, - KPROFILE_SOURCE_OBJECT, - ListEntry); - + LIST_FOR_EACH(CurrentSource, &KiProfileSourceListHead, KPROFILE_SOURCE_OBJECT, ListEntry) + { /* Check if it's the same as the one being requested now */ if (CurrentSource->Source == Profile->Source) { - - Source = CurrentSource; break; } } /* See if the loop found something */ - if (!Source) { + if (!CurrentSource) { /* Nothing found, use our allocated buffer */ - Source = SourceBuffer; + CurrentSource = SourceBuffer; /* Set up the Source Object */ - Source->Source = Profile->Source; - InsertHeadList(&KiProfileSourceListHead, &Source->ListEntry); + CurrentSource->Source = Profile->Source; + InsertHeadList(&KiProfileSourceListHead, &CurrentSource->ListEntry); /* Don't free the pool later on */ FreeBuffer = FALSE; @@ -138,7 +127,6 @@ STDCALL KeStopProfile(PKPROFILE Profile) { KIRQL OldIrql; - PLIST_ENTRY ListEntry; PKPROFILE_SOURCE_OBJECT CurrentSource = NULL; /* Raise to PROFILE_LEVEL and acquire spinlock */ @@ -153,18 +141,15 @@ KeStopProfile(PKPROFILE Profile) Profile->Active = FALSE; /* Find the Source Object */ - for (ListEntry = KiProfileSourceListHead.Flink; - CurrentSource->Source != Profile->Source; - ListEntry = ListEntry->Flink) { - - /* Get the Source Object */ - CurrentSource = CONTAINING_RECORD(ListEntry, - KPROFILE_SOURCE_OBJECT, - ListEntry); + LIST_FOR_EACH(CurrentSource, &KiProfileSourceListHead, KPROFILE_SOURCE_OBJECT, ListEntry) + { + if (CurrentSource->Source == Profile->Source) { + /* Remove it */ + RemoveEntryList(&CurrentSource->ListEntry); + break; + } } - /* Remove it */ - RemoveEntryList(&CurrentSource->ListEntry); } /* Lower IRQL */ @@ -240,14 +225,10 @@ KiParseProfileList(IN PKTRAP_FRAME TrapFrame, { PULONG BucketValue; PKPROFILE Profile; - PLIST_ENTRY NextEntry; /* Loop the List */ - for (NextEntry = ListHead->Flink; NextEntry != ListHead; NextEntry = NextEntry->Flink) { - - /* Get the Current Profile in the List */ - Profile = CONTAINING_RECORD(NextEntry, KPROFILE, ListEntry); - + LIST_FOR_EACH(Profile, ListHead, KPROFILE, ListEntry) + { /* Check if the source is good, and if it's within the range */ if ((Profile->Source != Source) || (TrapFrame->Eip < (ULONG_PTR)Profile->RegionStart) || diff --git a/reactos/ntoskrnl/ke/queue.c b/reactos/ntoskrnl/ke/queue.c index c9a8ad4d312..c5ac25ef006 100644 --- a/reactos/ntoskrnl/ke/queue.c +++ b/reactos/ntoskrnl/ke/queue.c @@ -182,17 +182,14 @@ KeRemoveQueue(IN PKQUEUE Queue, /* Loop until the queue is processed */ while (TRUE) { - /* Get the Entry */ - ListEntry = Queue->EntryListHead.Flink; - /* Check if the counts are valid and if there is still a queued entry */ if ((Queue->CurrentCount < Queue->MaximumCount) && - (ListEntry != &Queue->EntryListHead)) { + !IsListEmpty(&Queue->EntryListHead)) { /* Remove the Entry and Save it */ DPRINT("Removing Queue Entry. CurrentCount: %d, Maximum Count: %d\n", Queue->CurrentCount, Queue->MaximumCount); - ListEntry = RemoveHeadList(&Queue->EntryListHead); + ListEntry = Queue->EntryListHead.Flink; /* Decrease the number of entries */ Queue->Header.SignalState--; @@ -328,7 +325,7 @@ STDCALL KeRundownQueue(IN PKQUEUE Queue) { PLIST_ENTRY EnumEntry; - PLIST_ENTRY FirstEntry; + PLIST_ENTRY FirstEntry = NULL; PKTHREAD Thread; KIRQL OldIrql; @@ -337,19 +334,11 @@ KeRundownQueue(IN PKQUEUE Queue) /* Get the Dispatcher Lock */ OldIrql = KeAcquireDispatcherDatabaseLock(); - /* Get the First Empty Entry */ - FirstEntry = Queue->EntryListHead.Flink; - /* Make sure the list is not empty */ - if (FirstEntry == &Queue->EntryListHead) { - - /* It is, so don't return anything */ - FirstEntry = NULL; - - } else { - + if (!IsListEmpty(&Queue->EntryListHead)) + { /* Remove it */ - RemoveEntryList(&Queue->EntryListHead); + FirstEntry = RemoveHeadList(&Queue->EntryListHead); } /* Unlink threads and clear their Thread->Queue */ diff --git a/reactos/ntoskrnl/ke/timer.c b/reactos/ntoskrnl/ke/timer.c index 1867049e3dc..d5ceba267ff 100644 --- a/reactos/ntoskrnl/ke/timer.c +++ b/reactos/ntoskrnl/ke/timer.c @@ -217,7 +217,7 @@ KiExpireTimers(PKDPC Dpc, PVOID SystemArgument1, PVOID SystemArgument2) { - PKTIMER Timer; + PKTIMER Timer, tmp; ULONGLONG InterruptTime; LIST_ENTRY ExpiredTimerList; PLIST_ENTRY CurrentEntry = NULL; @@ -235,25 +235,22 @@ KiExpireTimers(PKDPC Dpc, InterruptTime = KeQueryInterruptTime(); /* Loop through the Timer List and remove Expired Timers. Insert them into the Expired Listhead */ - CurrentEntry = KiTimerListHead.Flink; - while (CurrentEntry != &KiTimerListHead) { - - /* Get the Current Timer */ - Timer = CONTAINING_RECORD(CurrentEntry, KTIMER, TimerListEntry); + LIST_FOR_EACH_SAFE(Timer, tmp, &KiTimerListHead, KTIMER, TimerListEntry) + { DPRINT("Looping for Timer: %x. Duetime: %I64d. InterruptTime %I64d \n", Timer, Timer->DueTime.QuadPart, InterruptTime); /* Check if we have to Expire it */ if (InterruptTime < Timer->DueTime.QuadPart) break; - CurrentEntry = CurrentEntry->Flink; - /* Remove it from the Timer List, add it to the Expired List */ RemoveEntryList(&Timer->TimerListEntry); InsertTailList(&ExpiredTimerList, &Timer->TimerListEntry); } /* Expire the Timers */ - while ((CurrentEntry = RemoveHeadList(&ExpiredTimerList)) != &ExpiredTimerList) { + while (!IsListEmpty(&ExpiredTimerList)) { + + CurrentEntry = RemoveHeadList(&ExpiredTimerList); /* Get the Timer */ Timer = CONTAINING_RECORD(CurrentEntry, KTIMER, TimerListEntry); @@ -373,9 +370,9 @@ KiInsertTimer(PKTIMER Timer, /* Now insert it into the Timer List */ DPRINT("Inserting Timer into list\n"); InsertAscendingList(&KiTimerListHead, + Timer, KTIMER, TimerListEntry, - Timer, DueTime.QuadPart); return TRUE; diff --git a/reactos/subsys/system/usetup/partlist.c b/reactos/subsys/system/usetup/partlist.c index cff9710b683..74192445197 100644 --- a/reactos/subsys/system/usetup/partlist.c +++ b/reactos/subsys/system/usetup/partlist.c @@ -838,7 +838,7 @@ AddDiskToList (HANDLE FileHandle, GetDriverName (DiskEntry); - InsertAscendingList(&List->DiskListHead, DISKENTRY, ListEntry, DiskEntry, BiosDiskNumber); + InsertAscendingList(&List->DiskListHead, DiskEntry, DISKENTRY, ListEntry, BiosDiskNumber); LayoutBuffer = (DRIVE_LAYOUT_INFORMATION*)RtlAllocateHeap (ProcessHeap, 0, @@ -1225,7 +1225,6 @@ PrintDiskData (PPARTLIST List, PDISKENTRY DiskEntry) { PPARTENTRY PartEntry; - PLIST_ENTRY Entry; CHAR LineBuffer[128]; COORD coPos; ULONG Written; @@ -1305,17 +1304,12 @@ PrintDiskData (PPARTLIST List, PrintEmptyLine (List); /* Print partition lines*/ - Entry = DiskEntry->PartListHead.Flink; - while (Entry != &DiskEntry->PartListHead) + LIST_FOR_EACH(PartEntry, &DiskEntry->PartListHead, PARTENTRY, ListEntry) { - PartEntry = CONTAINING_RECORD (Entry, PARTENTRY, ListEntry); - /* Print disk entry */ PrintPartitionData (List, DiskEntry, PartEntry); - - Entry = Entry->Flink; } /* Print separator line */ @@ -1507,16 +1501,11 @@ DrawPartitionList (PPARTLIST List) /* print list entries */ List->Line = - List->Offset; - Entry = List->DiskListHead.Flink; - while (Entry != &List->DiskListHead) + LIST_FOR_EACH(DiskEntry, &List->DiskListHead, DISKENTRY, ListEntry) { - DiskEntry = CONTAINING_RECORD (Entry, DISKENTRY, ListEntry); - /* Print disk entry */ PrintDiskData (List, DiskEntry); - - Entry = Entry->Flink; } } diff --git a/reactos/subsys/win32k/ntuser/class.c b/reactos/subsys/win32k/ntuser/class.c index 78fc81b81dc..16533db23fa 100644 --- a/reactos/subsys/win32k/ntuser/class.c +++ b/reactos/subsys/win32k/ntuser/class.c @@ -615,16 +615,6 @@ NtUserUnregisterClass( RETURN(FALSE); } -/* this was probably ment to prevent sysclass dereg -Seems wrong. Any class can have NULL hInst, not only sysclasses - -*/ -// if (Class->hInstance && Class->hInstance != hInstance) -// { -// SetLastWin32Error(ERROR_CLASS_DOES_NOT_EXIST); -// RETURN( FALSE); -// } - if (Class->refs) { /* NOTE: the class will not be freed when its refs become 0 ie. no more