From 5f823efa33ea598dabc3cb584363aa967bdf7b08 Mon Sep 17 00:00:00 2001 From: rodri Date: Mon, 29 Jun 2020 18:24:47 +0000 Subject: localize the draw channel. put the alloc code in its own module. --- alloc.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ fns.h | 3 ++- main.c | 45 +++++++++++++++------------------------------ mkfile | 1 + 4 files changed, 62 insertions(+), 31 deletions(-) create mode 100644 alloc.c diff --git a/alloc.c b/alloc.c new file mode 100644 index 0000000..2589e89 --- /dev/null +++ b/alloc.c @@ -0,0 +1,44 @@ +#include +#include +#include + +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; +} diff --git a/fns.h b/fns.h index 0b4e1ba..ece9b68 100644 --- 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); diff --git a/main.c b/main.c index 72112d0..14468fa 100644 --- a/main.c +++ b/main.c @@ -41,7 +41,6 @@ Rune keys[Ke] = { char stats[Se][256]; Mousectl *mctl; -Channel *drawc; int kdown; vlong t0, t; double Δt; @@ -70,30 +69,6 @@ Camcfg camcfgs[4] = { 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 @@ redraw(void) } 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 @@ void threadmain(int argc, char *argv[]) { OBJ *objmesh; + Channel *drawc; int i; GEOMfmtinstall(); @@ -363,10 +343,12 @@ threadmain(int argc, char *argv[]) }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 @@ threadmain(int argc, char *argv[]) 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}; diff --git a/mkfile b/mkfile index 7e1d254..14aa18c 100644 --- a/mkfile +++ b/mkfile @@ -3,6 +3,7 @@ BIN=$home/$objtype/bin TARG=3d OFILES=\ + alloc.$O\ main.$O\ HFILES=libgeometry/geometry.h\ -- cgit v1.2.3