]> git.donarmstrong.com Git - lilypond.git/blob - lily/smobs.cc
(protect_smob): experiment: O(1) GC (un)protection.
[lilypond.git] / lily / smobs.cc
1 /*
2   smobs.cc -- implement Smob protection
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 */
9
10 #include "smobs.hh"
11
12 static SCM smob_protection_list;
13
14 void
15 init_smob_protection()
16 {
17   smob_protection_list = scm_cons (SCM_UNDEFINED, SCM_EOL);
18   scm_permanent_object (smob_protection_list);
19 }
20 ADD_SCM_INIT_FUNC(init_smob_protection, init_smob_protection);
21
22 void
23 protect_smob (SCM smob, SCM *prot_cons)
24 {
25   SCM s = scm_cdr (smob_protection_list);
26   while (scm_is_pair (s) && scm_car (s) == SCM_UNDEFINED)
27     {
28       s = scm_cdr (s);
29     }
30
31   SCM prot = scm_cons (smob, s);
32   scm_set_cdr_x (smob_protection_list,
33                  prot);
34   *prot_cons = prot;
35 }
36
37 void
38 unprotect_smob (SCM *prot_cons)
39 {
40   SCM next = scm_cdr (*prot_cons);
41
42   if (next == SCM_EOL)
43     {
44       scm_set_car_x (*prot_cons, SCM_UNDEFINED);
45     }
46   else
47     {
48       scm_set_car_x (*prot_cons, SCM_UNDEFINED);
49       while (scm_is_pair (next)
50              && scm_car (next) == SCM_UNDEFINED)
51
52         {
53           next = scm_cdr (next);
54         }
55
56       scm_set_cdr_x (*prot_cons, next);
57     }
58
59   *prot_cons = SCM_EOL;
60 }