[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-gsl] gsl_rng_type that needs initialization and cleanup

From: Henric Jungheim
Subject: [Help-gsl] gsl_rng_type that needs initialization and cleanup
Date: Fri, 20 Jul 2007 06:14:48 -0700
User-agent: Mutt/

I'm trying to implement a standard gsl_rng_type that is
backed by a file.  The file should be opened when the
gsl_rng is created, a clone needs to dup() the file handle
and copy the file offset, and the file needs to be closed
when gsl_rng_free() is called.

For rng creation, gsl_rng_alloc() calls gsl_rng_type->set()
with a freshly malloc()ed "state" area.  Since the area is
uninitialized memory, one can't use it directly as a flag to
see if set() has been called before on this particular

Cleanup is also a problem.  The gsl_rng_free() directly
calls free() on both the state and gsl_rng itself.  This
means there is no obvious way to force the close needed to
clean up after a file-backed rng.

I don't see any clean way of implementing something like
this without adding a callback or two to gsl_rng_type.

Something like this might work:

typedef struct
     gsl_rng_type base
     void* create_state(const void* clone);
     destroy_state(void* state);

The argument to create_state() would let the code
distinguish between a new state object (NULL == clone) and
one being created through a call to gsl_rng_clone() (NULL !=

The library code could default to the old behavior if size >
0, but use the new callbacks if 0 == size.  (Are there any
existing implementations that have a zero size?)

Any thoughts on this matter?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]