Plan 9 from Bell Labs’s /usr/web/sources/contrib/yk/dist/9legacy/applied/libc-nsec.diff

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


--- /n/sources/plan9/sys/src/libc/9sys/privalloc.c	Fri Mar 16 02:23:22 2001
+++ /sys/src/libc/9sys/privalloc.c	Sun Feb 28 00:00:00 2016
@@ -3,7 +3,7 @@
 
 static Lock	privlock;
 static int	privinit;
-static void	**privs;
+static u32int	privmap;
 
 extern void	**_privates;
 extern int	_nprivates;
@@ -15,31 +15,30 @@
 	int i;
 
 	lock(&privlock);
-	if(!privinit){
-		privinit = 1;
-		if(_nprivates){
-			_privates[0] = 0;
-			for(i = 1; i < _nprivates; i++)
-				_privates[i] = &_privates[i - 1];
-			privs = &_privates[i - 1];
+	for(i = 0; i < 32 && i < _nprivates; i++){
+		if((privmap & (1<<i)) == 0){
+			privmap |= 1<<i;
+			unlock(&privlock);
+			p = &_privates[i];
+			*p = nil;
+			return p;
 		}
 	}
-	p = privs;
-	if(p != nil){
-		privs = *p;
-		*p = nil;
-	}
 	unlock(&privlock);
-	return p;
+	return nil;
 }
 
 void
 privfree(void **p)
 {
-	lock(&privlock);
-	if(p != nil && privinit){
-		*p = privs;
-		privs = p;
+	int i;
+
+	if(p != nil){
+		i = p - _privates;
+		if(i < 0 || i > _nprivates || (privmap & (1<<i)) == 0)
+			abort();
+		lock(&privlock);
+		privmap &= ~(1<<i);
+		unlock(&privlock);
 	}
-	unlock(&privlock);
 }

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.