This is the mail archive of the
gsl-discuss@sources.redhat.com
mailing list for the GSL project.
Re: modifying matrix allocation functions for use with R
- To: Faheem Mitha <faheem at email dot unc dot edu>
- Subject: Re: modifying matrix allocation functions for use with R
- From: "Timothy H. Keitt" <tklistaddr at keittlab dot bio dot sunysb dot edu>
- Date: Mon, 08 Oct 2001 18:02:58 -0400
- CC: gsl-discuss at sources dot redhat dot com
- Organization: State University of New York at Stony Brook
- References: <Pine.LNX.4.33.0110051535210.7289-100000@Chrestomanci>
This code shows how to wrap a foreign pointer and have it garbage
collected when the R object goes out of scope.
Tim
SEXP
PgSQLconnect(SEXP connInfo) {
PGconn *conn;
SEXP connPtr;
char *connInfoStr = CHAR(STRING_ELT(connInfo, 0));
PROTECT(connInfo = coerceVector(connInfo, STRSXP));
conn = PQconnectdb(connInfoStr);
UNPROTECT(1);
PROTECT(connPtr = R_MakeExternalPtr((void *) conn,
mkChar("PgSQL connection"),
R_NilValue));
R_RegisterCFinalizer(connPtr, (R_CFinalizer_t)PgSQLcloseConnection);
UNPROTECT(1);
return(connPtr);
}
Faheem Mitha wrote:
>Dear People,
>
>Here is a relatively esoteric question. Some of you may be familiar with
>R (sometimes called GNU S) which does statistical stuff. It is possible to
>use C code compiled into a shared library as a backend for R. Now, I was
>wanting to use some GSL routines in this code.
>
>In particular, I need to use allocation functions like gsl_matrix_alloc.
>Unfortunately, I'm not sure whether standard calloc/malloc that are used
>work with R. I'm going to check this, but I think the answer is most
>likely no. R uses a routine called R_alloc which takes storage from the R
>heap and manages the cleanup itself (so you don't need to use free).
>
>One option I was considering was rewriting gsl_matrix_alloc (leaving out
>the error checking) in terms of R_alloc. However, I can't find the code
>for FUNCTION(gsl_block, alloc) (I assume this means gsl_block_alloc)
>used in the code for gsl_matrix_alloc. gsl_matrix_alloc itself seems to be
>in matrix/init_source.c. Can someone point me to the code for
>gsl_block_alloc, or suggest any other options?
>
>I'm not subscribed to the list, so I would be obliged if you would cc me.
>
> Sincerely, Faheem Mitha.
>
--
Timothy H. Keitt
Department of Ecology and Evolution
State University of New York at Stony Brook
Stony Brook, New York 11794 USA
Phone: 631-632-1101, FAX: 631-632-7626
http://life.bio.sunysb.edu/ee/keitt/