Re: [pure-lang-users] C macros
Status: Beta
Brought to you by:
agraef
From: Albert G. <Dr....@t-...> - 2008-08-28 15:40:52
|
Albert Graef wrote: > Eddie Rucker wrote: >> Something else that would be neat. Since matrices are created in gsl >> with an gsl_matrix_alloc command and freed by gsl_matrix_free command, >> it would be nice if we had some way to mark stuff so that the garbage >> collector would call gsl_matrix_free when a matrix is no longer in use. >> Is this possible? I noticed vectors and a bunch of other stuff allocated >> and deallocated this way. > > Yes, sentinels. They're on my TODO list for 0.6, which I started working > on today (yes, you can already find some nifty new stuff in svn ;-). Ok, these are implemented now, you can find them in primitives.pure. I named them "sentries" this time around, that's shorter and can't be confused with the well-established notion of the sentinel value. Usage is easy, see the fopen function in system.pure for a real-world example. I've rewritten that so that it returns a file pointer that takes care of closing itself when it gets garbage-collected. Let's see these in action: > using system; > let x = sentry (\x->puts $ "done: "+str x) (foo 99); > clear x done: foo 99 How about doing smart pointers: mymalloc size = sentry free (malloc size); One thing worth noting here is that 'sentry' takes the sentry function as its first argument, which makes it easy to curry it. Add minimal error checking: mymalloc size = if null p then p else sentry free p when p = malloc size end; If you also want to be able to free the pointer explicitly, the sentry must of course then be released from his duty: myfree p = clear_sentry p $$ free p; Note that only a single sentry per expression is supported by 'sentry', but since you can find an existing sentry with get_sentry, it's easy to chain them if you need more than one. Also note that sentries can only be placed at applications and pointer objects, but this should cover all cases where they're really needed. Cheers, Albert -- Dr. Albert Gr"af Dept. of Music-Informatics, University of Mainz, Germany Email: Dr....@t-..., ag...@mu... WWW: http://www.musikinformatik.uni-mainz.de/ag |