From e50f8940a9e6e018d5db847f2953da7a68f3778f Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Tue, 6 Jan 2026 16:38:42 +0100 Subject: [PATCH] Redesign linked list --- kernel/amd64/proc.c | 9 ++- kernel/irq/irq.c | 18 +++--- kernel/irq/irq.h | 3 +- kernel/libk/list.h | 150 ++++++++------------------------------------ kernel/proc/proc.c | 2 +- kernel/proc/proc.h | 10 +-- 6 files changed, 51 insertions(+), 141 deletions(-) diff --git a/kernel/amd64/proc.c b/kernel/amd64/proc.c index fa36057..bb84b0b 100644 --- a/kernel/amd64/proc.c +++ b/kernel/amd64/proc.c @@ -63,12 +63,15 @@ struct proc* proc_from_elf (uint8_t* elf_contents) { void proc_cleanup (struct proc* proc) { struct limine_hhdm_response* hhdm = limine_hhdm_request.response; - struct proc_mapping *mapping, *mapping_tmp; + struct list_node_link *mapping_link, *mapping_link_tmp; spin_lock (&proc->pd.lock); - linklist_foreach (proc->mappings, mapping, mapping_tmp) { + list_foreach (proc->mappings, mapping_link, mapping_link_tmp) { + struct proc_mapping* mapping = + list_entry (mapping_link, struct proc_mapping, proc_mappings_link); + pmm_free (mapping->paddr, mapping->size / PAGE_SIZE); - linklist_remove (struct proc_mapping*, proc->mappings, mapping); + list_remove (proc->mappings, mapping_link); free (mapping); } diff --git a/kernel/irq/irq.c b/kernel/irq/irq.c index 4774868..86cdfa9 100644 --- a/kernel/irq/irq.c +++ b/kernel/irq/irq.c @@ -11,7 +11,7 @@ /* TODO: figure out a generic way to work with IRQs */ -static struct irq* irqs = NULL; +static struct list_node_link* irqs = NULL; static spin_lock_t irqs_lock; bool irq_attach (void (*func) (void*, void*), void* arg, uint32_t irq_num, uint32_t flags) { @@ -26,7 +26,7 @@ bool irq_attach (void (*func) (void*, void*), void* arg, uint32_t irq_num, uint3 irq->flags = flags; spin_lock (&irqs_lock); - linklist_append (struct irq*, irqs, irq); + list_append (irqs, &irq->irqs_link); spin_unlock (&irqs_lock); #if defined(__x86_64__) @@ -38,22 +38,26 @@ bool irq_attach (void (*func) (void*, void*), void* arg, uint32_t irq_num, uint3 } void irq_detach (void (*func) (void*, void*)) { + struct list_node_link *irq_link, *irq_link_tmp; spin_lock (&irqs_lock); - struct irq *irq, *irq_tmp; - linklist_foreach (irqs, irq, irq_tmp) { + list_foreach (irqs, irq_link, irq_link_tmp) { + struct irq* irq = list_entry (irq_link, struct irq, irqs_link); + if ((uintptr_t)irq->func == (uintptr_t)func) - linklist_remove (struct irq*, irqs, irq); + list_remove (irqs, irq_link); } spin_unlock (&irqs_lock); } struct irq* irq_find (uint32_t irq_num) { + struct list_node_link *irq_link, *irq_link_tmp; spin_lock (&irqs_lock); - struct irq *irq, *irq_tmp; - linklist_foreach (irqs, irq, irq_tmp) { + list_foreach (irqs, irq_link, irq_link_tmp) { + struct irq* irq = list_entry (irq_link, struct irq, irqs_link); + if (irq->irq_num == irq_num) { spin_unlock (&irqs_lock); return irq; diff --git a/kernel/irq/irq.h b/kernel/irq/irq.h index 3123dad..0bde639 100644 --- a/kernel/irq/irq.h +++ b/kernel/irq/irq.h @@ -1,6 +1,7 @@ #ifndef _KERNEL_IRQ_IRQ_H #define _KERNEL_IRQ_IRQ_H +#include #include #define IRQ_INTERRUPT_SAFE (1 << 0) @@ -9,7 +10,7 @@ typedef void (*irq_func_t) (void* arg, void* regs); struct irq { - struct irq* next; + struct list_node_link irqs_link; irq_func_t func; void* arg; diff --git a/kernel/libk/list.h b/kernel/libk/list.h index 74f547e..3757260 100644 --- a/kernel/libk/list.h +++ b/kernel/libk/list.h @@ -1,12 +1,19 @@ #ifndef _KERNEL_LIBK_LIST_H #define _KERNEL_LIBK_LIST_H -#define dlinklist_append(type, head, new) \ +struct list_node_link { + struct list_node_link* next; + struct list_node_link* prev; +}; + +#define list_entry(ptr, type, member) ((type*)((char*)(ptr) - offsetof (type, member))) + +#define list_append(head, new) \ do { \ if ((new) != NULL) { \ (new)->next = NULL; \ if ((head) != NULL) { \ - type __tmp = (head); \ + struct list_node_link* __tmp = (head); \ while (__tmp->next != NULL) { \ __tmp = __tmp->next; \ } \ @@ -19,7 +26,7 @@ } \ } while (0) -#define dlinklist_prepend(head, new) \ +#define list_prepend(head, new) \ do { \ if ((new) != NULL) { \ (new)->prev = NULL; \ @@ -31,7 +38,7 @@ } \ } while (0) -#define dlinklist_remove(head, ele) \ +#define list_remove(head, ele) \ do { \ if ((ele) != NULL) { \ if ((ele)->prev != NULL) { \ @@ -47,10 +54,10 @@ } \ } while (0) -#define dlinklist_find(type, head, out, propname, propvalue) \ +#define list_find(head, out, propname, propvalue) \ do { \ (out) = NULL; \ - type __tmp = (head); \ + struct list_node_link* __tmp = (head); \ while (__tmp) { \ if (__tmp->propname == (propvalue)) { \ (out) = __tmp; \ @@ -60,23 +67,23 @@ } \ } while (0) -#define dlinklist_foreach(head, var, tmp) \ +#define list_foreach(head, var, tmp) \ for (var = (head), tmp = (var ? var->next : NULL); var != NULL; \ var = tmp, tmp = (var ? var->next : NULL)) -#define dlinklist_foreach_index(head, var, tmp, idx) \ +#define list_foreach_index(head, var, tmp, idx) \ for ((idx) = 0, var = (head), tmp = (var ? var->next : NULL); var != NULL; \ var = tmp, tmp = (var ? var->next : NULL), (idx)++) -#define dlinklist_foreach_index_limit(head, var, tmp, idx, max) \ +#define list_foreach_index_limit(head, var, tmp, idx, max) \ for ((idx) = 0, var = (head), tmp = (var ? var->next : NULL); var != NULL && (idx) < (max); \ var = tmp, tmp = (var ? var->next : NULL), (idx)++) -#define dlinklist_back(type, head, out) \ +#define list_back(head, out) \ do { \ (out) = NULL; \ if ((head) != NULL) { \ - type __tmp = (head); \ + struct list_node_link* __tmp = (head); \ while (__tmp->next != NULL) { \ __tmp = __tmp->next; \ } \ @@ -84,11 +91,11 @@ } \ } while (0) -#define dlinklist_front(type, head, out) \ +#define list_front(head, out) \ do { \ (out) = NULL; \ if ((head) != NULL) { \ - type __tmp = (head); \ + struct list_node_link* __tmp = (head); \ while (__tmp->prev != NULL) { \ __tmp = __tmp->prev; \ } \ @@ -96,7 +103,7 @@ } \ } while (0) -#define dlinklist_insert_after(head, pos, new) \ +#define list_insert_after(head, pos, new) \ do { \ if ((pos) != NULL && (new) != NULL) { \ (new)->prev = (pos); \ @@ -112,7 +119,7 @@ } \ } while (0) -#define dlinklist_insert_before(head, pos, new) \ +#define list_insert_before(head, pos, new) \ do { \ if ((pos) != NULL && (new) != NULL) { \ (new)->next = (pos); \ @@ -130,11 +137,11 @@ } \ } while (0) -#define dlinklist_index_of(type, head, ele, out_idx) \ +#define list_index_of(head, ele, out_idx) \ do { \ (out_idx) = -1; \ int __idx = 0; \ - type __tmp = (head); \ + struct list_node_link* __tmp = (head); \ while (__tmp != NULL) { \ if (__tmp == (ele)) { \ (out_idx) = __idx; \ @@ -145,11 +152,11 @@ } \ } while (0) -#define dlinklist_index_of_prop(type, head, propname, propvalue, out_idx) \ +#define list_index_of_prop(head, propname, propvalue, out_idx) \ do { \ (out_idx) = -1; \ int __idx = 0; \ - type __tmp = (head); \ + struct list_node_link* __tmp = (head); \ while (__tmp != NULL) { \ if (__tmp->propname == (propvalue)) { \ (out_idx) = __idx; \ @@ -160,109 +167,4 @@ } \ } while (0) -#define linklist_index_of(type, head, ele, out_idx) \ - do { \ - (out_idx) = -1; \ - int __idx = 0; \ - type __tmp = (head); \ - while (__tmp != NULL) { \ - if (__tmp == (ele)) { \ - (out_idx) = __idx; \ - break; \ - } \ - __tmp = __tmp->next; \ - __idx++; \ - } \ - } while (0) - -#define linklist_index_of_prop(type, head, propname, propvalue, out_idx) \ - do { \ - (out_idx) = -1; \ - int __idx = 0; \ - type __tmp = (head); \ - while (__tmp != NULL) { \ - if (__tmp->propname == (propvalue)) { \ - (out_idx) = __idx; \ - break; \ - } \ - __tmp = __tmp->next; \ - __idx++; \ - } \ - } while (0) - -#define linklist_append(type, head, new) \ - do { \ - if ((new) != NULL) { \ - if ((head) != NULL) { \ - type __tmp; \ - (new)->next = NULL; \ - __tmp = (head); \ - while (__tmp->next != NULL) { \ - __tmp = __tmp->next; \ - } \ - __tmp->next = (new); \ - } else { \ - (new)->next = NULL; \ - (head) = (new); \ - } \ - } \ - } while (0) - -#define linklist_remove(type, head, ele) \ - do { \ - if ((head) != NULL && (ele) != NULL) { \ - type __cur = (head); \ - type __prev = NULL; \ - while (__cur != NULL && __cur != (ele)) { \ - __prev = __cur; \ - __cur = __cur->next; \ - } \ - if (__cur == (ele)) { \ - if (__prev != NULL) { \ - __prev->next = __cur->next; \ - } else { \ - (head) = __cur->next; \ - } \ - (ele)->next = NULL; \ - } \ - } \ - } while (0) - -#define linklist_find(type, head, out, propname, propvalue) \ - do { \ - (out) = NULL; \ - type __tmp = (head); \ - while (__tmp) { \ - if (__tmp->propname == (propvalue)) { \ - (out) = __tmp; \ - break; \ - } \ - __tmp = __tmp->next; \ - } \ - } while (0) - -#define linklist_foreach(head, var, tmp) \ - for (var = (head), tmp = (var ? var->next : NULL); var != NULL; \ - var = tmp, tmp = (var ? var->next : NULL)) - -#define linklist_foreach_index(head, var, tmp, idx) \ - for ((idx) = 0, var = (head), tmp = (var ? var->next : NULL); var != NULL; \ - var = tmp, tmp = (var ? var->next : NULL), (idx)++) - -#define linklist_foreach_index_limit(head, var, tmp, idx, max) \ - for ((idx) = 0, var = (head), tmp = (var ? var->next : NULL); var != NULL && (idx) < (max); \ - var = tmp, tmp = (var ? var->next : NULL), (idx)++) - -#define linklist_back(type, head, out) \ - do { \ - (out) = NULL; \ - if ((head) != NULL) { \ - type __tmp = (head); \ - while (__tmp->next != NULL) { \ - __tmp = __tmp->next; \ - } \ - (out) = __tmp; \ - } \ - } while (0) - #endif // _KERNEL_LIBK_LIST_H diff --git a/kernel/proc/proc.c b/kernel/proc/proc.c index 6438855..092104f 100644 --- a/kernel/proc/proc.c +++ b/kernel/proc/proc.c @@ -49,7 +49,7 @@ void proc_map (struct proc* proc, uintptr_t start_paddr, uintptr_t start_vaddr, spin_lock (&proc->pd.lock); - linklist_append (struct proc_mapping*, proc->mappings, mapping); + list_append (proc->mappings, &mapping->proc_mappings_link); for (uintptr_t vpage = start_vaddr, ppage = start_paddr; vpage < start_vaddr + pages * PAGE_SIZE; vpage += PAGE_SIZE, ppage += PAGE_SIZE) { diff --git a/kernel/proc/proc.h b/kernel/proc/proc.h index 43e281c..c45ab8f 100644 --- a/kernel/proc/proc.h +++ b/kernel/proc/proc.h @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -21,20 +22,19 @@ struct cpu; struct proc_mapping { - struct proc_mapping* next; + struct list_node_link proc_mappings_link; + uintptr_t paddr; uintptr_t vaddr; size_t size; -} PACKED; - -struct procw; +}; struct proc { int pid; struct rb_node_link proc_tree_link; struct rb_node_link cpu_run_q_link; - struct proc_mapping* mappings; /* pd.lock implicitly protects this field */ + struct list_node_link* mappings; /* pd.lock implicitly protects this field */ struct proc_platformdata pdata; struct pd pd; spin_lock_t lock;