3232namespace Rcpp {
3333
3434 // {{{ SexpStack
35- static SEXP RCPP_PROTECTION_STACK = R_NilValue ;
36- static SEXP* RCPP_PROTECTION_STACK_PTR = 0 ;
37- static bool RCPP_PROTECTION_STACK_READY = false ;
38-
39- #define GET_TOP () TRUELENGTH(RCPP_PROTECTION_STACK)
40- #define SET_TOP (TOP ) SET_TRUELENGTH(RCPP_PROTECTION_STACK, TOP)
41-
42- inline void init_ProtectionStack (){
43- if (!RCPP_PROTECTION_STACK_READY){
44- RCPP_PROTECTION_STACK = get_Rcpp_protection_stack () ;
45- RCPP_PROTECTION_STACK_PTR = get_vector_ptr (RCPP_PROTECTION_STACK) ;
46- RCPP_PROTECTION_STACK_READY = true ;
47- }
48- }
4935
5036 SEXP Rcpp_PreserveObject (SEXP x){
51- #if RCPP_USE_NEW_PRESERVE_RELEASE
52- if ( x != R_NilValue ){
53- init_ProtectionStack ();
54- int top = GET_TOP () ;
55- RCPP_DEBUG_2 ( " Rcpp_PreserveObject( <%p>), top = %d" , x, top )
56- top++ ;
57- // RCPP_PROTECTION_STACK_PTR[top] = x ;
58- set_vector_elt ( RCPP_PROTECTION_STACK, top, x ) ;
59- SET_TOP (top) ;
60- }
61- #if RCPP_DEBUG_LEVEL > 1
62- Rcpp_Stack_Debug () ;
63- #endif
64- #else
6537 if ( x != R_NilValue ) {
6638 R_PreserveObject (x);
6739 }
68- #endif
6940 return x ;
7041 }
7142 void Rcpp_ReleaseObject (SEXP x){
72- #if RCPP_USE_NEW_PRESERVE_RELEASE
73- if ( x != R_NilValue ){
74- init_ProtectionStack ();
75-
76- int top = GET_TOP ();
77- RCPP_DEBUG_2 ( " Rcpp_ReleaseObject( <%p>), top = %d )" , x, top )
78-
79- if ( x == RCPP_PROTECTION_STACK_PTR[top] ) {
80- RCPP_PROTECTION_STACK_PTR[top] = R_NilValue ;
81- top-- ;
82- SET_TOP (top) ;
83- } else {
84- int i = top ;
85- for ( ; i>=0 ; i--){
86- if ( x == RCPP_PROTECTION_STACK_PTR[i] ){
87- // swap position i and top
88- // perhaps should bubble down instead
89-
90- RCPP_PROTECTION_STACK_PTR[i] = RCPP_PROTECTION_STACK_PTR[top] ;
91- RCPP_PROTECTION_STACK_PTR[top] = R_NilValue ;
92- top-- ;
93-
94- SET_TOP (top) ;
95- break ;
96- }
97- }
98- #if RCPP_DEBUG_LEVEL > 0
99- if ( i < 0 ) RCPP_DEBUG_2 ( " !!!! STACK ERROR, did not find SEXP <%p> (i=%d)" , x, i ) ;
100- #endif
101- }
102- #if RCPP_DEBUG_LEVEL > 1
103- Rcpp_Stack_Debug () ;
104- #endif
105- }
106- #else
10743 if (x != R_NilValue) {
10844 R_ReleaseObject (x);
10945 }
110- #endif
11146 }
11247
11348 SEXP Rcpp_ReplaceObject (SEXP x, SEXP y){
@@ -116,26 +51,6 @@ namespace Rcpp {
11651 } else if ( y == R_NilValue ){
11752 Rcpp_ReleaseObject ( x ) ;
11853 } else {
119- #if RCPP_USE_NEW_PRESERVE_RELEASE
120- init_ProtectionStack ();
121-
122- int top = GET_TOP ();
123- RCPP_DEBUG_3 ( " Rcpp_ReplaceObject( <%p> , <%p> ), top = %d )" , x, y, top )
124- int i = top ;
125- for ( ; i>= 0 ; i--){
126- if ( x == RCPP_PROTECTION_STACK_PTR[i] ){
127- set_vector_elt ( RCPP_PROTECTION_STACK, i, y) ;
128- break ;
129- }
130- }
131- #if RCPP_DEBUG_LEVEL > 0
132- if ( i < 0 ) RCPP_DEBUG_1 ( " STACK ERROR, did not find SEXP <%p>" , x ) ;
133- #endif
134-
135- #if RCPP_DEBUG_LEVEL > 1
136- Rcpp_Stack_Debug () ;
137- #endif
138- #else
13954 // if we are setting to the same SEXP as we already have, do nothing
14055 if (x != y) {
14156
@@ -145,27 +60,10 @@ namespace Rcpp {
14560 // the new SEXP is not NULL, so preserve it
14661 Rcpp_PreserveObject (y);
14762
148- // update();
14963 }
150- #endif
15164 }
15265 return y ;
15366 }
154-
155- void Rcpp_Stack_Debug (){
156- init_ProtectionStack ();
157- int top = GET_TOP () ;
158- if ( top == -1 ){
159- Rprintf ( " Rcpp_Stack_Debug [<<%p>>] : empty stack\n " , RCPP_PROTECTION_STACK ) ;
160- } else {
161- int n = top + 1 ;
162- Rprintf ( " Rcpp_Stack_Debug, %d objects on stack [<<%p>>]\n " , n, RCPP_PROTECTION_STACK ) ;
163- for ( int i=0 ; i<n;i++){
164- SEXP ptr = RCPP_PROTECTION_STACK_PTR[i] ;
165- Rprintf ( " [%4d] TYPE = %s, pointer = <%p>\n " , i, sexp_to_name (TYPEOF (ptr)), ptr ) ;
166- }
167- }
168- }
16967 // }}}
17068
17169
0 commit comments