X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fprotected-scm.cc;h=225c64dc60fc220bf03910f25575a539a3ecbdfd;hb=255d19c2edfe2ec4dc648b582a96e6fcb0039197;hp=4447371a8318336d909cb4e10cc59e518423cf5a;hpb=1c846b2c2348b4e0ca4a3c2e8fb267047ba2d203;p=lilypond.git diff --git a/lily/protected-scm.cc b/lily/protected-scm.cc index 4447371a83..225c64dc60 100644 --- a/lily/protected-scm.cc +++ b/lily/protected-scm.cc @@ -1,7 +1,7 @@ /* This file is part of LilyPond, the GNU music typesetter. - Copyright (C) 1998--2011 Han-Wen Nienhuys + Copyright (C) 1998--2015 Han-Wen Nienhuys LilyPond is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -19,54 +19,63 @@ #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_); -} +// For static objects, this will be called at program exit. With the +// state of the memory system unknown, we refrain from any cleanup +// actions outside of the object memory itself. Protected_scm::~Protected_scm () { - if (SCM_NIMP (object_)) - scm_gc_unprotect_object (object_); + object_ = SCM_UNDEFINED; } +SCM Protected_scm::list_ = SCM_EOL; +SCM Protected_scm::last_ = SCM_EOL; + Protected_scm & Protected_scm::operator = (SCM s) { - if (object_ == s) - return *this; - - if (SCM_NIMP (object_)) - scm_gc_unprotect_object (object_); + if (SCM_CONSP (object_)) + SCM_SETCAR (object_, s); + else if (SCM_IMP (s)) + object_ = s; + else + { + s = scm_list_1 (s); + if (SCM_CONSP (last_)) + SCM_SETCDR (last_, s); + else + list_ = scm_permanent_object (s); + last_ = object_ = s; + } - 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 { - return object_; -} - -SCM -Protected_scm::to_SCM () const -{ - return object_; + return SCM_CONSP (object_) ? SCM_CAR (object_) : object_; }