9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* Re: [9fans] thornbird (another xscreensaver hack)
@ 2002-06-22  1:45 jmk
  0 siblings, 0 replies; 11+ messages in thread
From: jmk @ 2002-06-22  1:45 UTC (permalink / raw)
  To: 9fans

>I don't believe there is any requirement in the
>C standard (although I haven't checked) for the returned memory to
>be zeroed. A check of the 2 non-Plan 9 systems I have handy (Linux,
>Irix) show one will zero the memory and one won't.

Oops. I was confounded by the cursor control formatting when I
did 'man malloc' on the Linux system - it doesn't clear memory
either.


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

* Re: [9fans] thornbird (another xscreensaver hack)
@ 2002-06-24  5:40 David Gordon Hogan
  0 siblings, 0 replies; 11+ messages in thread
From: David Gordon Hogan @ 2002-06-24  5:40 UTC (permalink / raw)
  To: 9fans

>>	Give me zeroed memory,
>
> I feel it may be insulting Paul.
> Am I wrong?  :-)

No offense was meant to any former Beatles members,
dead or non-dead or otherwise.



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

* Re: [9fans] thornbird (another xscreensaver hack)
@ 2002-06-24  0:51 okamoto
  0 siblings, 0 replies; 11+ messages in thread
From: okamoto @ 2002-06-24  0:51 UTC (permalink / raw)
  To: 9fans

>	Give me zeroed memory,

I feel it may be insulting Paul.
Am I wrong?  :-)

Kenji



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

* RE: [9fans] thornbird (another xscreensaver hack)
@ 2002-06-22 12:39 Tiit Lankots
  0 siblings, 0 replies; 11+ messages in thread
From: Tiit Lankots @ 2002-06-22 12:39 UTC (permalink / raw)
  To: 9fans

Well, then he sure seems to know how put _our_ unused brain cycles to
work ;)

-----Original Message-----
From: Fariborz Tavakkolian [mailto:fst@real.com]
Sent: Saturday, June 22, 2002 10:43 AM
To: 9fans@cse.psu.edu
Subject: Re: [9fans] thornbird (another xscreensaver hack)


An idle mind is the devil's workshop :)

At 06:57 PM 6/21/2002 -0400, David Gordon Hogan wrote:
>(To the tune of Let It Be):
>
>        Oh, mallocz, mallocz!
>        Mallocz, mallocz!
>        Give me zeroed memory,
>        Mallocz, mallocz!



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

* Re: [9fans] thornbird (another xscreensaver hack)
  2002-06-21 22:57 David Gordon Hogan
@ 2002-06-22  7:42 ` Fariborz Tavakkolian
  0 siblings, 0 replies; 11+ messages in thread
From: Fariborz Tavakkolian @ 2002-06-22  7:42 UTC (permalink / raw)
  To: 9fans

An idle mind is the devil's workshop :)

At 06:57 PM 6/21/2002 -0400, David Gordon Hogan wrote:
>(To the tune of Let It Be):
>
>        Oh, mallocz, mallocz!
>        Mallocz, mallocz!
>        Give me zeroed memory,
>        Mallocz, mallocz!



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

* Re: [9fans] thornbird (another xscreensaver hack)
  2002-06-21 23:02 jmk
@ 2002-06-22  1:21 ` Mike Haertel
  0 siblings, 0 replies; 11+ messages in thread
From: Mike Haertel @ 2002-06-22  1:21 UTC (permalink / raw)
  To: 9fans

>I don't believe there is any requirement in the
>C standard (although I haven't checked) for the returned memory to
>be zeroed. A check of the 2 non-Plan 9 systems I have handy (Linux,
>Irix) show one will zero the memory and one won't.

The C standard specifically states that memory returned by malloc()
should be assumed to contain garbage.

Arguably any system library in which malloc() returns zeroed memory
should be considered broken in the sense that it may give people
the wrong expectation about the standard behavior.


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

* Re: [9fans] thornbird (another xscreensaver hack)
@ 2002-06-21 23:02 jmk
  2002-06-22  1:21 ` Mike Haertel
  0 siblings, 1 reply; 11+ messages in thread
From: jmk @ 2002-06-21 23:02 UTC (permalink / raw)
  To: 9fans

On Fri Jun 21 18:45:41 EDT 2002, andrey@lanl.gov wrote:

> > Reminder: malloc does not return zeroed storage.
> > Either zero it on return or use mallocz.
>
> it's not my code -- i'm simply fixing the X-related stuff, adding
> main(), drawing and that's all...  haven't really debugged the actual
> xscreensaver hack...
>
> unless malloc() behaves differently than the linux/unix ones i simply
> didn't think there may be a problem -- it works there, why shouldn't
> it here?

"it works there, why shouldn't it here?" - Because it's different.
The crash trace which someone posted was because Plan 9 malloc does
not return zeroed memory and the code does a check to see whether
pointers stored in that memory are NULL or not, and tries to free
the non-NULL ones. I don't believe there is any requirement in the
C standard (although I haven't checked) for the returned memory to
be zeroed. A check of the 2 non-Plan 9 systems I have handy (Linux,
Irix) show one will zero the memory and one won't.


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

* Re: [9fans] thornbird (another xscreensaver hack)
@ 2002-06-21 22:57 David Gordon Hogan
  2002-06-22  7:42 ` Fariborz Tavakkolian
  0 siblings, 1 reply; 11+ messages in thread
From: David Gordon Hogan @ 2002-06-21 22:57 UTC (permalink / raw)
  To: 9fans

> Reminder: malloc does not return zeroed storage.
> Either zero it on return or use mallocz.

(To the tune of Let It Be):

	Oh, mallocz, mallocz!
	Mallocz, mallocz!
	Give me zeroed memory,
	Mallocz, mallocz!



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

* Re: [9fans] thornbird (another xscreensaver hack)
@ 2002-06-21 22:41 andrey mirtchovski
  0 siblings, 0 replies; 11+ messages in thread
From: andrey mirtchovski @ 2002-06-21 22:41 UTC (permalink / raw)
  To: 9fans

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

> Reminder: malloc does not return zeroed storage.
> Either zero it on return or use mallocz.

it's not my code -- i'm simply fixing the X-related stuff, adding
main(), drawing and that's all...  haven't really debugged the actual
xscreensaver hack...

unless malloc() behaves differently than the linux/unix ones i simply
didn't think there may be a problem -- it works there, why shouldn't
it here?

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

From: jmk@plan9.bell-labs.com
To: 9fans@cse.psu.edu
Subject: Re: [9fans] thornbird (another xscreensaver hack)
Date: Fri, 21 Jun 2002 18:37:20 -0400
Message-ID: <a1d8b874c6f9b7d00ace2e59675ec517@plan9.bell-labs.com>

Reminder: malloc does not return zeroed storage.
Either zero it on return or use mallocz.

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

* Re: [9fans] thornbird (another xscreensaver hack)
@ 2002-06-21 22:37 jmk
  0 siblings, 0 replies; 11+ messages in thread
From: jmk @ 2002-06-21 22:37 UTC (permalink / raw)
  To: 9fans

Reminder: malloc does not return zeroed storage.
Either zero it on return or use mallocz.


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

* [9fans] thornbird (another xscreensaver hack)
@ 2002-06-21 22:21 andrey mirtchovski
  0 siblings, 0 replies; 11+ messages in thread
From: andrey mirtchovski @ 2002-06-21 22:21 UTC (permalink / raw)
  To: 9fans

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

this one compiles cleanly...

i'm doing just the simple ones that involve XPoints for now, the
complex ones will come later, as i become more familiar with X...

andrey

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

/* thornbird --- continuously varying Thornbird set */


/*-
 * Copyright (c) 1996 by Tim Auckland <Tim.Auckland@Procket.com>
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation.
 *
 * This file is provided AS IS with no warranties of any kind.  The author
 * shall have no liability with respect to the infringement of copyrights,
 * trade secrets or any patents by this file or any part thereof.  In no
 * event will the author be liable for any lost revenue or profits or
 * other special, indirect and consequential damages.
 *
 * "thornbird" shows a view of the "Bird in a Thornbush" fractal,
 * continuously varying the three free parameters.
 *
 * Revision History:
 * 01-Nov-2000: Allocation checks
 * 04-Jun-1999: 3D tumble added by Tim Auckland
 * 31-Jul-1997: Adapted from discrete.c Copyright (c) 1996 by Tim Auckland
 */


/* 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? */

#define BATCHCOUNT 3000
#define CYCLES 1
/* end of plan9-related stuff */


#define balance_rand(v)	((FLOATRAND*(v))-((v)/2))	/* random around 0 */

typedef struct thornbirdstruct thornbirdstruct;
struct  thornbirdstruct {
	int         maxx;
	int         maxy;	/* max of the screen */
	double      a;
	double      b;
	double      c;
	double      d;
	double      e;
	double      i;
	double      j;		/* thornbird parameters */
    struct {
	  double  f1;
	  double  f2;
	}           liss;
    struct {
	  double  theta;
	  double  dtheta;
	  double  phi;
	  double  dphi;
	}           tumble;
    int         inc;
	int         pix;
	int         count;
	int         nbuffers;
	XPoint    **pointBuffer;	/* pointer for XDrawPoints */
};

static thornbirdstruct *thornbirds = (thornbirdstruct *) NULL;

static void
free_thornbird(thornbirdstruct *hp)
{
	if (hp->pointBuffer != NULL) {
		int         buffer;

		for (buffer = 0; buffer < hp->nbuffers; buffer++)
			if (hp->pointBuffer[buffer] != NULL)
				 free((void *) hp->pointBuffer[buffer]);
		 free((void *) hp->pointBuffer);
		hp->pointBuffer = (XPoint **) NULL;
	}
}

void
init_thornbird(void)
{
	thornbirdstruct *hp;

	if (thornbirds == NULL) {
		if ((thornbirds =		     (thornbirdstruct *)calloc(1, sizeof (thornbirdstruct))) == NULL)
			return;
	}
	hp = &thornbirds[0];

	hp->maxx = Dx(screen->r);
	hp->maxy = Dy(screen->r);

	hp->b = 0.1;
	hp->i = hp->j = 0.1;

	hp->pix = 0;
	hp->inc = 0;

	hp->nbuffers = CYCLES;

	if (hp->pointBuffer == NULL)
		if ((hp->pointBuffer = (XPoint **) calloc(CYCLES,
				sizeof (XPoint *))) == NULL) {
			free_thornbird(hp);
			return;
		}

	if (hp->pointBuffer[0] == NULL)
		if ((hp->pointBuffer[0] = (XPoint *) malloc(BATCHCOUNT *
				sizeof (XPoint))) == NULL) {
			free_thornbird(hp);
			return;
		}

	/* select frequencies for parameter variation */
	hp->liss.f1 = LRAND() % 5000;
	hp->liss.f2 = LRAND() % 2000;

	/* choose random 3D tumbling */
	hp->tumble.theta = 0;
	hp->tumble.phi = 0;
	hp->tumble.dtheta = balance_rand(0.001);
	hp->tumble.dphi = balance_rand(0.005);

	hp->count = 0;
}


void
draw_thornbird(void)
{
	double      oldj, oldi;
	int         batchcount = BATCHCOUNT;
	int         k;
	XPoint     *xp;
	int         current;
	int i;

	double      sint, cost, sinp, cosp;
	thornbirdstruct *hp;

	if (thornbirds == NULL)
		return;
	hp = &thornbirds[0];
	if (hp->pointBuffer == NULL)
		return;

	current = hp->inc % CYCLES;
	k = batchcount;


	xp = hp->pointBuffer[current];

	/* vary papameters */
	hp->a = 1.99 + (0.4 * sin(hp->inc / hp->liss.f1) +
					0.05 * cos(hp->inc / hp->liss.f2));
	hp->c = 0.80 + (0.15 * cos(hp->inc / hp->liss.f1) +
					0.05 * sin(hp->inc / hp->liss.f2));

	/* vary view */
	hp->tumble.theta += hp->tumble.dtheta;
	hp->tumble.phi += hp->tumble.dphi;
	sint = sin(hp->tumble.theta);
	cost = cos(hp->tumble.theta);
	sinp = sin(hp->tumble.phi);
	cosp = cos(hp->tumble.phi);

	while (k--) {
		oldj = hp->j;
		oldi = hp->i;

		hp->j = oldi;
		hp->i = (1 - hp->c) * cos(M_PI * hp->a * oldj) + hp->c * hp->b;
		hp->b = oldj;

		xp->x = screen->r.min.x + (hp->maxx / 2 * (1 + sint*hp->j + cost*cosp*hp->i - cost*sinp*hp->b));
		xp->y = screen->r.min.y + (hp->maxy / 2 * (1 - cost*hp->j + sint*cosp*hp->i - sint*sinp*hp->b));
		xp++;
	}

	draw(screen, screen->r, display->black, nil, ZP);
	for(i = 0; i < batchcount; i++) {
		Rectangle r;
		Point p;

		p = hp->pointBuffer[current][i];
		r = Rpt(p, addpt(p, Pt(1, 1)));
		draw(screen, r, display->white, nil, ZP);
	}

	hp->inc++;

}

void
release_thornbird(void)
{
	if (thornbirds != NULL) {
		free_thornbird(&thornbirds[0]);
		free((void *) thornbirds);
		thornbirds = (thornbirdstruct *) NULL;
	}
}

void
refresh_thornbird()
{
	draw(screen, screen->r, display->black, nil, ZP);
}

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(thornbirds != nil)
		release_thornbird();
	init_thornbird();
}

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_thornbird();
		frames++;
	}
}

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

end of thread, other threads:[~2002-06-24  5:40 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-06-22  1:45 [9fans] thornbird (another xscreensaver hack) jmk
  -- strict thread matches above, loose matches on Subject: below --
2002-06-24  5:40 David Gordon Hogan
2002-06-24  0:51 okamoto
2002-06-22 12:39 Tiit Lankots
2002-06-21 23:02 jmk
2002-06-22  1:21 ` Mike Haertel
2002-06-21 22:57 David Gordon Hogan
2002-06-22  7:42 ` Fariborz Tavakkolian
2002-06-21 22:41 andrey mirtchovski
2002-06-21 22:37 jmk
2002-06-21 22:21 andrey mirtchovski

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).