Redesign linked list
All checks were successful
Build documentation / build-and-deploy (push) Successful in 49s

This commit is contained in:
2026-01-06 16:38:42 +01:00
parent d09e4d97ad
commit e50f8940a9
6 changed files with 51 additions and 141 deletions

View File

@@ -63,12 +63,15 @@ struct proc* proc_from_elf (uint8_t* elf_contents) {
void proc_cleanup (struct proc* proc) { void proc_cleanup (struct proc* proc) {
struct limine_hhdm_response* hhdm = limine_hhdm_request.response; 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); 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); pmm_free (mapping->paddr, mapping->size / PAGE_SIZE);
linklist_remove (struct proc_mapping*, proc->mappings, mapping); list_remove (proc->mappings, mapping_link);
free (mapping); free (mapping);
} }

View File

@@ -11,7 +11,7 @@
/* TODO: figure out a generic way to work with IRQs */ /* 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; static spin_lock_t irqs_lock;
bool irq_attach (void (*func) (void*, void*), void* arg, uint32_t irq_num, uint32_t flags) { 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; irq->flags = flags;
spin_lock (&irqs_lock); spin_lock (&irqs_lock);
linklist_append (struct irq*, irqs, irq); list_append (irqs, &irq->irqs_link);
spin_unlock (&irqs_lock); spin_unlock (&irqs_lock);
#if defined(__x86_64__) #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*)) { void irq_detach (void (*func) (void*, void*)) {
struct list_node_link *irq_link, *irq_link_tmp;
spin_lock (&irqs_lock); spin_lock (&irqs_lock);
struct irq *irq, *irq_tmp; list_foreach (irqs, irq_link, irq_link_tmp) {
linklist_foreach (irqs, irq, irq_tmp) { struct irq* irq = list_entry (irq_link, struct irq, irqs_link);
if ((uintptr_t)irq->func == (uintptr_t)func) if ((uintptr_t)irq->func == (uintptr_t)func)
linklist_remove (struct irq*, irqs, irq); list_remove (irqs, irq_link);
} }
spin_unlock (&irqs_lock); spin_unlock (&irqs_lock);
} }
struct irq* irq_find (uint32_t irq_num) { struct irq* irq_find (uint32_t irq_num) {
struct list_node_link *irq_link, *irq_link_tmp;
spin_lock (&irqs_lock); spin_lock (&irqs_lock);
struct irq *irq, *irq_tmp; list_foreach (irqs, irq_link, irq_link_tmp) {
linklist_foreach (irqs, irq, irq_tmp) { struct irq* irq = list_entry (irq_link, struct irq, irqs_link);
if (irq->irq_num == irq_num) { if (irq->irq_num == irq_num) {
spin_unlock (&irqs_lock); spin_unlock (&irqs_lock);
return irq; return irq;

View File

@@ -1,6 +1,7 @@
#ifndef _KERNEL_IRQ_IRQ_H #ifndef _KERNEL_IRQ_IRQ_H
#define _KERNEL_IRQ_IRQ_H #define _KERNEL_IRQ_IRQ_H
#include <libk/list.h>
#include <libk/std.h> #include <libk/std.h>
#define IRQ_INTERRUPT_SAFE (1 << 0) #define IRQ_INTERRUPT_SAFE (1 << 0)
@@ -9,7 +10,7 @@
typedef void (*irq_func_t) (void* arg, void* regs); typedef void (*irq_func_t) (void* arg, void* regs);
struct irq { struct irq {
struct irq* next; struct list_node_link irqs_link;
irq_func_t func; irq_func_t func;
void* arg; void* arg;

View File

@@ -1,12 +1,19 @@
#ifndef _KERNEL_LIBK_LIST_H #ifndef _KERNEL_LIBK_LIST_H
#define _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 { \ do { \
if ((new) != NULL) { \ if ((new) != NULL) { \
(new)->next = NULL; \ (new)->next = NULL; \
if ((head) != NULL) { \ if ((head) != NULL) { \
type __tmp = (head); \ struct list_node_link* __tmp = (head); \
while (__tmp->next != NULL) { \ while (__tmp->next != NULL) { \
__tmp = __tmp->next; \ __tmp = __tmp->next; \
} \ } \
@@ -19,7 +26,7 @@
} \ } \
} while (0) } while (0)
#define dlinklist_prepend(head, new) \ #define list_prepend(head, new) \
do { \ do { \
if ((new) != NULL) { \ if ((new) != NULL) { \
(new)->prev = NULL; \ (new)->prev = NULL; \
@@ -31,7 +38,7 @@
} \ } \
} while (0) } while (0)
#define dlinklist_remove(head, ele) \ #define list_remove(head, ele) \
do { \ do { \
if ((ele) != NULL) { \ if ((ele) != NULL) { \
if ((ele)->prev != NULL) { \ if ((ele)->prev != NULL) { \
@@ -47,10 +54,10 @@
} \ } \
} while (0) } while (0)
#define dlinklist_find(type, head, out, propname, propvalue) \ #define list_find(head, out, propname, propvalue) \
do { \ do { \
(out) = NULL; \ (out) = NULL; \
type __tmp = (head); \ struct list_node_link* __tmp = (head); \
while (__tmp) { \ while (__tmp) { \
if (__tmp->propname == (propvalue)) { \ if (__tmp->propname == (propvalue)) { \
(out) = __tmp; \ (out) = __tmp; \
@@ -60,23 +67,23 @@
} \ } \
} while (0) } while (0)
#define dlinklist_foreach(head, var, tmp) \ #define list_foreach(head, var, tmp) \
for (var = (head), tmp = (var ? var->next : NULL); var != NULL; \ for (var = (head), tmp = (var ? var->next : NULL); var != NULL; \
var = tmp, tmp = (var ? var->next : 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; \ for ((idx) = 0, var = (head), tmp = (var ? var->next : NULL); var != NULL; \
var = tmp, tmp = (var ? var->next : NULL), (idx)++) 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); \ for ((idx) = 0, var = (head), tmp = (var ? var->next : NULL); var != NULL && (idx) < (max); \
var = tmp, tmp = (var ? var->next : NULL), (idx)++) var = tmp, tmp = (var ? var->next : NULL), (idx)++)
#define dlinklist_back(type, head, out) \ #define list_back(head, out) \
do { \ do { \
(out) = NULL; \ (out) = NULL; \
if ((head) != NULL) { \ if ((head) != NULL) { \
type __tmp = (head); \ struct list_node_link* __tmp = (head); \
while (__tmp->next != NULL) { \ while (__tmp->next != NULL) { \
__tmp = __tmp->next; \ __tmp = __tmp->next; \
} \ } \
@@ -84,11 +91,11 @@
} \ } \
} while (0) } while (0)
#define dlinklist_front(type, head, out) \ #define list_front(head, out) \
do { \ do { \
(out) = NULL; \ (out) = NULL; \
if ((head) != NULL) { \ if ((head) != NULL) { \
type __tmp = (head); \ struct list_node_link* __tmp = (head); \
while (__tmp->prev != NULL) { \ while (__tmp->prev != NULL) { \
__tmp = __tmp->prev; \ __tmp = __tmp->prev; \
} \ } \
@@ -96,7 +103,7 @@
} \ } \
} while (0) } while (0)
#define dlinklist_insert_after(head, pos, new) \ #define list_insert_after(head, pos, new) \
do { \ do { \
if ((pos) != NULL && (new) != NULL) { \ if ((pos) != NULL && (new) != NULL) { \
(new)->prev = (pos); \ (new)->prev = (pos); \
@@ -112,7 +119,7 @@
} \ } \
} while (0) } while (0)
#define dlinklist_insert_before(head, pos, new) \ #define list_insert_before(head, pos, new) \
do { \ do { \
if ((pos) != NULL && (new) != NULL) { \ if ((pos) != NULL && (new) != NULL) { \
(new)->next = (pos); \ (new)->next = (pos); \
@@ -130,11 +137,11 @@
} \ } \
} while (0) } while (0)
#define dlinklist_index_of(type, head, ele, out_idx) \ #define list_index_of(head, ele, out_idx) \
do { \ do { \
(out_idx) = -1; \ (out_idx) = -1; \
int __idx = 0; \ int __idx = 0; \
type __tmp = (head); \ struct list_node_link* __tmp = (head); \
while (__tmp != NULL) { \ while (__tmp != NULL) { \
if (__tmp == (ele)) { \ if (__tmp == (ele)) { \
(out_idx) = __idx; \ (out_idx) = __idx; \
@@ -145,11 +152,11 @@
} \ } \
} while (0) } while (0)
#define dlinklist_index_of_prop(type, head, propname, propvalue, out_idx) \ #define list_index_of_prop(head, propname, propvalue, out_idx) \
do { \ do { \
(out_idx) = -1; \ (out_idx) = -1; \
int __idx = 0; \ int __idx = 0; \
type __tmp = (head); \ struct list_node_link* __tmp = (head); \
while (__tmp != NULL) { \ while (__tmp != NULL) { \
if (__tmp->propname == (propvalue)) { \ if (__tmp->propname == (propvalue)) { \
(out_idx) = __idx; \ (out_idx) = __idx; \
@@ -160,109 +167,4 @@
} \ } \
} while (0) } 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 #endif // _KERNEL_LIBK_LIST_H

View File

@@ -49,7 +49,7 @@ void proc_map (struct proc* proc, uintptr_t start_paddr, uintptr_t start_vaddr,
spin_lock (&proc->pd.lock); 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; for (uintptr_t vpage = start_vaddr, ppage = start_paddr; vpage < start_vaddr + pages * PAGE_SIZE;
vpage += PAGE_SIZE, ppage += PAGE_SIZE) { vpage += PAGE_SIZE, ppage += PAGE_SIZE) {

View File

@@ -3,6 +3,7 @@
#include <aux/compiler.h> #include <aux/compiler.h>
#include <aux/elf.h> #include <aux/elf.h>
#include <libk/list.h>
#include <libk/rbtree.h> #include <libk/rbtree.h>
#include <libk/std.h> #include <libk/std.h>
#include <sync/spin_lock.h> #include <sync/spin_lock.h>
@@ -21,20 +22,19 @@
struct cpu; struct cpu;
struct proc_mapping { struct proc_mapping {
struct proc_mapping* next; struct list_node_link proc_mappings_link;
uintptr_t paddr; uintptr_t paddr;
uintptr_t vaddr; uintptr_t vaddr;
size_t size; size_t size;
} PACKED; };
struct procw;
struct proc { struct proc {
int pid; int pid;
struct rb_node_link proc_tree_link; struct rb_node_link proc_tree_link;
struct rb_node_link cpu_run_q_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 proc_platformdata pdata;
struct pd pd; struct pd pd;
spin_lock_t lock; spin_lock_t lock;