Rewrite LL_* (link list) macros

This commit is contained in:
2025-09-21 18:32:22 +02:00
parent d5c2df7365
commit 475f77d30f
6 changed files with 106 additions and 100 deletions

View File

@ -68,6 +68,6 @@ void *sbrk(long inc) {
uint64_t blocks = _DIV_ROUNDUP(inc, BITMAP_BLOCK_SIZE); uint64_t blocks = _DIV_ROUNDUP(inc, BITMAP_BLOCK_SIZE);
uint8_t *virt = VIRT(pmm_alloc(blocks)); uint8_t *virt = VIRT(pmm_alloc(blocks));
hal_memset(virt, 0, blocks * BITMAP_BLOCK_SIZE); hal_memset(virt, 0, blocks * BITMAP_BLOCK_SIZE);
_last = (void *)(virt + (blocks * BITMAP_BLOCK_SIZE)); _last = (void *)(virt + inc);
return virt; return virt;
} }

View File

@ -200,7 +200,7 @@ void intr_handleintr(IntrStackFrame *frame) {
kprintf("ERROR %s, 0x%lX\n", exceptions[frame->trapnum], frame->errnum); kprintf("ERROR %s, 0x%lX\n", exceptions[frame->trapnum], frame->errnum);
intr_dumpframe(frame); intr_dumpframe(frame);
backtrace((BackTraceFrame *)frame->regs.rbp); backtrace((BackTraceFrame *)frame->regs.rbp);
if (frame->errnum & 0x4) { if (hal_vmm_current_cr3() != KERNEL_CR3) {
kprintf("killed pid %ld %s\n", PROCS.current->pid, PROCS.current->name); kprintf("killed pid %ld %s\n", PROCS.current->pid, PROCS.current->name);
proc_killself(); proc_killself();
proc_sched((void *)frame); proc_sched((void *)frame);

View File

@ -211,58 +211,50 @@ Proc *proc_nextready(void) {
void proc_reaper(void) { void proc_reaper(void) {
spinlock_acquire(&PROCS.spinlock); spinlock_acquire(&PROCS.spinlock);
Proc *head = PROCS.procs; Proc *head, *tmp;
while (head) { LL_FOREACH_SAFE(PROCS.procs, head, tmp) {
if (head->state == PROC_ZOMBIE) { if (head->state != PROC_ZOMBIE) {
Proc *zombie = head; continue;
head = head->next;
LL_REMOVE(PROCS.procs, zombie);
for (size_t i = 0; i < zombie->vobjcnt; i++) {
if (zombie->vobjs[i] != NULL) {
vfs_close(zombie->vobjs[i]);
zombie->vobjs[i] = NULL;
}
}
for (size_t i = 0; i < PROC_PIPEHANDLES_MAX; i++) {
if (zombie->pipes[i] != NULL && zombie->pipes[i]->ownerpid == zombie->pid) {
dlfree(zombie->pipes[i]);
ipc_pipefree(zombie->pipes[i]);
zombie->pipes[i] = NULL;
}
}
pmm_free((uintptr_t)(zombie->platformdata.kstack - PROC_STACKSIZE), PROC_STACKBLOCKS);
pmm_free((uintptr_t)(zombie->platformdata.pstack - PROC_STACKSIZE), PROC_STACKBLOCKS);
if (!zombie->kern) {
VasRange *vashead = zombie->vas;
size_t i = 0;
while (vashead) {
VasRange *tmp = vashead;
vashead = vashead->next;
hal_vmm_unmap_range(zombie->platformdata.cr3, tmp->virtstart, tmp->physstart, tmp->size);
pmm_free((uintptr_t)tmp->physstart, tmp->size / HAL_PAGE_SIZE);
dlfree(tmp);
i++;
}
pmm_free((uintptr_t)zombie->platformdata.cr3, 1);
ProcArg *arg = zombie->procargs.list;
while (arg) {
dlfree(arg->string);
ProcArg *tmp = arg;
arg = arg->next;
dlfree(tmp);
}
}
dlfree(zombie);
} else {
head = head->next;
} }
Proc *zombie = head;
LL_REMOVE(PROCS.procs, zombie);
for (size_t i = 0; i < zombie->vobjcnt; i++) {
if (zombie->vobjs[i] != NULL) {
vfs_close(zombie->vobjs[i]);
zombie->vobjs[i] = NULL;
}
}
for (size_t i = 0; i < PROC_PIPEHANDLES_MAX; i++) {
if (zombie->pipes[i] != NULL && zombie->pipes[i]->ownerpid == zombie->pid) {
dlfree(zombie->pipes[i]);
ipc_pipefree(zombie->pipes[i]);
zombie->pipes[i] = NULL;
}
}
pmm_free((uintptr_t)(zombie->platformdata.kstack - PROC_STACKSIZE), PROC_STACKBLOCKS);
pmm_free((uintptr_t)(zombie->platformdata.pstack - PROC_STACKSIZE), PROC_STACKBLOCKS);
if (!zombie->kern) {
VasRange *vas, *vastmp;
LL_FOREACH_SAFE(zombie->vas, vas, vastmp) {
hal_vmm_unmap_range(zombie->platformdata.cr3, vas->virtstart, vas->physstart, vas->size);
pmm_free((uintptr_t)vas->physstart, vas->size / HAL_PAGE_SIZE);
dlfree(vas);
}
pmm_free((uintptr_t)zombie->platformdata.cr3, 1);
ProcArg *arg, *argtmp;
LL_FOREACH_SAFE(zombie->procargs.list, arg, argtmp) {
dlfree(arg->string);
dlfree(arg);
}
}
dlfree(zombie);
} }
spinlock_release(&PROCS.spinlock); spinlock_release(&PROCS.spinlock);
} }

View File

@ -86,13 +86,12 @@ int32_t SYSCALL1(sys_mman_unmap, addr1) {
uint8_t *virt = NULL; uint8_t *virt = NULL;
VasRange *tofree = NULL; VasRange *tofree = NULL;
VasRange *range = proc->vas; VasRange *vas, *vastmp;
while (range) { LL_FOREACH_SAFE(proc->vas, vas, vastmp) {
if (range->virtstart == addr) { if (vas->virtstart == addr) {
tofree = range; tofree = vas;
break; break;
} }
range = range->next;
} }
if (tofree == NULL) { if (tofree == NULL) {

View File

@ -97,30 +97,23 @@ int32_t SYSCALL5(sys_processctl, pid1, cmd1, arg1, arg2, arg3) {
goto done; goto done;
} }
size_t len = arg2; size_t len = arg2;
ProcArg *arg = proc->procargs.list; size_t i;
size_t i = 0; ProcArg *arg, *argtmp;
while (arg) { LL_FOREACH_SAFE_IDX_LIMIT(proc->procargs.list, arg, argtmp, i, len) {
if (i == len) {
break;
}
if (argbuf[i] == NULL) { if (argbuf[i] == NULL) {
ret = E_INVALIDARGUMENT; ret = E_INVALIDARGUMENT;
goto done; goto done;
} }
hal_strcpy(argbuf[i], arg->string); hal_strcpy(argbuf[i], arg->string);
arg = arg->next;
i++;
} }
ret = E_OK; ret = E_OK;
} break; } break;
case PCTL_PLS_SZ: { case PCTL_PLS_SZ: {
size_t i = 0; Proc *p, *ptmp;
size_t i;
spinlock_acquire(&PROCS.spinlock); spinlock_acquire(&PROCS.spinlock);
Proc *p = PROCS.procs; LL_FOREACH_SAFE_IDX(PROCS.procs, p, ptmp, i);
while (p) {
i++;
p = p->next;
}
spinlock_release(&PROCS.spinlock); spinlock_release(&PROCS.spinlock);
ret = i; ret = i;
} break; } break;
@ -133,26 +126,22 @@ int32_t SYSCALL5(sys_processctl, pid1, cmd1, arg1, arg2, arg3) {
goto done; goto done;
} }
size_t i = 0; Proc *p, *ptmp;
size_t i;
spinlock_acquire(&PROCS.spinlock); spinlock_acquire(&PROCS.spinlock);
Proc *p = PROCS.procs; LL_FOREACH_SAFE_IDX(PROCS.procs, p, ptmp, i) {
while (p) {
if (i == pidx) { if (i == pidx) {
stat->pid = p->pid; stat->pid = p->pid;
hal_strcpy(stat->name, p->name); hal_strcpy(stat->name, p->name);
stat->state = p->state; stat->state = p->state;
stat->kern = p->kern; stat->kern = p->kern;
VasRange *range = p->vas; VasRange *vas, *vastmp;
while (range) { LL_FOREACH_SAFE(p->vas, vas, vastmp) {
stat->usemem += range->size; stat->usemem += vas->size;
range = range->next;
} }
break; break;
} }
i++;
p = p->next;
} }
spinlock_release(&PROCS.spinlock); spinlock_release(&PROCS.spinlock);
ret = E_OK; ret = E_OK;

View File

@ -8,35 +8,45 @@
#define LL_APPEND(head, new) \ #define LL_APPEND(head, new) \
do { \ do { \
if ((head) != NULL) { \ if ((new) != NULL) { \
typeof((head)) __tmp; \ if ((head) != NULL) { \
(new)->next = NULL; \ typeof((head)) __tmp; \
__tmp = (head); \ (new)->next = NULL; \
while (__tmp->next != NULL) { \ __tmp = (head); \
__tmp = __tmp->next; \ while (__tmp->next != NULL) { \
__tmp = __tmp->next; \
} \
__tmp->next = (new); \
} else { \
(new)->next = NULL; \
(head) = (new); \
} \ } \
__tmp->next = (new); \
} else { \
(new)->next = NULL; \
(head) = (new); \
} \ } \
} while(0) } while(0)
#define LL_REMOVE(head, ele) \ #define LL_REMOVE(head, ele) \
do { \ do { \
typeof((head)) __cur = (head); \ if ((head) != NULL && (ele) != NULL) { \
typeof((head)) __prev = NULL; \ typeof((head)) __cur = (head); \
while (__cur != (ele)) { \ typeof((head)) __prev = NULL; \
__prev = __cur; \ while (__cur != NULL && __cur != (ele)) { \
__cur = __cur->next; \ __prev = __cur; \
} \ __cur = __cur->next; \
if (__prev != NULL) { \ } \
__prev->next = __cur->next; \ if (__cur == (ele)) { \
if (__prev != NULL) { \
__prev->next = __cur->next; \
} else { \
(head) = __cur->next; \
} \
(ele)->next = NULL; \
} \
} \ } \
} while(0) } while(0)
#define LL_FINDPROP(head, out, propname, propvalue) \ #define LL_FINDPROP(head, out, propname, propvalue) \
do { \ do { \
(out) = NULL; \
typeof((head)) __tmp = (head); \ typeof((head)) __tmp = (head); \
while (__tmp) { \ while (__tmp) { \
if (__tmp->propname == (propvalue)) { \ if (__tmp->propname == (propvalue)) { \
@ -47,6 +57,22 @@
} \ } \
} while(0) } while(0)
#define LL_FOREACH_SAFE(head, var, tmp) \
for (typeof(head) var = (head), tmp = (var ? var->next : NULL); \
var != NULL; \
var = tmp, tmp = (var ? var->next : NULL) \
)
#define LL_FOREACH_SAFE_IDX(head, var, tmp, idx) \
for (typeof(head) var = (head), tmp = (var ? var->next : NULL); \
var != NULL && ((idx) = 0, 1); \
var = tmp, tmp = (var ? var->next : NULL), (idx)++)
#define LL_FOREACH_SAFE_IDX_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)++)
char *util_get_filename(char *path); char *util_get_filename(char *path);
#endif // UTIL_UTIL_H_ #endif // UTIL_UTIL_H_