diff --git a/kernel/acpi/functions.c b/kernel/acpi/functions.c index 6faea57cc..df0259d72 100644 --- a/kernel/acpi/functions.c +++ b/kernel/acpi/functions.c @@ -474,11 +474,9 @@ AcpiDeviceIrqRoutingCallback( ACPI_RESOURCE *Resource, void *Context) { - // Variables - IrqResource_t *IrqResource = NULL; - DataKey_t pKey; + IrqResource_t* IrqResource = NULL; + DataKey_t Key = { 0 }; - // Debug TRACE("AcpiDeviceIrqRoutingCallback(Type %u)", Resource->Type); // Sanitize the type of resource @@ -488,7 +486,6 @@ AcpiDeviceIrqRoutingCallback( // Initiate values IrqResource = (IrqResource_t*)Context; - pKey.Value = 0; // Right now we are just looking for Irq's if (Resource->Type == ACPI_RESOURCE_TYPE_IRQ) { @@ -532,7 +529,7 @@ AcpiDeviceIrqRoutingCallback( } else { // Append to list of irqs - CollectionAppend(IrqResource->IrqList, CollectionCreateNode(pKey, RoutingEntry)); + CollectionAppend(IrqResource->IrqList, CollectionCreateNode(Key, RoutingEntry)); } } } @@ -577,7 +574,7 @@ AcpiDeviceIrqRoutingCallback( } else { // Append to list of irqs - CollectionAppend(IrqResource->IrqList, CollectionCreateNode(pKey, RoutingEntry)); + CollectionAppend(IrqResource->IrqList, CollectionCreateNode(Key, RoutingEntry)); } } } @@ -741,15 +738,12 @@ ACPI_STATUS AcpiDeviceGetIrqRoutings( _In_ AcpiDevice_t *Device) { - // Variables - ACPI_PCI_ROUTING_TABLE *PciTable = NULL; - PciRoutings_t *Table = NULL; - ACPI_STATUS Status; - int i; - - // Buffers - IrqResource_t IrqResource; - ACPI_BUFFER aBuff; + ACPI_PCI_ROUTING_TABLE* PciTable = NULL; + PciRoutings_t* Table = NULL; + ACPI_STATUS Status; + IrqResource_t IrqResource; + ACPI_BUFFER aBuff; + int i; // Debug TRACE("AcpiDeviceGetIrqRoutings()"); @@ -787,8 +781,8 @@ AcpiDeviceGetIrqRoutings( ACPI_HANDLE SourceHandle = NULL; unsigned InterruptIndex = 0; unsigned DeviceIndex = 0; - CollectionItem_t *Node = NULL; - DataKey_t Key; + CollectionItem_t* Node = NULL; + DataKey_t Key = { 0 }; // Convert the addresses DeviceIndex = (unsigned)((PciTable->Address >> 16) & 0xFFFF); @@ -806,12 +800,11 @@ AcpiDeviceGetIrqRoutings( // Allocate a new entry and store information RoutingEntry = (PciRoutingEntry_t*)kmalloc(sizeof(PciRoutingEntry_t)); - RoutingEntry->AcType = ACPI_RESOURCE_TYPE_IRQ; - RoutingEntry->Irq = (int)PciTable->SourceIndex; - RoutingEntry->Polarity = ACPI_ACTIVE_LOW; - RoutingEntry->Trigger = ACPI_LEVEL_SENSITIVE; - RoutingEntry->Fixed = 1; - Key.Value = 0; + RoutingEntry->AcType = ACPI_RESOURCE_TYPE_IRQ; + RoutingEntry->Irq = (int)PciTable->SourceIndex; + RoutingEntry->Polarity = ACPI_ACTIVE_LOW; + RoutingEntry->Trigger = ACPI_LEVEL_SENSITIVE; + RoutingEntry->Fixed = 1; // Save interrupt CollectionAppend(Table->InterruptEntries[InterruptIndex], @@ -822,7 +815,8 @@ AcpiDeviceGetIrqRoutings( // Ok, so we have a valid handle, lets see if we already have // the handle cached in memory - Key.String = &PciTable->Source[0]; + Key.Value.String.Pointer = &PciTable->Source[0]; + Key.Value.String.Length = strlen(&PciTable->Source[0]); Node = CollectionGetNodeByKey(Table->Sources, Key, 0); if (Node != NULL) { Source = (PciRoutingSource_t*)Node->Data; diff --git a/kernel/acpi/interrupts.c b/kernel/acpi/interrupts.c index 0bb51d898..912222b0b 100644 --- a/kernel/acpi/interrupts.c +++ b/kernel/acpi/interrupts.c @@ -104,13 +104,8 @@ AcpiDeriveInterrupt( _In_ int Pin, _Out_ Flags_t* AcpiConform) { - // Variables - AcpiDevice_t *Dev = NULL; - DataKey_t iKey; - - // Calculate routing index - unsigned rIndex = (Device * 4) + (Pin - 1); - iKey.Value = 0; + AcpiDevice_t* Dev; + unsigned rIndex = (Device * 4) + (Pin - 1); // Trace TRACE("AcpiDeriveInterrupt(Bus %u, Device %u, Pin %i)", Bus, Device, Pin); diff --git a/kernel/acpi/scan.c b/kernel/acpi/scan.c index 439a64c90..8d66c7262 100644 --- a/kernel/acpi/scan.c +++ b/kernel/acpi/scan.c @@ -39,15 +39,13 @@ AcpiDevice_t* AcpiDeviceLookupBusRoutings( _In_ int Bus) { - // Variables - AcpiDevice_t *Dev = NULL; - DataKey_t Key; - int Index = 0; + AcpiDevice_t* Dev; + DataKey_t Key = { .Value.Integer = ACPI_BUS_ROOT_BRIDGE }; + int Index = 0; // Loop through buses while (1) { - Key.Value = ACPI_BUS_ROOT_BRIDGE; - Dev = (AcpiDevice_t*)CollectionGetNodeByKey(&PciToAcpiDevices, Key, Index); + Dev = (AcpiDevice_t*)CollectionGetNodeByKey(&PciToAcpiDevices, Key, Index); if (Dev == NULL) { break; } @@ -214,7 +212,7 @@ AcpiDeviceCreate( } // Add the device to device-list - Device->Header.Key.Value = Device->Type; + Device->Header.Key.Value.Integer = Device->Type; return CollectionAppend(&PciToAcpiDevices, &Device->Header); } diff --git a/kernel/arch/x86/x32/portio.s b/kernel/arch/x86/x32/portio.s index 6af161e40..021bd5523 100644 --- a/kernel/arch/x86/x32/portio.s +++ b/kernel/arch/x86/x32/portio.s @@ -33,145 +33,56 @@ global _outl ; uint8_t inb(uint16_t port) ; Recieves a byte from a port _inb: - ; Setup frame - push ebp - mov ebp, esp - - ; Save stuff - push edx - - ; Get byte - xor al, al - xor edx, edx - mov dx, [ebp + 8] - in al, dx - - ; Restore - pop edx - - ; Leave frame - pop ebp - ret + xor eax, eax + xor edx, edx + mov dx, word [esp + 4] + in al, dx + ret ; uint16_t inw(uint16_t port) ; Recieves a word from a port _inw: - ; Setup frame - push ebp - mov ebp, esp - - ; Save stuff - push edx - - ; Get word - xor ax, ax - xor edx, edx - mov dx, [ebp + 8] - in ax, dx - - ; Restore - pop edx - - ; Leave - pop ebp - ret + xor eax, eax + xor edx, edx + mov dx, word [esp + 4] + in ax, dx + ret ; uint32_t inl(uint16_t port) ; Recieves a long from a port _inl: - ; Setup frame - push ebp - mov ebp, esp - - ; Save stuff - push edx - - ; Get dword - xor eax, eax - xor edx, edx - mov dx, [ebp + 8] - in eax, dx - - ; Restore - pop edx - - ; Leave - pop ebp - ret + xor eax, eax + xor edx, edx + mov dx, word [esp + 4] + in eax, dx + ret ; void outb(uint16_t port, uint8_t data) ; Sends a byte to a port _outb: - ; Setup frame - push ebp - mov ebp, esp - - ; Save stuff - push eax - push edx - - ; Get data - xor eax, eax - xor edx, edx - mov dx, [ebp + 8] - mov al, [ebp + 12] - out dx, al - - ; Restore - pop edx - pop eax - - ; Leave - pop ebp - ret + xor eax, eax + xor edx, edx + mov dx, word [esp + 4] + mov al, byte [esp + 8] + out dx, al + ret ; void outw(uint16_t port, uint16_t data) ; Sends a word to a port _outw: - ; Stack Frame - push ebp - mov ebp, esp - - ; Save stuff - push eax - push edx - - ; Get data - xor eax, eax - xor edx, edx - mov dx, [ebp + 8] - mov ax, [ebp + 12] - out dx, ax - - ; Restore - pop edx - pop eax - - ; Release stack frame - pop ebp - ret + xor eax, eax + xor edx, edx + mov dx, word [esp + 4] + mov ax, word [esp + 8] + out dx, ax + ret ; void outl(uint16_t port, uint32_t data) ; Sends a long to a port _outl: - ; Stack Frame - push ebp - mov ebp, esp - - ; Save stuff - push eax - push edx - - ; Get data - xor edx, edx - mov dx, [ebp + 8] - mov eax, [ebp + 12] - out dx, eax - - ; Restore - pop edx - pop eax - - ; Release stack frame - pop ebp - ret \ No newline at end of file + xor eax, eax + xor edx, edx + mov dx, word [esp + 4] + mov eax, dword [esp + 8] + out dx, eax + ret \ No newline at end of file diff --git a/kernel/deviceio.c b/kernel/deviceio.c index 15d5066de..5548063bd 100644 --- a/kernel/deviceio.c +++ b/kernel/deviceio.c @@ -33,7 +33,7 @@ #include #include -static Collection_t IoSpaces = COLLECTION_INIT(KeyInteger); +static Collection_t IoSpaces = COLLECTION_INIT(KeyId); static _Atomic(UUId_t) IoSpaceIdGenerator = 1; /* RegisterSystemDeviceIo @@ -51,13 +51,13 @@ RegisterSystemDeviceIo( // Allocate a new system only copy of the io-space // as we don't want anyone to edit our copy - SystemIo = (SystemDeviceIo_t*)kmalloc(sizeof(SystemDeviceIo_t)); + SystemIo = (SystemDeviceIo_t*)kmalloc(sizeof(SystemDeviceIo_t)); memset(SystemIo, 0, sizeof(SystemDeviceIo_t)); SystemIo->Owner = UUID_INVALID; - IoSpace->Id = atomic_fetch_add(&IoSpaceIdGenerator, 1); + IoSpace->Id = atomic_fetch_add(&IoSpaceIdGenerator, 1); memcpy(&SystemIo->Io, IoSpace, sizeof(DeviceIo_t)); - SystemIo->Header.Key.Value = (int)IoSpace->Id; + SystemIo->Header.Key.Value.Id = IoSpace->Id; return CollectionAppend(&IoSpaces, &SystemIo->Header); } @@ -75,8 +75,8 @@ DestroySystemDeviceIo( TRACE("DestroySystemDeviceIo(Id %u)", IoSpace); // Lookup the system copy to validate - Key.Value = (int)IoSpace; - SystemIo = (SystemDeviceIo_t*)CollectionGetNodeByKey(&IoSpaces, Key, 0); + Key.Value.Id = IoSpace->Id; + SystemIo = (SystemDeviceIo_t*)CollectionGetNodeByKey(&IoSpaces, Key, 0); if (SystemIo == NULL || SystemIo->Owner != UUID_INVALID) { return OsError; } @@ -101,8 +101,8 @@ AcquireSystemDeviceIo( TRACE("AcquireSystemDeviceIo(Id %u)", IoSpace->Id); // Lookup the system copy to validate this requested operation - Key.Value = (int)IoSpace->Id; - SystemIo = (SystemDeviceIo_t*)CollectionGetNodeByKey(&IoSpaces, Key, 0); + Key.Value.Id = IoSpace->Id; + SystemIo = (SystemDeviceIo_t*)CollectionGetNodeByKey(&IoSpaces, Key, 0); // Sanitize the system copy if (Service == NULL || Service->Type != ProcessService || @@ -129,9 +129,9 @@ AcquireSystemDeviceIo( } // Adjust for offset and store in io copies - MappedAddress += BaseAddress % PageSize; - IoSpace->Access.Memory.VirtualBase = MappedAddress; - SystemIo->MappedAddress = MappedAddress; + MappedAddress += BaseAddress % PageSize; + IoSpace->Access.Memory.VirtualBase = MappedAddress; + SystemIo->MappedAddress = MappedAddress; return OsSuccess; } break; @@ -167,8 +167,8 @@ ReleaseSystemDeviceIo( TRACE("ReleaseSystemDeviceIo(Id %u)", IoSpace->Id); // Lookup the system copy to validate this requested operation - Key.Value = (int)IoSpace->Id; - SystemIo = (SystemDeviceIo_t*)CollectionGetNodeByKey(&IoSpaces, Key, 0); + Key.Value.Id = IoSpace->Id; + SystemIo = (SystemDeviceIo_t*)CollectionGetNodeByKey(&IoSpaces, Key, 0); // Sanitize the system copy and do some security checks if (Service == NULL || Service->Type != ProcessService || @@ -216,8 +216,8 @@ CreateKernelSystemDeviceIo( SystemDeviceIo_t* SystemIo; DataKey_t Key; - Key.Value = (int)SourceIoSpace->Id; - SystemIo = (SystemDeviceIo_t*)CollectionGetNodeByKey(&IoSpaces, Key, 0); + Key.Value.Id = SourceIoSpace->Id; + SystemIo = (SystemDeviceIo_t*)CollectionGetNodeByKey(&IoSpaces, Key, 0); if (SystemIo == NULL) { return OsError; } @@ -253,8 +253,8 @@ ReleaseKernelSystemDeviceIo( SystemDeviceIo_t* SystemIo; DataKey_t Key; - Key.Value = (int)SystemIoSpace->Id; - SystemIo = (SystemDeviceIo_t*)CollectionGetNodeByKey(&IoSpaces, Key, 0); + Key.Value.Id = SystemIoSpace->Id; + SystemIo = (SystemDeviceIo_t*)CollectionGetNodeByKey(&IoSpaces, Key, 0); if (SystemIo == NULL) { return OsError; } diff --git a/kernel/garbagecollector.c b/kernel/garbagecollector.c index 7b50316be..037ba4be5 100644 --- a/kernel/garbagecollector.c +++ b/kernel/garbagecollector.c @@ -36,8 +36,8 @@ void GcWorker(void *Args); /* Globals * Needed for state-keeping */ static SlimSemaphore_t GlbGcEventLock; -static Collection_t GcHandlers = COLLECTION_INIT(KeyInteger); -static Collection_t GcEvents = COLLECTION_INIT(KeyInteger); +static Collection_t GcHandlers = COLLECTION_INIT(KeyId); +static Collection_t GcEvents = COLLECTION_INIT(KeyId); static _Atomic(UUId_t) GcIdGenerator= ATOMIC_VAR_INIT(0); /* GcConstruct @@ -68,9 +68,9 @@ GcRegister( _In_ GcHandler_t Handler) { DataKey_t Key; - Key.Value = (int)atomic_fetch_add(&GcIdGenerator, 1); + Key.Value.Id = atomic_fetch_add(&GcIdGenerator, 1); CollectionAppend(&GcHandlers, CollectionCreateNode(Key, (void*)Handler)); - return (UUId_t)Key.Value; + return Key.Value.Id; } /* GcUnregister @@ -80,7 +80,7 @@ GcUnregister( _In_ UUId_t Handler) { DataKey_t Key; - Key.Value = (int)Handler; + Key.Value.Id = Handler; if (CollectionGetDataByKey(&GcHandlers, Key, 0) == NULL) { return OsError; } @@ -95,7 +95,7 @@ GcSignal( _In_ void* Data) { DataKey_t Key; - Key.Value = (int)Handler; + Key.Value.Id = Handler; // Sanitize the status of the gc if (CollectionGetDataByKey(&GcHandlers, Key, 0) == NULL) { diff --git a/kernel/handle.c b/kernel/handle.c index 653fb21b3..48eeb4586 100644 --- a/kernel/handle.c +++ b/kernel/handle.c @@ -35,7 +35,7 @@ #include #include -static Collection_t SystemHandles = COLLECTION_INIT(KeyInteger); +static Collection_t SystemHandles = COLLECTION_INIT(KeyId); static _Atomic(UUId_t) IdGenerator = 1; static HandleDestructorFn HandleDestructors[HandleTypeCount] = { DestroyMemoryBuffer, @@ -60,7 +60,7 @@ CreateHandle( Id = atomic_fetch_add(&IdGenerator, 1); memset((void*)Handle, 0, sizeof(SystemHandle_t)); - Handle->Header.Key.Value = (int)Id; + Handle->Header.Key.Value.Id = Id; Handle->Type = Type; Handle->Capabilities = Capabilities; Handle->Resource = Resource; @@ -81,8 +81,8 @@ AcquireHandle( int PreviousReferences; // Lookup the handle - Key.Value = (int)Handle; - Instance = (SystemHandle_t*)CollectionGetNodeByKey(&SystemHandles, Key, 0); + Key.Value.Id = Handle; + Instance = (SystemHandle_t*)CollectionGetNodeByKey(&SystemHandles, Key, 0); if (Instance == NULL) { return NULL; } @@ -107,8 +107,8 @@ LookupHandle( DataKey_t Key; // Lookup the handle - Key.Value = (int)Handle; - Instance = (SystemHandle_t*)CollectionGetNodeByKey(&SystemHandles, Key, 0); + Key.Value.Id = Handle; + Instance = (SystemHandle_t*)CollectionGetNodeByKey(&SystemHandles, Key, 0); if (Instance == NULL) { return NULL; } @@ -128,8 +128,8 @@ DestroyHandle( int References; // Lookup the handle - Key.Value = (int)Handle; - Instance = (SystemHandle_t*)CollectionGetNodeByKey(&SystemHandles, Key, 0); + Key.Value.Id = Handle; + Instance = (SystemHandle_t*)CollectionGetNodeByKey(&SystemHandles, Key, 0); if (Instance == NULL) { return OsError; } diff --git a/kernel/include/revision.h b/kernel/include/revision.h index 3ca46f320..84ebe5724 100644 --- a/kernel/include/revision.h +++ b/kernel/include/revision.h @@ -4,11 +4,11 @@ #define _REVISION_H_ #define BUILD_DATE "01 November 2018" -#define BUILD_TIME "12:53:08" +#define BUILD_TIME "15:10:49" #define BUILD_SYSTEM "clang" #define REVISION_MAJOR 0 #define REVISION_MINOR 4 -#define REVISION_BUILD 9241 +#define REVISION_BUILD 9251 #endif //!_REVISION_H_ diff --git a/kernel/modules.c b/kernel/modules.c index 031eb4824..04bf8ec14 100644 --- a/kernel/modules.c +++ b/kernel/modules.c @@ -33,8 +33,8 @@ #define LIST_MODULE 1 #define LIST_SERVER 2 -Collection_t *GlbModules = NULL; -int GlbModulesInitialized = 0; +static Collection_t Modules = COLLECTION_INIT(KeyInteger); +static int ModulesAvailable = 0; /* ModulesInitialize * Loads the ramdisk, iterates all headers and @@ -44,21 +44,18 @@ OsStatus_t ModulesInitialize( _In_ Multiboot_t *BootInformation) { - // Variables - MCoreRamDiskHeader_t *Ramdisk = NULL; - MCoreRamDiskEntry_t *Entry = NULL; - int Counter = 0; + MCoreRamDiskHeader_t* Ramdisk; + MCoreRamDiskEntry_t* Entry; + int Counter = 0; // Debug TRACE("ModulesInitialize(Address 0x%x, Size 0x%x)", BootInformation->RamdiskAddress, BootInformation->RamdiskSize); // Sanitize the boot-parameters - // We will consider the possiblity of - // 0 values to be there is no ramdisk - if (BootInformation == NULL - || BootInformation->RamdiskAddress == 0 - || BootInformation->RamdiskSize == 0) { + // We will consider the possiblity of 0 values to be there is no ramdisk + if (BootInformation == NULL || BootInformation->RamdiskAddress == 0 || + BootInformation->RamdiskSize == 0) { TRACE("No ramdisk supplied by the boot-descriptor"); return OsSuccess; } @@ -74,10 +71,6 @@ ModulesInitialize( return OsError; } - // Initialize the list of modules - // and servers so we can add later :-) - GlbModules = CollectionCreate(KeyInteger); - // Store filecount so we can iterate Entry = (MCoreRamDiskEntry_t*) (BootInformation->RamdiskAddress + sizeof(MCoreRamDiskHeader_t)); @@ -89,10 +82,10 @@ ModulesInitialize( if (Entry->Type == RAMDISK_MODULE || Entry->Type == RAMDISK_FILE) { MCoreRamDiskModuleHeader_t *Header = (MCoreRamDiskModuleHeader_t*)(uintptr_t)(BootInformation->RamdiskAddress + Entry->DataHeaderOffset); - MCoreModule_t *Module = NULL; - uint8_t *ModuleData = NULL; - uint32_t CrcOfData = 0; - DataKey_t Key; + MCoreModule_t* Module; + uint8_t* ModuleData; + uint32_t CrcOfData; + DataKey_t Key; // Perform CRC validation ModuleData = (uint8_t*)(BootInformation->RamdiskAddress @@ -113,12 +106,12 @@ ModulesInitialize( // Update key based on the type of module // either its a server or a driver if (Header->Flags & RAMDISK_MODULE_SERVER) { - Key.Value = LIST_SERVER; + Key.Value.Integer = LIST_SERVER; } else { - Key.Value = LIST_MODULE; + Key.Value.Integer = LIST_MODULE; } - CollectionAppend(GlbModules, CollectionCreateNode(Key, Module)); + CollectionAppend(&Modules, CollectionCreateNode(Key, Module)); } else { WARNING("Unknown entry type: %u", Entry->Type); @@ -128,11 +121,9 @@ ModulesInitialize( } // Debug - TRACE("Found %i Modules and Servers", CollectionLength(GlbModules)); - - // All is fine - GlbModulesInitialized = 1; - return CollectionLength(GlbModules) == 0 ? OsError : OsSuccess; + TRACE("Found %i Modules and Servers", CollectionLength(&Modules)); + ModulesAvailable = 1; + return CollectionLength(&Modules) == 0 ? OsError : OsSuccess; } /* ModulesRunServers @@ -142,7 +133,7 @@ void ModulesRunServers(void) { IntStatus_t IrqState = 0; - if (GlbModulesInitialized != 1) { + if (ModulesAvailable != 1) { return; } @@ -153,8 +144,8 @@ ModulesRunServers(void) // Iterate module list and spawn all servers // then they will "run" the system for us - foreach(sNode, GlbModules) { - if (sNode->Key.Value == LIST_SERVER) { + foreach(sNode, &Modules) { + if (sNode->Key.Value.Integer == LIST_SERVER) { MString_t *Path = MStringCreate("rd:/", StrUTF8); MStringAppendString(Path, ((MCoreModule_t*)sNode->Data)->Name); CreateService(Path, 0, 0, 0, 0); @@ -173,16 +164,12 @@ ModulesQueryPath( _Out_ void **Buffer, _Out_ size_t *Length) { - // Variables MString_t *Token = Path; OsStatus_t Result = OsError; int Index = -1; - - // Debug TRACE("ModulesQueryPath(%s)", MStringRaw(Path)); - // Sanitize status - if (GlbModulesInitialized != 1) { + if (ModulesAvailable != 1) { goto Exit; } @@ -194,7 +181,7 @@ ModulesQueryPath( TRACE("TokenToSearchFor(%s)", MStringRaw(Token)); // Locate the module - foreach(sNode, GlbModules) { + foreach(sNode, &Modules) { MCoreModule_t *Mod = (MCoreModule_t*)sNode->Data; TRACE("Comparing(%s)To(%s)", MStringRaw(Token), MStringRaw(Mod->Name)); if (MStringCompare(Token, Mod->Name, 1) != MSTRING_NO_MATCH) { @@ -224,13 +211,12 @@ ModulesFindGeneric( _In_ DevInfo_t DeviceType, _In_ DevInfo_t DeviceSubType) { - // Sanitize status - if (GlbModulesInitialized != 1) { + if (ModulesAvailable != 1) { return NULL; } // Locate the module - foreach(sNode, GlbModules) { + foreach(sNode, &Modules) { MCoreModule_t *Mod = (MCoreModule_t*)sNode->Data; if (Mod->Header.DeviceType == DeviceType && Mod->Header.DeviceSubType == DeviceSubType) { @@ -249,8 +235,7 @@ ModulesFindSpecific( _In_ DevInfo_t VendorId, _In_ DevInfo_t DeviceId) { - // Sanitize status - if (GlbModulesInitialized != 1) { + if (ModulesAvailable != 1) { return NULL; } @@ -260,7 +245,7 @@ ModulesFindSpecific( } // Locate the module - foreach(sNode, GlbModules) { + foreach(sNode, &Modules) { MCoreModule_t *Mod = (MCoreModule_t*)sNode->Data; if (Mod->Header.VendorId == VendorId && Mod->Header.DeviceId == DeviceId) { @@ -277,13 +262,12 @@ MCoreModule_t* ModulesFindString( _In_ MString_t *Module) { - // Sanitize status - if (GlbModulesInitialized != 1) { + if (ModulesAvailable != 1) { return NULL; } // Locate the module - foreach(sNode, GlbModules) { + foreach(sNode, &Modules) { MCoreModule_t *Mod = (MCoreModule_t*)sNode->Data; if (MStringCompare(Module, Mod->Name, 1) == MSTRING_FULL_MATCH) { return Mod; diff --git a/kernel/process/loaders/pe.c b/kernel/process/loaders/pe.c index e60f75a9c..933a122af 100644 --- a/kernel/process/loaders/pe.c +++ b/kernel/process/loaders/pe.c @@ -770,8 +770,7 @@ PeLoadImage( // Before loading imports, add us to parent list of libraries // so we might be reused, instead of reloaded if (Parent != NULL) { - DataKey_t Key; - Key.Value = 0; + DataKey_t Key = { 0 }; CollectionAppend(Parent->LoadedLibraries, CollectionCreateNode(Key, PeInfo)); } diff --git a/kernel/process/phoenix_pipes.c b/kernel/process/phoenix_pipes.c index 392da648e..2cf81e48a 100644 --- a/kernel/process/phoenix_pipes.c +++ b/kernel/process/phoenix_pipes.c @@ -55,7 +55,7 @@ CreateProcessPipe( // Make sure that a pipe on the given Port // doesn't already exist! - Key.Value = Port; + Key.Value.Integer = Port; if (CollectionGetDataByKey(Process->Pipes, Key, 0) != NULL) { WARNING("The requested pipe already exists"); return OsSuccess; @@ -88,7 +88,7 @@ WaitForProcessPipe( } // Wait for wake-event on pipe - Key.Value = Port; + Key.Value.Integer = Port; while (Run) { if (CollectionGetDataByKey(Process->Pipes, Key, 0) != NULL) { break; @@ -116,7 +116,7 @@ DestroyProcessPipe( return OsSuccess; } - Key.Value = Port; + Key.Value.Integer = Port; Pipe = (SystemPipe_t*)CollectionGetDataByKey(Process->Pipes, Key, 0); if (Pipe == NULL) { return OsError; @@ -137,6 +137,6 @@ GetProcessPipe( if (Process == NULL || Port < 0) { return NULL; } - Key.Value = Port; + Key.Value.Integer = Port; return (SystemPipe_t*)CollectionGetDataByKey(Process->Pipes, Key, 0); } diff --git a/kernel/process/signal.c b/kernel/process/signal.c index 5fbbc036b..c59cdb6b4 100644 --- a/kernel/process/signal.c +++ b/kernel/process/signal.c @@ -34,44 +34,44 @@ /* Globals * Keep track of signal-consequences */ char GlbSignalIsDeadly[] = { - 0, /* 0? */ - 1, /* SIGHUP */ - 1, /* SIGINT */ - 2, /* SIGQUIT */ - 2, /* SIGILL */ - 2, /* SIGTRAP */ - 2, /* SIGABRT */ - 2, /* SIGEMT */ - 2, /* SIGFPE */ - 1, /* SIGKILL */ - 2, /* SIGBUS */ - 2, /* SIGSEGV */ - 2, /* SIGSYS */ - 1, /* SIGPIPE */ - 1, /* SIGALRM */ - 1, /* SIGTERM */ - 0, /* SIGUSR1 */ - 0, /* SIGUSR2 */ - 0, /* SIGCHLD */ - 0, /* SIGPWR */ - 0, /* SIGWINCH */ - 0, /* SIGURG */ - 0, /* SIGPOLL */ - 3, /* SIGSTOP */ - 3, /* SIGTSTP */ - 0, /* SIGCONT */ - 3, /* SIGTTIN */ - 3, /* SIGTTOUT */ - 1, /* SIGVTALRM */ - 1, /* SIGPROF */ - 2, /* SIGXCPU */ - 2, /* SIGXFSZ */ - 0, /* SIGWAITING */ - 1, /* SIGDIAF */ - 0, /* SIGHATE */ - 0, /* SIGWINEVENT*/ - 0, /* SIGCAT */ - 0 /* SIGEND */ + 0, /* 0? */ + 1, /* SIGHUP */ + 1, /* SIGINT */ + 2, /* SIGQUIT */ + 2, /* SIGILL */ + 2, /* SIGTRAP */ + 2, /* SIGABRT */ + 2, /* SIGEMT */ + 2, /* SIGFPE */ + 1, /* SIGKILL */ + 2, /* SIGBUS */ + 2, /* SIGSEGV */ + 2, /* SIGSYS */ + 1, /* SIGPIPE */ + 1, /* SIGALRM */ + 1, /* SIGTERM */ + 0, /* SIGUSR1 */ + 0, /* SIGUSR2 */ + 0, /* SIGCHLD */ + 0, /* SIGPWR */ + 0, /* SIGWINCH */ + 0, /* SIGURG */ + 0, /* SIGPOLL */ + 3, /* SIGSTOP */ + 3, /* SIGTSTP */ + 0, /* SIGCONT */ + 3, /* SIGTTIN */ + 3, /* SIGTTOUT */ + 1, /* SIGVTALRM */ + 1, /* SIGPROF */ + 2, /* SIGXCPU */ + 2, /* SIGXFSZ */ + 0, /* SIGWAITING */ + 1, /* SIGDIAF */ + 0, /* SIGHATE */ + 0, /* SIGWINEVENT*/ + 0, /* SIGCAT */ + 0 /* SIGEND */ }; /* Create Signal @@ -82,26 +82,26 @@ SignalCreate( _In_ UUId_t ThreadId, _In_ int Signal) { - MCoreThread_t* Target = ThreadingGetThread(ThreadId); - SystemSignal_t* Sig; - DataKey_t sKey; + MCoreThread_t* Target = ThreadingGetThread(ThreadId); + SystemSignal_t* Sig; + DataKey_t Key; TRACE("SignalCreate(Thread %u, Signal %i)", ThreadId, Signal); // Sanitize input, and then sanitize if we have a handler - if (Target == NULL || Signal >= NUMSIGNALS) { + if (Target == NULL || Signal >= NUMSIGNALS) { ERROR("Signal %i was not in range"); - return OsError; // Invalid - } + return OsError; // Invalid + } if (Target->SignalInformation[Signal] == 1) { ERROR("Signal %i was blocked"); return OsError; // Ignored } - Sig = (SystemSignal_t*)kmalloc(sizeof(SystemSignal_t)); - Sig->Ignorable = GlbSignalIsDeadly[Signal]; - Sig->Signal = Signal; - sKey.Value = Signal; - CollectionAppend(Target->SignalQueue, CollectionCreateNode(sKey, Sig)); + Sig = (SystemSignal_t*)kmalloc(sizeof(SystemSignal_t)); + Sig->Ignorable = GlbSignalIsDeadly[Signal]; + Sig->Signal = Signal; + Key.Value.Integer = Signal; + CollectionAppend(Target->SignalQueue, CollectionCreateNode(Key, Sig)); // Wake up thread if neccessary if (Target->SchedulerFlags & SCHEDULER_FLAG_BLOCKED) { @@ -116,10 +116,10 @@ SignalCreate( OsStatus_t SignalReturn(void) { - UUId_t Cpu = CpuGetCurrentId(); - MCoreThread_t* Thread = ThreadingGetCurrentThread(Cpu); - Thread->ActiveSignal.Signal = -1; - return SignalHandle(Thread->Id); + UUId_t Cpu = CpuGetCurrentId(); + MCoreThread_t* Thread = ThreadingGetCurrentThread(Cpu); + Thread->ActiveSignal.Signal = -1; + return SignalHandle(Thread->Id); } /* Handle Signal @@ -127,31 +127,31 @@ SignalReturn(void) * and if it has, it executes the first in list */ OsStatus_t SignalHandle( - _In_ UUId_t ThreadId) + _In_ UUId_t ThreadId) { - CollectionItem_t* Node; - MCoreThread_t* Thread = ThreadingGetThread(ThreadId);; - SystemSignal_t* Signal; - - if (Thread == NULL) { - return OsError; - } - - // Even if there is a Ash, we might want not - // to Ash any signals ATM if there is already - // one active - if (Thread->ActiveSignal.Signal != -1) { - return OsError; - } - Node = CollectionPopFront(Thread->SignalQueue); - - // Sanitize the node, no more signals? - if (Node != NULL) { - Signal = (SystemSignal_t*)Node->Data; - CollectionDestroyNode(Thread->SignalQueue, Node); - SignalExecute(Thread, Signal); - } - return OsSuccess; + CollectionItem_t* Node; + MCoreThread_t* Thread = ThreadingGetThread(ThreadId);; + SystemSignal_t* Signal; + + if (Thread == NULL) { + return OsError; + } + + // Even if there is a Ash, we might want not + // to Ash any signals ATM if there is already + // one active + if (Thread->ActiveSignal.Signal != -1) { + return OsError; + } + Node = CollectionPopFront(Thread->SignalQueue); + + // Sanitize the node, no more signals? + if (Node != NULL) { + Signal = (SystemSignal_t*)Node->Data; + CollectionDestroyNode(Thread->SignalQueue, Node); + SignalExecute(Thread, Signal); + } + return OsSuccess; } /* SignalExecute @@ -172,23 +172,23 @@ SignalExecute( return; } - // If there is no handler for the process and we + // If there is no handler for the process and we // can't ignore signal, we must kill - if (Process->SignalHandler == 0) { - char Action = GlbSignalIsDeadly[Signal->Signal]; - if (Action == 1 || Action == 2) { + if (Process->SignalHandler == 0) { + char Action = GlbSignalIsDeadly[Signal->Signal]; + if (Action == 1 || Action == 2) { ThreadingTerminateThread(Process->MainThreadId, Signal->Signal, 1); - } + } kfree(Signal); - return; - } + return; + } - // Update active and dispatch + // Update active and dispatch memcpy(&Thread->ActiveSignal, Signal, sizeof(SystemSignal_t)); Thread->ActiveSignal.Context = Thread->ContextActive; // Cleanup signal and dispatch kfree(Signal); TRACE("Signal dispatching.."); - ThreadingSignalDispatch(Thread); + ThreadingSignalDispatch(Thread); } diff --git a/kernel/synchronization/semaphore_global.c b/kernel/synchronization/semaphore_global.c index 528d447e1..41d336b1d 100644 --- a/kernel/synchronization/semaphore_global.c +++ b/kernel/synchronization/semaphore_global.c @@ -27,7 +27,7 @@ #include // Global list of existing semaphores -static Collection_t Semaphores = COLLECTION_INIT(KeyInteger); +static Collection_t Semaphores = COLLECTION_INIT(KeyId); /* CreateGlobalSemaphore * Allocates a completely new instance of the global semaphore. If a semaphore with @@ -39,17 +39,16 @@ CreateGlobalSemaphore( _In_ int MaximumValue, _Out_ GlobalSemaphore_t** Semaphore) { - // Variables - GlobalSemaphore_t *Instance = NULL; - DataKey_t hKey; + GlobalSemaphore_t* Instance = NULL; + DataKey_t Key; assert(InitialValue >= 0); assert(MaximumValue >= InitialValue); // First of all, make sure there is no conflicting semaphores in system if (Identifier != NULL) { - hKey.Value = (int)MStringHash(Identifier); - void *Exists = CollectionGetDataByKey(&Semaphores, hKey, 0); + Key.Value.Id = MStringHash(Identifier); + void *Exists = CollectionGetDataByKey(&Semaphores, Key, 0); if (Exists != NULL) { *Semaphore = (GlobalSemaphore_t*)Exists; return OsError; @@ -62,7 +61,7 @@ CreateGlobalSemaphore( if (Identifier != NULL) { Instance->Hash = MStringHash(Identifier); - CollectionAppend(&Semaphores, CollectionCreateNode(hKey, Semaphore)); + CollectionAppend(&Semaphores, CollectionCreateNode(Key, Semaphore)); } else { Instance->Hash = 0; @@ -78,13 +77,12 @@ void DestroyGlobalSemaphore( _In_ GlobalSemaphore_t* Semaphore) { - // Variables DataKey_t Key; assert(Semaphore != NULL); if (Semaphore->Hash != 0) { - Key.Value = (int)Semaphore->Hash; + Key.Value.Id = Semaphore->Hash; CollectionRemoveByKey(&Semaphores, Key); } SlimSemaphoreDestroy(&Semaphore->Semaphore); diff --git a/kernel/threading.c b/kernel/threading.c index 1642e7ea1..557af26bb 100644 --- a/kernel/threading.c +++ b/kernel/threading.c @@ -39,7 +39,7 @@ OsStatus_t ThreadingReap(void *Context); -static Collection_t Threads = COLLECTION_INIT(KeyInteger); +static Collection_t Threads = COLLECTION_INIT(KeyId); static UUId_t GlbThreadGcId = UUID_INVALID; static _Atomic(UUId_t) GlbThreadId = ATOMIC_VAR_INIT(1); @@ -71,7 +71,7 @@ ThreadingEnable(void) SchedulerThreadInitialize(Thread, Thread->Flags); Thread->Pipe = CreateSystemPipe(0, 6); // 64 entries, 4kb Thread->SignalQueue = CollectionCreate(KeyInteger); - Key.Value = (int)Thread->Id; + Key.Value.Id = Thread->Id; COLLECTION_NODE_INIT(&Thread->CollectionHeader, Key); // Initialize arch-dependant members @@ -125,11 +125,11 @@ ThreadingCreateThread( TRACE("ThreadingCreateThread(%s, 0x%x)", Name, Flags); - Key.Value = (int)atomic_fetch_add(&GlbThreadId, 1); - Cpu = CpuGetCurrentId(); - Parent = ThreadingGetCurrentThread(Cpu); + Key.Value.Id = atomic_fetch_add(&GlbThreadId, 1); + Cpu = CpuGetCurrentId(); + Parent = ThreadingGetCurrentThread(Cpu); - Thread = (MCoreThread_t*)kmalloc(sizeof(MCoreThread_t)); + Thread = (MCoreThread_t*)kmalloc(sizeof(MCoreThread_t)); memset(Thread, 0, sizeof(MCoreThread_t)); if (ThreadingRegister(Thread) != OsSuccess) { ERROR("Failed to register a new thread with system."); @@ -149,7 +149,7 @@ ThreadingCreateThread( // Initialize some basic thread information // The only flags we want to copy for now are the running-mode - Thread->Id = (UUId_t)Key.Value; + Thread->Id = Key.Value.Id; Thread->ParentThreadId = Parent->Id; Thread->ProcessHandle = Parent->ProcessHandle; Thread->Function = Function; diff --git a/kernel/timers.c b/kernel/timers.c index 14d8da2a7..c0fdc7f91 100644 --- a/kernel/timers.c +++ b/kernel/timers.c @@ -38,7 +38,7 @@ static MCoreTimePerformanceOps_t PerformanceTimer = { 0 }; static CriticalSection_t TimersSyncObject = CRITICALSECTION_INITIALIZE(CRITICALSECTION_PLAIN); static MCoreSystemTimer_t *ActiveSystemTimer = NULL; static Collection_t SystemTimers = COLLECTION_INIT(KeyInteger); -static Collection_t Timers = COLLECTION_INIT(KeyInteger); +static Collection_t Timers = COLLECTION_INIT(KeyId); static _Atomic(UUId_t) TimerIdGenerator = ATOMIC_VAR_INIT(0); /* TimersStart @@ -63,7 +63,7 @@ TimersStart( Timer->Interval = IntervalNs; Timer->Current = 0; Timer->Periodic = Periodic; - Key.Value = (int)Timer->Id; + Key.Value.Id = Timer->Id; CollectionAppend(&Timers, CollectionCreateNode(Key, Timer)); return Timer->Id; } @@ -139,13 +139,11 @@ TimersRegisterSystemTimer( _In_ size_t TickNs, _In_ clock_t (*SystemTickHandler)(void)) { - // Variables MCoreSystemTimer_t* SystemTimer; - SystemInterrupt_t* Interrupt; - DataKey_t tKey; - int Delta = abs(1000 - (int)TickNs); + SystemInterrupt_t* Interrupt; + DataKey_t Key = { 0 }; + int Delta = abs(1000 - (int)TickNs); - // Trace TRACE("TimersRegisterSystemTimer()"); // Do some validation about the timer source @@ -164,8 +162,7 @@ TimersRegisterSystemTimer( SystemTimer->SystemTick = SystemTickHandler; // Add the new timer to the list - tKey.Value = 0; - CollectionAppend(&SystemTimers, CollectionCreateNode(tKey, SystemTimer)); + CollectionAppend(&SystemTimers, CollectionCreateNode(Key, SystemTimer)); // Ok, for a system timer we want something optimum // of 1 ms per interrupt diff --git a/librt/include/ds/ds.h b/librt/include/ds/ds.h index 2a104334f..cbb5927ff 100644 --- a/librt/include/ds/ds.h +++ b/librt/include/ds/ds.h @@ -26,17 +26,22 @@ /* The definition of a key * in generic data-structures this can be values or data */ -typedef union _DataKey { - int Value; - void* Pointer; - char* String; +typedef struct _DataKey { + union { + int Integer; + UUId_t Id; + struct { + const char* Pointer; + size_t Length; + } String; + } Value; } DataKey_t; /* This enumeration denotes * the kind of key that is to be interpreted by the data-structure */ typedef enum _KeyType { KeyInteger, - KeyPointer, + KeyId, KeyString } KeyType_t; @@ -50,37 +55,32 @@ typedef struct _SafeMemoryLock { /* dsalloc * Seperate portable memory allocator for data-structures */ -CRTDECL( -void*, +CRTDECL(void*, dsalloc( _In_ size_t size)); /* dsfree * Seperate portable memory freeing for data-structures */ -CRTDECL( -void, +CRTDECL(void, dsfree( _In_ void *p)); /* dslock * Acquires the lock given, this is a blocking call and will wait untill * the lock is acquired. */ -CRTDECL( -void, +CRTDECL(void, dslock( - _In_ SafeMemoryLock_t *MemoryLock)); + _In_ SafeMemoryLock_t* MemoryLock)); /* dsunlock * Releases the lock given and restores any previous flags. */ -CRTDECL( -void, +CRTDECL(void, dsunlock( - _In_ SafeMemoryLock_t *MemoryLock)); + _In_ SafeMemoryLock_t* MemoryLock)); /* Helper Function * Matches two keys based on the key type returns 0 if they are equal, or -1 if not */ -CRTDECL( -int, +CRTDECL(int, dsmatchkey( _In_ KeyType_t KeyType, _In_ DataKey_t Key1, @@ -91,8 +91,7 @@ dsmatchkey( * - 1 if 1 > 2, * - 0 if 1 == 2 and * - -1 if 2 > 1 */ -CRTDECL( -int, +CRTDECL(int, dssortkey( _In_ KeyType_t KeyType, _In_ DataKey_t Key1, diff --git a/librt/include/ds/hashtable.h b/librt/include/ds/hashtable.h index d4774593f..2ddac9231 100644 --- a/librt/include/ds/hashtable.h +++ b/librt/include/ds/hashtable.h @@ -31,14 +31,14 @@ #define HASHTABLE_DEFAULT_LOADFACTOR 75 // Equals 75 percent -typedef size_t(*HashFn)(void* Value); +typedef size_t(*HashFn)(const char*, size_t); typedef struct _HashTable { size_t Capacity; size_t Size; size_t LoadFactor; HashFn GetHashCode; - Collection_t** Array; + Collection_t* Array; } HashTable_t; /* HashTableCreate @@ -52,37 +52,38 @@ HashTableCreate( /* HashTableSetHashFunction * Overrides the default hash function with a user provided hash function. To * reset this set with NULL. */ +CRTDECL(void, +HashTableSetHashFunction( + _In_ HashTable_t* HashTable, + _In_ HashFn Fn)); /* HashTableDestroy - * Releases all resources - * associated with the hashtable */ + * Cleans up all resources associated with the hashtable. This does not clear up the values + * registered in the hash-table. */ CRTDECL(void, HashTableDestroy( - _In_ HashTable_t *HashTable)); + _In_ HashTable_t* HashTable)); /* HashTableInsert - * Inserts an object with the given - * string key from the hash table */ + * Inserts or overwrites the existing key in the hashtable. */ CRTDECL(void, HashTableInsert( - _In_ HashTable_t *HashTable, - _In_ DataKey_t Key, - _In_ void *Data)); + _In_ HashTable_t* HashTable, + _In_ DataKey_t Key, + _In_ void* Data)); /* HashTableRemove - * Removes an object with the given - * string key from the hash table */ + * Removes the entry with the matching key from the hashtable. */ CRTDECL(void, HashTableRemove( - _In_ HashTable_t *HashTable, - _In_ DataKey_t Key)); + _In_ HashTable_t* HashTable, + _In_ DataKey_t Key)); /* HashTableGetValue - * Retrieves the data associated with - * a value from the hash table */ + * Retrieves the data associated with the given key from the hashtable */ CRTDECL(void*, HashTableGetValue( - _In_ HashTable_t *HashTable, - _In_ DataKey_t Key)); + _In_ HashTable_t* HashTable, + _In_ DataKey_t Key)); #endif //!_HASHTABLE_H_ diff --git a/librt/libc/os/sharedobject.c b/librt/libc/os/sharedobject.c index 92569794e..da52e7836 100644 --- a/librt/libc/os/sharedobject.c +++ b/librt/libc/os/sharedobject.c @@ -21,13 +21,8 @@ * and functionality, refer to the individual things for descriptions */ -/* Includes - * - System */ #include #include - -/* Includes - * - Library */ #include #include #include @@ -42,7 +37,7 @@ typedef struct _LibraryItem { /* Globals * - Function blueprint for dll-entry */ typedef void (*SOInitializer_t)(int); -static Collection_t LoadedLibraries = COLLECTION_INIT(KeyInteger); +static Collection_t LoadedLibraries = COLLECTION_INIT(KeyId); /* SharedObjectHash * Helper utility to identify shared libraries */ @@ -65,11 +60,10 @@ Handle_t SharedObjectLoad( _In_ const char* SharedObject) { - // Variables SOInitializer_t Initializer = NULL; LibraryItem_t *Library = NULL; Handle_t Result = HANDLE_INVALID; - DataKey_t Key = { (int)SharedObjectHash(SharedObject) }; + DataKey_t Key = { .Value.Id = SharedObjectHash(SharedObject) }; // Special case if (SharedObject == NULL) { diff --git a/librt/libc/stdio/common/supp.c b/librt/libc/stdio/common/supp.c index 337469cf9..d21a5fe55 100644 --- a/librt/libc/stdio/common/supp.c +++ b/librt/libc/stdio/common/supp.c @@ -395,10 +395,10 @@ StdioFdAllocate( _In_ int fd, _In_ int flag) { - StdioObject_t *io = NULL; - int result = -1; - DataKey_t Key; - int i, j; + StdioObject_t* io = NULL; + int result = -1; + DataKey_t Key; + int i, j; // Trying to allocate a specific fd? SpinlockAcquire(&BitmapLock); @@ -446,7 +446,7 @@ StdioFdAllocate( SpinlockReset(&io->lock); // Add to list - Key.Value = result; + Key.Value.Integer = result; CollectionAppend(&IoObjects, CollectionCreateNode(Key, io)); } return result; @@ -460,11 +460,10 @@ StdioFdFree( { int Block; int Offset; - DataKey_t Key; + DataKey_t Key = { .Value.Integer = fd }; void* Object; - Key.Value = fd; - Object = CollectionGetDataByKey(&IoObjects, Key, 0); + Object = CollectionGetDataByKey(&IoObjects, Key, 0); if (Object != NULL) { if (CollectionRemoveByKey(&IoObjects, Key) != OsSuccess) { ERROR(" > failed to remove io object for fd %i, it may not exist", fd); @@ -489,12 +488,10 @@ StdioHandle_t* StdioFdToHandle( _In_ int fd) { - // Variables - CollectionItem_t *fNode = NULL; - DataKey_t Key; + CollectionItem_t* fNode; + DataKey_t Key = { .Value.Integer = fd }; // Free any resources allocated by the fd - Key.Value = fd; fNode = CollectionGetNodeByKey(&IoObjects, Key, 0); if (fNode != NULL) { return &((StdioObject_t*)fNode->Data)->handle; @@ -514,10 +511,9 @@ StdioFdInitialize( { StdioObject_t* Object; CollectionItem_t* Node; - DataKey_t Key; + DataKey_t Key = { .Value.Integer = fd }; - Key.Value = fd; - Node = CollectionGetNodeByKey(&IoObjects, Key, 0); + Node = CollectionGetNodeByKey(&IoObjects, Key, 0); if (Node != NULL) { Object = (StdioObject_t*)Node->Data; if (Object->wxflag & WX_OPEN) { @@ -926,8 +922,7 @@ StdioObject_t* get_ioinfo( _In_ int fd) { - DataKey_t Key; - Key.Value = fd; + DataKey_t Key = { .Value.Integer = fd }; return (StdioObject_t*)CollectionGetDataByKey(&IoObjects, Key, 0); } diff --git a/librt/libc/threads/tls.c b/librt/libc/threads/tls.c index 37c0a35e8..e70b30cdc 100644 --- a/librt/libc/threads/tls.c +++ b/librt/libc/threads/tls.c @@ -88,9 +88,9 @@ typedef struct _TlsProcessInstance { static Spinlock_t TlsLock = SPINLOCK_INIT; static TlsProcessInstance_t TlsGlobal = { { 0 }, { 0 }, - COLLECTION_INIT(KeyInteger), - COLLECTION_INIT(KeyInteger), - COLLECTION_INIT(KeyInteger), + COLLECTION_INIT(KeyId), + COLLECTION_INIT(KeyId), + COLLECTION_INIT(KeyId), 0 }; @@ -209,21 +209,18 @@ tss_get( { CollectionItem_t* Node = NULL; void *Result = NULL; - thrd_t thr = UUID_INVALID; - DataKey_t tKey; + DataKey_t tKey = { .Value.Id = thrd_current() }; if (tss_key >= TLS_MAX_KEYS) { return NULL; } - thr = thrd_current(); - tKey.Value = thr; // Iterate the list of TLS instances and // find the one that contains the tls-key SpinlockAcquire(&TlsLock); _foreach(Node, &TlsGlobal.Tls) { TlsThreadInstance_t* Tls = (TlsThreadInstance_t*)Node; - if (!dsmatchkey(KeyInteger, tKey, Node->Key) && Tls->Key == tss_key) { + if (!dsmatchkey(KeyId, tKey, Node->Key) && Tls->Key == tss_key) { Result = Tls->Value; break; } @@ -242,22 +239,19 @@ tss_set( { TlsThreadInstance_t *NewTls = NULL; CollectionItem_t *Node = NULL; - thrd_t thr = UUID_INVALID; - DataKey_t tKey; + DataKey_t tKey = { .Value.Id = thrd_current() }; // Sanitize key value if (tss_id >= TLS_MAX_KEYS) { return thrd_error; } - thr = thrd_current(); - tKey.Value = thr; // Iterate and find if it // exists, if exists we override SpinlockAcquire(&TlsLock); _foreach(Node, &TlsGlobal.Tls) { TlsThreadInstance_t *Tls = (TlsThreadInstance_t*)Node; - if (!dsmatchkey(KeyInteger, tKey, Node->Key) && Tls->Key == tss_id) { + if (!dsmatchkey(KeyId, tKey, Node->Key) && Tls->Key == tss_id) { Tls->Value = val; SpinlockRelease(&TlsLock); return thrd_success; @@ -267,11 +261,11 @@ tss_set( NewTls = (TlsThreadInstance_t*)malloc(sizeof(TlsThreadInstance_t)); memset(NewTls, 0, sizeof(TlsThreadInstance_t)); - NewTls->ListHeader.Key.Value = tKey.Value; - NewTls->ListHeader.Data = NewTls; - NewTls->Key = tss_id; - NewTls->Value = val; - NewTls->Destructor = TlsGlobal.Dss[tss_id]; + NewTls->ListHeader.Key = tKey; + NewTls->ListHeader.Data = NewTls; + NewTls->Key = tss_id; + NewTls->Value = val; + NewTls->Destructor = TlsGlobal.Dss[tss_id]; // Last thing is to append it to the tls-list SpinlockAcquire(&TlsLock); @@ -329,9 +323,9 @@ tls_register_atexit( AtExitFn = (TlsAtExit_t*)malloc(sizeof(TlsAtExit_t)); memset(AtExitFn, 0, sizeof(TlsAtExit_t)); - AtExitFn->ListHeader.Key.Value = (int)ThreadId; - AtExitFn->Argument = Argument; - AtExitFn->DsoHandle = DsoHandle; + AtExitFn->ListHeader.Key.Value.Id = ThreadId; + AtExitFn->Argument = Argument; + AtExitFn->DsoHandle = DsoHandle; if (ThreadId == UUID_INVALID) { AtExitFn->Type = TLS_ATEXIT_CXA; AtExitFn->AtExit.Function = (void(*)(void*, int))Function; @@ -365,7 +359,7 @@ void tls_callatexit(_In_ Collection_t* List, _In_ thrd_t ThreadId, _In_ void* DsoHandle, _In_ int ExitCode) { CollectionItem_t* Node; - DataKey_t Key; + DataKey_t Key = { .Value.Id = ThreadId }; int Skip = 0; TRACE("tls_callatexit(%u, 0x%x, %i)", ThreadId, DsoHandle, ExitCode); @@ -379,7 +373,6 @@ tls_callatexit(_In_ Collection_t* List, _In_ thrd_t ThreadId, _In_ void* DsoHand TlsGlobal.TlsAtExitHasRun = 1; } - Key.Value = (int)ThreadId; Node = CollectionGetNodeByKey(List, Key, Skip); while (Node != NULL) { TlsAtExit_t* Function = (TlsAtExit_t*)Node; @@ -409,11 +402,9 @@ tls_cleanup(_In_ thrd_t thr, _In_ void* DsoHandle, _In_ int ExitCode) { int NumberOfPassesLeft = TSS_DTOR_ITERATIONS; int NumberOfValsLeft = 0; - DataKey_t Key; + DataKey_t Key = { .Value.Id = thr }; TRACE("tls_cleanup(%u, 0x%x, %i)", thr, DsoHandle, ExitCode); - Key.Value = thr; - // Execute all stored destructors untill there is no // more values left or we reach the maximum number of passes CollectionExecuteOnKey(&TlsGlobal.Tls, tls_callback, Key, &NumberOfValsLeft); diff --git a/librt/libds/collection.c b/librt/libds/collection.c index e1734fc67..2c1dd6a61 100644 --- a/librt/libds/collection.c +++ b/librt/libds/collection.c @@ -153,9 +153,8 @@ CollectionDestroyNode( // Behave different based on the type of key switch (Collection->KeyType) { - case KeyPointer: case KeyString: - dsfree(Node->Key.Pointer); + dsfree((void*)Node->Key.Value.String.Pointer); break; default: diff --git a/librt/libds/hashtable.c b/librt/libds/hashtable.c index 86db2222b..ba3a4a328 100644 --- a/librt/libds/hashtable.c +++ b/librt/libds/hashtable.c @@ -23,53 +23,112 @@ */ #include +#include #include +static size_t +GetDefaultHashValue(const char* Value, size_t Length) +{ + return 0; +} + /* HashTableCreate - * Initializes a new hash table - * of the given capacity */ + * Initializes a new hash table structure of the desired capacity, and load factor. + * The load factor defaults to HASHTABLE_DEFAULT_LOADFACTOR. */ HashTable_t* HashTableCreate( _In_ size_t Capacity, _In_ size_t LoadFactor) { - - return NULL; + HashTable_t* HashTable = (HashTable_t*)dsalloc(sizeof(HashTable_t)); + assert(HashTable != NULL); + + HashTable->Array = (Collection_t*)dsalloc(sizeof(Collection_t) * Capacity); + assert(HashTable->Array != NULL); + memset(HashTable->Array, 0, sizeof(sizeof(Collection_t) * Capacity)); + for (size_t i = 0; i < Capacity; i++) { + HashTable->Array[i].KeyType = KeyId; + } + + HashTable->Size = 0; + HashTable->Capacity = Capacity; + HashTable->LoadFactor = LoadFactor; + HashTable->GetHashCode = GetDefaultHashValue; + return HashTable; } /* HashTableDestroy - * Releases all resources - * associated with the hashtable */ -void HashTableDestroy(HashTable_t *HashTable) + * Cleans up all resources associated with the hashtable. This does not clear up the values + * registered in the hash-table. */ +void +HashTableDestroy( + _In_ HashTable_t* HashTable) { - _CRT_UNUSED(HashTable); + assert(HashTable != NULL); + for (size_t i = 0; i < HashTable->Capacity; i++) { + CollectionClear(&HashTable->Array[i]); + } + dsfree(HashTable->Array); + dsfree(HashTable); +} + +/* HashTableSetHashFunction + * Overrides the default hash function with a user provided hash function. To + * reset this set with NULL. */ +void +HashTableSetHashFunction( + _In_ HashTable_t* HashTable, + _In_ HashFn Fn) +{ + assert(HashTable != NULL); + HashTable->GetHashCode = Fn; } /* HashTableInsert - * Inserts an object with the given - * string key from the hash table */ -void HashTableInsert(HashTable_t *HashTable, DataKey_t Key, void *Data) + * Inserts or overwrites the existing key in the hashtable. */ +void +HashTableInsert( + _In_ HashTable_t* HashTable, + _In_ DataKey_t Key, + _In_ void* Data) { - _CRT_UNUSED(HashTable); - _CRT_UNUSED(Key); - _CRT_UNUSED(Data); + assert(HashTable != NULL); + size_t ArrayIndex = GetDefaultHashValue(Key.Value.String.Pointer, Key.Value.String.Length) % HashTable->Capacity; + Collection_t* Array = &HashTable->Array[ArrayIndex]; + DataKey_t ArrayKey = { .Value.Id = ArrayIndex }; + CollectionItem_t* Existing = CollectionGetNodeByKey(Array, ArrayKey, 0); + if (Existing == NULL) { + CollectionAppend(Array, CollectionCreateNode(ArrayKey, Data)); + } + else { + Existing->Data = Data; + } } /* HashTableRemove - * Removes an object with the given - * string key from the hash table */ -void HashTableRemove(HashTable_t *HashTable, DataKey_t Key) + * Removes the entry with the matching key from the hashtable. */ +void +HashTableRemove( + _In_ HashTable_t* HashTable, + _In_ DataKey_t Key) { - _CRT_UNUSED(HashTable); - _CRT_UNUSED(Key); + assert(HashTable != NULL); + size_t ArrayIndex = GetDefaultHashValue(Key.Value.String.Pointer, Key.Value.String.Length) % HashTable->Capacity; + Collection_t* Array = &HashTable->Array[ArrayIndex]; + DataKey_t ArrayKey = { .Value.Id = ArrayIndex }; + CollectionRemoveByKey(Array, ArrayKey); } /* HashTableGetValue - * Retrieves the data associated with - * a value from the hash table */ -void *HashTableGetValue(HashTable_t *HashTable, DataKey_t Key) + * Retrieves the data associated with the given key from the hashtable */ +void* +HashTableGetValue( + _In_ HashTable_t* HashTable, + _In_ DataKey_t Key) { - _CRT_UNUSED(HashTable); - _CRT_UNUSED(Key); - return NULL; -} \ No newline at end of file + assert(HashTable != NULL); + size_t ArrayIndex = GetDefaultHashValue(Key.Value.String.Pointer, Key.Value.String.Length) % HashTable->Capacity; + Collection_t* Array = &HashTable->Array[ArrayIndex]; + DataKey_t ArrayKey = { .Value.Id = ArrayIndex }; + return CollectionGetDataByKey(Array, ArrayKey, 0); +} diff --git a/librt/libds/support/ds.c b/librt/libds/support/ds.c index 2401df096..ed024546a 100644 --- a/librt/libds/support/ds.c +++ b/librt/libds/support/ds.c @@ -97,16 +97,18 @@ dsmatchkey( _In_ DataKey_t Key2) { switch (KeyType) { + case KeyId: { + if (Key1.Value.Id == Key2.Value.Id) { + return 0; + } + } break; case KeyInteger: { - if (Key1.Value == Key2.Value) - return 0; - } break; - case KeyPointer: { - if (Key1.Pointer == Key2.Pointer) - return 0; + if (Key1.Value.Integer == Key2.Value.Integer) { + return 0; + } } break; case KeyString: { - return strcmp(Key1.String, Key2.String); + return strcmp(Key1.Value.String.Pointer, Key2.Value.String.Pointer); } break; } return -1; @@ -114,8 +116,7 @@ dsmatchkey( /* Helper Function * Used by sorting, it compares to values - * and returns 1 if 1 > 2, 0 if 1 == 2 and - * -1 if 2 > 1 */ + * and returns 1 if 1 > 2, 0 if 1 == 2 and -1 if 2 > 1 */ int dssortkey( _In_ KeyType_t KeyType, @@ -123,19 +124,24 @@ dssortkey( _In_ DataKey_t Key2) { switch (KeyType) { + case KeyId: { + if (Key1.Value.Id == Key2.Value.Id) + return 0; + else if (Key1.Value.Id > Key2.Value.Id) + return 1; + else + return -1; + } break; case KeyInteger: { - if (Key1.Value == Key2.Value) + if (Key1.Value.Integer == Key2.Value.Integer) return 0; - else if (Key1.Value > Key2.Value) + else if (Key1.Value.Integer > Key2.Value.Integer) return 1; else return -1; } break; - case KeyPointer: { - return 0; - } break; case KeyString: { - return strcmp(Key1.String, Key2.String); + return strcmp(Key1.Value.String.Pointer, Key2.Value.String.Pointer); } break; } return 0; diff --git a/modules/input/hid/main.c b/modules/input/hid/main.c index 3cd23cf9c..460560d30 100644 --- a/modules/input/hid/main.c +++ b/modules/input/hid/main.c @@ -77,7 +77,7 @@ OsStatus_t OnLoad(void) { // Initialize state for this driver - GlbHidDevices = CollectionCreate(KeyInteger); + GlbHidDevices = CollectionCreate(KeyId); return UsbInitialize(); } @@ -104,9 +104,8 @@ OsStatus_t OnRegister( _In_ MCoreDevice_t *Device) { - // Variables - HidDevice_t *HidDevice = NULL; - DataKey_t Key; + HidDevice_t* HidDevice = NULL; + DataKey_t Key = { .Value.Id = Device->Id }; // Register the new controller HidDevice = HidDeviceCreate((MCoreUsbDevice_t*)Device); @@ -115,14 +114,7 @@ OnRegister( if (HidDevice == NULL) { return OsError; } - - // Use the device-id as key - Key.Value = (int)Device->Id; - - // Append the controller to our list CollectionAppend(GlbHidDevices, CollectionCreateNode(Key, HidDevice)); - - // Done - no error return OsSuccess; } @@ -133,13 +125,8 @@ OsStatus_t OnUnregister( _In_ MCoreDevice_t *Device) { - // Variables - HidDevice_t *HidDevice = NULL; - DataKey_t Key; - - // Set the key to the id of the device to find - // the bound controller - Key.Value = (int)Device->Id; + HidDevice_t* HidDevice = NULL; + DataKey_t Key = { .Value.Id = Device->Id }; // Lookup controller HidDevice = (HidDevice_t*) diff --git a/modules/serial/usb/common/manager.c b/modules/serial/usb/common/manager.c index fb15b9a67..70b67e09f 100644 --- a/modules/serial/usb/common/manager.c +++ b/modules/serial/usb/common/manager.c @@ -62,7 +62,7 @@ OnTimeout( * all controllers and all attached devices */ OsStatus_t UsbManagerInitialize(void) { - __GlbControllers = CollectionCreate(KeyInteger); + __GlbControllers = CollectionCreate(KeyId); __GlbTimerEvent = UUID_INVALID; __GlbTimerCallback = NULL; return OsSuccess; @@ -103,14 +103,21 @@ OsStatus_t UsbManagerRegisterController( _In_ UsbManagerController_t* Controller) { - // Variables - DataKey_t Key; - Key.Value = (int)Controller->Device.Id; - - // Register controller with usbmanager service - if (UsbControllerRegister(&Controller->Device, - Controller->Type, Controller->PortCount) != OsSuccess) { - return OsError; + DataKey_t Key = { .Value.Id = Controller->Device.Id }; + int Retries = 3; + OsStatus_t Status = OsError; + + // Register controller with usbmanager service, sometimes the usb service is a tad + // slow in starting up, so try 3 times, with 1 second between + for (int i = 0; i < Retries; i++) { + Status = UsbControllerRegister(&Controller->Device, Controller->Type, Controller->PortCount); + if (Status == OsSuccess) { + break; + } + thrd_sleepex(1000); + } + if (Status != OsSuccess) { + return Status; } return CollectionAppend(__GlbControllers, CollectionCreateNode(Key, Controller)); } @@ -124,10 +131,9 @@ UsbManagerDestroyController( { // Variables CollectionItem_t *cNode = NULL; - DataKey_t Key; + DataKey_t Key = { .Value.Id = Controller->Device.Id }; // Unregister controller with usbmanager service - Key.Value = (int)Controller->Device.Id; if (UsbControllerUnregister(Controller->Device.Id) != OsSuccess) { return OsError; } @@ -196,7 +202,7 @@ UsbManagerGetToggle( // Create an unique id for this endpoint Pipe = ((uint32_t)Address->DeviceAddress << 8) | Address->EndpointAddress; - Key.Value = (int)Pipe; + Key.Value.Integer = (int)Pipe; // Iterate list of controllers foreach(cNode, __GlbControllers) { @@ -206,8 +212,7 @@ UsbManagerGetToggle( // Locate the correct endpoint foreach(eNode, Controller->Endpoints) { // Cast data again - UsbManagerEndpoint_t *Endpoint = - (UsbManagerEndpoint_t*)eNode->Data; + UsbManagerEndpoint_t *Endpoint = (UsbManagerEndpoint_t*)eNode->Data; if (Endpoint->Pipe == Pipe) { return Endpoint->Toggle; } @@ -228,8 +233,7 @@ UsbManagerGetToggle( Endpoint->Toggle = 0; // Add it to the list - CollectionAppend(Controller->Endpoints, - CollectionCreateNode(Key, Endpoint)); + CollectionAppend(Controller->Endpoints, CollectionCreateNode(Key, Endpoint)); } } return 0; @@ -249,7 +253,7 @@ UsbManagerSetToggle( // Create an unique id for this endpoint Pipe = ((uint32_t)Address->DeviceAddress << 8) | Address->EndpointAddress; - Key.Value = (int)Pipe; + Key.Value.Integer = (int)Pipe; // Iterate list of controllers foreach(cNode, __GlbControllers) { diff --git a/modules/serial/usb/ehci/controller.c b/modules/serial/usb/ehci/controller.c index 3c67ee524..cae9b38e6 100644 --- a/modules/serial/usb/ehci/controller.c +++ b/modules/serial/usb/ehci/controller.c @@ -540,7 +540,7 @@ EhciSetup( // Register the controller before starting if (UsbManagerRegisterController(&Controller->Base) != OsSuccess) { - ERROR("Failed to register ehci controller with the system."); + ERROR(" > failed to register ehci controller with the system."); } // Now, controller is up and running diff --git a/modules/serial/usb/ehci/transfer_generic.c b/modules/serial/usb/ehci/transfer_generic.c index d0208aa53..079eccdae 100644 --- a/modules/serial/usb/ehci/transfer_generic.c +++ b/modules/serial/usb/ehci/transfer_generic.c @@ -244,7 +244,7 @@ HciQueueTransferGeneric( } // Store transaction in queue if it's not there already - Key.Value = (int)Transfer->Id; + Key.Value.Integer = (int)Transfer->Id; if (CollectionGetDataByKey(Controller->Base.TransactionList, Key, 0) == NULL) { CollectionAppend(Controller->Base.TransactionList, CollectionCreateNode(Key, Transfer)); EhciTransactionCount(Controller, Transfer, &Transfer->TransactionsTotal); diff --git a/modules/serial/usb/ehci/transfer_isoc.c b/modules/serial/usb/ehci/transfer_isoc.c index 627110dac..f8d51b41c 100644 --- a/modules/serial/usb/ehci/transfer_isoc.c +++ b/modules/serial/usb/ehci/transfer_isoc.c @@ -104,7 +104,7 @@ HciQueueTransferIsochronous( // Add transfer Transfer->EndpointDescriptor = (void*)FirstTd; - Key.Value = 0; + Key.Value.Integer = 0; CollectionAppend(Controller->Base.TransactionList, CollectionCreateNode(Key, Transfer)); return EhciTransactionDispatch(Controller, Transfer); } diff --git a/modules/serial/usb/ohci/transfer_generic.c b/modules/serial/usb/ohci/transfer_generic.c index 89643a45c..83b482d9c 100644 --- a/modules/serial/usb/ohci/transfer_generic.c +++ b/modules/serial/usb/ohci/transfer_generic.c @@ -247,7 +247,7 @@ HciQueueTransferGeneric( } // Store transaction in queue if it's not there already - Key.Value = (int)Transfer->Id; + Key.Value.Integer = (int)Transfer->Id; if (CollectionGetDataByKey(Controller->Base.TransactionList, Key, 0) == NULL) { CollectionAppend(Controller->Base.TransactionList, CollectionCreateNode(Key, Transfer)); OhciTransactionCount(Controller, Transfer, &Transfer->TransactionsTotal); diff --git a/modules/serial/usb/ohci/transfer_isoc.c b/modules/serial/usb/ohci/transfer_isoc.c index b4d963e77..9b3a8f1e4 100644 --- a/modules/serial/usb/ohci/transfer_isoc.c +++ b/modules/serial/usb/ohci/transfer_isoc.c @@ -171,7 +171,7 @@ HciQueueTransferIsochronous( } // Store transaction in queue if it's not there already - Key.Value = (int)Transfer->Id; + Key.Value.Integer = (int)Transfer->Id; if (CollectionGetDataByKey(Controller->Base.TransactionList, Key, 0) == NULL) { CollectionAppend(Controller->Base.TransactionList, CollectionCreateNode(Key, Transfer)); OhciTransactionCount(Controller, Transfer, &Transfer->TransactionsTotal); diff --git a/modules/serial/usb/uhci/transfer_generic.c b/modules/serial/usb/uhci/transfer_generic.c index 50f6236c7..a55e5f12f 100644 --- a/modules/serial/usb/uhci/transfer_generic.c +++ b/modules/serial/usb/uhci/transfer_generic.c @@ -321,7 +321,7 @@ HciQueueTransferGeneric( } // Store transaction in queue if it's not there already - Key.Value = (int)Transfer->Id; + Key.Value.Integer = (int)Transfer->Id; if (CollectionGetDataByKey(Controller->Base.TransactionList, Key, 0) == NULL) { CollectionAppend(Controller->Base.TransactionList, CollectionCreateNode(Key, Transfer)); UhciTransactionCount(Controller, Transfer, &Transfer->TransactionsTotal); @@ -350,7 +350,7 @@ HciQueueTransferIsochronous( Transfer->Status = TransferNotProcessed; // Store transaction in queue if it's not there already - Key.Value = (int)Transfer->Id; + Key.Value.Integer = (int)Transfer->Id; if (CollectionGetDataByKey(Controller->Base.TransactionList, Key, 0) == NULL) { CollectionAppend(Controller->Base.TransactionList, CollectionCreateNode(Key, Transfer)); UhciTransactionCount(Controller, Transfer, &Transfer->TransactionsTotal); diff --git a/modules/serial/usb/xhci/main.c b/modules/serial/usb/xhci/main.c index 7fd246997..75ef788d9 100644 --- a/modules/serial/usb/xhci/main.c +++ b/modules/serial/usb/xhci/main.c @@ -108,7 +108,7 @@ OsStatus_t OnRegister(MCoreDevice_t *Device) } // Use the device-id as key - Key.Value = (int)Device->Id; + Key.Value.Integer = (int)Device->Id; // Append the controller to our list ListAppend(GlbControllers, ListCreateNode(Key, Key, Controller)); @@ -128,7 +128,7 @@ OsStatus_t OnUnregister(MCoreDevice_t *Device) // Set the key to the id of the device to find // the bound controller - Key.Value = (int)Device->Id; + Key.Value.Integer = (int)Device->Id; // Lookup controller Controller = (AhciController_t*) diff --git a/modules/storage/ahci/dispatch.c b/modules/storage/ahci/dispatch.c index f49e22185..4450b6e9f 100644 --- a/modules/storage/ahci/dispatch.c +++ b/modules/storage/ahci/dispatch.c @@ -169,7 +169,7 @@ AhciCommandDispatch( // Set the port multiplier Transaction->Device->Port->CommandList->Headers[Transaction->Slot].Flags |= (DISPATCH_MULTIPLIER(Flags) << 12); - Key.Value = Transaction->Slot; + Key.Value.Integer = Transaction->Slot; // Add transaction to list tNode = CollectionCreateNode(Key, Transaction); diff --git a/modules/storage/ahci/main.c b/modules/storage/ahci/main.c index c059bc025..dcf4933bc 100644 --- a/modules/storage/ahci/main.c +++ b/modules/storage/ahci/main.c @@ -31,7 +31,7 @@ #include // Static storage for the driver -static Collection_t Controllers = COLLECTION_INIT(KeyInteger); +static Collection_t Controllers = COLLECTION_INIT(KeyId); /* OnFastInterrupt * Is called for the sole purpose to determine if this source @@ -149,16 +149,13 @@ OnRegister( _In_ MCoreDevice_t* Device) { AhciController_t *Controller = NULL; - DataKey_t Key; + DataKey_t Key = { .Value.Id = Device->Id }; // Register the new controller Controller = AhciControllerCreate(Device); if (Controller == NULL) { return OsError; } - - // Use the device-id as key - Key.Value = (int)Device->Id; return CollectionAppend(&Controllers, CollectionCreateNode(Key, Controller)); } @@ -171,11 +168,7 @@ OnUnregister( { // Variables AhciController_t *Controller = NULL; - DataKey_t Key; - - // Set the key to the id of the device to find - // the bound controller - Key.Value = (int)Device->Id; + DataKey_t Key = { .Value.Id = Device->Id }; Controller = (AhciController_t*)CollectionGetDataByKey(&Controllers, Key, 0); if (Controller == NULL) { return OsError; diff --git a/modules/storage/ahci/manager.c b/modules/storage/ahci/manager.c index af1e5b31b..0163ac270 100644 --- a/modules/storage/ahci/manager.c +++ b/modules/storage/ahci/manager.c @@ -30,7 +30,7 @@ #include "manager.h" // Static storage for the disk manager -static Collection_t Disks = COLLECTION_INIT(KeyInteger); +static Collection_t Disks = COLLECTION_INIT(KeyId); static UUId_t DiskIdGenerator = 0; /* AHCIStringFlip @@ -214,7 +214,7 @@ AhciManagerCreateDeviceCallback( memcpy(&Device->Descriptor.Serial[0], (const void*)&DeviceInformation->SerialNo[0], 20); // Add disk to list - Key.Value = (int)Device->Descriptor.Device; + Key.Value.Id = Device->Descriptor.Device; CollectionAppend(&Disks, CollectionCreateNode(Key, Device)); return RegisterDisk(Device->Descriptor.Device, Device->Descriptor.Flags); } @@ -228,15 +228,12 @@ AhciManagerRemoveDevice( { CollectionItem_t *dNode = NULL; AhciDevice_t *Device = NULL; - DataKey_t Key; + DataKey_t Key = { .Value.Id = UUID_INVALID }; // Trace TRACE("AhciManagerRemoveDevice(Controller %i, Port %i)", Controller->Device.Id, Port->Id); - // Set initial val - Key.Value = -1; - // Iterate all available devices and find // the one that matches the port/controller _foreach(dNode, &Disks) { @@ -246,7 +243,7 @@ AhciManagerRemoveDevice( break; } } - if (Key.Value == -1) { + if (Key.Value.Id == UUID_INVALID) { return OsError; } @@ -256,7 +253,7 @@ AhciManagerRemoveDevice( // Cleanup resources DestroyBuffer(Device->Buffer); free(Device); - return UnregisterDisk(Key.Value, __DISK_FORCED_REMOVE); + return UnregisterDisk(Key.Value.Id, __DISK_FORCED_REMOVE); } /* AhciManagerGetDevice @@ -265,8 +262,6 @@ AhciDevice_t* AhciManagerGetDevice( _In_ UUId_t Disk) { - // Variables - DataKey_t Key; - Key.Value = (int)Disk; + DataKey_t Key = { .Value.Id = Disk }; return CollectionGetDataByKey(&Disks, Key, 0); } diff --git a/modules/storage/ahci/port.c b/modules/storage/ahci/port.c index 8e58fe507..4a91329b2 100644 --- a/modules/storage/ahci/port.c +++ b/modules/storage/ahci/port.c @@ -358,11 +358,12 @@ AhciPortInterruptHandler( if (DoneCommands != 0) { for (i = 0; i < AHCI_MAX_PORTS; i++) { if (DoneCommands & (1 << i)) { - size_t Offset = i * AHCI_RECIEVED_FIS_SIZE; - Key.Value = i; - tNode = CollectionGetNodeByKey(Port->Transactions, Key, 0); + size_t Offset = i * AHCI_RECIEVED_FIS_SIZE; + Key.Value.Integer = i; + tNode = CollectionGetNodeByKey(Port->Transactions, Key, 0); + assert(tNode != NULL); - Transaction = (AhciTransaction_t*)tNode->Data; + Transaction = (AhciTransaction_t*)tNode->Data; // Remove and destroy node CollectionRemoveByNode(Port->Transactions, tNode); diff --git a/modules/storage/msd/main.c b/modules/storage/msd/main.c index e4e62c9c6..28d91be6b 100644 --- a/modules/storage/msd/main.c +++ b/modules/storage/msd/main.c @@ -79,7 +79,7 @@ OsStatus_t OnLoad(void) { // Initialize state for this driver - GlbMsdDevices = CollectionCreate(KeyInteger); + GlbMsdDevices = CollectionCreate(KeyId); return UsbInitialize(); } @@ -106,9 +106,8 @@ OsStatus_t OnRegister( _In_ MCoreDevice_t *Device) { - // Variables MsdDevice_t *MsdDevice = NULL; - DataKey_t Key; + DataKey_t Key = { .Value.Id = Device->Id }; // Register the new controller MsdDevice = MsdDeviceCreate((MCoreUsbDevice_t*)Device); @@ -118,8 +117,6 @@ OnRegister( return OsError; } - // Append the controller to our list - Key.Value = (int)Device->Id; CollectionAppend(GlbMsdDevices, CollectionCreateNode(Key, MsdDevice)); return OsSuccess; } @@ -131,13 +128,8 @@ OsStatus_t OnUnregister( _In_ MCoreDevice_t *Device) { - // Variables MsdDevice_t *MsdDevice = NULL; - DataKey_t Key; - - // Set the key to the id of the device to find - // the bound controller - Key.Value = (int)Device->Id; + DataKey_t Key = { .Value.Id = Device->Id }; // Lookup controller MsdDevice = (MsdDevice_t*) @@ -187,10 +179,7 @@ OnQuery( // Get parameters StorageDescriptor_t NullDescriptor; MsdDevice_t *Device = NULL; - DataKey_t Key; - - // Lookup device - Key.Value = (int)Arg0->Data.Value; + DataKey_t Key = { .Value.Id = Arg0->Data.Value }; Device = (MsdDevice_t*)CollectionGetDataByKey(GlbMsdDevices, Key, 0); // Write the descriptor back @@ -210,10 +199,7 @@ OnQuery( // Get parameters StorageOperation_t *Operation = (StorageOperation_t*)Arg1->Data.Buffer; MsdDevice_t *Device = NULL; - DataKey_t Key; - - // Lookup device - Key.Value = (int)Arg0->Data.Value; + DataKey_t Key = { .Value.Id = Arg0->Data.Value }; Device = (MsdDevice_t*)CollectionGetDataByKey(GlbMsdDevices, Key, 0); // Determine the kind of operation diff --git a/revision.h b/revision.h index 3ca46f320..84ebe5724 100644 --- a/revision.h +++ b/revision.h @@ -4,11 +4,11 @@ #define _REVISION_H_ #define BUILD_DATE "01 November 2018" -#define BUILD_TIME "12:53:08" +#define BUILD_TIME "15:10:49" #define BUILD_SYSTEM "clang" #define REVISION_MAJOR 0 #define REVISION_MINOR 4 -#define REVISION_BUILD 9241 +#define REVISION_BUILD 9251 #endif //!_REVISION_H_ diff --git a/services/devicemanager/arch/x86/pcienumerate.c b/services/devicemanager/arch/x86/pcienumerate.c index 40d42f65c..641ea6863 100644 --- a/services/devicemanager/arch/x86/pcienumerate.c +++ b/services/devicemanager/arch/x86/pcienumerate.c @@ -229,14 +229,14 @@ PciCheckFunction( } // Add to the flat list - lKey.Value = 0; + lKey.Value.Integer = 0; CollectionAppend(__GlbPciDevices, CollectionCreateNode(lKey, Device)); // Add to list if (Pcs->Class == PCI_CLASS_BRIDGE && Pcs->Subclass == PCI_BRIDGE_SUBCLASS_PCI) { Device->IsBridge = 1; - lKey.Value = 1; + lKey.Value.Integer = 1; CollectionAppend(Parent->Children, CollectionCreateNode(lKey, Device)); Device->Children = CollectionCreate(KeyInteger); @@ -302,7 +302,7 @@ PciCheckFunction( // Set keys and type Device->IsBridge = 0; - lKey.Value = 0; + lKey.Value.Integer = 0; CollectionAppend(Parent->Children, CollectionCreateNode(lKey, Device)); } } diff --git a/services/devicemanager/main.c b/services/devicemanager/main.c index 467b21257..3c49ae914 100644 --- a/services/devicemanager/main.c +++ b/services/devicemanager/main.c @@ -34,8 +34,8 @@ /* Globals * Keep track of all devices and contracts */ -static Collection_t Contracts = COLLECTION_INIT(KeyInteger); -static Collection_t Devices = COLLECTION_INIT(KeyInteger); +static Collection_t Contracts = COLLECTION_INIT(KeyId); +static Collection_t Devices = COLLECTION_INIT(KeyId); static UUId_t DeviceIdGenerator = 0; static UUId_t ContractIdGenerator = 0; @@ -117,10 +117,7 @@ OnEvent( // Extract argumenters MCoreDevice_t *Device = NULL; OsStatus_t Result = OsError; - DataKey_t Key; - - // Lookup device - Key.Value = (int)Message->Arguments[0].Data.Value; + DataKey_t Key = { .Value.Id = Message->Arguments[0].Data.Value }; Device = CollectionGetDataByKey(&Devices, Key, 0); // Sanitizie @@ -211,9 +208,8 @@ DmRegisterDevice( _In_ Flags_t Flags, _Out_ UUId_t* Id) { - // Variables - MCoreDevice_t *CopyDevice = NULL; - DataKey_t Key; + MCoreDevice_t* CopyDevice = NULL; + DataKey_t Key; // Argument checks _CRT_UNUSED(Parent); @@ -232,7 +228,7 @@ DmRegisterDevice( // Generate id and update out *Id = Device->Id = DeviceIdGenerator++; - Key.Value = (int)Device->Id; + Key.Value.Id = Device->Id; // Allocate our own copy of the device CopyDevice = (MCoreDevice_t*)malloc(Device->Length); @@ -272,7 +268,7 @@ DmRegisterContract( Contract->DeviceId, &Contract->Name[0]); // Lookup device - Key.Value = (int)Contract->DeviceId; + Key.Value.Id = Contract->DeviceId; if (CollectionGetDataByKey(&Devices, Key, 0) == NULL) { ERROR("Device id %u was not registered with the device manager", Contract->DeviceId); @@ -282,7 +278,7 @@ DmRegisterContract( // Update contract id *Id = ContractIdGenerator++; Contract->ContractId = *Id; - Key.Value = (int)*Id; + Key.Value.Id = *Id; // Allocate our own copy of the contract CopyContract = (MContract_t*)malloc(sizeof(MContract_t)); diff --git a/services/filemanager/disk.c b/services/filemanager/disk.c index 26c96553a..79a6d8573 100644 --- a/services/filemanager/disk.c +++ b/services/filemanager/disk.c @@ -54,11 +54,8 @@ VfsResolveQueueExecute(void) // Iterate nodes and resolve foreach(fNode, VfsGetResolverQueue()) { FileSystem_t *Fs = (FileSystem_t*)fNode->Data; - DataKey_t Key; - - // Try to resolve it now + DataKey_t Key = { .Value.Id = Fs->Descriptor.Disk.Device }; Fs->Module = VfsResolveFileSystem(Fs); - Key.Value = (int)Fs->Descriptor.Disk.Device; // Sanitize the module - must exist if (Fs->Module == NULL) { @@ -96,7 +93,7 @@ DiskRegisterFileSystem( // Variables FileSystem_t *Fs = NULL; char IdentBuffer[8]; - DataKey_t Key; + DataKey_t Key = { .Value.Id = Disk->Device }; // Trace TRACE("DiskRegisterFileSystem(Sector %u, Size %u, Type %u)", @@ -104,7 +101,6 @@ DiskRegisterFileSystem( // Allocate a new disk id UUId_t Id = VfsIdentifierAllocate(Disk); - Key.Value = (int)Disk->Device; // Prep the buffer so we can build // a new fs-identifier @@ -184,7 +180,7 @@ VfsRegisterDisk( { // Variables FileSystemDisk_t *Disk = NULL; - DataKey_t Key; + DataKey_t Key = { .Value.Id = Device }; // Trace TRACE("RegisterDisk(Driver %u, Device %u, Flags 0x%x)", @@ -196,7 +192,6 @@ VfsRegisterDisk( Disk->Driver = Driver; Disk->Device = Device; Disk->Flags = Flags; - Key.Value = (int)Device; if (StorageQuery(Driver, Device, &Disk->Descriptor) != OsSuccess) { free(Disk); return OsError; @@ -218,13 +213,9 @@ VfsUnregisterDisk( _In_ UUId_t Device, _In_ Flags_t Flags) { - // Variables FileSystemDisk_t *Disk = NULL; CollectionItem_t *lNode = NULL; - DataKey_t Key; - - /* Setup pre-stuff */ - Key.Value = (int)Device; + DataKey_t Key = { .Value.Id = Device }; lNode = CollectionGetNodeByKey(VfsGetFileSystems(), Key, 0); // Keep iterating untill no more FS's are present on disk diff --git a/services/filemanager/functions.c b/services/filemanager/functions.c index 5e6e811d3..1899c16c0 100644 --- a/services/filemanager/functions.c +++ b/services/filemanager/functions.c @@ -84,9 +84,7 @@ VfsIsHandleValid( _Out_ FileSystemEntryHandle_t** EntryHandle) { CollectionItem_t *Node; - DataKey_t Key; - - Key.Value = (int)Handle; + DataKey_t Key = { .Value.Id = Handle }; Node = CollectionGetNodeByKey(VfsGetOpenHandles(), Key, 0); if (Node == NULL) { ERROR("Invalid handle given for file"); @@ -176,7 +174,7 @@ VfsVerifyAccessToPath( // If our requested mode is exclusive, then we must verify // none in our sub-path is opened in exclusive if (Access & __FILE_WRITE_ACCESS && !(Access & __FILE_WRITE_SHARE) && - Node->Key.Value != PathHash) { + Node->Key.Value.Id != PathHash) { // Check if contains the entirety of , if it does then deny // the request as we try to open a higher-level entry in exclusive mode if (MStringCompare(Entry->Path, Path, 0) != MSTRING_NO_MATCH) { @@ -186,7 +184,7 @@ VfsVerifyAccessToPath( } // Have we found the existing already opened file? - if (Node->Key.Value == PathHash) { + if (Node->Key.Value.Id == PathHash) { if (Entry->IsLocked != UUID_INVALID) { ERROR("File is opened in exclusive mode already, access denied."); return FsAccessDenied; @@ -265,7 +263,7 @@ VfsOpenInternal( if ((Options & __FILE_TRUNCATE) && Created == 0 && VfsEntryIsFile(Entry)) { Code = Filesystem->Module->ChangeFileSize(&Filesystem->Descriptor, Entry, 0); } - Key.Value = (int)Entry->Hash; + Key.Value.Id = Entry->Hash; CollectionAppend(VfsGetOpenFiles(), CollectionCreateNode(Key, Entry)); } } @@ -388,7 +386,7 @@ VfsOpenEntry( Handle->Access = Access; Handle->Options = Options; - Key.Value = (int)Handle->Id; + Key.Value.Id = Handle->Id; CollectionAppend(VfsGetOpenHandles(), CollectionCreateNode(Key, Handle)); *FileId = Handle->Id; } @@ -408,7 +406,7 @@ VfsCloseEntry( FileSystemCode_t Code; CollectionItem_t* Node; FileSystem_t *Fs; - DataKey_t Key; + DataKey_t Key = { .Value.Id = Handle }; TRACE("VfsCloseEntry(Handle %u)", Handle); @@ -416,7 +414,6 @@ VfsCloseEntry( if (Code != FsOk) { return Code; } - Key.Value = (int)Handle; Node = CollectionGetNodeByKey(VfsGetOpenHandles(), Key, 0); Entry = EntryHandle->Entry; @@ -448,7 +445,7 @@ VfsCloseEntry( // Last reference? // Cleanup the file in case of no refs if (Entry->References == 0) { - Key.Value = (int)Entry->Hash; + Key.Value.Id = Entry->Hash; CollectionRemoveByKey(VfsGetOpenFiles(), Key); Code = Fs->Module->CloseEntry(&Fs->Descriptor, Entry); } @@ -496,12 +493,12 @@ VfsDeletePath( if (Code != FsOk) { return Code; } - Key.Value = (int)EntryHandle->Entry->Hash; - Code = Fs->Module->DeleteEntry(&Fs->Descriptor, EntryHandle); + Key.Value.Id = EntryHandle->Entry->Hash; + Code = Fs->Module->DeleteEntry(&Fs->Descriptor, EntryHandle); if (Code == FsOk) { // Cleanup handles and open file CollectionRemoveByKey(VfsGetOpenFiles(), Key); - Key.Value = (int)Handle; + Key.Value.Id = Handle; CollectionRemoveByKey(VfsGetOpenHandles(), Key); } } diff --git a/services/filemanager/main.c b/services/filemanager/main.c index 552af75cb..a466c9f3e 100644 --- a/services/filemanager/main.c +++ b/services/filemanager/main.c @@ -59,12 +59,12 @@ static const char *FunctionNames[] = { // Static storage for the filemanager static int DiskTable[__FILEMANAGER_MAXDISKS] = { 0 }; -static Collection_t ResolveQueue = COLLECTION_INIT(KeyInteger); -static Collection_t FileSystems = COLLECTION_INIT(KeyInteger); -static Collection_t OpenHandles = COLLECTION_INIT(KeyInteger); -static Collection_t OpenFiles = COLLECTION_INIT(KeyInteger); -static Collection_t Modules = COLLECTION_INIT(KeyInteger); -static Collection_t Disks = COLLECTION_INIT(KeyInteger); +static Collection_t ResolveQueue = COLLECTION_INIT(KeyId); +static Collection_t FileSystems = COLLECTION_INIT(KeyId); +static Collection_t OpenHandles = COLLECTION_INIT(KeyId); +static Collection_t OpenFiles = COLLECTION_INIT(KeyId); +static Collection_t Modules = COLLECTION_INIT(KeyId); +static Collection_t Disks = COLLECTION_INIT(KeyId); //static UUId_t FileSystemIdGenerator = 0; static UUId_t FileIdGenerator = 0; diff --git a/services/filemanager/modules.c b/services/filemanager/modules.c index 1ce2fa4e6..4896ffed2 100644 --- a/services/filemanager/modules.c +++ b/services/filemanager/modules.c @@ -49,7 +49,7 @@ VfsResolveFileSystem( // Variables FileSystemModule_t *Module = NULL; CollectionItem_t *fNode = NULL; - DataKey_t Key; + DataKey_t Key = { 0 }; // Trace TRACE("VfsResolveFileSystem(Type %u)", FileSystem->Type); @@ -142,9 +142,6 @@ VfsResolveFileSystem( // Trace TRACE("Function table present, loading was successful"); - - // Last thing is to add it to the list - Key.Value = 0; CollectionAppend(VfsGetModules(), CollectionCreateNode(Key, Module)); return Module; } diff --git a/services/usbmanager/core.c b/services/usbmanager/core.c index c7390405b..edc287647 100644 --- a/services/usbmanager/core.c +++ b/services/usbmanager/core.c @@ -329,8 +329,6 @@ UsbQueryConfigurationDescriptors( // Cleanup buffer free(FullDescriptor); - - // Done return OsSuccess; } @@ -377,7 +375,7 @@ UsbCoreControllerRegister( { // Variables UsbController_t *Controller = NULL; - DataKey_t Key; + DataKey_t Key = { 0 }; // Allocate a new instance and reset all members Controller = (UsbController_t*)malloc(sizeof(UsbController_t)); @@ -391,7 +389,6 @@ UsbCoreControllerRegister( // Reserve address 0, it's setup address Controller->AddressMap[0] |= 0x1; - Key.Value = 0; return CollectionAppend(GlbUsbControllers, CollectionCreateNode(Key, Controller)); }