[nginx] Dynamic modules: moved module-related stuff to separate ...

Maxim Dounin mdounin at mdounin.ru
Thu Feb 4 18:40:30 UTC 2016


details:   http://hg.nginx.org/nginx/rev/0f203a2af17c
branches:  
changeset: 6378:0f203a2af17c
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Thu Feb 04 18:30:21 2016 +0300
description:
Dynamic modules: moved module-related stuff to separate files.

diffstat:

 auto/sources             |   2 +
 src/core/nginx.c         |   7 +---
 src/core/ngx_conf_file.h |  51 ---------------------------------
 src/core/ngx_core.h      |   1 +
 src/core/ngx_cycle.c     |  10 +----
 src/core/ngx_module.c    |  65 ++++++++++++++++++++++++++++++++++++++++++
 src/core/ngx_module.h    |  74 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/event/ngx_event.c    |   9 +-----
 src/http/ngx_http.c      |   9 +-----
 src/mail/ngx_mail.c      |   9 +-----
 src/stream/ngx_stream.c  |   9 +-----
 11 files changed, 151 insertions(+), 95 deletions(-)

diffs (truncated from 365 to 300 lines):

diff --git a/auto/sources b/auto/sources
--- a/auto/sources
+++ b/auto/sources
@@ -36,6 +36,7 @@ CORE_DEPS="src/core/nginx.h \
            src/core/ngx_connection.h \
            src/core/ngx_cycle.h \
            src/core/ngx_conf_file.h \
+           src/core/ngx_module.h \
            src/core/ngx_resolver.h \
            src/core/ngx_open_file_cache.h \
            src/core/ngx_crypt.h \
@@ -71,6 +72,7 @@ CORE_SRCS="src/core/nginx.c \
            src/core/ngx_rwlock.c \
            src/core/ngx_cpuinfo.c \
            src/core/ngx_conf_file.c \
+           src/core/ngx_module.c \
            src/core/ngx_resolver.c \
            src/core/ngx_open_file_cache.c \
            src/core/ngx_crypt.c \
diff --git a/src/core/nginx.c b/src/core/nginx.c
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -160,8 +160,6 @@ ngx_module_t  ngx_core_module = {
 };
 
 
-ngx_uint_t          ngx_max_module;
-
 static ngx_uint_t   ngx_show_help;
 static ngx_uint_t   ngx_show_version;
 static ngx_uint_t   ngx_show_configure;
@@ -260,9 +258,8 @@ main(int argc, char *const *argv)
         return 1;
     }
 
-    ngx_max_module = 0;
-    for (i = 0; ngx_modules[i]; i++) {
-        ngx_modules[i]->index = ngx_max_module++;
+    if (ngx_preinit_modules() != NGX_OK) {
+        return 1;
     }
 
     cycle = ngx_init_cycle(&init_cycle);
diff --git a/src/core/ngx_conf_file.h b/src/core/ngx_conf_file.h
--- a/src/core/ngx_conf_file.h
+++ b/src/core/ngx_conf_file.h
@@ -96,53 +96,6 @@ struct ngx_open_file_s {
 };
 
 
-#define NGX_MODULE_V1          0, 0, 0, 0, 0, 0, 1
-#define NGX_MODULE_V1_PADDING  0, 0, 0, 0, 0, 0, 0, 0
-
-struct ngx_module_s {
-    ngx_uint_t            ctx_index;
-    ngx_uint_t            index;
-
-    ngx_uint_t            spare0;
-    ngx_uint_t            spare1;
-    ngx_uint_t            spare2;
-    ngx_uint_t            spare3;
-
-    ngx_uint_t            version;
-
-    void                 *ctx;
-    ngx_command_t        *commands;
-    ngx_uint_t            type;
-
-    ngx_int_t           (*init_master)(ngx_log_t *log);
-
-    ngx_int_t           (*init_module)(ngx_cycle_t *cycle);
-
-    ngx_int_t           (*init_process)(ngx_cycle_t *cycle);
-    ngx_int_t           (*init_thread)(ngx_cycle_t *cycle);
-    void                (*exit_thread)(ngx_cycle_t *cycle);
-    void                (*exit_process)(ngx_cycle_t *cycle);
-
-    void                (*exit_master)(ngx_cycle_t *cycle);
-
-    uintptr_t             spare_hook0;
-    uintptr_t             spare_hook1;
-    uintptr_t             spare_hook2;
-    uintptr_t             spare_hook3;
-    uintptr_t             spare_hook4;
-    uintptr_t             spare_hook5;
-    uintptr_t             spare_hook6;
-    uintptr_t             spare_hook7;
-};
-
-
-typedef struct {
-    ngx_str_t             name;
-    void               *(*create_conf)(ngx_cycle_t *cycle);
-    char               *(*init_conf)(ngx_cycle_t *cycle, void *conf);
-} ngx_core_module_t;
-
-
 typedef struct {
     ngx_file_t            file;
     ngx_buf_t            *buffer;
@@ -340,8 +293,4 @@ char *ngx_conf_set_enum_slot(ngx_conf_t 
 char *ngx_conf_set_bitmask_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 
 
-extern ngx_uint_t     ngx_max_module;
-extern ngx_module_t  *ngx_modules[];
-
-
 #endif /* _NGX_CONF_FILE_H_INCLUDED_ */
diff --git a/src/core/ngx_core.h b/src/core/ngx_core.h
--- a/src/core/ngx_core.h
+++ b/src/core/ngx_core.h
@@ -83,6 +83,7 @@ typedef void (*ngx_connection_handler_pt
 #endif
 #include <ngx_process_cycle.h>
 #include <ngx_conf_file.h>
+#include <ngx_module.h>
 #include <ngx_open_file_cache.h>
 #include <ngx_os.h>
 #include <ngx_connection.h>
diff --git a/src/core/ngx_cycle.c b/src/core/ngx_cycle.c
--- a/src/core/ngx_cycle.c
+++ b/src/core/ngx_cycle.c
@@ -612,13 +612,9 @@ ngx_init_cycle(ngx_cycle_t *old_cycle)
 
     pool->log = cycle->log;
 
-    for (i = 0; ngx_modules[i]; i++) {
-        if (ngx_modules[i]->init_module) {
-            if (ngx_modules[i]->init_module(cycle) != NGX_OK) {
-                /* fatal */
-                exit(1);
-            }
-        }
+    if (ngx_init_modules(cycle) != NGX_OK) {
+        /* fatal */
+        exit(1);
     }
 
 
diff --git a/src/core/ngx_module.c b/src/core/ngx_module.c
new file mode 100644
--- /dev/null
+++ b/src/core/ngx_module.c
@@ -0,0 +1,65 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ * Copyright (C) Maxim Dounin
+ * Copyright (C) Nginx, Inc.
+ */
+
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+
+
+ngx_uint_t  ngx_max_module;
+
+
+ngx_int_t
+ngx_preinit_modules()
+{
+    ngx_uint_t  i;
+
+    ngx_max_module = 0;
+    for (i = 0; ngx_modules[i]; i++) {
+        ngx_modules[i]->index = ngx_max_module++;
+    }
+
+    return NGX_OK;
+}
+
+
+ngx_int_t
+ngx_init_modules(ngx_cycle_t *cycle)
+{
+    ngx_uint_t  i;
+
+    for (i = 0; ngx_modules[i]; i++) {
+        if (ngx_modules[i]->init_module) {
+            if (ngx_modules[i]->init_module(cycle) != NGX_OK) {
+                return NGX_ERROR;
+            }
+        }
+    }
+
+    return NGX_OK;
+}
+
+
+ngx_int_t
+ngx_count_modules(ngx_cycle_t *cycle, ngx_uint_t type)
+{
+    ngx_uint_t  i, max;
+
+    max = 0;
+
+    /* count appropriate modules, set up their indices */
+
+    for (i = 0; ngx_modules[i]; i++) {
+        if (ngx_modules[i]->type != type) {
+            continue;
+        }
+
+        ngx_modules[i]->ctx_index = max++;
+    }
+
+    return max;
+}
diff --git a/src/core/ngx_module.h b/src/core/ngx_module.h
new file mode 100644
--- /dev/null
+++ b/src/core/ngx_module.h
@@ -0,0 +1,74 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ * Copyright (C) Maxim Dounin
+ * Copyright (C) Nginx, Inc.
+ */
+
+
+#ifndef _NGX_MODULE_H_INCLUDED_
+#define _NGX_MODULE_H_INCLUDED_
+
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+
+
+#define NGX_MODULE_V1          0, 0, 0, 0, 0, 0, 1
+#define NGX_MODULE_V1_PADDING  0, 0, 0, 0, 0, 0, 0, 0
+
+
+struct ngx_module_s {
+    ngx_uint_t            ctx_index;
+    ngx_uint_t            index;
+
+    ngx_uint_t            spare0;
+    ngx_uint_t            spare1;
+    ngx_uint_t            spare2;
+    ngx_uint_t            spare3;
+
+    ngx_uint_t            version;
+
+    void                 *ctx;
+    ngx_command_t        *commands;
+    ngx_uint_t            type;
+
+    ngx_int_t           (*init_master)(ngx_log_t *log);
+
+    ngx_int_t           (*init_module)(ngx_cycle_t *cycle);
+
+    ngx_int_t           (*init_process)(ngx_cycle_t *cycle);
+    ngx_int_t           (*init_thread)(ngx_cycle_t *cycle);
+    void                (*exit_thread)(ngx_cycle_t *cycle);
+    void                (*exit_process)(ngx_cycle_t *cycle);
+
+    void                (*exit_master)(ngx_cycle_t *cycle);
+
+    uintptr_t             spare_hook0;
+    uintptr_t             spare_hook1;
+    uintptr_t             spare_hook2;
+    uintptr_t             spare_hook3;
+    uintptr_t             spare_hook4;
+    uintptr_t             spare_hook5;
+    uintptr_t             spare_hook6;
+    uintptr_t             spare_hook7;
+};
+
+
+typedef struct {
+    ngx_str_t             name;
+    void               *(*create_conf)(ngx_cycle_t *cycle);
+    char               *(*init_conf)(ngx_cycle_t *cycle, void *conf);
+} ngx_core_module_t;
+
+
+ngx_int_t ngx_preinit_modules();
+ngx_int_t ngx_init_modules(ngx_cycle_t *cycle);
+ngx_int_t ngx_count_modules(ngx_cycle_t *cycle, ngx_uint_t type);
+
+
+extern ngx_module_t  *ngx_modules[];
+extern ngx_uint_t     ngx_max_module;
+
+
+#endif /* _NGX_MODULE_H_INCLUDED_ */
diff --git a/src/event/ngx_event.c b/src/event/ngx_event.c
--- a/src/event/ngx_event.c
+++ b/src/event/ngx_event.c
@@ -891,14 +891,7 @@ ngx_events_block(ngx_conf_t *cf, ngx_com
 
     /* count the number of the event modules and set up their indices */
 
-    ngx_event_max_module = 0;
-    for (i = 0; ngx_modules[i]; i++) {
-        if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
-            continue;



More information about the nginx-devel mailing list