shithub: riscv

Download patch

ref: a03d9a7985c7a2a6e72c997921c6fa4e73cddaca
parent: 4b8eb68f8d4fec6e49d03f1aceea68df0543aeeb
author: cinap_lenrek <cinap_lenrek@centraldogma>
date: Tue Dec 13 18:35:21 EST 2011

kernel: more alloc stuff

--- a/sys/src/9/pc/audioac97.c
+++ b/sys/src/9/pc/audioac97.c
@@ -389,10 +389,6 @@
 		p = nil;
 		while(p = ac97match(p)){
 			ctlr = xspanalloc(sizeof(Ctlr), 8, 0);
-			if(ctlr == nil){
-				print("ac97: can't allocate memory\n");
-				break;
-			}
 			memset(ctlr, 0, sizeof(Ctlr));
 			ctlr->pcidev = p;
 			ctlr->next = cards;
--- a/sys/src/9/pc/audiohda.c
+++ b/sys/src/9/pc/audiohda.c
@@ -616,7 +616,7 @@
 static void
 enumconns(Widget *w)
 {
-	uint r, i, mask, bits, nlist;
+	uint r, i, j, mask, bits, nlist;
 	Widget **ws, **list;
 	
 	ws = w->fg->codec->widgets;
@@ -624,11 +624,17 @@
 	bits = (r & 0x80) == 0 ? 8 : 16;
 	nlist = r & 0x7f;
 	mask = (1 << bits) - 1;
-	list = malloc(sizeof *list * nlist);
+	list = mallocz(sizeof *list * nlist, 1);
+	if(list == nil){
+		print("hda: no memory for Widget list\n");
+		nlist = 0;
+	}
 	for(i=0; i<nlist; i++){
 		if(i * bits % 32 == 0)
-			r = cmd(w->id, Getconnlist, i);		
-		list[i] = ws[(r >> (i * bits % 32)) & mask];
+			r = cmd(w->id, Getconnlist, i);
+		j = (r >> (i * bits % 32)) & mask;
+		if(j < Maxwidgets)
+			list[i] = ws[j];
 	}
 	w->nlist = nlist;
 	w->list = list;
@@ -662,6 +668,11 @@
 		return nil;
 
 	fg = mallocz(sizeof *fg, 1);
+	if(fg == nil){
+Nomem:
+		print("hda: enumfungroup: out of memory\n");
+		return nil;
+	}
 	fg->codec = codec;
 	fg->id = id;
 	fg->type = r;
@@ -670,11 +681,22 @@
 	n = r & 0xff;
 	base = (r >> 8) & 0xff;
 	
-	if(base + n > Maxwidgets)
+	if(base + n > Maxwidgets){
+		free(fg);
 		return nil;
+	}
 	
 	for(i=n, next=nil; i--; next=w){
 		w = mallocz(sizeof(Widget), 1);
+		if(w == nil){
+			while(w = next){
+				next = w->next;
+				codec->widgets[w->id.nid] = nil;
+				free(w);
+			}
+			free(fg);
+			goto Nomem;
+		}
 		w->id = newnid(id, base + i);
 		w->fg = fg;
 		w->next = next;
@@ -887,7 +909,7 @@
 	return 0;
 }
 
-static int
+static void
 streamalloc(Ctlr *ctlr)
 {
 	uchar *p;
@@ -899,8 +921,6 @@
 	r->size = Bufsize;
 	r->blocksize = Blocksize;
 	r->buf = xspanalloc(r->size, 128, 0);
-	if(r->buf == nil)
-		return -1;
 	ringreset(r);
 	
 	ctlr->active = 0;
@@ -909,8 +929,6 @@
 		Fmtmul1 | Fmtbase441;
 	
 	ctlr->blds = xspanalloc(Nblocks * sizeof(Bld), 128, 0);
-	if(ctlr->blds == nil)
-		return -1;
 	b = ctlr->blds;
 	p = r->buf;
 	for(i=0; i<Nblocks; i++){
@@ -921,7 +939,6 @@
 		p += Blocksize;
 		b++;
 	}
-	return 0;
 }
 
 static void
@@ -1402,10 +1419,7 @@
 		print("#A%d: unable to start hda\n", ctlr->no);
 		return -1;
 	}
-	if(streamalloc(ctlr) < 0){
-		print("#A%d: unable to allocate stream buffer\n", ctlr->no);
-		return -1;
-	}
+	streamalloc(ctlr);
 	if(enumdev(ctlr) < 0){
 		print("#A%d: no audio codecs found\n", ctlr->no);
 		return -1;
--- a/sys/src/9/pc/mmu.c
+++ b/sys/src/9/pc/mmu.c
@@ -87,10 +87,9 @@
 	memglobal();
 	m->pdb[PDX(VPT)] = PADDR(m->pdb)|PTEWRITE|PTEVALID;
 	
-	m->tss = malloc(sizeof(Tss));
+	m->tss = mallocz(sizeof(Tss), 1);
 	if(m->tss == nil)
 		panic("mmuinit: no memory for Tss");
-	memset(m->tss, 0, sizeof(Tss));
 	m->tss->iomap = 0xDFFF<<16;
 
 	/*