about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2023-01-08 23:43:21 +0000
committerEric Wong <e@80x24.org>2023-01-09 05:07:27 +0000
commitc5b0ca668258f0e386bcb0b12cf3628899d6bc12 (patch)
treeb497cacf963f414e82618fbc44896b08fb8849d8
parentafa6a2ea602fb26a16c6f0b9408339086f2790bd (diff)
downloadmwrap-c5b0ca668258f0e386bcb0b12cf3628899d6bc12.tar.gz
By including them at the bottom.  This will be done for Perl
headers in the future, too, since they break assert().
-rw-r--r--ext/mwrap/httpd.h8
-rw-r--r--ext/mwrap/mwrap_core.h104
2 files changed, 53 insertions, 59 deletions
diff --git a/ext/mwrap/httpd.h b/ext/mwrap/httpd.h
index da7ff6d..03aef9f 100644
--- a/ext/mwrap/httpd.h
+++ b/ext/mwrap/httpd.h
@@ -43,14 +43,6 @@
 #define TYPE_CSV "text/csv"
 #define TYPE_PLAIN "text/plain"
 
-/*
- * C ruby defines snprintf to ruby_snprintf, we can't have that in
- * non-ruby processes spawned by C ruby
- */
-#if MWRAP_RUBY && defined(snprintf)
-#        undef snprintf
-#endif
-
 enum mw_qev {
         MW_QEV_IGNORE = 0,
         MW_QEV_RD = POLLIN,
diff --git a/ext/mwrap/mwrap_core.h b/ext/mwrap/mwrap_core.h
index 721e5d3..48669d5 100644
--- a/ext/mwrap/mwrap_core.h
+++ b/ext/mwrap/mwrap_core.h
@@ -46,13 +46,6 @@
 #        include "ppport.h"
 #endif
 
-#if MWRAP_RUBY
-#        undef _GNU_SOURCE /* ruby.h redefines it */
-#        include <ruby.h> /* defines HAVE_RUBY_RACTOR_H on 3.0+ */
-#        include <ruby/thread.h>
-#        include <ruby/io.h>
-#endif
-
 /*
  * XXH3 (truncated to 32-bits) seems to provide a ~2% speedup.
  * XXH32 doesn't show improvements over jhash despite rculfhash
@@ -90,44 +83,11 @@ static size_t *root_locating; /* determines if PL_curcop is our thread */
 #endif /* MWRAP_PERL */
 
 #if MWRAP_RUBY
-const char *rb_source_location_cstr(int *line); /* requires 2.6.0dev or later */
-
-#        ifdef HAVE_RUBY_RACTOR_H /* Ruby 3.0+ */
-extern MWRAP_TSD void * __attribute__((weak)) ruby_current_ec;
-#        else /* Ruby 2.6-2.7 */
-extern void * __attribute__((weak)) ruby_current_execution_context_ptr;
-#                define ruby_current_ec ruby_current_execution_context_ptr
-#        endif /* HAVE_RUBY_RACTOR_H */
-
-extern void * __attribute__((weak)) ruby_current_vm_ptr; /* for rb_gc_count */
-extern size_t __attribute__((weak)) rb_gc_count(void);
-int __attribute__((weak)) ruby_thread_has_gvl_p(void);
-
-/*
- * rb_source_location_cstr relies on GET_EC(), and it's possible
- * to have a native thread but no EC during the early and late
- * (teardown) phases of the Ruby process
- */
-static int has_ec_p(void)
-{
-        return ruby_thread_has_gvl_p && ruby_thread_has_gvl_p() &&
-                ruby_current_vm_ptr && ruby_current_ec;
-}
-
-static void set_generation(size_t *gen, size_t size)
-{
-        if (rb_gc_count) {
-                uatomic_add_return(&total_bytes_inc, size);
-                if (has_ec_p())
-                        *gen = rb_gc_count();
-        } else {
-                *gen = uatomic_add_return(&total_bytes_inc, size);
-        }
-}
-#        define SET_GENERATION(gen, size) set_generation(gen, size)
+static void mw_ruby_set_generation(size_t *, size_t);
+#        define SET_GENERATION(gen, size) mw_ruby_set_generation(gen, size)
 #endif /* MWRAP_RUBY */
 
-#ifndef SET_GENERATION
+#ifndef SET_GENERATION /* C-only builds w/o Perl|Ruby */
 #        define SET_GENERATION(gen, size) \
                 *gen = uatomic_add_return(&total_bytes_inc, size)
 #endif /* !SET_GENERATION */
@@ -438,14 +398,7 @@ static const char *mw_perl_src_file_cstr(unsigned *lineno)
 #endif /* MWRAP_PERL */
 
 #if MWRAP_RUBY
-static const char *mw_ruby_src_file_cstr(unsigned *lineno)
-{
-        if (!has_ec_p()) return NULL;
-        int line;
-        const char *fn = rb_source_location_cstr(&line);
-        *lineno = line < 0 ? UINT_MAX : (unsigned)line;
-        return fn;
-}
+static const char *mw_ruby_src_file_cstr(unsigned *lineno);
 #        define SRC_FILE_CSTR(lineno) mw_ruby_src_file_cstr(lineno)
 #endif /* MWRAP_RUBY */
 
@@ -1088,3 +1041,52 @@ __attribute__((constructor)) static void mwrap_ctor(void)
         }
         --locating;
 }
+
+#if MWRAP_RUBY
+#        undef _GNU_SOURCE /* ruby.h redefines it */
+#        include <ruby.h> /* defines HAVE_RUBY_RACTOR_H on 3.0+ */
+#        include <ruby/thread.h>
+#        include <ruby/io.h>
+#        ifdef HAVE_RUBY_RACTOR_H /* Ruby 3.0+ */
+extern MWRAP_TSD void * __attribute__((weak)) ruby_current_ec;
+#        else /* Ruby 2.6-2.7 */
+extern void * __attribute__((weak)) ruby_current_execution_context_ptr;
+#                define ruby_current_ec ruby_current_execution_context_ptr
+#        endif /* HAVE_RUBY_RACTOR_H */
+
+extern void * __attribute__((weak)) ruby_current_vm_ptr; /* for rb_gc_count */
+extern size_t __attribute__((weak)) rb_gc_count(void);
+int __attribute__((weak)) ruby_thread_has_gvl_p(void);
+
+const char *rb_source_location_cstr(int *line); /* requires 2.6.0dev or later */
+/*
+ * rb_source_location_cstr relies on GET_EC(), and it's possible
+ * to have a native thread but no EC during the early and late
+ * (teardown) phases of the Ruby process
+ */
+static int has_ec_p(void)
+{
+        return ruby_thread_has_gvl_p && ruby_thread_has_gvl_p() &&
+                ruby_current_vm_ptr && ruby_current_ec;
+}
+
+static void mw_ruby_set_generation(size_t *gen, size_t size)
+{
+        if (rb_gc_count) {
+                uatomic_add_return(&total_bytes_inc, size);
+                if (has_ec_p())
+                        *gen = rb_gc_count();
+        } else {
+                *gen = uatomic_add_return(&total_bytes_inc, size);
+        }
+}
+
+static const char *mw_ruby_src_file_cstr(unsigned *lineno)
+{
+        if (!has_ec_p()) return NULL;
+        int line;
+        const char *fn = rb_source_location_cstr(&line);
+        *lineno = line < 0 ? UINT_MAX : (unsigned)line;
+        return fn;
+}
+#endif /* !MWRAP_RUBY */