X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fprotected-scm.cc;h=225c64dc60fc220bf03910f25575a539a3ecbdfd;hb=b579529b5f3f89a5b8a17760bb199b0eacc671be;hp=cf06c84a5d94ba56683dde3ce246c798c520fc64;hpb=8f58f4428d70961938e9151097886d861b3faeb3;p=lilypond.git diff --git a/lily/protected-scm.cc b/lily/protected-scm.cc index cf06c84a5d..225c64dc60 100644 --- a/lily/protected-scm.cc +++ b/lily/protected-scm.cc @@ -1,73 +1,81 @@ -/* - protected-scm.cc -- implement Protected_scm - - source file of the GNU LilyPond music typesetter - - (c) 1998--1999 Han-Wen Nienhuys - - */ +/* + This file is part of LilyPond, the GNU music typesetter. + + 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 + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + LilyPond is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LilyPond. If not, see . +*/ + #include "protected-scm.hh" -#include "lily-guile.hh" -#include "main.hh" -#ifdef LYPROT -#define PROTECT ly_protect_scm -#define UNPROTECT ly_unprotect_scm -#else -#define PROTECT scm_protect_object -#define UNPROTECT scm_unprotect_object -#endif +// 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_ = 0; } Protected_scm::Protected_scm (SCM s) + : object_ (s) { - object_ = s ? PROTECT (s): 0; + // 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_ = s.object_ ? PROTECT (s.object_) : 0; -} - -Protected_scm & -Protected_scm::operator =(SCM s) -{ - if (object_ == s) - return *this; - if (object_) - UNPROTECT(object_); - - object_ = s ? PROTECT (s): 0; - return *this; -} +// 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::operator = (Protected_scm const &s) +Protected_scm::~Protected_scm () { - return operator= (s.object_); + object_ = SCM_UNDEFINED; } +SCM Protected_scm::list_ = SCM_EOL; +SCM Protected_scm::last_ = SCM_EOL; -Protected_scm::~Protected_scm () +Protected_scm & +Protected_scm::operator = (SCM s) { - if (object_) + if (SCM_CONSP (object_)) + SCM_SETCAR (object_, s); + else if (SCM_IMP (s)) + object_ = s; + else { - UNPROTECT (object_); - object_ =0L; // be nice to conservative GC + s = scm_list_1 (s); + if (SCM_CONSP (last_)) + SCM_SETCDR (last_, s); + else + list_ = scm_permanent_object (s); + last_ = object_ = s; } + + return *this; } -Protected_scm::operator SCM () const +Protected_scm & +Protected_scm::operator = (Protected_scm const &s) { - return object_; + return *this = (SCM) s; } -SCM -Protected_scm::to_SCM () const +Protected_scm::operator SCM () const { - return object_; + return SCM_CONSP (object_) ? SCM_CAR (object_) : object_; }