shithub: mpl

Download patch

ref: be9670c51e39ac12eb42dabb0ff30831f0d47401
parent: 079edb19cc359122f59436ee54397a31ed80d338
author: Jacob Moody <[email protected]>
date: Tue Dec 10 06:25:25 EST 2019

Reduce memory fragmentation

--- a/dat.h
+++ b/dat.h
@@ -56,8 +56,8 @@
 
 typedef struct FlacMeta FlacMeta;
 struct FlacMeta{
-	VorbisMeta *com;
-	FlacPic *pic;
+	VorbisMeta;
+	FlacPic;
 };
 
 
--- a/dir.c
+++ b/dir.c
@@ -72,7 +72,7 @@
 
 	switch(s1->type){
 	case FLAC:
-		t1 = s1->fmeta->com->tracknumber;
+		t1 = s1->fmeta->tracknumber;
 		break;
 	case VORBIS:
 		t1 = s1->vmeta->tracknumber;
@@ -83,7 +83,7 @@
 
 	switch(s2->type){
 	case FLAC:
-		t2 = s2->fmeta->com->tracknumber;
+		t2 = s2->fmeta->tracknumber;
 		break;
 	case VORBIS:
 		t2 = s2->vmeta->tracknumber;
@@ -133,7 +133,7 @@
 		if(a->name == nil){
 			switch((a->songs+songcount)->type){
 			case FLAC:
-				albumtitle = (a->songs+songcount)->fmeta->com->album;
+				albumtitle = (a->songs+songcount)->fmeta->album;
 				break;
 			case MP3:
 				albumtitle = (a->songs+songcount)->idmeta->album;
--- a/draw.c
+++ b/draw.c
@@ -137,7 +137,6 @@
 Image*
 readcover(Song *s)
 {
-	FlacPic *p;
 	char buf[512], cover[512];
 	char *dot, *end;
 	int fd, n, i;
@@ -144,12 +143,6 @@
 	Dir *files;
 	Image *im;
 
-
-	if(s->type == FLAC && s->fmeta->pic != nil){
-		p = s->fmeta->pic;
-		return convpicbuf(p->data, p->size, p->mime);
-	}
-
 	dot = strrchr(s->path, '/');
 	if(dot == nil)
 		sysfatal("readcover: bad song path");
@@ -210,7 +203,7 @@
 	for(i=0;i<a->nsong;i++){
 		switch((a->songs+i)->type){
 		case FLAC:
-			tracktitle = (a->songs+i)->fmeta->com->title;
+			tracktitle = (a->songs+i)->fmeta->title;
 			break;
 		case MP3:
 			tracktitle = (a->songs+i)->idmeta->title;
@@ -297,8 +290,7 @@
 	for(i=0;i<n;i++){
 		if((dot = strrchr(files[i].name, '.')) != nil)
 			*dot = '\0';
-		//vals can not be nil so we use a dummy
-		mapinsert(h, files[i].name, &r);
+		mapinsert(h, files[i].name, nil);
 	}
 	keys = emalloc(sizeof(char*)*n);
 	n = mapdumpkey(h, keys, n);
--- a/flac.c
+++ b/flac.c
@@ -16,14 +16,12 @@
 	PICTURE,
 };
 
-FlacPic*
-readflacpic(int fd, vlong offset)
+void
+readflacpic(int fd, vlong offset, FlacPic *pic)
 {
 	uchar buf[1024];
 	uint len;
-	FlacPic *pic;
 
-	pic = emalloc(sizeof(FlacPic));
 	/* We skip the picture type */
 	offset+=4;
 
@@ -66,8 +64,6 @@
 
 	pic->data = emalloc(pic->size);
 	pread(fd, pic->data, pic->size, offset);
-
-	return pic;
 }
 
 FlacMeta*
@@ -103,13 +99,13 @@
 		case SEEKTABLE:
 			break;
 		case VORBIS_COMMENT:
-			f->com = parsevorbismeta(fd, off);
+			parsevorbismeta(fd, off, f);
 			break;
 		case CUESHEET:
 			break;
 		case PICTURE:
 			if(readpic > 0)
-				f->pic = readflacpic(fd, off);
+				readflacpic(fd, off, f);
 			break;
 		}
 		off+=bebtoi(buf, 3);
@@ -118,4 +114,4 @@
 	}
 
 	return f;
-}
\ No newline at end of file
+}
--- a/fncs.h
+++ b/fncs.h
@@ -17,7 +17,7 @@
 void	destroyid3(ID3v1 *id);
 
 /* vorbis.c */
-VorbisMeta*	parsevorbismeta(int, uvlong);
+void	parsevorbismeta(int, uvlong, VorbisMeta*);
 
 /* flac.c */
 FlacMeta*	readflacmeta(int, int);
--- a/index.c
+++ b/index.c
@@ -76,15 +76,13 @@
 }
 
 void
-unmarshalvorbis(int fd, VorbisMeta **v)
+unmarshalvorbis(int fd, VorbisMeta *v)
 {
-	*v = emalloc(sizeof(VorbisMeta));
-	setmalloctag(*v, getcallerpc(&fd));
-	(*v)->title = unmarshalrune(fd);
-	(*v)->artist = unmarshalrune(fd);
-	(*v)->album = unmarshalrune(fd);
-	read(fd, &((*v)->year), sizeof (*v)->year);
-	read(fd, &((*v)->tracknumber), sizeof (*v)->tracknumber);
+	v->title = unmarshalrune(fd);
+	v->artist = unmarshalrune(fd);
+	v->album = unmarshalrune(fd);
+	read(fd, &(v->year), sizeof v->year);
+	read(fd, &(v->tracknumber), sizeof v->tracknumber);
 }
 
 void
@@ -108,7 +106,7 @@
 void
 marshalflacmeta(int fd, FlacMeta *f)
 {
-	marshalvorbis(fd, f->com);
+	marshalvorbis(fd, f);
 //	marshalflacpic(fd, f->pic);
 }
 
@@ -116,7 +114,7 @@
 unmarshalflacmeta(int fd, FlacMeta **f)
 {
 	*f = emalloc(sizeof(FlacMeta));
-	unmarshalvorbis(fd, &((*f)->com));
+	unmarshalvorbis(fd, &((*f)->VorbisMeta));
 //	unmarshalflacpic(fd, &((*f)->pic));
 }
 
--- a/lib.c
+++ b/lib.c
@@ -33,9 +33,6 @@
 handlemsg(enum cmsg msg)
 {
 	switch(msg){
-	case DUMP:
-		dumplib(&lib);
-		break;
 	case NEXT:
 		lib.cursong++;
 		sendp(queuein, nextsong(&lib));
@@ -90,6 +87,13 @@
 	for(;;){
 		switch(alt(alts)){
 		case LMSG:
+			if(msg == DUMP){
+				/* TODO: rename chan to imply this use */
+				toload = recvp(loadc);
+				free(lib.name);
+				lib.name = toload;
+				dumplib(&lib);
+			}
 			handlemsg(msg);
 			break;
 		case QUEUEPOP:
@@ -105,7 +109,7 @@
 			case CLIST:
 				//TODO clean more
 				free(lib.name);
-				lib.name = c.list;
+				lib.name = strdup(c.list);
 				goto Load;
 			}
 			break;
--- a/list.c
+++ b/list.c
@@ -126,15 +126,29 @@
 	int size, cap;
 	int fd;
 	long r;
+	static Hmap *cache = nil;
+	Lib *new;
+	Lib *prev;
 
 	assert(l->name != nil);
-	libdir(buf, 512);
+	if(cache == nil)
+		cache = allocmap(32);
 
+	if((prev = mapget(cache, l->name)) != nil){
+		*l = *prev;
+		return;
+	}
+
+	new = emalloc(sizeof(Lib));
+
+	libdir(buf, 512);
 	/* Check for db cache file first */
 	snprint(fname, 512, "%s/%s.db", buf, l->name);
 	if((fd = open(fname, OREAD))>0){
-		unmarshallib(fd, l);
+		unmarshallib(fd, new);
 		close(fd);
+		mapinsert(cache, l->name, new);
+		*l = *new;
 		return;
 	}
 
@@ -145,23 +159,25 @@
 		quit(nil);
 	}
 	cap = 10;
-	l->start = emalloc(sizeof(Album)*cap);
+	new->start = emalloc(sizeof(Album)*cap);
 	for(size = 0;(r = Bgetrune(b)) > 0;size++){
 		if(size == cap-1){
 			cap = cap * 2;
-			l->start = realloc(l->start, sizeof(Album)*cap);
+			new->start = realloc(new->start, sizeof(Album)*cap);
 		}
 		if(r != L'\n'){
 			Bungetrune(b);
 		}
-		(l->start+size)->name = nil;
-		loadalbum(b, l->start+size);
+		(new->start+size)->name = nil;
+		loadalbum(b, new->start+size);
 	}
-	if((l->start+size)->name == nil){
+	if((new->start+size)->name == nil){
 		size--;
 	}
 	close(Bfildes(b));
 	free(b);
-	l->start = realloc(l->start, sizeof(Album)*size);
-	l->nalbum = size;
+	new->start = realloc(new->start, sizeof(Album)*size);
+	new->nalbum = size;
+	mapinsert(cache, l->name, new);
+	*l = *new;
 }
--- a/vorbis.c
+++ b/vorbis.c
@@ -32,17 +32,14 @@
 	}
 }
 
-VorbisMeta*
-parsevorbismeta(int fd, uvlong offset)
+void
+parsevorbismeta(int fd, uvlong offset, VorbisMeta *v)
 {
 	u32int size;
 	uchar buf[1024];
 	uint i;
-	VorbisMeta *v;
 	char *sep;
 
-	v = emalloc(sizeof(VorbisMeta));
-
 	/* Vendor String */
 	pread(fd, buf, 4, offset);
 	size = lebtoi(buf, 4);
@@ -75,5 +72,4 @@
 	}
 
 	finddefmeta(v);
-	return v;
 }