X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fprotected-scm.cc;h=3a16254187a05b0b7880a53ec9250c47071e8aee;hb=90e4d7057f3857da049dfda3d130017d4719bd6b;hp=c0235496f4ee73aa3f5cbbdbe7ba69004331fa5b;hpb=47db9a3883d726ca53e2133a3b2298f78dd6a32e;p=lilypond.git diff --git a/lily/protected-scm.cc b/lily/protected-scm.cc index c0235496f4..3a16254187 100644 --- a/lily/protected-scm.cc +++ b/lily/protected-scm.cc @@ -19,54 +19,78 @@ #include "protected-scm.hh" +// We store the data in the car of a cons cell: it is faster to keep +// only one object protected during the life time of Protected_scm +// than several. + Protected_scm::Protected_scm () + : object_ (SCM_UNDEFINED) { - object_ = SCM_UNDEFINED; } Protected_scm::Protected_scm (SCM s) + : object_ (s) { - object_ = SCM_NIMP (s) ? scm_gc_protect_object (s) : s; + // Only allow immediate objects at construction time. Protected_scm + // is intended for variables of static duration, and creating + // non-immediate objects when GUILE is not yet up is a bad idea. + assert (SCM_IMP (s)); } -Protected_scm::Protected_scm (Protected_scm const &s) -{ - object_ = (SCM_NIMP (s.object_) ? scm_gc_protect_object (s.object_) - : s.object_); -} +SCM Protected_scm::list_ = SCM_EOL; +SCM Protected_scm::last_ = SCM_EOL; -Protected_scm::~Protected_scm () +void +Protected_scm::protectify (SCM s) { - if (SCM_NIMP (object_)) - scm_gc_unprotect_object (object_); + s = scm_list_1 (s); + if (SCM_CONSP (last_)) + SCM_SETCDR (last_, s); + else + list_ = scm_permanent_object (s); + last_ = object_ = s; } Protected_scm & Protected_scm::operator = (SCM s) { - if (object_ == s) - return *this; + if (SCM_CONSP (object_)) + SCM_SETCAR (object_, s); + else if (SCM_IMP (s)) + object_ = s; + else + protectify (s); - if (SCM_NIMP (object_)) - scm_gc_unprotect_object (object_); - - object_ = SCM_NIMP (s) ? scm_gc_protect_object (s) : s; return *this; } Protected_scm & Protected_scm::operator = (Protected_scm const &s) { - return operator = (s.object_); + return *this = (SCM) s; } -Protected_scm::operator SCM () const +Protected_scm::operator SCM const & () const { + if (SCM_CONSP (object_)) + return *SCM_CARLOC (object_); return object_; } -SCM -Protected_scm::to_SCM () const +Protected_scm::operator SCM & () { - return object_; + // The reference may be used to overwrite an immediate value with a + // non-immediate one, so we _have_ to create full protection. + if (!SCM_CONSP (object_)) + protectify (object_); + + return *SCM_CARLOC (object_); +} + +bool +Protected_scm::is_bound () const +{ + if (SCM_CONSP (object_)) + return !SCM_UNBNDP (SCM_CAR (object_)); + return !SCM_UNBNDP (object_); }