Dear R-devel This one seems a bit arcane for R-help. I very often use R to call routines written in Delphi (son of Pascal), doing "persistent" memory allocation within Delphi. That is, I start R and load the Delphi DLL; then I use .C to call a Delphi routine which allocates a piece of memory X and returns to R a pointer to X; then I do some more stuff in R; then I call another Delphi routine, passing it the pointer to X. Finally, I call another Delphi routine from R to free the memory at X. For years, I've used Delphi's own memory manager for this (the basic one, not SHAREMEM.DLL), but recently I've been wondering whether it would be better to use R's wrappers for "alloc" etc. instead. The Delphi routines seem generally to work fine, but my nagging concern [based on ignorance] is whether R's memory manager will somehow not realize that Delphi has already allocated the memory, and will overwrite it inadvertently. It's fairly easy to replace Delphi's manager with the R routines, following the instructions in section 5.1.2 of R-EXTS and the Delphi manual. So my first question is: should I bother using the R routines instead? My second question is: if I do use the R replacements, what are their names nowadays? R-EXTS refers to Calloc, Realloc and Free, but these aren't exported by R.DLL (for R1.9.0 or 1.9.1) and I couldn't find mention of any name change in the NEWS file. Running dumpbin /exports R.dll | find /i "alloc" suggests either "memalloc/memrealloc/memfree" or "Rm_alloc..." (or "R_chk_alloc..."). I gather that the S_alloc family is intended for temporary allocations that are cleared at the end of the the .C call. Any insights appreciated. FWIW I use Windows 2000 and Delphi 6. Mark ******************************* Mark Bravington CSIRO (CMIS) PO Box 1538 Castray Esplanade Hobart TAS 7001 phone (61) 3 6232 5118 fax (61) 3 6232 5012 Mark.Bravington@csiro.au
On Tue, 14 Sep 2004 Mark.Bravington@csiro.au wrote:> My second question is: > > if I do use the R replacements, what are their names nowadays?> R-EXTS refers to Calloc, Realloc and Free, but these aren't exported by > R.DLL (for R1.9.0 or 1.9.1) and I couldn't find mention of any name > change in the NEWS file.There is no change. R-exts does say you must include the appropriate headers, in this case include/R_ext/RS.h. They are defines, and not just simple calls to entry points. I'll leave you to take a closer look. -- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On Tue, 14 Sep 2004 09:38:55 +1000, <Mark.Bravington@csiro.au> wrote :>Dear R-devel > >This one seems a bit arcane for R-help. I very often use R to call routines written in Delphi (son of Pascal), doing "persistent" memory allocation within Delphi. That is, I start R and load the Delphi DLL; then I use .C to call a Delphi routine which allocates a piece of memory X and returns to R a pointer to X; then I do some more stuff in R; then I call another Delphi routine, passing it the pointer to X. Finally, I call another Delphi routine from R to free the memory at X. > >For years, I've used Delphi's own memory manager for this (the basic one, not SHAREMEM.DLL), but recently I've been wondering whether it would be better to use R's wrappers for "alloc" etc. instead. The Delphi routines seem generally to work fine, but my nagging concern [based on ignorance] is whether R's memory manager will somehow not realize that Delphi has already allocated the memory, and will overwrite it inadvertently. It's fairly easy to replace Delphi's manager with the R routines, following the instructions in section 5.1.2 of R-EXTS and the Delphi manual. So my first question is: > >should I bother using the R routines instead?There is not much reason to need to use the R routines. The disadvantages of using Delphi allocation are: - Your allocations won't be automatically deleted by the garbage collector (but neither are those of Calloc, you need R_alloc for that) - If allocation fails, it will be up to you to detect that and deal with it. The disadvantage of using the R routines Calloc etc. is that - You need to translate C macros to Delphi. - You need to worry about whether the RS.h file has changed with each release (but the Calloc etc. macros are likely to be very stable, they haven't changed since 2000). Duncan Murdoch
Thanks both All seems to work now, whether I use R_chk_alloc etc. (as per RS.h) or the Delphi routines. Sounds like I might as well stick with the latter though, since I have to do the housekeeping anyway. Mark #>should I bother using the R [memory allocation] routines instead [of the Delphi built-ins]? # #There is not much reason to need to use the R routines. The #disadvantages of using Delphi allocation are: # # - Your allocations won't be automatically deleted by the garbage #collector (but neither are those of Calloc, you need R_alloc for that) # - If allocation fails, it will be up to you to detect that and deal #with it. # #The disadvantage of using the R routines Calloc etc. is that # - You need to translate C macros to Delphi. # - You need to worry about whether the RS.h file has changed with #each release (but the Calloc etc. macros are likely to be very stable, #they haven't changed since 2000). # #Duncan Murdoch # #> if I do use the R replacements, what are their names nowadays? # #> R-EXTS refers to Calloc, Realloc and Free, but these aren't exported by #> R.DLL (for R1.9.0 or 1.9.1) and I couldn't find mention of any name #> change in the NEWS file. # #There is no change. R-exts does say you must include the appropriate #headers, in this case include/R_ext/RS.h. They are defines, and not just #simple calls to entry points. I'll leave you to take a closer look. # #-- #Brian D. Ripley, ripley@stats.ox.ac.uk