chicken-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/4] Make -heap-size work


From: megane
Subject: [PATCH 2/4] Make -heap-size work
Date: Mon, 15 Mar 2021 16:55:44 +0200
User-agent: mu4e 1.0; emacs 27.1

Hello,

Here's a patch for #1726.

Compiling this requires some thought: Compile the first one with your
current snapshot and the rest with the chicken built in previous step.

Also this will probably need a new snapshot, but I'm not familiar with
the process.

Feel free to golf this patch set into fewer steps. :)

>From ebf61282318f92bbbae2e4180fcf0b7e3e5da422 Mon Sep 17 00:00:00 2001
From: megane <meganeka@gmail.com>
Date: Sun, 14 Mar 2021 08:08:21 +0200
Subject: [PATCH 1/4] * chicken.h: Add temporary C_main_entry_point2

---
 c-backend.scm | 2 +-
 chicken.h     | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/c-backend.scm b/c-backend.scm
index 22d9d677..71d40edd 100644
--- a/c-backend.scm
+++ b/c-backend.scm
@@ -853,7 +853,7 @@
                 [else
                  (gen "static C_TLS int toplevel_initialized=0;")
                  (unless unit-name
-                   (gen #t "C_main_entry_point") )
+                   (gen #t "C_main_entry_point2") )
                  (gen #t #t "void C_ccall C_" topname) ] )
           (gen #\()
           (unless customizable (gen "C_word c,"))
diff --git a/chicken.h b/chicken.h
index b35e1a07..7226e7d2 100644
--- a/chicken.h
+++ b/chicken.h
@@ -1595,6 +1595,8 @@ typedef void (C_ccall *C_proc)(C_word, C_word *) C_noret;
 # define C_main_entry_point
 #endif
 
+#define C_main_entry_point2 C_main_entry_point
+
 #define C_alloc_flonum                  C_word *___tmpflonum = 
C_alloc(WORDS_PER_FLONUM)
 #define C_kontinue_flonum(k, n)         C_kontinue((k), 
C_flonum(&___tmpflonum, (n)))
 
-- 
2.17.1

>From 765e1dc41e29ab7791e21ad937f3eb0f365a4d8e Mon Sep 17 00:00:00 2001
From: megane <meganeka@gmail.com>
Date: Sun, 14 Mar 2021 09:14:31 +0200
Subject: [PATCH 2/4] Make -heap-size work

Pass the given fixed heap size down the C_main_entry_point ->
CHICKEN_main -> CHICKEN_parse_command_line chain.

Fixes #1726

* chicken.h: Remove temporary C_main_entry_point2
---
 NEWS          |  1 +
 c-backend.scm |  8 +-------
 chicken.h     | 14 +++++++-------
 runtime.c     | 11 +++++++++--
 4 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/NEWS b/NEWS
index 18c225c5..56465191 100644
--- a/NEWS
+++ b/NEWS
@@ -45,6 +45,7 @@
 - Compiler
   - Avoid re-using argvector when inline rest operations are being
     used in CPS calls (#1703, thanks to Jakob L. Keuze).
+  - Fix -heap-size. (see #1726)
 
 - Build system
   - Auto-configure at build time on most platforms. Cross-compilation
diff --git a/c-backend.scm b/c-backend.scm
index 71d40edd..7bc60aaf 100644
--- a/c-backend.scm
+++ b/c-backend.scm
@@ -853,7 +853,7 @@
                 [else
                  (gen "static C_TLS int toplevel_initialized=0;")
                  (unless unit-name
-                   (gen #t "C_main_entry_point2") )
+                   (gen #t "C_main_entry_point (" (or target-heap-size 0) ")"))
                  (gen #t #t "void C_ccall C_" topname) ] )
           (gen #\()
           (unless customizable (gen "C_word c,"))
@@ -891,12 +891,6 @@
                      (gen #t "C_register_debug_info(C_debug_info);"))
                    (when disable-stack-overflow-checking
                      (gen #t "C_disable_overflow_check=1;") )
-                   (unless unit-name
-                     (when target-heap-size
-                       (gen #t "C_set_or_change_heap_size(" target-heap-size 
",1);"
-                            #t "C_heap_size_is_fixed=1;"))
-                     (when target-stack-size
-                       (gen #t "C_resize_stack(" target-stack-size ");") ) )
                    (gen #t "C_check_nursery_minimum(C_calculate_demand(" 
demand ",c," max-av "));"
                         #t "if(C_unlikely(!C_demand(C_calculate_demand(" 
demand ",c," max-av ")))){"
                         #t "C_save_and_reclaim((void*)C_" topname ",c,av);}"
diff --git a/chicken.h b/chicken.h
index 7226e7d2..c778fb11 100644
--- a/chicken.h
+++ b/chicken.h
@@ -1573,29 +1573,29 @@ typedef void (C_ccall *C_proc)(C_word, C_word *) 
C_noret;
 
 #if !defined(C_EMBEDDED) && !defined(C_SHARED)
 # if defined(C_GUI) && defined(_WIN32)
-#  define C_main_entry_point            \
+#  define C_main_entry_point (fixed_heap_size) \
   int WINAPI WinMain(HINSTANCE me, HINSTANCE you, LPSTR cmdline, int show) \
   { \
     C_gui_mode = 1; \
     C_set_main_exe(argv[0]);                           \
     C_private_repository();                            \
-    return CHICKEN_main(0, NULL, (void *)C_toplevel); \
+    return CHICKEN_main(0, NULL, (void *)C_toplevel, fixed_heap_size);      \
   }
 # else
-#  define C_main_entry_point            \
+#  define C_main_entry_point(fixed_heap_size)     \
   int main(int argc, char *argv[]) \
   { \
     C_set_gui_mode; \
     C_set_main_exe(argv[0]);                           \
     C_private_repository();                            \
-    return CHICKEN_main(argc, argv, (void*)C_toplevel); \
+    return CHICKEN_main(argc, argv, (void*)C_toplevel, fixed_heap_size); \
   }
 # endif
 #else
-# define C_main_entry_point
+# define C_main_entry_point(fixed_heap_size)
 #endif
 
-#define C_main_entry_point2 C_main_entry_point
+#define C_main_entry_point2 C_main_entry_point(0)
 
 #define C_alloc_flonum                  C_word *___tmpflonum = 
C_alloc(WORDS_PER_FLONUM)
 #define C_kontinue_flonum(k, n)         C_kontinue((k), 
C_flonum(&___tmpflonum, (n)))
@@ -1726,7 +1726,7 @@ C_varextern C_TLS C_word 
(*C_get_unbound_variable_value_hook)(C_word sym);
 C_BEGIN_C_DECLS
 
 C_fctexport void C_register_debug_info(C_DEBUG_INFO *);
-C_fctexport int CHICKEN_main(int argc, char *argv[], void *toplevel);
+C_fctexport int CHICKEN_main(int argc, char *argv[], void *toplevel, C_word 
fixed_heap_size);
 C_fctexport int CHICKEN_initialize(int heap, int stack, int symbols, void 
*toplevel);
 C_fctexport C_word CHICKEN_run(void *toplevel);
 C_fctexport C_word CHICKEN_continue(C_word k);
diff --git a/runtime.c b/runtime.c
index 51cfa7be..519e353b 100644
--- a/runtime.c
+++ b/runtime.c
@@ -617,7 +617,7 @@ C_dbg(C_char *prefix, C_char *fstr, ...)
 
 /* Startup code: */
 
-int CHICKEN_main(int argc, char *argv[], void *toplevel) 
+int CHICKEN_main(int argc, char *argv[], void *toplevel, C_word 
fixed_heap_size) 
 {
   C_word h, s, n;
 
@@ -632,6 +632,7 @@ int CHICKEN_main(int argc, char *argv[], void *toplevel)
   }
 
   pass_serious_signals = 0;
+  h = fixed_heap_size;
   CHICKEN_parse_command_line(argc, argv, &h, &s, &n);
   
   if(!CHICKEN_initialize(h, s, n, toplevel))
@@ -1354,10 +1355,16 @@ void CHICKEN_parse_command_line(int argc, char *argv[], 
C_word *heap, C_word *st
   C_main_argc = argc;
   C_main_argv = argv;
 
-  *heap = DEFAULT_HEAP_SIZE;
   *stack = DEFAULT_STACK_SIZE;
   *symbols = DEFAULT_SYMBOL_TABLE_SIZE;
 
+  if (!*heap) {
+    *heap = DEFAULT_HEAP_SIZE;
+  } else {
+    C_heap_size_is_fixed=1;
+    heap_size_changed = 1;
+  }
+
   for(i = 1; i < C_main_argc; ++i)
     if(!strncmp(C_main_argv[ i ], C_text("-:"), 2)) {
       for(ptr = &C_main_argv[ i ][ 2 ]; *ptr != '\0';) {
-- 
2.17.1

>From 03cfb060d86f941905594ec148a65924c3a49967 Mon Sep 17 00:00:00 2001
From: megane <meganeka@gmail.com>
Date: Mon, 15 Mar 2021 14:09:49 +0200
Subject: [PATCH 3/4] * chicken.h: Remove temporary C_main_entry_point2

---
 chicken.h | 2 --
 1 file changed, 2 deletions(-)

diff --git a/chicken.h b/chicken.h
index c778fb11..3889b72c 100644
--- a/chicken.h
+++ b/chicken.h
@@ -1595,8 +1595,6 @@ typedef void (C_ccall *C_proc)(C_word, C_word *) C_noret;
 # define C_main_entry_point(fixed_heap_size)
 #endif
 
-#define C_main_entry_point2 C_main_entry_point(0)
-
 #define C_alloc_flonum                  C_word *___tmpflonum = 
C_alloc(WORDS_PER_FLONUM)
 #define C_kontinue_flonum(k, n)         C_kontinue((k), 
C_flonum(&___tmpflonum, (n)))
 
-- 
2.17.1

>From 19674dc78a572b47d927b31779e77b4582850ae6 Mon Sep 17 00:00:00 2001
From: megane <meganeka@gmail.com>
Date: Sun, 14 Mar 2021 13:57:07 +0200
Subject: [PATCH 4/4] Replace C_set_or_change_heap_size with C_initialize_heap

C_set_or_change_heap_size cannot be used for resizing the heap.
See #1726 for details.
---
 chicken.h |  1 -
 runtime.c | 16 +++++++---------
 2 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/chicken.h b/chicken.h
index 3889b72c..78b488dd 100644
--- a/chicken.h
+++ b/chicken.h
@@ -1749,7 +1749,6 @@ C_fctexport C_word C_fcall C_a_i_provide(C_word **a, int 
c, C_word id) C_regparm
 C_fctexport C_word C_fcall C_i_providedp(C_word id) C_regparm;
 C_fctexport C_word C_fcall C_enable_interrupts(void) C_regparm;
 C_fctexport C_word C_fcall C_disable_interrupts(void) C_regparm;
-C_fctexport void C_set_or_change_heap_size(C_word heap, int reintern);
 C_fctexport void C_do_resize_stack(C_word stack);
 C_fctexport C_word C_resize_pending_finalizers(C_word size);
 C_fctexport void C_initialize_lf(C_word *lf, int count);
diff --git a/runtime.c b/runtime.c
index 519e353b..2d0a1667 100644
--- a/runtime.c
+++ b/runtime.c
@@ -509,6 +509,7 @@ static C_TLS int random_state_index = 0;
 
 static void parse_argv(C_char *cmds);
 static void initialize_symbol_table(void);
+static void C_initialize_heap(C_word heap);
 static void global_signal_handler(int signum);
 static C_word arg_val(C_char *arg);
 static void barf(int code, char *loc, ...) C_noret;
@@ -735,7 +736,8 @@ int CHICKEN_initialize(int heap, int stack, int symbols, 
void *toplevel)
 
   page_size = 0;
   stack_size = stack ? stack : DEFAULT_STACK_SIZE;
-  C_set_or_change_heap_size(heap ? heap : DEFAULT_HEAP_SIZE, 0);
+
+  C_initialize_heap(heap ? heap : DEFAULT_HEAP_SIZE);
 
   /* Allocate temporary stack: */
   temporary_stack_size = fixed_temporary_stack_size ? 
fixed_temporary_stack_size : DEFAULT_TEMPORARY_STACK_SIZE;
@@ -1257,10 +1259,7 @@ heap_realloc (C_byte *ptr, size_t old_size,
   return p;
 }
 
-
-/* Modify heap size at runtime: */
-
-void C_set_or_change_heap_size(C_word heap, int reintern)
+void C_initialize_heap(C_word heap)
 {
   C_byte *ptr1, *ptr2, *ptr1a, *ptr2a;
   C_word size = heap / 2;
@@ -1270,7 +1269,8 @@ void C_set_or_change_heap_size(C_word heap, int reintern)
   if(fromspace_start && heap_size >= heap) return;
 
   if(debug_mode)
-    C_dbg(C_text("debug"), C_text("heap resized to " UWORD_COUNT_FORMAT_STRING 
" bytes\n"), heap);
+    C_dbg(C_text("debug"), C_text("allocating " UWORD_COUNT_FORMAT_STRING
+                                  " bytes for initial heap\n"), heap);
 
   heap_size = heap;
 
@@ -1293,10 +1293,8 @@ void C_set_or_change_heap_size(C_word heap, int reintern)
   tospace_top = tospace_start;
   tospace_limit = tospace_start + size;
   mutation_stack_top = mutation_stack_bottom;
-
-  if(reintern) initialize_symbol_table();
 }
- 
+
 
 /* Modify stack-size at runtime: */
 
-- 
2.17.1


reply via email to

[Prev in Thread] Current Thread [Next in Thread]