From vograno at evafunds.com  Tue Apr 12 22:38:17 2005
From: vograno at evafunds.com (Vadim Ogranovich)
Date: Tue Apr 12 22:38:56 2005
Subject: [Rd] How allocate STRSXP outside of gc
Message-ID: <C698D707214E6F4AB39AB7096C3DE5A59E8931@phost015.EVAFUNDS.intermedia.net>

Thanks Duncan!

R_PreserveObject will do. One thought, wouldn't it make sense to modify
R_PreserveObject to return its argument?
This would allow things like

static SEXP fooSexp = R_PreserveObject(mkChar("foo"));

and would also make R_PreserveObject more similar to Rf_protect().

There should be no problem w/ backward compatability, at least not that
I could see.

Thanks,
Vadim

> -----Original Message-----
> From: Duncan Temple Lang [mailto:duncan@wald.ucdavis.edu] 
> Sent: Tuesday, April 12, 2005 12:52 PM
> To: Vadim Ogranovich
> Cc: r-devel@stat.math.ethz.ch
> Subject: Re: [Rd] How allocate STRSXP outside of gc
> 
> Look at R_PreserveObject to see if it will do what you want.
> It certainly used to.
> 
> SEXP getFoo()
> {
>   static SEXP val = NULL;
> 
>   if(!val) {
>     val = mkChar("foo");
>     R_PreserveObject(val);
>   }
> 
>   return(val);
> }
> 
> 
> You may want to have a routine that is called when the 
> package is unloaded that calls R_ReleaseObject().
> 
> Alternatively, store the object in a package's namespace 
> environment and it won't be gc'ed.
> 
>  D.
> 
> 
> Vadim Ogranovich wrote:
> > Hi,
> >  
> > I am trying to figure a way to allocate a string SEXP so that gc() 
> > won't ever collect it.
> >  
> > Here is a little bit of a background. Suppose I want to write a 
> > .Call-callable function that upon each call returns the same value, 
> > say
> > mkChar("foo"):
> >  
> > SEXP getFoo() {
> >    return mkChar("foo");
> > }
> >  
> > The above implementation doesn't take advantage of the fact that
> > mkChar("foo") could be pre-computed only once, and then the 
> function 
> > would return the pre-computed value. So the question is how 
> to create 
> > this precomputed value.
> >  
> >  
> > The closest thing I could find in the sources is 
> R_NaString, but I was 
> > not able to trace down how it comes about.
> >  
> >  
> > Thanks,
> > Vadim
> >  
> >  
> > P.S. I was able to solve a similar problem with symbols. If 
> I need a 
> > symbol "foo", I do
> >  
> > static SEXP  FooSymbol = install("foo");
> >  
> > and then use FooSymbol instead of install("foo")
> >  
> >  
> >  
> > 
> > 	[[alternative HTML version deleted]]
> > 
> > ______________________________________________
> > R-devel@stat.math.ethz.ch mailing list 
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> -- 
> Duncan Temple Lang                duncan@wald.ucdavis.edu
> Department of Statistics          work:  (530) 752-4782
> 371 Kerr Hall                     fax:   (530) 752-7099
> One Shields Ave.
> University of California at Davis
> Davis, CA 95616, USA
> 
> 
> 
>


[Rd] How allocate STRSXP outside of gc
Vadim Ogranovich