From: hanwen <hanwen>
Date: Fri, 10 Jun 2005 00:36:22 +0000 (+0000)
Subject: (ly_make_anonymous_module): call make-module
X-Git-Tag: release/2.5.32^2~28
X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=2ae714627136db2b9b053991954621038eb812b5;p=lilypond.git

(ly_make_anonymous_module): call make-module
directly. This fixes a massive memory leak, provided you use CVS
GUILE.
---

diff --git a/ChangeLog b/ChangeLog
index c49ac73986..f6654afcb6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2005-06-10  Han-Wen Nienhuys  <hanwen@xs4all.nl>
+
+	* lily/ly-module.cc (ly_make_anonymous_module): call make-module
+	directly. This fixes a massive memory leak, provided you use CVS
+	GUILE.
+
 2005-06-10  Erik Sandberg  <ersa9195@student.uu.se>
 
 	* ly/property-init.ly: Added tieDashed.
diff --git a/lily/include/lily-lexer.hh b/lily/include/lily-lexer.hh
index 37e25fa101..cfc09af6bf 100644
--- a/lily/include/lily-lexer.hh
+++ b/lily/include/lily-lexer.hh
@@ -22,8 +22,6 @@ void set_lexer ();
 class Lily_lexer : public Includable_lexer
 {
   DECLARE_SMOBS (Lily_lexer,);
-public:
-  SCM scopes_;
 
 private:
   int lookup_keyword (String);
@@ -34,7 +32,8 @@ private:
   char escaped_char (char) const;
 
   Keyword_table *keytable_;
-
+  SCM scopes_;
+  SCM start_module_;
 public:
   String main_input_name_;
   void *lexval;
@@ -61,6 +60,7 @@ public:
   Input here_input () const;
 
   void add_scope (SCM);
+  void set_current_scope ();
   SCM remove_scope ();
 
   void start_main_input ();
diff --git a/lily/lily-lexer.cc b/lily/lily-lexer.cc
index 455bbfaea0..6194400ab0 100644
--- a/lily/lily-lexer.cc
+++ b/lily/lily-lexer.cc
@@ -93,7 +93,7 @@ Lily_lexer::Lily_lexer (Sources *sources)
   scopes_ = SCM_EOL;
   error_level_ = 0;
   is_main_input_ = false;
-
+  start_module_ = SCM_EOL;
   smobify_self ();
 
   add_scope (ly_make_anonymous_module (false));
@@ -108,7 +108,8 @@ Lily_lexer::Lily_lexer (Lily_lexer const &src)
   chordmodifier_tab_ = src.chordmodifier_tab_;
   pitchname_tab_stack_ = src.pitchname_tab_stack_;
   sources_ = src.sources_;
-
+  start_module_ = SCM_EOL;
+  
   error_level_ = src.error_level_;
   is_main_input_ = src.is_main_input_;
 
@@ -139,6 +140,9 @@ void
 Lily_lexer::add_scope (SCM module)
 {
   ly_reexport_module (scm_current_module ());
+  if (!scm_is_pair (scopes_))
+    start_module_ = scm_current_module ();
+  
   scm_set_current_module (module);
   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
     {
@@ -147,16 +151,25 @@ Lily_lexer::add_scope (SCM module)
   scopes_ = scm_cons (module, scopes_);
 }
 
+
 SCM
 Lily_lexer::remove_scope ()
 {
   SCM sc = scm_car (scopes_);
   scopes_ = scm_cdr (scopes_);
-  scm_set_current_module (scm_car (scopes_));
-
+  set_current_scope ();
   return sc;
 }
 
+void
+Lily_lexer::set_current_scope ()
+{
+  if (scm_is_pair (scopes_))
+    scm_set_current_module (scm_car (scopes_));
+  else
+    scm_set_current_module (start_module_);
+}
+
 int
 Lily_lexer::lookup_keyword (String s)
 {
diff --git a/lily/lily-parser.cc b/lily/lily-parser.cc
index 1c284b4138..76915d07e8 100644
--- a/lily/lily-parser.cc
+++ b/lily/lily-parser.cc
@@ -87,6 +87,8 @@ Lily_parser::parse_file (String init, String name, String out_name)
       try_load_text_metrics (out_name);
     }
 
+  SCM oldmod = scm_current_module ();
+
   lexer_ = new Lily_lexer (sources_);
   scm_gc_unprotect_object (lexer_->self_scm ());
   // TODO: use $parser 
@@ -120,6 +122,8 @@ Lily_parser::parse_file (String init, String name, String out_name)
 
   error_level_ = error_level_ | lexer_->error_level_;
   lexer_ = 0;
+
+  scm_set_current_module (oldmod);
 }
 
 void
@@ -131,12 +135,10 @@ Lily_parser::parse_string (String ly_code)
   scm_gc_unprotect_object (lexer_->self_scm ());
 
   SCM oldmod = scm_current_module ();
-  scm_set_current_module (scm_car (lexer_->scopes_));
-
   // TODO: use $parser 
   lexer_->set_identifier (ly_symbol2scm ("parser"),
 			  self_scm ());
-
+  
   lexer_->main_input_name_ = "<string>";
   lexer_->is_main_input_ = true;
 
diff --git a/lily/ly-module.cc b/lily/ly-module.cc
index 2cd0403e28..6a6e5d58c6 100644
--- a/lily/ly-module.cc
+++ b/lily/ly-module.cc
@@ -13,22 +13,17 @@
 
 #define FUNC_NAME __FUNCTION__
 
-static int module_count;
-
-void
-ly_init_anonymous_module (void *data)
-{
-  (void) data;
-}
-
 SCM
 ly_make_anonymous_module (bool safe)
 {
   SCM mod = SCM_EOL;
   if (!safe)
     {
-      String s = "*anonymous-ly-" + to_string (module_count++) + "*";
-      mod = scm_c_define_module (s.to_str0 (), ly_init_anonymous_module, 0);
+      SCM maker = ly_lily_module_constant ("make-module");
+      SCM scm_module = ly_lily_module_constant ("the-scm-module");
+      
+      mod = scm_call_0 (maker);
+      ly_use_module (mod, scm_module);
       ly_use_module (mod, global_lily_module);
     }
   else
diff --git a/lily/output-def.cc b/lily/output-def.cc
index eedf75c988..e08816a7b8 100644
--- a/lily/output-def.cc
+++ b/lily/output-def.cc
@@ -19,6 +19,10 @@
 
 #include "ly-smobs.icc"
 
+#include "program-option.hh"
+
+#include "string-convert.hh"
+
 Output_def::Output_def ()
 {
   scope_ = SCM_EOL;
diff --git a/scm/lily.scm b/scm/lily.scm
index 6fd1db2d5e..82aea7b532 100644
--- a/scm/lily.scm
+++ b/scm/lily.scm
@@ -268,7 +268,7 @@ The syntax is the same as `define*-public'."
 		    (lambda (a b)
 		      (< (object-address (car a))
 			 (object-address (car b))))))
-	 
+
 	 (out-file-name (string-append
 			 "gcstat-" (number->string gc-protect-stat-count)
 			 ".scm"))
@@ -288,7 +288,24 @@ The syntax is the same as `define*-public'."
 		 " ")
 		"\n")))
 	   protects))
-     outfile)))
+     outfile)
+
+    (if (defined? 'gc-live-object-stats)
+	(let*
+	    ((dummy (gc))
+	     (dummy2 (gc))
+	     (stats (gc-live-object-stats))
+	     )
+
+	  (for-each
+	   (lambda (x)
+	     (format outfile "~a: ~a\n" (car x) (cdr x)))
+	   (sort (gc-live-object-stats)
+		 (lambda (x y)
+		   (string<? (car x) (car y)))))
+	   ))
+
+    ))
 
 (define-public (tweak-grob-property grob sym val)
   (set! (ly:grob-property grob sym) val))