shithub: riscv

Download patch

ref: 3bf6ef0196645fabb0d45c92462753c4554c9fd8
parent: 86e0099835f99de6ccc3bee33a297387993aa037
author: ben <ben@cirno>
date: Wed Apr 27 13:36:43 EDT 2016

better memory management of threads (thanks knuth)

--- a/sys/include/regexp.h
+++ b/sys/include/regexp.h
@@ -39,7 +39,6 @@
 {
 	Reinst *startinst;
 	Rethread *threads;
-	Rethread **thrpool;
 	char *regstr;
 	int len;
 	int nthr;
--- a/sys/src/libregexp/mkfile
+++ b/sys/src/libregexp/mkfile
@@ -20,9 +20,3 @@
 	${LIB:/$objtype/%=/386/%}\
 
 </sys/src/cmd/mksyslib
-
-$O.regextest: tests/regextest.$O $LIB
-	$LD -o $target regextest.$O
-
-$O.sysregextest: tests/sysregextest.$O
-	$LD -o $target sysregextest.$O
--- a/sys/src/libregexp/regcomp.c
+++ b/sys/src/libregexp/regcomp.c
@@ -190,13 +190,11 @@
 //	prtree(parsetr, 0, 1);
 	reprog = malloc(sizeof(Reprog) +
 	                sizeof(Reinst) * plex.instrs +
-	                sizeof(Rethread) * maxthr +
-	                sizeof(Rethread*) * maxthr);
+	                sizeof(Rethread) * maxthr);
 	reprog->len = plex.instrs;
 	reprog->nthr = maxthr;
 	reprog->startinst = compile(parsetr, reprog, nl);
 	reprog->threads = (Rethread*)(reprog->startinst + reprog->len);
-	reprog->thrpool = (Rethread**)(reprog->threads + reprog->nthr);
 	reprog->regstr = regstr;
 
 	free(plex.nodes);
--- a/sys/src/libregexp/regexec.c
+++ b/sys/src/libregexp/regexec.c
@@ -14,7 +14,7 @@
 regexec(Reprog *prog, char *str, Resub *sem, int msize)
 {
 	RethreadQ lists[2], *clist, *nlist, *tmp;
-	Rethread *t, *nextthr, **availthr;
+	Rethread *t, *next, *pooltop, *avail;
 	Reinst *curinst;
 	Rune r;
 	char *sp, *ep, endc;
@@ -35,9 +35,8 @@
 	nlist->head = nil;
 	nlist->tail = &nlist->head;
 
-	for(i = 0; i < prog->nthr; i++)
-		prog->thrpool[i] = prog->threads + i;
-	availthr = prog->thrpool + prog->nthr;
+	pooltop = prog->threads + prog->nthr;
+	avail = nil;
 
 	pri = matchpri = gen = match = 0;
 	sp = str;
@@ -71,14 +70,14 @@
 				goto Done;
 		case OANY: /* fallthrough */
 		Any:
-			nextthr = t->next;
+			next = t->next;
 			t->pc = curinst + 1;
 			t->next = nil;
 			*nlist->tail = t;
 			nlist->tail = &t->next;
-			if(nextthr == nil)
+			if(next == nil)
 				break;
-			t = nextthr;
+			t = next;
 			curinst = t->pc;
 			goto Again;
 		case OCLASS:
@@ -89,14 +88,14 @@
 				curinst++;
 				goto Class;
 			}
-			nextthr = t->next;
+			next = t->next;
 			t->pc = curinst->a;
 			t->next = nil;
 			*nlist->tail = t;
 			nlist->tail = &t->next;
-			if(nextthr == nil)
+			if(next == nil)
 				break;
-			t = nextthr;
+			t = next;
 			curinst = t->pc;
 			goto Again;
 		case ONOTNL:
@@ -123,13 +122,18 @@
 			curinst = curinst->a;
 			goto Again;
 		case OSPLIT:
-			nextthr = *--availthr;
-			nextthr->pc = curinst->b;
+			if(avail == nil)
+				next = --pooltop;
+			else {
+				next = avail;
+				avail = avail->next;
+			}
+			next->pc = curinst->b;
 			if(msize > 0)
-				memcpy(nextthr->sem, t->sem, sizeof(Resub)*msize);
-			nextthr->pri = t->pri;
-			nextthr->next = t->next;
-			t->next = nextthr;
+				memcpy(next->sem, t->sem, sizeof(Resub)*msize);
+			next->pri = t->pri;
+			next->next = t->next;
+			t->next = next;
 			curinst = curinst->a;
 			goto Again;
 		case OSAVE:
@@ -155,10 +159,12 @@
 			curinst++;
 			goto Again;
 		Done:
-			*availthr++ = t;
-			t = t->next;
-			if(t == nil)
+			next = t->next;
+			t->next = avail;
+			avail = t;
+			if(next == nil)
 				break;
+			t = next;
 			curinst = t->pc;
 			goto Again;
 		}
@@ -166,7 +172,12 @@
 		/* Start again once if we haven't found anything. */
 		if(first == 1 && match == 0) {
 			first = 0;
-			t = *--availthr;
+			if(avail == nil)
+				t = --pooltop;
+			else {
+				t = avail;
+				avail = avail->next;
+			}
 			if(msize > 0)
 				memset(t->sem, 0, sizeof(Resub)*msize);
 			/* "Lower" priority thread */
--- a/sys/src/libregexp/rregexec.c
+++ b/sys/src/libregexp/rregexec.c
@@ -14,10 +14,10 @@
 rregexec(Reprog *prog, Rune *str, Resub *sem, int msize)
 {
 	RethreadQ lists[2], *clist, *nlist, *tmp;
-	Rethread *t, *nextthr, **availthr;
+	Rethread *t, *next, *pooltop, *avail;
 	Reinst *curinst;
 	Rune *rsp, *rep, endr, last;
-	int i, match, first, gen, pri, matchpri;
+	int match, first, gen, pri, matchpri;
 
 	if(msize > NSUBEXPM)
 		msize = NSUBEXPM;
@@ -34,9 +34,8 @@
 	nlist->head = nil;
 	nlist->tail = &nlist->head;
 
-	for(i = 0; i < prog->nthr; i++)
-		prog->thrpool[i] = prog->threads + i;
-	availthr = prog->thrpool + prog->nthr;
+	pooltop = prog->threads + prog->nthr;
+	avail = nil;
 
 	pri = matchpri = gen = match = 0;
 	rsp = str;
@@ -70,14 +69,14 @@
 				goto Done;
 		case OANY: /* fallthrough */
 		Any:
-			nextthr = t->next;
+			next = t->next;
 			t->pc = curinst + 1;
 			t->next = nil;
 			*nlist->tail = t;
 			nlist->tail = &t->next;
-			if(nextthr == nil)
+			if(next == nil)
 				break;
-			t = nextthr;
+			t = next;
 			curinst = t->pc;
 			goto Again;
 		case OCLASS:
@@ -88,14 +87,14 @@
 				curinst++;
 				goto Class;
 			}
-			nextthr = t->next;
+			next = t->next;
 			t->pc = curinst->a;
 			t->next = nil;
 			*nlist->tail = t;
 			nlist->tail = &t->next;
-			if(nextthr == nil)
+			if(next == nil)
 				break;
-			t = nextthr;
+			t = next;
 			curinst = t->pc;
 			goto Again;
 		case ONOTNL:
@@ -122,13 +121,18 @@
 			curinst = curinst->a;
 			goto Again;
 		case OSPLIT:
-			nextthr = *--availthr;
-			nextthr->pc = curinst->b;
+			if(avail == nil)
+				next = --pooltop;
+			else {
+				next = avail;
+				avail = avail->next;
+			}
+			next->pc = curinst->b;
 			if(msize > 0)
-				memcpy(nextthr->sem, t->sem, sizeof(Resub)*msize);
-			nextthr->pri = t->pri;
-			nextthr->next = t->next;
-			t->next = nextthr;
+				memcpy(next->sem, t->sem, sizeof(Resub)*msize);
+			next->pri = t->pri;
+			next->next = t->next;
+			t->next = next;
 			curinst = curinst->a;
 			goto Again;
 		case OSAVE:
@@ -154,10 +158,12 @@
 			curinst++;
 			goto Again;
 		Done:
-			*availthr++ = t;
-			t = t->next;
-			if(t == nil)
+			next = t->next;
+			t->next = avail;
+			avail = t;
+			if(next == nil)
 				break;
+			t = next;
 			curinst = t->pc;
 			goto Again;
 		}
@@ -165,7 +171,12 @@
 		/* Start again once if we haven't found anything. */
 		if(first == 1 && match == 0) {
 			first = 0;
-			t = *--availthr;
+			if(avail == nil)
+				t = --pooltop;
+			else {
+				t = avail;
+				avail = avail->next;
+			}
 			if(msize > 0)
 				memset(t->sem, 0, sizeof(Resub)*msize);
 			/* "Lower" priority thread */