From 965937dc482db1417ee3e00db6fe911216ab4b4d Mon Sep 17 00:00:00 2001
From: Ivan Maidanski <ivmai@mail.ru>
Date: Sat, 5 Dec 2015 19:28:20 +0300
Subject: [PATCH 2/4] Move 'extern C' from boehm.cc to boehm-gc header files;
 avoid explicit include gc_local_alloc.h from boehm.cc; remove
 GC_enable/disable duplicate prototyping (code refactoring to match BDWGC v7
 API)

boehm-gc/
    * include/gc.h (GC_suspend_thread, GC_resume_thread,
    GC_is_thread_suspended): Move inside 'extern "C"' block.
    [GC_REDIRECT_TO_LOCAL]: Include gc_local_alloc.h outside 'extern "C"'
    block; do not include gc_local_alloc.h if gc.h is included from
    gc_gcj.h (so that GC_[LOCAL_]GCJ_MALLOC to be defined after other GCJ
    symbols).
    * include/gc_gcj.h [__cplusplus]: Wrap all declarations into
    'extern "C"' block.
    * include/gc_local_alloc.h [__cplusplus]: Likewise.
    * include/javaxfc.h [__cplusplus]: Likewise.
    * include/gc_gcj.h [GC_REDIRECT_TO_LOCAL]: Include gc_local_alloc.h.

libjava/
    * boehm.cc (GC_enable, GC_disable): Remove prototype (as declared in
    boehm-gc/include/gc.h).
    [THREAD_LOCAL_ALLOC] (GC_REDIRECT_TO_LOCAL): Define before include
    gc_gcj.h file.
    * boehm.cc: Remove 'extern "C"' for included boehm-gc/include header
    files (they are properly decorated inside now); remove include
    "gc_local_alloc.h" (as it is implicitly included now by
    boehm-gc/include/gc_gcj.h); include "gc_gcj.h" before any other
    boehm-gc header file (to let GC_GCJ_MALLOC be properly redefined to
    a thread-local allocator in boehm-gc/include/gc_local_alloc.h).
---
 boehm-gc/ChangeLog                | 14 ++++++++++++++
 boehm-gc/include/gc.h             | 22 ++++++++++++++--------
 boehm-gc/include/gc_gcj.h         | 12 ++++++++++++
 boehm-gc/include/gc_local_alloc.h |  8 ++++++++
 boehm-gc/include/gc_mark.h        |  9 ++++++++-
 boehm-gc/include/javaxfc.h        |  8 +++++++-
 libjava/ChangeLog                 | 13 +++++++++++++
 libjava/boehm.cc                  | 14 +++-----------
 8 files changed, 79 insertions(+), 21 deletions(-)

diff --git a/boehm-gc/ChangeLog b/boehm-gc/ChangeLog
index 1d86061..9988605 100644
--- a/boehm-gc/ChangeLog
+++ b/boehm-gc/ChangeLog
@@ -1,3 +1,17 @@
+2015-12-05  Ivan Maidanski  <ivmai@mail.ru>
+
+	* include/gc.h (GC_suspend_thread, GC_resume_thread,
+	GC_is_thread_suspended): Move inside 'extern "C"' block.
+	[GC_REDIRECT_TO_LOCAL]: Include gc_local_alloc.h outside 'extern "C"'
+	block; do not include gc_local_alloc.h if gc.h is included from
+	gc_gcj.h (so that GC_[LOCAL_]GCJ_MALLOC to be defined after other GCJ
+	symbols).
+	* include/gc_gcj.h [__cplusplus]: Wrap all declarations into
+	'extern "C"' block.
+	* include/gc_local_alloc.h [__cplusplus]: Likewise.
+	* include/javaxfc.h [__cplusplus]: Likewise.
+	* include/gc_gcj.h [GC_REDIRECT_TO_LOCAL]: Include gc_local_alloc.h.
+
 2015-12-03  Ivan Maidanski  <ivmai@mail.ru>
 
 	* include/gc.h (GC_set_free_space_divisor): Change return type from
diff --git a/boehm-gc/include/gc.h b/boehm-gc/include/gc.h
index d02dab2..f193065 100644
--- a/boehm-gc/include/gc.h
+++ b/boehm-gc/include/gc.h
@@ -1061,14 +1061,6 @@ GC_API void GC_register_has_static_roots_callback
 # include "gc_amiga_redirects.h"
 #endif
 
-#if defined(GC_REDIRECT_TO_LOCAL) && !defined(GC_LOCAL_ALLOC_H)
-#  include  "gc_local_alloc.h"
-#endif
-
-#ifdef __cplusplus
-    }  /* end of extern "C" */
-#endif
-
 /* External thread suspension support. These functions do not implement
  * suspension counts or any other higher-level abstraction. Threads which
  * have been suspended numerous times will resume with the very first call
@@ -1080,4 +1072,18 @@ GC_API void GC_suspend_thread GC_PROTO((pthread_t));
 GC_API void GC_resume_thread GC_PROTO((pthread_t));
 GC_API int GC_is_thread_suspended GC_PROTO((pthread_t));
 #endif
+
+#ifdef __cplusplus
+    } /* end of extern "C" */
+#endif
+
+#if defined(GC_REDIRECT_TO_LOCAL) && !defined(GC_LOCAL_ALLOC_H) \
+    && !defined(GC_GCJ_H)
+  /* In case of gc.h is included from gc_gcj.h file, gc_local_alloc.h	*/
+  /* should be included at the end of gc_gcj.h instead of gc.h to have	*/
+  /* all GCJ symbols defined before some of them are redefined to	*/
+  /* their thread-local counterparts.					*/
+# include "gc_local_alloc.h"
+#endif
+
 #endif /* _GC_H */
diff --git a/boehm-gc/include/gc_gcj.h b/boehm-gc/include/gc_gcj.h
index 5e79e27..b771734 100644
--- a/boehm-gc/include/gc_gcj.h
+++ b/boehm-gc/include/gc_gcj.h
@@ -44,6 +44,10 @@
 #   include "gc.h"
 #endif
 
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
 /* The following allocators signal an out of memory condition with	*/
 /* return GC_oom_fn(bytes);						*/
 
@@ -110,4 +114,12 @@ extern int GC_gcj_debug_kind;
 	GC_gcj_malloc_ignore_off_page(s,d)
 # endif
 
+#ifdef __cplusplus
+  } /* end of extern "C" */
+#endif
+
+#if defined(GC_REDIRECT_TO_LOCAL)
+# include "gc_local_alloc.h"
+#endif
+
 #endif /* GC_GCJ_H */
diff --git a/boehm-gc/include/gc_local_alloc.h b/boehm-gc/include/gc_local_alloc.h
index 1874c7b..e4fe205 100644
--- a/boehm-gc/include/gc_local_alloc.h
+++ b/boehm-gc/include/gc_local_alloc.h
@@ -49,6 +49,10 @@
 #   include "gc_gcj.h"
 #endif
 
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
 /* We assume ANSI C for this interface.	*/
 
 GC_PTR GC_local_malloc(size_t bytes);
@@ -86,4 +90,8 @@ GC_PTR GC_local_malloc_atomic(size_t bytes);
 #   endif
 # endif
 
+#ifdef __cplusplus
+  } /* end of extern "C" */
+#endif
+
 #endif /* GC_LOCAL_ALLOC_H */
diff --git a/boehm-gc/include/gc_mark.h b/boehm-gc/include/gc_mark.h
index 953bb74..7c92946 100644
--- a/boehm-gc/include/gc_mark.h
+++ b/boehm-gc/include/gc_mark.h
@@ -29,6 +29,10 @@
 #   include "gc.h"
 # endif
 
+# ifdef __cplusplus
+    extern "C" {
+# endif
+
 /* A client supplied mark procedure.  Returns new mark stack pointer.	*/
 /* Primary effect should be to push new entries on the mark stack.	*/
 /* Mark stack pointer values are passed and returned explicitly.	*/
@@ -199,5 +203,8 @@ void GC_register_describe_type_fn GC_PROTO((int kind, GC_describe_type_fn knd));
 				/* to be used when printing objects	*/
 				/* of a particular kind.		*/
 
-#endif  /* GC_MARK_H */
+# ifdef __cplusplus
+    } /* end of extern "C" */
+# endif
 
+#endif  /* GC_MARK_H */
diff --git a/boehm-gc/include/javaxfc.h b/boehm-gc/include/javaxfc.h
index 23e0100..669ba64 100644
--- a/boehm-gc/include/javaxfc.h
+++ b/boehm-gc/include/javaxfc.h
@@ -2,6 +2,10 @@
 #   include "gc.h"
 # endif
 
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
 /*
  * Invoke all remaining finalizers that haven't yet been run.
  * This is needed for strict compliance with the Java standard, 
@@ -18,4 +22,6 @@
  */
 void GC_finalize_all();
 
-
+#ifdef __cplusplus
+  } /* end of extern "C" */
+#endif
diff --git a/libjava/ChangeLog b/libjava/ChangeLog
index 954b1a1..d581c8f 100644
--- a/libjava/ChangeLog
+++ b/libjava/ChangeLog
@@ -1,3 +1,16 @@
+2015-12-05  Ivan Maidanski  <ivmai@mail.ru>
+
+	* boehm.cc (GC_enable, GC_disable): Remove prototype (as declared in
+	boehm-gc/include/gc.h).
+	[THREAD_LOCAL_ALLOC] (GC_REDIRECT_TO_LOCAL): Define before include
+	gc_gcj.h file.
+	* boehm.cc: Remove 'extern "C"' for included boehm-gc/include header
+	files (they are properly decorated inside now); remove include
+	"gc_local_alloc.h" (as it is implicitly included now by
+	boehm-gc/include/gc_gcj.h); include "gc_gcj.h" before any other
+	boehm-gc header file (to let GC_GCJ_MALLOC be properly redefined to
+	a thread-local allocator in boehm-gc/include/gc_local_alloc.h).
+
 2015-12-03  Ivan Maidanski  <ivmai@mail.ru>
 
 	* boehm.cc (_Jv_SetGCFreeSpaceDivisor): Use GC_get_free_space_divisor
diff --git a/libjava/boehm.cc b/libjava/boehm.cc
index 34a6ead..39563f0 100644
--- a/libjava/boehm.cc
+++ b/libjava/boehm.cc
@@ -38,8 +38,6 @@ details.  */
 #include <dlfcn.h>
 #endif
 
-extern "C"
-{
 #include <gc_config.h>
 
 // Set GC_DEBUG before including gc.h!
@@ -47,19 +45,13 @@ extern "C"
 # define GC_DEBUG
 #endif
 
-#include <gc_mark.h>
-#include <gc_gcj.h>
-#include <javaxfc.h>  // GC_finalize_all declaration.  
-
 #ifdef THREAD_LOCAL_ALLOC
 # define GC_REDIRECT_TO_LOCAL
-# include <gc_local_alloc.h>
 #endif
+#include <gc_gcj.h> // includes gc.h and gc_local_alloc.h
 
-  // From boehm's misc.c 
-  void GC_enable();
-  void GC_disable();
-};
+#include <gc_mark.h>
+#include <javaxfc.h>  // GC_finalize_all declaration.
 
 #define MAYBE_MARK(Obj, Top, Limit, Source)  \
 	Top=GC_MARK_AND_PUSH((GC_PTR) Obj, Top, Limit, (GC_PTR *) Source)
-- 
2.1.4

