Skip to content

Commit 048ee1e

Browse files
replace PROTECT/UNPROTECT by Shield
1 parent f474dfe commit 048ee1e

File tree

21 files changed

+282
-409
lines changed

21 files changed

+282
-409
lines changed

inst/include/Rcpp/DataFrame.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,10 @@ namespace Rcpp{
9797
obj.erase(strings_as_factors_index) ;
9898
names.erase(strings_as_factors_index) ;
9999
obj.attr( "names") = names ;
100-
SEXP call = PROTECT( Rf_lang3(as_df_symb, obj, wrap( strings_as_factors ) ) ) ;
100+
Shield<SEXP> call( Rf_lang3(as_df_symb, obj, wrap( strings_as_factors ) ) ) ;
101101
SET_TAG( CDDR(call), strings_as_factors_symb ) ;
102-
SEXP res = PROTECT( Rcpp_eval( call ) ) ;
102+
Shield<SEXP> res( Rcpp_eval( call ) ) ;
103103
DataFrame_Impl out( res ) ;
104-
UNPROTECT(2) ;
105104
return out ;
106105

107106
}

inst/include/Rcpp/Function.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ namespace Rcpp{
5555
*/
5656
Function_Impl(const std::string& name) {
5757
SEXP nameSym = Rf_install( name.c_str() ); // cannot be gc()'ed once in symbol table
58-
SEXP x = PROTECT( Rf_findFun( nameSym, R_GlobalEnv ) ) ;
58+
Shield<SEXP> x( Rf_findFun( nameSym, R_GlobalEnv ) ) ;
5959
Storage::set__(x) ;
60-
UNPROTECT(1) ;
6160
}
6261

6362
SEXP operator()() const {

inst/include/Rcpp/String.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ namespace Rcpp {
385385
template <int RTYPE>
386386
SEXP string_element_converter<RTYPE>::get( const Rcpp::String& input) {
387387
RCPP_DEBUG( "string_element_converter::get< Rcpp::String >()" )
388-
return input.get_sexp() ;
388+
return input.get_sexp() ;
389389
}
390390

391391
template <>
@@ -396,23 +396,22 @@ namespace Rcpp {
396396
template <int RTYPE>
397397
template <typename T>
398398
string_proxy<RTYPE>& string_proxy<RTYPE>::operator+=(const T& rhs) {
399-
String tmp = get() ;
400-
tmp += rhs ;
401-
set( tmp ) ;
402-
return *this ;
399+
String tmp = get() ;
400+
tmp += rhs ;
401+
set( tmp ) ;
402+
return *this ;
403403
}
404404

405405
}
406406

407407

408408
template <>
409409
inline SEXP wrap<Rcpp::String>( const Rcpp::String& object) {
410-
RCPP_STRING_DEBUG( "wrap<String>()" ) ;
411-
SEXP res = PROTECT( Rf_allocVector( STRSXP, 1 ) ) ;
412-
SEXP data = object.get_sexp();
413-
SET_STRING_ELT( res, 0, data ) ;
414-
UNPROTECT(1) ;
415-
return res ;
410+
RCPP_STRING_DEBUG( "wrap<String>()" ) ;
411+
Shield<SEXP> res( Rf_allocVector( STRSXP, 1 ) ) ;
412+
SEXP data = object.get_sexp();
413+
SET_STRING_ELT( res, 0, data ) ;
414+
return res ;
416415
}
417416

418417

inst/include/Rcpp/api/meat/Matrix.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,14 @@ namespace Rcpp{
135135
template <int RTYPE>
136136
template <typename U>
137137
void Matrix<RTYPE>::fill_diag__dispatch( traits::false_type, const U& u){
138-
SEXP elem = PROTECT( converter_type::get( u ) ) ;
138+
Shield<SEXP> elem( converter_type::get( u ) ) ;
139139
int n = Matrix::ncol() ;
140140
int offset = n +1 ;
141141
iterator it( VECTOR::begin()) ;
142142
for( int i=0; i<n; i++){
143143
*it = ::Rf_duplicate( elem );
144144
it += offset;
145145
}
146-
UNPROTECT(1); // elem
147146
}
148147

149148
template <int RTYPE>

inst/include/Rcpp/api/meat/Vector.h

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ namespace Rcpp{
237237
Storage::set__( target.get__() ) ;
238238
return begin()+i ;
239239
} else {
240-
SEXP newnames = PROTECT(::Rf_allocVector( STRSXP, n-1 ));
240+
Shield<SEXP> newnames(::Rf_allocVector( STRSXP, n-1 ));
241241
int i= 0 ;
242242
for( ; it < position; ++it, ++target_it,i++){
243243
*target_it = *it;
@@ -251,7 +251,6 @@ namespace Rcpp{
251251
SET_STRING_ELT( newnames, i-1, STRING_ELT(names,i) ) ;
252252
}
253253
target.attr( "names" ) = newnames ;
254-
UNPROTECT(1) ; /* newnames */
255254
Storage::set__( target.get__() ) ;
256255
return begin()+result ;
257256
}
@@ -281,7 +280,7 @@ namespace Rcpp{
281280
*target_it = *it ;
282281
}
283282
} else{
284-
SEXP newnames = PROTECT( ::Rf_allocVector(STRSXP, target_size) ) ;
283+
Shield<SEXP> newnames( ::Rf_allocVector(STRSXP, target_size) ) ;
285284
int i= 0 ;
286285
for( ; it < first; ++it, ++target_it, i++ ){
287286
*target_it = *it ;
@@ -293,7 +292,6 @@ namespace Rcpp{
293292
SET_STRING_ELT( newnames, i, STRING_ELT(names, i + nremoved ) );
294293
}
295294
target.attr("names" ) = newnames ;
296-
UNPROTECT(1) ; /* newnames */
297295
}
298296
Storage::set__( target.get__() ) ;
299297

@@ -314,15 +312,14 @@ namespace Rcpp{
314312
*target_it = *it ;
315313
}
316314
} else {
317-
SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n + 1) ) ;
315+
Shield<SEXP> newnames( ::Rf_allocVector( STRSXP, n + 1) ) ;
318316
int i = 0 ;
319317
for( ; it < this_end; ++it, ++target_it, i++ ){
320318
*target_it = *it ;
321319
SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) ) ;
322320
}
323321
SET_STRING_ELT( newnames, i, Rf_mkChar("") ) ;
324322
target.attr("names") = newnames ;
325-
UNPROTECT(1) ; /* newnames */
326323
}
327324
*target_it = object;
328325
Storage::set__( target.get__() ) ;
@@ -331,7 +328,7 @@ namespace Rcpp{
331328

332329
template <int RTYPE, template <class> class StoragePolicy>
333330
void Vector<RTYPE, StoragePolicy>::push_back__impl(const stored_type& object, traits::true_type){
334-
SEXP object_sexp = PROTECT( object ) ;
331+
Shield<SEXP> object_sexp( object ) ;
335332
int n = size() ;
336333
Vector target( n + 1 ) ;
337334
SEXP names = RCPP_GET_NAMES(Storage::get__()) ;
@@ -343,20 +340,17 @@ namespace Rcpp{
343340
*target_it = *it ;
344341
}
345342
} else {
346-
SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n + 1) ) ;
343+
Shield<SEXP> newnames( ::Rf_allocVector( STRSXP, n + 1) ) ;
347344
int i = 0 ;
348345
for( ; it < this_end; ++it, ++target_it, i++ ){
349346
*target_it = *it ;
350347
SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) ) ;
351348
}
352349
SET_STRING_ELT( newnames, i, Rf_mkChar("") ) ;
353350
target.attr("names") = newnames ;
354-
UNPROTECT(1) ; /* newnames */
355351
}
356352
*target_it = object_sexp;
357353
Storage::set__( target.get__() ) ;
358-
359-
UNPROTECT(1) ;
360354
}
361355

362356
template <int RTYPE, template <class> class StoragePolicy>
@@ -367,15 +361,14 @@ namespace Rcpp{
367361
iterator it(begin()) ;
368362
iterator this_end(end());
369363
SEXP names = RCPP_GET_NAMES(Storage::get__()) ;
370-
SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n+1 ) ) ;
364+
Shield<SEXP> newnames( ::Rf_allocVector( STRSXP, n+1 ) ) ;
371365
int i=0;
372366
if( names == R_NilValue ){
373367
SEXP dummy = PROTECT( Rf_mkChar("") );
374368
for( ; it < this_end; ++it, ++target_it,i++ ){
375369
*target_it = *it ;
376370
SET_STRING_ELT( newnames, i , dummy );
377371
}
378-
UNPROTECT(1) ; /* dummy */
379372
} else {
380373
for( ; it < this_end; ++it, ++target_it, i++ ){
381374
*target_it = *it ;
@@ -386,29 +379,25 @@ namespace Rcpp{
386379
target.attr("names") = newnames ;
387380

388381
*target_it = object;
389-
UNPROTECT(1) ; /* newnames, */
390382
Storage::set__( target.get__() ) ;
391-
392383
}
393384

394385
template <int RTYPE, template <class> class StoragePolicy>
395386
void Vector<RTYPE, StoragePolicy>::push_back_name__impl(const stored_type& object, const std::string& name, traits::true_type ){
396-
SEXP object_sexp = PROTECT( object ) ;
387+
Shield<SEXP> object_sexp( object ) ;
397388
int n = size() ;
398389
Vector target( n + 1 ) ;
399390
iterator target_it( target.begin() ) ;
400391
iterator it(begin()) ;
401392
iterator this_end(end());
402393
SEXP names = RCPP_GET_NAMES(Storage::get__()) ;
403-
SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n+1 ) ) ;
394+
Shield<SEXP> newnames( ::Rf_allocVector( STRSXP, n+1 ) ) ;
404395
int i=0;
405396
if( names == R_NilValue ){
406-
SEXP dummy = PROTECT( Rf_mkChar("") );
407397
for( ; it < this_end; ++it, ++target_it,i++ ){
408398
*target_it = *it ;
409-
SET_STRING_ELT( newnames, i , dummy );
399+
SET_STRING_ELT( newnames, i , R_BlankString );
410400
}
411-
UNPROTECT(1) ; /* dummy */
412401
} else {
413402
for( ; it < this_end; ++it, ++target_it, i++ ){
414403
*target_it = *it ;
@@ -419,9 +408,7 @@ namespace Rcpp{
419408
target.attr("names") = newnames ;
420409

421410
*target_it = object_sexp;
422-
UNPROTECT(2) ; /* newnames, object_sexp */
423411
Storage::set__( target.get__() ) ;
424-
425412
}
426413

427414
template <int RTYPE, template <class> class StoragePolicy>
@@ -439,45 +426,42 @@ namespace Rcpp{
439426
*target_it = *it ;
440427
}
441428
} else{
442-
SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n + 1) );
429+
Shield<SEXP> newnames( ::Rf_allocVector( STRSXP, n + 1) );
443430
int i=1 ;
444431
SET_STRING_ELT( newnames, 0, Rf_mkChar("") ) ;
445432
for( ; it<this_end; ++it, ++target_it, i++){
446433
*target_it = *it ;
447434
SET_STRING_ELT( newnames, i, STRING_ELT(names, i-1 ) ) ;
448435
}
449436
target.attr("names") = newnames ;
450-
UNPROTECT(1) ; /* newnames */
451437
}
452438
Storage::set__( target.get__() ) ;
453439
}
454440

455441
template <int RTYPE, template <class> class StoragePolicy>
456442
void Vector<RTYPE, StoragePolicy>::push_front__impl(const stored_type& object, traits::true_type ){
457-
SEXP object_sexp = PROTECT( object ) ;
443+
Shield<SEXP> object_sexp( object ) ;
458444
int n = size() ;
459445
Vector target( n+1);
460446
iterator target_it(target.begin());
461447
iterator it(begin());
462448
iterator this_end(end());
463449
*target_it = object_sexp ;
464-
UNPROTECT(1); /* object_sexp */
465450
++target_it ;
466451
SEXP names = RCPP_GET_NAMES(Storage::get__()) ;
467452
if( names == R_NilValue ){
468453
for( ; it<this_end; ++it, ++target_it){
469454
*target_it = *it ;
470455
}
471456
} else{
472-
SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n + 1) );
457+
Shield<SEXP> newnames( ::Rf_allocVector( STRSXP, n + 1) );
473458
int i=1 ;
474459
SET_STRING_ELT( newnames, 0, Rf_mkChar("") ) ;
475460
for( ; it<this_end; ++it, ++target_it, i++){
476461
*target_it = *it ;
477462
SET_STRING_ELT( newnames, i, STRING_ELT(names, i-1 ) ) ;
478463
}
479464
target.attr("names") = newnames ;
480-
UNPROTECT(1) ; /* newnames */
481465
}
482466
Storage::set__( target.get__() ) ;
483467
}
@@ -490,19 +474,17 @@ namespace Rcpp{
490474
iterator it(begin()) ;
491475
iterator this_end(end());
492476
SEXP names = RCPP_GET_NAMES(Storage::get__()) ;
493-
SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n+1 ) ) ;
477+
Shield<SEXP> newnames( ::Rf_allocVector( STRSXP, n+1 ) ) ;
494478
int i=1;
495479
SET_STRING_ELT( newnames, 0, Rf_mkChar( name.c_str() ) );
496480
*target_it = object;
497481
++target_it ;
498482

499483
if( names == R_NilValue ){
500-
SEXP dummy = PROTECT( Rf_mkChar("") );
501484
for( ; it < this_end; ++it, ++target_it,i++ ){
502485
*target_it = *it ;
503-
SET_STRING_ELT( newnames, i , dummy );
486+
SET_STRING_ELT( newnames, i , R_BlankString );
504487
}
505-
UNPROTECT(1) ; /* dummy */
506488
} else {
507489
for( ; it < this_end; ++it, ++target_it, i++ ){
508490
*target_it = *it ;
@@ -511,41 +493,36 @@ namespace Rcpp{
511493
}
512494
target.attr("names") = newnames ;
513495

514-
UNPROTECT(1) ; /* newnames, */
515496
Storage::set__( target.get__() ) ;
516497
}
517498

518499
template <int RTYPE, template <class> class StoragePolicy>
519500
void Vector<RTYPE, StoragePolicy>::push_front_name__impl(const stored_type& object, const std::string& name, traits::true_type ){
520-
SEXP object_sexp = PROTECT(object) ;
501+
Shield<SEXP> object_sexp(object) ;
521502
int n = size() ;
522503
Vector target( n + 1 ) ;
523504
iterator target_it( target.begin() ) ;
524505
iterator it(begin()) ;
525506
iterator this_end(end());
526507
SEXP names = RCPP_GET_NAMES(Storage::get__()) ;
527-
SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n+1 ) ) ;
508+
Shield<SEXP> newnames( ::Rf_allocVector( STRSXP, n+1 ) ) ;
528509
int i=1;
529510
SET_STRING_ELT( newnames, 0, Rf_mkChar( name.c_str() ) );
530511
*target_it = object_sexp;
531512
++target_it ;
532513

533514
if( names == R_NilValue ){
534-
SEXP dummy = PROTECT( Rf_mkChar("") );
535515
for( ; it < this_end; ++it, ++target_it,i++ ){
536516
*target_it = *it ;
537-
SET_STRING_ELT( newnames, i , dummy );
517+
SET_STRING_ELT( newnames, i , R_BlankString );
538518
}
539-
UNPROTECT(1) ; /* dummy */
540519
} else {
541520
for( ; it < this_end; ++it, ++target_it, i++ ){
542521
*target_it = *it ;
543522
SET_STRING_ELT( newnames, i, STRING_ELT(names, i-1 ) ) ;
544523
}
545524
}
546525
target.attr("names") = newnames ;
547-
548-
UNPROTECT(2) ; /* newnames, object_sexp */
549526
Storage::set__( target.get__() ) ;
550527
}
551528

@@ -569,7 +546,7 @@ namespace Rcpp{
569546
*target_it = *it ;
570547
}
571548
} else{
572-
SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n + 1 ) ) ;
549+
Shield<SEXP> newnames( ::Rf_allocVector( STRSXP, n + 1 ) ) ;
573550
int i=0;
574551
for( ; it < position; ++it, ++target_it, i++){
575552
*target_it = *it ;
@@ -585,15 +562,14 @@ namespace Rcpp{
585562
SET_STRING_ELT( newnames, i, STRING_ELT(names, i - 1) ) ;
586563
}
587564
target.attr( "names" ) = newnames ;
588-
UNPROTECT(1) ; /* newmanes */
589565
}
590566
Storage::set__( target.get__() ) ;
591567
return result ;
592568
}
593569

594570
template <int RTYPE, template <class> class StoragePolicy>
595-
typename Vector<RTYPE, StoragePolicy>::iterator Vector<RTYPE, StoragePolicy>::insert__impl( iterator position, const stored_type& object, traits::true_type){
596-
PROTECT( object ) ;
571+
typename Vector<RTYPE, StoragePolicy>::iterator Vector<RTYPE, StoragePolicy>::insert__impl( iterator position, const stored_type& object_, traits::true_type){
572+
Shield<SEXP> object( object_ ) ;
597573
int n = size() ;
598574
Vector target( n+1 ) ;
599575
iterator target_it = target.begin();
@@ -612,7 +588,7 @@ namespace Rcpp{
612588
*target_it = *it ;
613589
}
614590
} else{
615-
SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n + 1 ) ) ;
591+
Shield<SEXP> newnames( ::Rf_allocVector( STRSXP, n + 1 ) ) ;
616592
int i=0;
617593
for( ; it < position; ++it, ++target_it, i++){
618594
*target_it = *it ;
@@ -628,10 +604,8 @@ namespace Rcpp{
628604
SET_STRING_ELT( newnames, i, STRING_ELT(names, i - 1) ) ;
629605
}
630606
target.attr( "names" ) = newnames ;
631-
UNPROTECT(1) ; /* newmanes */
632607
}
633608
Storage::set__( target.get__() ) ;
634-
UNPROTECT(1); /* object */
635609
return result ;
636610
}
637611

0 commit comments

Comments
 (0)