From 91f79ce9fab7c9920b6f331ca074d58c17755fc9 Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Wed, 17 Aug 2022 17:38:46 +0000 Subject: kernel: allocate notes in heap de-bloat the proc structure by allocating notes with on the heap instead of embedding them in the proc structure. This saves around 640 bytes per process. --- sys/src/9/port/proc.c | 67 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 20 deletions(-) (limited to 'sys/src/9/port/proc.c') diff --git a/sys/src/9/port/proc.c b/sys/src/9/port/proc.c index f0822f575..713e0818a 100644 --- a/sys/src/9/port/proc.c +++ b/sys/src/9/port/proc.c @@ -912,32 +912,35 @@ popnote(Ureg *u) if(up->nnote == 0) return nil; assert(up->nnote > 0); + assert(up->note[0] != nil); /* hold off user notes during note handling */ - if(up->notified && up->note[0].flag == NUser) + if(up->notified && up->note[0]->flag == NUser) return nil; - memmove(&up->lastnote, &up->note[0], sizeof(Note)); - if(--up->nnote) - memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note)); + free(up->lastnote); + up->lastnote = up->note[0]; + if(--up->nnote > 0) + memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note*)); + up->note[up->nnote] = nil; - if(u != nil && strncmp(up->lastnote.msg, "sys:", 4) == 0){ - int l = strlen(up->lastnote.msg); + if(u != nil && strncmp(up->lastnote->msg, "sys:", 4) == 0){ + int l = strlen(up->lastnote->msg); assert(l < ERRMAX); - snprint(up->lastnote.msg+l, ERRMAX-l, " pc=%#p", u->pc); + snprint(up->lastnote->msg+l, ERRMAX-l, " pc=%#p", u->pc); } if(up->notify == nil || up->notified){ qunlock(&up->debug); - if(up->lastnote.flag == NDebug){ + if(up->lastnote->flag == NDebug){ up->fpstate &= ~FPillegal; - pprint("suicide: %s\n", up->lastnote.msg); + pprint("suicide: %s\n", up->lastnote->msg); } - pexit(up->lastnote.msg, up->lastnote.flag!=NDebug); + pexit(up->lastnote->msg, up->lastnote->flag!=NDebug); } up->notified = 1; - return up->lastnote.msg; + return up->lastnote->msg; } /* @@ -948,10 +951,11 @@ popnote(Ureg *u) * lock if we can't get the r->lock and retrying. */ int -postnote(Proc *p, int dolock, char *n, int flag) +postnote(Proc *p, int dolock, char *msg, int flag) { int s, ret; QLock *q; + Note *n; if(p == nil) return 0; @@ -965,14 +969,22 @@ postnote(Proc *p, int dolock, char *n, int flag) return 0; } - if(n != nil && flag != NUser && (p->notify == nil || p->notified)) - p->nnote = 0; - ret = 0; - if(p->nnote < NNOTE && n != nil) { - kstrcpy(p->note[p->nnote].msg, n, ERRMAX); - p->note[p->nnote++].flag = flag; - ret = 1; + if(msg != nil){ + if(flag != NUser && (p->notify == nil || p->notified)) + freenotes(p); + if(p->nnote < NNOTE){ + if(flag != NUser) + n = smalloc(sizeof(Note)); + else + n = malloc(sizeof(Note)); + if(n != nil){ + kstrcpy(n->msg, msg, ERRMAX); + n->flag = flag; + p->note[p->nnote++] = n; + ret = 1; + } + } } p->notepending = 1; if(dolock) @@ -1120,6 +1132,15 @@ freebroken(void) return n; } +void +freenotes(Proc *p) +{ + while(p->nnote > 0){ + free(p->note[--p->nnote]); + up->note[p->nnote] = nil; + } +} + void pexit(char *exitstr, int freemem) { @@ -1231,6 +1252,11 @@ pexit(char *exitstr, int freemem) free(wq); } + freenotes(up); + free(up->lastnote); + up->lastnote = nil; + up->notified = 0; + /* release debuggers */ if(up->pdbg != nil) { wakeup(&up->pdbg->sleep); @@ -1464,9 +1490,10 @@ kproc(char *name, void (*func)(void *), void *arg) } p->nnote = 0; - p->notify = nil; + p->notify = nil; p->notified = 0; p->notepending = 0; + p->lastnote = nil; p->procmode = 0640; p->privatemem = 1; -- cgit v1.2.3