void list_push_front(list_t* list, void* data) { list->head = list_create_node(data, 0, list->head); if (list->head->next) list->head->next->prev = list->head; if (!list->tail) list->tail = list->head; }
void list_push_back(list_t* list, void* data) { list->tail = list_create_node(data, list->tail, 0); if (list->tail->prev) list->tail->prev->next = list->tail; if (!list->head) list->head = list->tail; }
list_t *list_insert_node(list_t *list, list_node *pos, void *value, int before) { list_node *node = list_create_node(list, value); if (node == NULL) { return NULL; } if (before) { node->next = pos; node->prev = pos->prev; if (list->head == pos) { list->head = node; } } else { node->next = pos->next; node->prev = pos; if (list->tail == pos) { list->tail = node; } } if (node->prev != NULL) { node->prev->next = node; } if (node->next != NULL) { node->next->prev = node; } list->len += 1; return list; }
/** add item to any position */ void list_add (linked_list* _this, void* item, int position) { // index out of list size if (position > _this->size) { return; } // add to head if (position == 0) { list_add_first(_this, item); } else if (position == _this->size) { // add to tail list_add_last(_this, item); } else { // insert between head and tail node* n = _this->head; int i = 0; // loop until the position while (i < position) { n = n->next; i++; } // insert new node to position node* newNode = list_create_node(item); list_insert_before(_this, n, newNode); _this->size++; } }
TmId_t TimersCreateTimer(TimerHandler_t Callback, void *Args, MCoreTimerType_t Type, uint32_t Timeout) { MCoreTimer_t *TimerInfo; TmId_t Id; /* Sanity */ if (GlbTimersInitialized != 1) TimersInit(); /* Allocate */ TimerInfo = (MCoreTimer_t*)kmalloc(sizeof(MCoreTimer_t)); TimerInfo->Callback = Callback; TimerInfo->Args = Args; TimerInfo->Type = Type; TimerInfo->PeriodicMs = Timeout; TimerInfo->MsLeft = Timeout; /* Append to list */ list_append(GlbTimers, list_create_node(GlbTimerIds, TimerInfo)); /* Increase */ Id = GlbTimerIds; GlbTimerIds++; return Id; }
DevId_t DmCreateDevice(char *Name, MCoreDevice_t *Device) { /* Grap lock */ SpinlockAcquire(&GlbDmLock); /* Set name and data */ Device->Name = strdup(Name); Device->Id = GlbDmIdentfier; /* Increase */ GlbDmIdentfier++; /* Release */ SpinlockRelease(&GlbDmLock); /* Add to list */ list_append(GlbDmDeviceList, list_create_node(Device->Id, (void*)Device)); /* Call some broadcast function so systems know a new device is avaiable * depending on the device type */ switch (Device->Type) { /* Give access to timer */ case DeviceTimer: { /* Cast */ MCoreTimerDevice_t *Timer = (MCoreTimerDevice_t*)Device->Data; Timer->ReportMs = TimersApplyMs; } break; /* Give access to input */ case DeviceInput: { /* Cast */ MCoreInputDevice_t *Input = (MCoreInputDevice_t*)Device->Data; Input->ReportEvent = EmCreateEvent; } break; /* Register with Vfs */ case DeviceStorage: { /* Call */ VfsRegisterDisk(Device->Id); } break; /* No special actions */ default: break; } /* Info Log */ LogInformation("DRVM", "New Device: %s", Name); /* Done */ return Device->Id; }
int list_push_front(List* l, int data) { ListNode *node = list_create_node(data, l->head, NULL); if (!node) return 0; l->head = node; return 1; }
t_bool list_add_elem_at_front(t_list *front_ptr, void *elem) { t_node *node = list_create_node(elem); if (!node) { return FALSE; } node->next = *front_ptr; *front_ptr = node; return TRUE; }
/* * Inserts an element in the head of the list. */ int32_t collections_list_insert(collections_listnode *start, void *data) { collections_listnode *ret = list_create_node(start, start, data); if (ret) { // success ... return 0; } return 1; }
int list_push_front(List* l, int data) { int pos; ListNode *node = list_create_node(l, data, l->head, &pos); if (!node) return 0; l->head = pos; ++l->total; return 1; }
int list_push_back(List *l, int data) { ListNode *node; ListNode *last = list_get_tail(l); if (!last) return list_push_front(l, data); node = list_create_node(data, NULL, last); if (!node) return 0; return 1; }
/* Register fs */ void VfsInstallFileSystem(MCoreFileSystem_t *Fs) { /* Ready the buffer */ char IdentBuffer[8]; memset(IdentBuffer, 0, 8); /* Copy the storage ident over */ strcpy(IdentBuffer, "St"); itoa(GlbFileSystemId, (IdentBuffer + 2), 10); /* Construct the identifier */ Fs->Identifier = MStringCreate(&IdentBuffer, StrASCII); /* Setup last */ Fs->Lock = MutexCreate(); /* Add to list */ list_append(GlbFileSystems, list_create_node(Fs->DiskId, Fs)); /* Increament */ GlbFileSystemId++; /* Start init? */ if (Fs->Flags & VFS_MAIN_DRIVE && !GlbVfsInitHasRun) { /* Process Request */ MCoreProcessRequest_t *ProcRequest = (MCoreProcessRequest_t*)kmalloc(sizeof(MCoreProcessRequest_t)); /* Print */ LogInformation("VFSM", "Boot Drive Detected, Running Init"); /* Append init path */ MString_t *Path = MStringCreate(Fs->Identifier->Data, StrUTF8); MStringAppendChars(Path, FILESYSTEM_INIT); /* Create Request */ ProcRequest->Type = ProcessSpawn; ProcRequest->Path = Path; ProcRequest->Arguments = NULL; ProcRequest->Cleanup = 1; /* Send */ PmCreateRequest(ProcRequest); /* Set */ GlbVfsInitHasRun = 1; } }
int main(void) { linked_list_t *list = (linked_list_t*)malloc(sizeof(linked_list_t)); list_init(list); for (int i = 0; i < 10; i++){ char string[100]; sprintf(string, "Number: %d", i); list_add_first(list_create_node(i, string), list); list_add_last (list_create_node(i, string), list); } printf("Non-empty list\n"); list_print(list); list_clear(list); printf("Empty list\n"); list_print(list); list_free(list); free(list); return 0; }
int list_push_back(List *l, int data) { ListNode *node; ListNode *last = list_get_tail(l); int pos; if (!last) return list_push_front(l, data); node = list_create_node(l, data, -1, &pos); if (!node) return 0; last->next = pos; ++l->total; return 1; }
int list_insert_at(List *l, int pos, int data) { int i; ListNode *at = list_get_head(l), *node; if (pos==0) return list_push_front(l, data); for (i=0; i<pos; i++) { at = at->next; if (!at && i!=pos-1) return 0; } node = list_create_node(data, at, at->previous); return node!=NULL; }
/** add item to tail */ void list_add_last (linked_list* _this, void* item) { node* newNode = list_create_node(item); node* head = _this->head; node* tail = _this->tail; // list is empty if (head == NULL) _this->head = newNode; else { // has item(s) node* lastNode = tail; if (tail == NULL) // only head node lastNode = head; lastNode->next = newNode; newNode->prev = lastNode; _this->tail = newNode; } _this->size++; }
/* Create a request */ void DmCreateRequest(MCoreDeviceRequest_t *Request) { /* Sanity */ if (Request->Length > DEVICEMANAGER_MAX_IO_SIZE) { Request->Status = RequestInvalidParameters; return; } /* Append it to our request list */ list_append(GlbDmEventQueue, list_create_node(0, Request)); /* Set Pending */ Request->Status = RequestPending; /* Notify request thread */ SemaphoreV(GlbDmEventLock); }
t_bool list_add_elem_at_back(t_list* front_ptr, void *elem) { t_node *node = list_create_node(elem); t_list list = *front_ptr; if (!node) { return FALSE; } if (!list) { *front_ptr = node; } else { list = *front_ptr; while (list->next) { list = list->next; } list->next = node; } return TRUE; }
list_t *list_add_node_head(list_t *list, void *value) { list_node *node = list_create_node(list, value); if (node == NULL) { return NULL; } if (list->len == 0) { node->next = node->prev = NULL; list->head = list->tail = node; } else { node->next = list->head; node->prev = NULL; list->head->prev = node; list->head = node; } list->len += 1; return list; }
/* * Add a Node to the Middle * Of the Passed List * The Node will Be Added * AFTER the passed lnptr * node pointer. * Return a Pointer to the Data * * position: * ADD_BEFORE before the given lnptr node * ADD_AFTER after the given lnptr node */ void* list_add_node_middle(struct linkList* lptr, struct linkNode* lnptr, void* dptr, int position) { struct linkNode* nptr; if (lnptr == NULL) return NULL; if ((position != ADD_BEFORE) && (position != ADD_AFTER)) return NULL; /* Create the Node */ nptr = list_create_node(dptr); /* Link the Node into The List */ if (lptr->root == NULL) { /* Set as Root Node */ lptr->root = nptr; lptr->last = nptr; } else if (lnptr->next != NULL) { /* Link Node in Middle */ if (position == ADD_AFTER) { /* link before */ lnptr->next->prev = nptr; nptr->next = lnptr->next; lnptr->next = nptr; nptr->prev = lnptr; } else { /* link after */ lnptr->prev->next = nptr; nptr->prev = lnptr->prev; nptr->next = lnptr; lnptr->prev = nptr; } } else { /* Link Node as Last */ lptr->last->next = nptr; nptr->prev = lptr->last; lptr->last = nptr; } return nptr->data; }
/* * Add a Node to the End * Of the Passed List * Return a Pointer to the Data */ void* list_add_node_end(struct linkList* lptr, void* dptr) { struct linkNode* nptr; if (lptr == NULL) return NULL; /* Create the Node */ nptr = list_create_node(dptr); /* Link the Node into The List */ if (lptr->root == NULL) { /* Set as Root Node */ lptr->root = nptr; lptr->last = nptr; } else { /* Link Node as Last */ lptr->last->next = nptr; nptr->prev = lptr->last; lptr->last = nptr; } return nptr->data; }
t_bool list_add_elem_at_position(t_list* front_ptr, void *elem, unsigned int position) { if (position == 0) { return list_add_elem_at_front(front_ptr, elem); } if (!*front_ptr) { return FALSE; } t_list list = *front_ptr; while (--position) { list = list->next; if (!list) { return FALSE; } } t_node *node = list_create_node(elem); if (!node) { return FALSE; } node->next = list->next; list->next = node; return TRUE; }
int list_insert_at(List *l, int pos, int data) { ListNode *node; int i, position; int at = l->head, pat; if (pos==0) return list_push_front(l, data); for (i=0; i<pos; i++) { pat = at; at = list_get_node(l, at)->next; if (at<0 && i!=pos-1) return 0; } node = list_create_node(l, data, at, &position); if (node) list_get_node(l, pat)->next = position; ++l->total; return node!=NULL; }
/* For each function we create a * pci_device and add it to the list */ void PciCheckFunction(list_t *Bridge, uint8_t Bus, uint8_t Device, uint8_t Function) { uint8_t SecondBus; PciNativeHeader_t *Pcs; PciDevice_t *PciDevice; /* Allocate */ Pcs = (PciNativeHeader_t*)kmalloc(sizeof(PciNativeHeader_t)); PciDevice = (PciDevice_t*)kmalloc(sizeof(PciDevice_t)); /* Get full information */ PciReadFunction(Pcs, Bus, Device, Function); /* Set information */ PciDevice->Header = Pcs; PciDevice->Bus = Bus; PciDevice->Device = Device; PciDevice->Function = Function; PciDevice->Children = NULL; /* Info * Ignore the spam of device_id 0x7a0 in VMWare*/ if (Pcs->DeviceId != 0x7a0) { #ifdef X86_PCI_DIAGNOSE printf(" * [%d:%d:%d][%d:%d:%d] Vendor 0x%x, Device 0x%x : %s\n", Pcs->Class, Pcs->Subclass, Pcs->Interface, Bus, Device, Function, Pcs->VendorId, Pcs->DeviceId, PciToString(Pcs->Class, Pcs->Subclass, Pcs->Interface)); #endif } /* Do some disabling, but NOT on the video or bridge */ if ((Pcs->Class != PCI_DEVICE_CLASS_BRIDGE) && (Pcs->Class != PCI_DEVICE_CLASS_VIDEO)) { /* Disable Device untill further notice */ uint16_t PciSettings = PciRead16(Bus, Device, Function, 0x04); PciWrite16(Bus, Device, Function, 0x04, PciSettings | X86_PCI_COMMAND_INTDISABLE); } /* Add to list */ if (Pcs->Class == PCI_DEVICE_CLASS_BRIDGE && Pcs->Subclass == PCI_DEVICE_SUBCLASS_PCI) { PciDevice->Type = X86_PCI_TYPE_BRIDGE; list_append(Bridge, list_create_node(X86_PCI_TYPE_BRIDGE, PciDevice)); } else { PciDevice->Type = X86_PCI_TYPE_DEVICE; list_append(Bridge, list_create_node(X86_PCI_TYPE_DEVICE, PciDevice)); } /* Is this a secondary (PCI) bus */ if ((Pcs->Class == PCI_DEVICE_CLASS_BRIDGE) && (Pcs->Subclass == PCI_DEVICE_SUBCLASS_PCI)) { /* Uh oh, this dude has children */ PciDevice->Children = list_create(LIST_SAFE); SecondBus = PciReadSecondaryBusNumber(Bus, Device, Function); PciCheckBus(PciDevice->Children, SecondBus); } }
/* For each function we create a * pci_device and add it to the list */ void PciCheckFunction(list_t *Bridge, PciBus_t *BusIo, uint8_t Bus, uint8_t Device, uint8_t Function) { /* Vars */ MCoreDevice_t *mDevice; uint8_t SecondBus; PciNativeHeader_t *Pcs; PciDevice_t *PciDevice; /* Allocate */ Pcs = (PciNativeHeader_t*)kmalloc(sizeof(PciNativeHeader_t)); PciDevice = (PciDevice_t*)kmalloc(sizeof(PciDevice_t)); /* Get full information */ PciReadFunction(Pcs, BusIo, Bus, Device, Function); /* Set information */ PciDevice->PciBus = BusIo; PciDevice->Header = Pcs; PciDevice->Bus = Bus; PciDevice->Device = Device; PciDevice->Function = Function; PciDevice->Children = NULL; /* Info * Ignore the spam of device_id 0x7a0 in VMWare*/ if (Pcs->DeviceId != 0x7a0) { #ifdef X86_PCI_DIAGNOSE printf(" * [%d:%d:%d][%d:%d:%d] Vendor 0x%x, Device 0x%x : %s\n", Pcs->Class, Pcs->Subclass, Pcs->Interface, Bus, Device, Function, Pcs->VendorId, Pcs->DeviceId, PciToString(Pcs->Class, Pcs->Subclass, Pcs->Interface)); #endif } /* Do some disabling, but NOT on the video or bridge */ if ((Pcs->Class != PCI_DEVICE_CLASS_BRIDGE) && (Pcs->Class != PCI_DEVICE_CLASS_VIDEO)) { /* Disable Device untill further notice */ uint16_t PciSettings = PciRead16(BusIo, Bus, Device, Function, 0x04); PciWrite16(BusIo, Bus, Device, Function, 0x04, PciSettings | X86_PCI_COMMAND_INTDISABLE); } /* Add to list */ if (Pcs->Class == PCI_DEVICE_CLASS_BRIDGE && Pcs->Subclass == PCI_DEVICE_SUBCLASS_PCI) { PciDevice->Type = X86_PCI_TYPE_BRIDGE; list_append(Bridge, list_create_node(X86_PCI_TYPE_BRIDGE, PciDevice)); /* Uh oh, this dude has children */ PciDevice->Children = list_create(LIST_SAFE); /* Get secondary bus no and iterate */ SecondBus = PciReadSecondaryBusNumber(BusIo, Bus, Device, Function); PciCheckBus(PciDevice->Children, BusIo, SecondBus); } else { /* This is an device */ PciDevice->Type = X86_PCI_TYPE_DEVICE; list_append(Bridge, list_create_node(X86_PCI_TYPE_DEVICE, PciDevice)); /* Register device with the OS */ mDevice = (MCoreDevice_t*)kmalloc(sizeof(MCoreDevice_t)); memset(mDevice, 0, sizeof(MCoreDevice_t)); /* Setup information */ mDevice->VendorId = Pcs->VendorId; mDevice->DeviceId = Pcs->DeviceId; mDevice->Class = Pcs->Class; mDevice->Subclass = Pcs->Subclass; mDevice->Interface = Pcs->Interface; mDevice->Segment = (DevInfo_t)BusIo->Segment; mDevice->Bus = Bus; mDevice->Device = Device; mDevice->Function = Function; mDevice->IrqLine = -1; mDevice->IrqPin = (int)Pcs->InterruptPin; mDevice->IrqAvailable[0] = Pcs->InterruptLine; /* Type */ mDevice->Type = DeviceUnknown; mDevice->Data = NULL; /* Save bus information */ mDevice->BusInformation = PciDevice; /* Initial */ mDevice->Driver.Name = NULL; mDevice->Driver.Version = -1; mDevice->Driver.Data = NULL; mDevice->Driver.Status = DriverNone; /* Read Bars */ PciReadBars(BusIo, mDevice, Pcs->HeaderType); /* Register */ DmCreateDevice((char*)PciToString(Pcs->Class, Pcs->Subclass, Pcs->Interface), mDevice); } }
/* Loads the RD */ void ModuleMgrInit(MCoreBootDescriptor *BootDescriptor) { /* Get pointers */ MCoreRamDiskHeader_t *RdHeader = (MCoreRamDiskHeader_t*)BootDescriptor->RamDiskAddress; /* Sanity */ if (BootDescriptor->RamDiskAddress == 0 || BootDescriptor->RamDiskSize == 0) return; /* Info */ LogInformation("MDMG", "Initializing"); /* Parse Kernel Exports */ PeLoadKernelExports(BootDescriptor->KernelAddress, BootDescriptor->ExportsAddress); /* Validate Members */ if (RdHeader->Magic != RAMDISK_MAGIC) { /* Error! */ LogFatal("MDMG", "Invalid Magic in Ramdisk - 0x%x", RdHeader->Magic); return; } /* Valid Version? */ if (RdHeader->Version != RAMDISK_VERSION_1) { /* Error! */ LogFatal("MDMG", "Invalid RamDisk Version - 0x%x", RdHeader->Version); return; } /* Allocate list */ GlbModMgrModules = list_create(LIST_NORMAL); /* Save Module-Count */ uint32_t FileCount = RdHeader->FileCount; Addr_t RdPtr = BootDescriptor->RamDiskAddress + sizeof(MCoreRamDiskHeader_t); /* Point to first entry */ MCoreRamDiskFileHeader_t *FilePtr = (MCoreRamDiskFileHeader_t*)RdPtr; /* Iterate */ while (FileCount != 0) { /* We only care about modules */ if (FilePtr->Type == RAMDISK_MODULE) { /* Get a pointer to the module header */ MCoreRamDiskModuleHeader_t *ModuleHeader = (MCoreRamDiskModuleHeader_t*)(BootDescriptor->RamDiskAddress + FilePtr->DataOffset); /* Allocate a new module */ MCoreModule_t *Module = (MCoreModule_t*)kmalloc(sizeof(MCoreModule_t)); /* Set */ Module->Name = MStringCreate(ModuleHeader->ModuleName, StrUTF8); Module->Header = ModuleHeader; Module->Descriptor = NULL; /* Add to list */ list_append(GlbModMgrModules, list_create_node(0, Module)); } /* Next! */ FilePtr++; FileCount--; } /* Info */ LogInformation("MDMG", "Found %i Modules", GlbModMgrModules->length); /* Done! */ GlbModMgrInitialized = 1; }