9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* Re: [9fans] galaxy crash continued ...
@ 2002-06-21 21:09 andrey mirtchovski
  0 siblings, 0 replies; 3+ messages in thread
From: andrey mirtchovski @ 2002-06-21 21:09 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 469 bytes --]

> Also, the warning about frames being used and not set is a bug in the
> programme. The warnings given by the compiler should really be heeded.

the frames part is my bad -- i moved the variable in main() and forgot
that 'globals are initialized, locals are not'.  the other warnings
involved USED() for argc and argv, and commenting out the cast to
(void) in front of every free() in the code...

here's a version that compiles without warnings...

andrey

[-- Attachment #2: galaxy.c --]
[-- Type: text/plain, Size: 10615 bytes --]

/* standard defines -- it's better to have them here than
  * having to s/X/Y/g the whole thing over and over again...
  * andrey
  */

/* plan9-related stuff */
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <event.h>

#define NULL nil
#define XPoint Point
#define SINF sin
#define COSF cos
#define NRAND nrand
#define LRAND lrand
#define MAXRAND ((2<<31)-1)
#define FLOATRAND frand()
#define M_PI 3.141592653589793238462643383 /* should be enough, no? */
/* end of plan9-related stuff */


#define MINSIZE       1
#define MINGALAXIES    2
#define MAX_STARS    1000
#define MAX_IDELTAT    50
/* These come originally from the Cluster-version */
#define DEFAULT_GALAXIES  3
#define DEFAULT_STARS    1000
#define DEFAULT_HITITERATIONS  7500
#define DEFAULT_IDELTAT    200 /* 0.02 */
#define EPSILON 0.00000001

#define sqrt_EPSILON 0.0001

#define DELTAT (MAX_IDELTAT * 0.0001)

#define GALAXYRANGESIZE  0.1
#define GALAXYMINSIZE  0.15
#define QCONS    0.001


#define COLORBASE  16
  /* Colors for stars start here */
#define COLORSTEP  (NUMCOLORS/COLORBASE)
/* NUMCOLORS / COLORBASE colors per galaxy */


typedef struct {
 double      pos[3], vel[3];
} Star;


typedef struct {
 int         mass;
 int         nstars;
 Star       *stars;
 XPoint     *oldpoints;
 XPoint     *newpoints;
 double      pos[3], vel[3];
 int         galcol;
} Galaxy;

typedef struct {
 double      mat[3][3]; /* Movement of stars(?) */
 double      scale; /* Scale */
 int         midx; /* Middle of screen, x */
 int         midy; /* Middle of screen, y */
 double      size; /* */
 double      diff[3]; /* */
 Galaxy     *galaxies; /* the Whole Universe */
 int         ngalaxies; /* # galaxies */
 int         f_hititerations; /* # iterations before restart */
 int         step; /* */
 double      rot_y; /* rotation of eye around center of universe, around
y-axis*/
 double      rot_x; /* rotation of eye around center of universe, around
x-axis */
} unistruct;

static unistruct *universes = NULL;

static void
free_galaxies(unistruct * gp)
{
 if (gp->galaxies != NULL) {
  int         i;

  for (i = 0; i < gp->ngalaxies; i++) {
   Galaxy     *gt = &gp->galaxies[i];

   if (gt->stars != NULL)
    free((void *) gt->stars);
   if (gt->oldpoints != NULL)
     free((void *) gt->oldpoints);
   if (gt->newpoints != NULL)
      free((void *) gt->newpoints);
  }
   free((void *) gp->galaxies);
  gp->galaxies = NULL;
 }
}

static void
startover(void)
{
 unistruct  *gp = &universes[0];
 int         i, j; /* more tmp */
 double      w1, w2; /* more tmp */
 double      d, v, w, h; /* yet more tmp */

 gp->step = 0;
 gp->rot_y = 0;
 gp->rot_x = 0;

  gp->ngalaxies = MINGALAXIES + (int)(FLOATRAND+0.5);
 if (gp->galaxies == NULL)
  gp->galaxies = (Galaxy *) calloc(gp->ngalaxies, sizeof (Galaxy));

 for (i = 0; i < gp->ngalaxies; ++i) {
  Galaxy     *gt = &gp->galaxies[i];
  double      sinw1, sinw2, cosw1, cosw2;

  gt->galcol = NRAND(COLORBASE - 2);
  if (gt->galcol > 1)
   gt->galcol += 2; /* Mult 8; 16..31 no green stars */
  /* Galaxies still may have some green stars but are not all green. */

 if (gt->stars != NULL) {
    free((void *) gt->stars);
   gt->stars = NULL;
  }
  gt->nstars = (NRAND(MAX_STARS / 2)) + MAX_STARS / 2;
  gt->stars = (Star *) malloc(gt->nstars * sizeof (Star));
  gt->oldpoints = (XPoint *) malloc(gt->nstars * sizeof (XPoint));
  gt->newpoints = (XPoint *) malloc(gt->nstars * sizeof (XPoint));

  w1 = 2.0 * M_PI * FLOATRAND;
  w2 = 2.0 * M_PI * FLOATRAND;
  sinw1 = SINF(w1);
  sinw2 = SINF(w2);
  cosw1 = COSF(w1);
  cosw2 = COSF(w2);

  gp->mat[0][0] = cosw2;
  gp->mat[0][1] = -sinw1 * sinw2;
  gp->mat[0][2] = cosw1 * sinw2;
  gp->mat[1][0] = 0.0;
  gp->mat[1][1] = cosw1;
  gp->mat[1][2] = sinw1;
  gp->mat[2][0] = -sinw2;
  gp->mat[2][1] = -sinw1 * cosw2;
  gp->mat[2][2] = cosw1 * cosw2;

  gt->vel[0] = FLOATRAND * 2.0 - 1.0;
  gt->vel[1] = FLOATRAND * 2.0 - 1.0;
  gt->vel[2] = FLOATRAND * 2.0 - 1.0;
  gt->pos[0] = -gt->vel[0] * DELTAT * gp->f_hititerations + FLOATRAND -
0.5;
  gt->pos[1] = -gt->vel[1] * DELTAT * gp->f_hititerations + FLOATRAND -
0.5;
  gt->pos[2] = -gt->vel[2] * DELTAT * gp->f_hititerations + FLOATRAND -
0.5;

 gt->mass = (int) (FLOATRAND * 1000.0) + 1;

  gp->size = GALAXYRANGESIZE * FLOATRAND;
 gp->size  +=  GALAXYMINSIZE;
 for (j = 0; j < gt->nstars; ++j) {
   Star       *st = &gt->stars[j];
   XPoint     *oldp = &gt->oldpoints[j];
   XPoint     *newp = &gt->newpoints[j];

   double      sinw, cosw;

   w = 2.0 * M_PI * FLOATRAND;
   sinw = SINF(w);
   cosw = COSF(w);
   d = FLOATRAND * gp->size;
   h = FLOATRAND * exp(-2.0 * (d / gp->size)) / 5.0 * gp->size;
   if (FLOATRAND < 0.5)
    h = -h;
   st->pos[0] = gp->mat[0][0] * d * cosw + gp->mat[1][0] * d * sinw +
gp->mat[2][0] * h + gt->pos[0];
   st->pos[1] = gp->mat[0][1] * d * cosw + gp->mat[1][1] * d * sinw +
gp->mat[2][1] * h + gt->pos[1];
   st->pos[2] = gp->mat[0][2] * d * cosw + gp->mat[1][2] * d * sinw +
gp->mat[2][2] * h + gt->pos[2];

   v = sqrt(gt->mass * QCONS / sqrt(d * d + h * h));
   st->vel[0] = -gp->mat[0][0] * v * sinw + gp->mat[1][0] * v * cosw +
gt->vel[0];
   st->vel[1] = -gp->mat[0][1] * v * sinw + gp->mat[1][1] * v * cosw +
gt->vel[1];
   st->vel[2] = -gp->mat[0][2] * v * sinw + gp->mat[1][2] * v * cosw +
gt->vel[2];

   st->vel[0] *= DELTAT;
   st->vel[1] *= DELTAT;
   st->vel[2] *= DELTAT;

   oldp->x = 0;
   oldp->y = 0;
   newp->x = 0;
   newp->y = 0;
  }

 }

 draw(screen, screen->r, display->black, nil, ZP);

}

void
init_galaxy(void)
{
 unistruct  *gp;

 if (universes == NULL) {
  if ((universes = (unistruct *) calloc(1, sizeof (unistruct))) == NULL)
   return;
 }
 gp = &universes[0];

 gp->f_hititerations = 100;

 gp->scale = (double) (Dx(screen->r) + Dy(screen->r)) / 8.0;
 gp->midx =  screen->r.min.x + Dx(screen->r)  / 2;
 gp->midy =  screen->r.min.y +Dy(screen->r) / 2;
startover();
}

void
draw_galaxy(void)
{
 unistruct  *gp = &universes[0];
 double      d, eps, cox, six, cor, sir;  /* tmp */
 int         i, j, k; /* more tmp */
 int 	     i2;
    XPoint    *dummy;

//draw(screen, screen->r, display->black, nil, ZP);

gp->rot_y += 0.01;
 gp->rot_x += 0.004;

 cox = COSF(gp->rot_y);
 six = SINF(gp->rot_y);
    cor = COSF(gp->rot_x);
    sir = SINF(gp->rot_x);


    eps = 1/(EPSILON * sqrt_EPSILON * DELTAT * DELTAT * QCONS);

 for (i = 0; i < gp->ngalaxies; ++i) {
  Galaxy     *gt = &gp->galaxies[i];

  for (j = 0; j < gp->galaxies[i].nstars; ++j) {
   Star       *st = &gt->stars[j];
   XPoint     *newp = &gt->newpoints[j];
   double      v0 = st->vel[0];
   double      v1 = st->vel[1];
   double      v2 = st->vel[2];

   for (k = 0; k < gp->ngalaxies; ++k) {
    Galaxy     *gtk = &gp->galaxies[k];
    double      d0 = gtk->pos[0] - st->pos[0];
    double      d1 = gtk->pos[1] - st->pos[1];
    double      d2 = gtk->pos[2] - st->pos[2];

    d = d0 * d0 + d1 * d1 + d2 * d2;
    if (d > EPSILON)
     d = gt->mass / (d * sqrt(d)) * DELTAT * DELTAT * QCONS;
    else
        d = gt->mass * eps;
    v0 += d0 * d;
    v1 += d1 * d;
    v2 += d2 * d;
   }

   st->vel[0] = v0;
   st->vel[1] = v1;
   st->vel[2] = v2;

   st->pos[0] += v0;
   st->pos[1] += v1;
   st->pos[2] += v2;

   newp->x = (short) (((st->pos[0]) - (st->pos[2])) * gp->scale) + gp->midx;
   newp->y = (short) (((st->pos[1]) - (((st->pos[0]) + (st->pos[2])))) * gp->scale) + gp->midy;
//   newp->x = (short) (((cox * st->pos[0]) - (six * st->pos[2])) * gp->scale) + gp->midx;
//   newp->y = (short) (((cor * st->pos[1]) - (sir * ((six * st->pos[0]) + (cox * st->pos[2])))) * gp->scale) + gp->midy;

  }

  for (k = i + 1; k < gp->ngalaxies; ++k) {
   Galaxy     *gtk = &gp->galaxies[k];
   double      d0 = gtk->pos[0] - gt->pos[0];
   double      d1 = gtk->pos[1] - gt->pos[1];
   double      d2 = gtk->pos[2] - gt->pos[2];

   d = d0 * d0 + d1 * d1 + d2 * d2;
   if (d > EPSILON)
    d = gt->mass * gt->mass / (d * sqrt(d)) * DELTAT * QCONS;
   else
    d = gt->mass * gt->mass / (EPSILON * sqrt_EPSILON) * DELTAT * QCONS;

   d0 *= d;
   d1 *= d;
   d2 *= d;
   gt->vel[0] += d0 / gt->mass;
   gt->vel[1] += d1 / gt->mass;
   gt->vel[2] += d2 / gt->mass;
   gtk->vel[0] -= d0 / gtk->mass;
   gtk->vel[1] -= d1 / gtk->mass;
   gtk->vel[2] -= d2 / gtk->mass;
  }

  gt->pos[0] += gt->vel[0] * DELTAT;
  gt->pos[1] += gt->vel[1] * DELTAT;
  gt->pos[2] += gt->vel[2] * DELTAT;


/* original drawing routines */
//         	XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
//   		XDrawPoints(display, window, gc, gt->oldpoints, gt->nstars, CoordModeOrigin);
// 		XSetForeground(display, gc, MI_PIXEL(mi, COLORSTEP * gt->galcol));
//        	XDrawPoints(display, window, gc, gt->newpoints, gt->nstars,CoordModeOrigin);

		for(i2 = 0; i2 < gt->nstars; i2++) {
			Rectangle r;
			Point p = Pt(1, 1);

			p = gt->oldpoints[i2];
			r = Rpt(p, addpt(p, Pt(2, 2)));
			draw(screen, r, display->black, nil, ZP);

			p = gt->newpoints[i2];
			r = Rpt(p, addpt(p, Pt(2, 2)));
			draw(screen, r, display->white, nil, ZP);

		}
	dummy = gt->oldpoints;
	gt->oldpoints = gt->newpoints;
	gt->newpoints = dummy;
 }

 gp->step++;
 if (gp->step > gp->f_hititerations * 4)
  startover();
}

void
release_galaxy(void)
{
 if (universes != NULL) {

   free_galaxies(&universes[0]);
   free((void *) universes);
  universes = NULL;
 }
}

void
refresh_galaxy(void)
{
 /* Do nothing, it will refresh by itself */
}

void
eresized(int new)
{

	if(new && getwindow(display, Refnone) < 0) {
		sysfatal("can't reattach to window");
	}

	draw(screen, screen->r, display->black, nil, ZP);

	if(universes != nil)
		release_galaxy();
	init_galaxy();
}

char *buttons[] =
{
	"exit",
	0
};

Menu menu =
{
	buttons
};

void
main(int argc, char **argv)
{
	Mouse m;
	vlong tbegin, tend;
	ulong frames = 0;

	USED(argc, argv);
	srand(time(0));

	if(initdraw(nil, nil, "bez") < 0)
		sysfatal("initdraw failed: %r");

	einit(Emouse);

	eresized(0);
	tbegin = nsec();
	for(;;) {
		if(ecanmouse()) {
			m = emouse();
			if(m.buttons&4)
				if(emenuhit(3, &m, &menu) == 0) {
					tend = nsec();
					print("fps: %uf\n", frames/((tend - tbegin)/1000000000.0));
					exits(0);
				}
		}
		draw_galaxy();
		frames++;
	}
}--upas-bvxqotwudtbymdvbzckjrzqntg--

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [9fans] galaxy crash continued ...
@ 2002-06-21 20:57 jmk
  0 siblings, 0 replies; 3+ messages in thread
From: jmk @ 2002-06-21 20:57 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 253 bytes --]

I suspect it's the problem that the space returned by malloc is not
zeroed and the programme assumes it is.

Also, the warning about frames being used and not set is a bug in the
programme. The warnings given by the compiler should really be heeded.

[-- Attachment #2: Type: message/rfc822, Size: 2880 bytes --]

From: Sam <sah@softcardsystems.com>
To: <9fans@cse.psu.edu>
Subject: [9fans] galaxy crash continued ...
Date: Fri, 21 Jun 2002 15:50:52 -0400 (EDT)
Message-ID: <Pine.LNX.4.30.0206211547340.8523-100000@athena>


Not that this warrants a lot of effort, because if it's the galaxy
program it's not really life threatening and if it's the system it
has probably been fixed, but it looks more like a memory management
problem to me.

term% galaxy
panic: D2B called on non-block 3a2a8
galaxy 293: suicide: sys: trap: fault read addr=0x0 pc=0x00005670
term% acid 293
/proc/293/text:386 plan 9 executable

/sys/lib/acid/port
/sys/lib/acid/386
acid: stk()
At pc:0x00005670:abort /sys/src/libc/9sys/abort.c:6
abort() /sys/src/libc/9sys/abort.c:6
	called from ppanic+0x14e /sys/src/libc/port/malloc.c:163
ppanic(p=0x00013110,fmt=0x00018af4) /sys/src/libc/port/malloc.c:135
	called from D2B+0x32 /sys/src/libc/port/pool.c:896
D2B(v=0x0003a2a8,p=0x00013110) /sys/src/libc/port/pool.c:892
	called from poolfreel+0x20 /sys/src/libc/port/pool.c:1015
poolfreel(v=0x0003a2a8,p=0x00013110) /sys/src/libc/port/pool.c:1007
	called from poolfree+0x41 /sys/src/libc/port/pool.c:1115
poolfree(p=0x00013110,v=0x0003a2a8) /sys/src/libc/port/pool.c:1106
	called from free+0x23 /sys/src/libc/port/malloc.c:233
free(v=0x0003a2b0) /sys/src/libc/port/malloc.c:230
	called from startover+0xf0 /usr/sah/ss/galaxy.c:130
startover() /usr/sah/ss/galaxy.c:104
	called from draw_galaxy+0x784 /usr/sah/ss/galaxy.c:349
draw_galaxy() /usr/sah/ss/galaxy.c:233
	called from main+0x159 /usr/sah/ss/galaxy.c:422
main() /usr/sah/ss/galaxy.c:396
	called from _main+0x31 /sys/src/libc/386/main9.s:16

Sam

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [9fans] galaxy crash continued ...
@ 2002-06-21 19:50 Sam
  0 siblings, 0 replies; 3+ messages in thread
From: Sam @ 2002-06-21 19:50 UTC (permalink / raw)
  To: 9fans


Not that this warrants a lot of effort, because if it's the galaxy
program it's not really life threatening and if it's the system it
has probably been fixed, but it looks more like a memory management
problem to me.

term% galaxy
panic: D2B called on non-block 3a2a8
galaxy 293: suicide: sys: trap: fault read addr=0x0 pc=0x00005670
term% acid 293
/proc/293/text:386 plan 9 executable

/sys/lib/acid/port
/sys/lib/acid/386
acid: stk()
At pc:0x00005670:abort /sys/src/libc/9sys/abort.c:6
abort() /sys/src/libc/9sys/abort.c:6
	called from ppanic+0x14e /sys/src/libc/port/malloc.c:163
ppanic(p=0x00013110,fmt=0x00018af4) /sys/src/libc/port/malloc.c:135
	called from D2B+0x32 /sys/src/libc/port/pool.c:896
D2B(v=0x0003a2a8,p=0x00013110) /sys/src/libc/port/pool.c:892
	called from poolfreel+0x20 /sys/src/libc/port/pool.c:1015
poolfreel(v=0x0003a2a8,p=0x00013110) /sys/src/libc/port/pool.c:1007
	called from poolfree+0x41 /sys/src/libc/port/pool.c:1115
poolfree(p=0x00013110,v=0x0003a2a8) /sys/src/libc/port/pool.c:1106
	called from free+0x23 /sys/src/libc/port/malloc.c:233
free(v=0x0003a2b0) /sys/src/libc/port/malloc.c:230
	called from startover+0xf0 /usr/sah/ss/galaxy.c:130
startover() /usr/sah/ss/galaxy.c:104
	called from draw_galaxy+0x784 /usr/sah/ss/galaxy.c:349
draw_galaxy() /usr/sah/ss/galaxy.c:233
	called from main+0x159 /usr/sah/ss/galaxy.c:422
main() /usr/sah/ss/galaxy.c:396
	called from _main+0x31 /sys/src/libc/386/main9.s:16

Sam



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-06-21 21:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-06-21 21:09 [9fans] galaxy crash continued andrey mirtchovski
  -- strict thread matches above, loose matches on Subject: below --
2002-06-21 20:57 jmk
2002-06-21 19:50 Sam

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).