shithub: 3dee

Download patch

ref: 5f823efa33ea598dabc3cb584363aa967bdf7b08
parent: 5d9dbc72e1c63b378023a283e53e43dc01a7b98c
author: rodri <rgl@antares-labs.eu>
date: Mon Jun 29 14:24:47 EDT 2020

localize the draw channel. put the alloc code in its own module.

--- /dev/null
+++ b/alloc.c
@@ -1,0 +1,44 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+
+void*
+emalloc(ulong n)
+{
+	void *p;
+
+	p = malloc(n);
+	if(p == nil)
+		sysfatal("malloc: %r");
+	setmalloctag(p, getcallerpc(&n));
+	return p;
+}
+
+void*
+erealloc(void *p, ulong n)
+{
+	void *np;
+
+	np = realloc(p, n);
+	if(np == nil){
+		if(n == 0)
+			return nil;
+		sysfatal("realloc: %r");
+	}
+	if(p == nil)
+		setmalloctag(np, getcallerpc(&p));
+	else
+		setrealloctag(np, getcallerpc(&p));
+	return np;
+}
+
+Image*
+eallocimage(Display *d, Rectangle r, ulong chan, int repl, ulong col)
+{
+	Image *i;
+
+	i = allocimage(d, r, chan, repl, col);
+	if(i == nil)
+		sysfatal("allocimage: %r");
+	return i;
+}
--- a/fns.h
+++ b/fns.h
@@ -1,2 +1,3 @@
-void *erealloc(void*, ulong);
 void *emalloc(ulong);
+void *erealloc(void*, ulong);
+Image *eallocimage(Display*, Rectangle, ulong, int, ulong);
--- a/main.c
+++ b/main.c
@@ -41,7 +41,6 @@
 char stats[Se][256];
 
 Mousectl *mctl;
-Channel *drawc;
 int kdown;
 vlong t0, t;
 double Δt;
@@ -70,30 +69,6 @@
 	120*DEG, 0.1, 100, Ppersp
 };
 
-void *
-emalloc(ulong n)
-{
-	void *p;
-
-	p = malloc(n);
-	if(p == nil)
-		sysfatal("malloc: %r");
-	setmalloctag(p, getcallerpc(&n));
-	return p;
-}
-
-void *
-erealloc(void *p, ulong n)
-{
-	void *np;
-
-	np = realloc(p, n);
-	if(np == nil)
-		sysfatal("realloc: %r");
-	setrealloctag(np, getcallerpc(&p));
-	return np;
-}
-
 int
 depthcmp(void *a, void *b)
 {
@@ -203,11 +178,15 @@
 }
 
 void
-drawproc(void *)
+drawproc(void *drawc)
 {
+	Channel *c;
+
 	threadsetname("drawproc");
+
+	c = drawc;
 	for(;;){
-		send(drawc, nil);
+		send(c, nil);
 		sleep(MS2FR);
 	}
 }
@@ -352,6 +331,7 @@
 threadmain(int argc, char *argv[])
 {
 	OBJ *objmesh;
+	Channel *drawc;
 	int i;
 
 	GEOMfmtinstall();
@@ -363,10 +343,12 @@
 	}ARGEND;
 	if(argc != 0)
 		usage();
+
 	if(initdraw(nil, nil, "3d") < 0)
 		sysfatal("initdraw: %r");
 	if((mctl = initmouse(nil, screen)) == nil)
 		sysfatal("initmouse: %r");
+
 	for(i = 0; i < nelem(cams); i++){
 		placecamera(&cams[i], camcfgs[i].p, camcfgs[i].lookat, camcfgs[i].up);
 		configcamera(&cams[i], screen, camcfgs[i].fov, camcfgs[i].clipn, camcfgs[i].clipf, camcfgs[i].ptype);
@@ -374,11 +356,14 @@
 	maincam = &cams[0];
 	if((objmesh = objparse(mdlpath)) == nil)
 		sysfatal("objparse: %r");
-	drawc = chancreate(1, 0);
+
 	display->locking = 1;
 	unlockdisplay(display);
-	proccreate(drawproc, nil, mainstacksize);
-	proccreate(kbdproc, nil, mainstacksize);
+
+	drawc = chancreate(1, 0);
+	proccreate(drawproc, drawc, 1024);
+	proccreate(kbdproc, nil, 4096);
+
 	t0 = nsec();
 	for(;;){
 		enum {MOUSE, RESIZE, DRAW};
--- a/mkfile
+++ b/mkfile
@@ -3,6 +3,7 @@
 BIN=$home/$objtype/bin
 TARG=3d
 OFILES=\
+	alloc.$O\
 	main.$O\
 
 HFILES=libgeometry/geometry.h\