[Rd] How to maintain memory in R extension

2014-11-12 Thread Zheng Da
Hello,

I wrote a system to perform data analysis in C++. Now I am integrating
it to R. I need to allocate memory for my own C++ data structures,
which can't be represented by any R data structures. I create a global
hashtable to keep a reference to the C++ data structures. Whenever I
allocate one, I register it in the hashtable and return its key to the
R code. So later on, the R code can access the C++ data structures
with their keys.

The problem is how to perform garbage collection on the C++ data
structures. Once an R object that contains the key is garbage
collected, the R code can no longer access the corresponding C++ data
structure, so I need to deallocate it. Is there any way that the C++
code can get notification when an R object gets garbage collected? If
not, what is the usual way to manage memory in R extensions?

Thanks,
Da

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] How to maintain memory in R extension

2014-11-12 Thread Gábor Csárdi
Hi,

I think you need external pointers:
http://cran.r-project.org/doc/manuals/r-release/R-exts.html#External-pointers-and-weak-references
The docs also has an example.

See more examples from other R packages here:
https://github.com/search?q=R_MakeExternalPtr+user%3Acrantype=Codeutf8=%E2%9C%93

Gabor

On Wed, Nov 12, 2014 at 8:36 AM, Zheng Da zhengda1...@gmail.com wrote:
 Hello,

 I wrote a system to perform data analysis in C++. Now I am integrating
 it to R. I need to allocate memory for my own C++ data structures,
 which can't be represented by any R data structures. I create a global
 hashtable to keep a reference to the C++ data structures. Whenever I
 allocate one, I register it in the hashtable and return its key to the
 R code. So later on, the R code can access the C++ data structures
 with their keys.

 The problem is how to perform garbage collection on the C++ data
 structures. Once an R object that contains the key is garbage
 collected, the R code can no longer access the corresponding C++ data
 structure, so I need to deallocate it. Is there any way that the C++
 code can get notification when an R object gets garbage collected? If
 not, what is the usual way to manage memory in R extensions?

 Thanks,
 Da

 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] How to maintain memory in R extension

2014-11-12 Thread Martin Morgan

On 11/12/2014 05:36 AM, Zheng Da wrote:

Hello,

I wrote a system to perform data analysis in C++. Now I am integrating
it to R. I need to allocate memory for my own C++ data structures,
which can't be represented by any R data structures. I create a global
hashtable to keep a reference to the C++ data structures. Whenever I
allocate one, I register it in the hashtable and return its key to the
R code. So later on, the R code can access the C++ data structures
with their keys.

The problem is how to perform garbage collection on the C++ data
structures. Once an R object that contains the key is garbage
collected, the R code can no longer access the corresponding C++ data
structure, so I need to deallocate it. Is there any way that the C++
code can get notification when an R object gets garbage collected? If
not, what is the usual way to manage memory in R extensions?


register a finalizer that runs when there are no longer references to the R 
object, see ?reg.finalizer or the interface to R and C finalizers in 
Rinternals.h. If you return more than one reference to a key, then of course 
you'll have to manage these in your own C++ code.


Martin Morgan



Thanks,
Da

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel




--
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] How to maintain memory in R extension

2014-11-12 Thread Zheng Da
Thank you, Gabor and Martin. It helps a lot.

Da

On Wed, Nov 12, 2014 at 1:20 PM, Martin Morgan mtmor...@fredhutch.org wrote:
 On 11/12/2014 05:36 AM, Zheng Da wrote:

 Hello,

 I wrote a system to perform data analysis in C++. Now I am integrating
 it to R. I need to allocate memory for my own C++ data structures,
 which can't be represented by any R data structures. I create a global
 hashtable to keep a reference to the C++ data structures. Whenever I
 allocate one, I register it in the hashtable and return its key to the
 R code. So later on, the R code can access the C++ data structures
 with their keys.

 The problem is how to perform garbage collection on the C++ data
 structures. Once an R object that contains the key is garbage
 collected, the R code can no longer access the corresponding C++ data
 structure, so I need to deallocate it. Is there any way that the C++
 code can get notification when an R object gets garbage collected? If
 not, what is the usual way to manage memory in R extensions?


 register a finalizer that runs when there are no longer references to the R
 object, see ?reg.finalizer or the interface to R and C finalizers in
 Rinternals.h. If you return more than one reference to a key, then of course
 you'll have to manage these in your own C++ code.

 Martin Morgan



 Thanks,
 Da

 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel



 --
 Computational Biology / Fred Hutchinson Cancer Research Center
 1100 Fairview Ave. N.
 PO Box 19024 Seattle, WA 98109

 Location: Arnold Building M1 B861
 Phone: (206) 667-2793

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] How to maintain memory in R extension

2014-11-12 Thread Henrik Bengtsson
On Wed, Nov 12, 2014 at 10:20 AM, Martin Morgan mtmor...@fredhutch.org wrote:
 On 11/12/2014 05:36 AM, Zheng Da wrote:

 Hello,

 I wrote a system to perform data analysis in C++. Now I am integrating
 it to R. I need to allocate memory for my own C++ data structures,
 which can't be represented by any R data structures. I create a global
 hashtable to keep a reference to the C++ data structures. Whenever I
 allocate one, I register it in the hashtable and return its key to the
 R code. So later on, the R code can access the C++ data structures
 with their keys.

 The problem is how to perform garbage collection on the C++ data
 structures. Once an R object that contains the key is garbage
 collected, the R code can no longer access the corresponding C++ data
 structure, so I need to deallocate it. Is there any way that the C++
 code can get notification when an R object gets garbage collected? If
 not, what is the usual way to manage memory in R extensions?


 register a finalizer that runs when there are no longer references to the R
 object, see ?reg.finalizer or the interface to R and C finalizers in
 Rinternals.h. If you return more than one reference to a key, then of course
 you'll have to manage these in your own C++ code.

A small but important addition: Make sure your registered finalizer
also works, or at least don't core dump R, if your package (or one of
its dependencies) happens be unloaded by the time the garbage
collector runs.  This task seems easy but can be quite tricky, e.g.
should you reload you package temporarily and what are the side
effects from doing that?

/Henrik


 Martin Morgan


 Thanks,
 Da

 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel



 --
 Computational Biology / Fred Hutchinson Cancer Research Center
 1100 Fairview Ave. N.
 PO Box 19024 Seattle, WA 98109

 Location: Arnold Building M1 B861
 Phone: (206) 667-2793


 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel