Optional API: Fix handling of sources that are both provider and user.
authorCorey Farrell <git@cfware.com>
Mon, 13 Apr 2015 10:57:02 +0000 (06:57 -0400)
committerCorey Farrell <git@cfware.com>
Mon, 13 Apr 2015 11:06:28 +0000 (07:06 -0400)
OPTIONAL_API has conditionals to define AST_OPTIONAL_API and
AST_OPTIONAL_API_ATTR differently based on if AST_API_MODULE is defined.
Unfortunately this is inside the include protection block, so only the
first status of AST_API_MODULE is respected.  For example res_monitor
is an optional API provider, but uses func_periodic_hook.  This makes
func_periodic_hook non-optional to res_monitor.

This changes optional_api.h so that AST_OPTIONAL_API and
AST_OPTIONAL_API_ATTR is redefined every time the header is included.

ASTERISK-17608 #close
Reported by: Warren Selby

Change-Id: I8fcf2a5e7b481893e17484ecde4f172c9ffb5679

include/asterisk/optional_api.h

index 394aed0..1118dc7 100644 (file)
@@ -178,10 +178,7 @@ void ast_optional_api_unuse(const char *symname, ast_optional_fn *optional_ref,
 
 #define AST_OPTIONAL_API_NAME(name) __##name
 
-#if defined(AST_API_MODULE)
-/* Module defining the API */
-
-#define AST_OPTIONAL_API_IMPL_INIT(name)                               \
+#define AST_OPTIONAL_API_INIT_IMPL(name)                               \
        static void __attribute__((constructor)) __init__##name##_impl(void) { \
                ast_optional_api_provide(#name,                         \
                        (ast_optional_fn)AST_OPTIONAL_API_NAME(name));  \
@@ -191,22 +188,7 @@ void ast_optional_api_unuse(const char *symname, ast_optional_fn *optional_ref,
                        (ast_optional_fn)AST_OPTIONAL_API_NAME(name));  \
        }
 
-#define AST_OPTIONAL_API(result, name, proto, stub)                    \
-       result AST_OPTIONAL_API_NAME(name) proto;                       \
-       static attribute_unused typeof(AST_OPTIONAL_API_NAME(name)) * const \
-            name = AST_OPTIONAL_API_NAME(name);                        \
-       AST_OPTIONAL_API_IMPL_INIT(name)
-
-#define AST_OPTIONAL_API_ATTR(result, attr, name, proto, stub)         \
-       result  __attribute__((attr)) AST_OPTIONAL_API_NAME(name) proto; \
-       static attribute_unused typeof(AST_OPTIONAL_API_NAME(name)) * const \
-            name = AST_OPTIONAL_API_NAME(name);                        \
-       AST_OPTIONAL_API_IMPL_INIT(name)
-
-#else
-/* Module using the API */
-
-#define AST_OPTIONAL_API_INIT(name)                                    \
+#define AST_OPTIONAL_API_INIT_USER(name)                               \
        static void __attribute__((constructor)) __init__##name(void) { \
                ast_optional_api_use(#name, (ast_optional_fn *)&name,   \
                        (ast_optional_fn)__stub__##name,                \
@@ -217,19 +199,31 @@ void ast_optional_api_unuse(const char *symname, ast_optional_fn *optional_ref,
                        AST_MODULE);                                    \
        }
 
-#define AST_OPTIONAL_API(result, name, proto, stub)                    \
+#define AST_OPTIONAL_API_IMPL(result, name, proto, stub)                       \
+       result AST_OPTIONAL_API_NAME(name) proto;                       \
+       static attribute_unused typeof(AST_OPTIONAL_API_NAME(name)) * const \
+            name = AST_OPTIONAL_API_NAME(name);                        \
+       AST_OPTIONAL_API_INIT_IMPL(name)
+
+#define AST_OPTIONAL_API_USER(result, name, proto, stub)                       \
        static result __stub__##name proto stub;                        \
        static attribute_unused                                         \
                typeof(__stub__##name) * name;                          \
-       AST_OPTIONAL_API_INIT(name)
+       AST_OPTIONAL_API_INIT_USER(name)
+
 
-#define AST_OPTIONAL_API_ATTR(result, attr, name, proto, stub)         \
+/* AST_OPTIONAL_API_ATTR */
+#define AST_OPTIONAL_API_ATTR_IMPL(result, attr, name, proto, stub)            \
+       result  __attribute__((attr)) AST_OPTIONAL_API_NAME(name) proto; \
+       static attribute_unused typeof(AST_OPTIONAL_API_NAME(name)) * const \
+            name = AST_OPTIONAL_API_NAME(name);                        \
+       AST_OPTIONAL_API_INIT_IMPL(name)
+
+#define AST_OPTIONAL_API_ATTR_USER(result, attr, name, proto, stub)            \
        static __attribute__((attr)) result __stub__##name proto stub;  \
        static attribute_unused __attribute__((attr))                   \
                typeof(__stub__##name) * name;                          \
-       AST_OPTIONAL_API_INIT(name)
-
-#endif /* defined(AST_API_MODULE) */
+       AST_OPTIONAL_API_INIT_USER(name)
 
 #else /* defined(OPTIONAL_API) */
 
@@ -245,6 +239,38 @@ void ast_optional_api_unuse(const char *symname, ast_optional_fn *optional_ref,
 
 #endif /* defined(OPTIONAL_API) */
 
-#undef AST_API_MODULE
-
 #endif /* __ASTERISK_OPTIONAL_API_H */
+
+/*
+ * Some Asterisk sources are both consumer and provider of optional API's.  The
+ * following definitons are intentionally outside the include protected portion
+ * of this header so AST_OPTIONAL_API and AST_OPTIONAL_API_ATTR can be redefined
+ * each time the header is included.  This also ensures that AST_API_MODULE is
+ * undefined after every include of this header.
+ */
+#if defined(OPTIONAL_API)
+
+#undef AST_OPTIONAL_API
+#undef AST_OPTIONAL_API_ATTR
+
+#if defined(AST_API_MODULE)
+
+#define AST_OPTIONAL_API(result, name, proto, stub) \
+       AST_OPTIONAL_API_IMPL(result, name, proto, stub)
+
+#define AST_OPTIONAL_API_ATTR(result, attr, name, proto, stub) \
+       AST_OPTIONAL_API_ATTR_IMPL(result, attr, name, proto, stub)
+
+#else
+
+#define AST_OPTIONAL_API(result, name, proto, stub) \
+       AST_OPTIONAL_API_USER(result, name, proto, stub)
+
+#define AST_OPTIONAL_API_ATTR(result, attr, name, proto, stub) \
+       AST_OPTIONAL_API_ATTR_USER(result, attr, name, proto, stub)
+
+#endif /* defined(AST_API_MODULE) */
+
+#endif /* defined(OPTIONAL_API) */
+
+#undef AST_API_MODULE