Call atexit() via hb_atexit()

Part of https://github.com/harfbuzz/harfbuzz/issues/3197
diff --git a/src/hb-common.cc b/src/hb-common.cc
index cbba1a7..754f37f 100644
--- a/src/hb-common.cc
+++ b/src/hb-common.cc
@@ -280,8 +280,7 @@
 
 static hb_atomic_ptr_t <hb_language_item_t> langs;
 
-#if HB_USE_ATEXIT
-static void
+static inline void
 free_langs ()
 {
 retry:
@@ -296,7 +295,6 @@
     first_lang = next;
   }
 }
-#endif
 
 static hb_language_item_t *
 lang_find_or_insert (const char *key)
@@ -327,10 +325,8 @@
     goto retry;
   }
 
-#if HB_USE_ATEXIT
   if (!first_lang)
-    atexit (free_langs); /* First person registers atexit() callback. */
-#endif
+    hb_atexit (free_langs); /* First person registers atexit() callback. */
 
   return lang;
 }
diff --git a/src/hb-ft.cc b/src/hb-ft.cc
index a6beb9f..97a2c82 100644
--- a/src/hb-ft.cc
+++ b/src/hb-ft.cc
@@ -561,9 +561,7 @@
   return true;
 }
 
-#if HB_USE_ATEXIT
-static void free_static_ft_funcs ();
-#endif
+static inline void free_static_ft_funcs ();
 
 static struct hb_ft_font_funcs_lazy_loader_t : hb_font_funcs_lazy_loader_t<hb_ft_font_funcs_lazy_loader_t>
 {
@@ -591,21 +589,17 @@
 
     hb_font_funcs_make_immutable (funcs);
 
-#if HB_USE_ATEXIT
-    atexit (free_static_ft_funcs);
-#endif
+    hb_atexit (free_static_ft_funcs);
 
     return funcs;
   }
 } static_ft_funcs;
 
-#if HB_USE_ATEXIT
-static
+static inline
 void free_static_ft_funcs ()
 {
   static_ft_funcs.free_instance ();
 }
-#endif
 
 static hb_font_funcs_t *
 _hb_ft_get_font_funcs ()
@@ -905,9 +899,7 @@
   return hb_ft_font_create (ft_face, _hb_ft_face_destroy);
 }
 
-#if HB_USE_ATEXIT
-static void free_static_ft_library ();
-#endif
+static inline void free_static_ft_library ();
 
 static struct hb_ft_library_lazy_loader_t : hb_lazy_loader_t<hb_remove_pointer<FT_Library>,
 							     hb_ft_library_lazy_loader_t>
@@ -918,9 +910,7 @@
     if (FT_Init_FreeType (&l))
       return nullptr;
 
-#if HB_USE_ATEXIT
-    atexit (free_static_ft_library);
-#endif
+    hb_atexit (free_static_ft_library);
 
     return l;
   }
@@ -934,13 +924,11 @@
   }
 } static_ft_library;
 
-#if HB_USE_ATEXIT
-static
+static inline
 void free_static_ft_library ()
 {
   static_ft_library.free_instance ();
 }
-#endif
 
 static FT_Library
 get_ft_library ()
diff --git a/src/hb-glib.cc b/src/hb-glib.cc
index f93bb88..8ddc7eb 100644
--- a/src/hb-glib.cc
+++ b/src/hb-glib.cc
@@ -218,9 +218,7 @@
 }
 
 
-#if HB_USE_ATEXIT
-static void free_static_glib_funcs ();
-#endif
+static inline void free_static_glib_funcs ();
 
 static struct hb_glib_unicode_funcs_lazy_loader_t : hb_unicode_funcs_lazy_loader_t<hb_glib_unicode_funcs_lazy_loader_t>
 {
@@ -237,21 +235,17 @@
 
     hb_unicode_funcs_make_immutable (funcs);
 
-#if HB_USE_ATEXIT
-    atexit (free_static_glib_funcs);
-#endif
+    hb_atexit (free_static_glib_funcs);
 
     return funcs;
   }
 } static_glib_funcs;
 
-#if HB_USE_ATEXIT
-static
+static inline
 void free_static_glib_funcs ()
 {
   static_glib_funcs.free_instance ();
 }
-#endif
 
 /**
  * hb_glib_get_unicode_funcs:
diff --git a/src/hb-icu.cc b/src/hb-icu.cc
index 008a39e..e46401f 100644
--- a/src/hb-icu.cc
+++ b/src/hb-icu.cc
@@ -233,9 +233,7 @@
 }
 
 
-#if HB_USE_ATEXIT
-static void free_static_icu_funcs ();
-#endif
+static inline void free_static_icu_funcs ();
 
 static struct hb_icu_unicode_funcs_lazy_loader_t : hb_unicode_funcs_lazy_loader_t<hb_icu_unicode_funcs_lazy_loader_t>
 {
@@ -257,21 +255,17 @@
 
     hb_unicode_funcs_make_immutable (funcs);
 
-#if HB_USE_ATEXIT
-    atexit (free_static_icu_funcs);
-#endif
+    hb_atexit (free_static_icu_funcs);
 
     return funcs;
   }
 } static_icu_funcs;
 
-#if HB_USE_ATEXIT
-static
+static inline
 void free_static_icu_funcs ()
 {
   static_icu_funcs.free_instance ();
 }
-#endif
 
 /**
  * hb_icu_get_unicode_funcs:
diff --git a/src/hb-ot-font.cc b/src/hb-ot-font.cc
index fae7b5b..5c044c1 100644
--- a/src/hb-ot-font.cc
+++ b/src/hb-ot-font.cc
@@ -253,9 +253,7 @@
 	 _hb_ot_metrics_get_position_common (font, HB_OT_METRICS_TAG_VERTICAL_LINE_GAP, &metrics->line_gap);
 }
 
-#if HB_USE_ATEXIT
-static void free_static_ot_funcs ();
-#endif
+static inline void free_static_ot_funcs ();
 
 static struct hb_ot_font_funcs_lazy_loader_t : hb_font_funcs_lazy_loader_t<hb_ot_font_funcs_lazy_loader_t>
 {
@@ -281,21 +279,17 @@
 
     hb_font_funcs_make_immutable (funcs);
 
-#if HB_USE_ATEXIT
-    atexit (free_static_ot_funcs);
-#endif
+    hb_atexit (free_static_ot_funcs);
 
     return funcs;
   }
 } static_ot_funcs;
 
-#if HB_USE_ATEXIT
-static
+static inline
 void free_static_ot_funcs ()
 {
   static_ot_funcs.free_instance ();
 }
-#endif
 
 static hb_font_funcs_t *
 _hb_ot_get_font_funcs ()
diff --git a/src/hb-shape.cc b/src/hb-shape.cc
index f303a56..c1f619c 100644
--- a/src/hb-shape.cc
+++ b/src/hb-shape.cc
@@ -48,9 +48,7 @@
  **/
 
 
-#if HB_USE_ATEXIT
-static void free_static_shaper_list ();
-#endif
+static inline void free_static_shaper_list ();
 
 static const char *nil_shaper_list[] = {nullptr};
 
@@ -69,9 +67,7 @@
       shaper_list[i] = shapers[i].name;
     shaper_list[i] = nullptr;
 
-#if HB_USE_ATEXIT
-    atexit (free_static_shaper_list);
-#endif
+    hb_atexit (free_static_shaper_list);
 
     return shaper_list;
   }
@@ -81,13 +77,11 @@
   { return nil_shaper_list; }
 } static_shaper_list;
 
-#if HB_USE_ATEXIT
-static
+static inline
 void free_static_shaper_list ()
 {
   static_shaper_list.free_instance ();
 }
-#endif
 
 
 /**
diff --git a/src/hb-shaper.cc b/src/hb-shaper.cc
index 265df10..a11ed83 100644
--- a/src/hb-shaper.cc
+++ b/src/hb-shaper.cc
@@ -38,9 +38,7 @@
 static_assert (0 != ARRAY_LENGTH_CONST (all_shapers), "No shaper enabled.");
 #endif
 
-#if HB_USE_ATEXIT
-static void free_static_shapers ();
-#endif
+static inline void free_static_shapers ();
 
 static struct hb_shapers_lazy_loader_t : hb_lazy_loader_t<const hb_shaper_entry_t,
 							  hb_shapers_lazy_loader_t>
@@ -83,9 +81,7 @@
 	p = end + 1;
     }
 
-#if HB_USE_ATEXIT
-    atexit (free_static_shapers);
-#endif
+    hb_atexit (free_static_shapers);
 
     return shapers;
   }
@@ -93,13 +89,11 @@
   static const hb_shaper_entry_t *get_null ()      { return all_shapers; }
 } static_shapers;
 
-#if HB_USE_ATEXIT
-static
+static inline
 void free_static_shapers ()
 {
   static_shapers.free_instance ();
 }
-#endif
 
 const hb_shaper_entry_t *
 _hb_shapers_get ()
diff --git a/src/hb-ucd.cc b/src/hb-ucd.cc
index ad72a26..baea224 100644
--- a/src/hb-ucd.cc
+++ b/src/hb-ucd.cc
@@ -203,9 +203,7 @@
 }
 
 
-#if HB_USE_ATEXIT
 static void free_static_ucd_funcs ();
-#endif
 
 static struct hb_ucd_unicode_funcs_lazy_loader_t : hb_unicode_funcs_lazy_loader_t<hb_ucd_unicode_funcs_lazy_loader_t>
 {
@@ -222,21 +220,17 @@
 
     hb_unicode_funcs_make_immutable (funcs);
 
-#if HB_USE_ATEXIT
-    atexit (free_static_ucd_funcs);
-#endif
+    hb_atexit (free_static_ucd_funcs);
 
     return funcs;
   }
 } static_ucd_funcs;
 
-#if HB_USE_ATEXIT
-static
+static inline
 void free_static_ucd_funcs ()
 {
   static_ucd_funcs.free_instance ();
 }
-#endif
 
 hb_unicode_funcs_t *
 hb_ucd_get_unicode_funcs ()
diff --git a/src/hb-uniscribe.cc b/src/hb-uniscribe.cc
index 2e14211..3dc4c09 100644
--- a/src/hb-uniscribe.cc
+++ b/src/hb-uniscribe.cc
@@ -239,9 +239,7 @@
   }
 };
 
-#if HB_USE_ATEXIT
-static void free_static_uniscribe_shaper_funcs ();
-#endif
+static inline void free_static_uniscribe_shaper_funcs ();
 
 static struct hb_uniscribe_shaper_funcs_lazy_loader_t : hb_lazy_loader_t<hb_uniscribe_shaper_funcs_t,
 									 hb_uniscribe_shaper_funcs_lazy_loader_t>
@@ -254,9 +252,7 @@
 
     funcs->init ();
 
-#if HB_USE_ATEXIT
-    atexit (free_static_uniscribe_shaper_funcs);
-#endif
+    hb_atexit (free_static_uniscribe_shaper_funcs);
 
     return funcs;
   }
@@ -270,13 +266,11 @@
   }
 } static_uniscribe_shaper_funcs;
 
-#if HB_USE_ATEXIT
-static
+static inline
 void free_static_uniscribe_shaper_funcs ()
 {
   static_uniscribe_shaper_funcs.free_instance ();
 }
-#endif
 
 static hb_uniscribe_shaper_funcs_t *
 hb_uniscribe_shaper_get_funcs ()
diff --git a/src/hb.hh b/src/hb.hh
index 052cabe..4ba46ea 100644
--- a/src/hb.hh
+++ b/src/hb.hh
@@ -405,6 +405,9 @@
 #  define errno _hb_errno
 #endif
 
+#define HB_STMT_START do
+#define HB_STMT_END   while (0)
+
 #if defined(HAVE_ATEXIT) && !defined(HB_USE_ATEXIT)
 /* atexit() is only safe to be called from shared libraries on certain
  * platforms.  Whitelist.
@@ -433,16 +436,22 @@
  */
 #    define HB_USE_ATEXIT 1
 #  endif
-#endif
+#endif /* defined(HAVE_ATEXIT) && !defined(HB_USE_ATEXIT) */
 #ifdef HB_NO_ATEXIT
 #  undef HB_USE_ATEXIT
 #endif
 #ifndef HB_USE_ATEXIT
 #  define HB_USE_ATEXIT 0
 #endif
-
-#define HB_STMT_START do
-#define HB_STMT_END   while (0)
+#if !HB_USE_ATEXIT
+#  define hb_atexit(_) HB_STMT_START { if (0) (_) (); } HB_STMT_END
+#else /* HB_USE_ATEXIT */
+#  ifdef HAVE_ATEXIT
+#    define hb_atexit atexit
+#  else
+#    error "atexit not found."
+#  endif
+#endif
 
 /* Lets assert int types.  Saves trouble down the road. */
 static_assert ((sizeof (hb_codepoint_t) == 4), "");