diff options
Diffstat (limited to 'src/framework/init/fw_module.c')
-rw-r--r-- | src/framework/init/fw_module.c | 342 |
1 files changed, 166 insertions, 176 deletions
diff --git a/src/framework/init/fw_module.c b/src/framework/init/fw_module.c index 1f22ecb..66c06ee 100644 --- a/src/framework/init/fw_module.c +++ b/src/framework/init/fw_module.c @@ -20,6 +20,7 @@ #include <stdlib.h> #include "nstack_securec.h" #include "fw_module.h" +#include "nsfw_init_api.h" #ifdef __cplusplus /* *INDENT-OFF* */ @@ -27,8 +28,8 @@ extern "C"{ /* *INDENT-ON* */ #endif /* __cplusplus */ -COMPAT_PROTECT (NSFW_MODULE_INSTANCE_POOL_SIZE, 64); -COMPAT_PROTECT (NSFW_MODULE_DEPENDS_POOL_SIZE, 128); +COMPAT_PROTECT_RETURN(NSFW_MODULE_INSTANCE_POOL_SIZE, 64); +COMPAT_PROTECT_RETURN(NSFW_MODULE_DEPENDS_POOL_SIZE, 128); nsfw_module_instance_pool_t g_nsfw_module_inst_pool; nsfw_module_depends_pool_t g_nsfw_module_deps_pool; @@ -37,291 +38,280 @@ nsfw_module_depends_pool_t g_nsfw_module_deps_pool; * WARNING!!!: * This function is only used in constructor progress. Multi-thread concurrent is not supported! */ -NSTACK_STATIC nsfw_module_instance_t * -nsfw_module_malloc_instance () +NSTACK_STATIC nsfw_module_instance_t *nsfw_module_malloc_instance() { - if (g_nsfw_module_inst_pool.last_idx >= NSFW_MODULE_INSTANCE_POOL_SIZE) + if (g_nsfw_module_inst_pool.last_idx >= NSFW_MODULE_INSTANCE_POOL_SIZE) { - return NULL; + return NULL; } - return - &g_nsfw_module_inst_pool.module_instance_pool[g_nsfw_module_inst_pool. - last_idx++]; + return + &g_nsfw_module_inst_pool.module_instance_pool[g_nsfw_module_inst_pool. + last_idx++]; } /* * WARNING!!!: * This function is only used in constructor progress. Multi-thread concurrent is not supported! */ -NSTACK_STATIC nsfw_module_depends_t * -nsfw_module_malloc_depends () +NSTACK_STATIC nsfw_module_depends_t *nsfw_module_malloc_depends() { - if (g_nsfw_module_deps_pool.last_idx >= NSFW_MODULE_DEPENDS_POOL_SIZE) + if (g_nsfw_module_deps_pool.last_idx >= NSFW_MODULE_DEPENDS_POOL_SIZE) { - return NULL; + return NULL; } - return - &g_nsfw_module_deps_pool.module_depends_pool[g_nsfw_module_deps_pool. - last_idx++]; + return + &g_nsfw_module_deps_pool.module_depends_pool[g_nsfw_module_deps_pool. + last_idx++]; } -NSTACK_STATIC void -nsfw_module_setChildInstance (nsfw_module_instance_t * - father, nsfw_module_instance_t * child) +/*Change module malloc to pool array End*/ + +NSTACK_STATIC void nsfw_module_set_child_instance(nsfw_module_instance_t * + father, + nsfw_module_instance_t * + child) { - if (NULL == father || NULL == child) - return; - child->father = father; + if (NULL == father || NULL == child) + return; + child->father = father; } -nsfw_module_depends_t * -nsfw_module_create_depends (char *name) +nsfw_module_depends_t *nsfw_module_create_depends(char *name) { - if (NULL == name) - return NULL; - - /*Change module malloc to pool array */ - nsfw_module_depends_t *dep = nsfw_module_malloc_depends (); - - if (NULL == dep) - return NULL; - - if (EOK != - MEMSET_S (dep, sizeof (nsfw_module_depends_t), 0, - sizeof (nsfw_module_depends_t))) + if (NULL == name) + return NULL; + /*Change module malloc to pool array */ + nsfw_module_depends_t *dep = nsfw_module_malloc_depends(); + if (NULL == dep) + return NULL; + + if (EOK != + memset_s(dep, sizeof(nsfw_module_depends_t), 0, + sizeof(nsfw_module_depends_t))) { - goto fail; + goto fail; } - /*change destMax from nameSize - 1 to sizeof(dep->name) */ - if (EOK != STRCPY_S (dep->name, sizeof (dep->name), name)) + /*change destMax from nameSize - 1 to sizeof(dep->name) */ + if (EOK != strcpy_s(dep->name, sizeof(dep->name), name)) { - goto fail; + goto fail; } - dep->isReady = 0; + dep->isReady = 0; - return dep; + return dep; -fail: - // NOTE: if dep is not null, we do not free it and just leave it there. - return NULL; + fail: + // NOTE: if dep is not null, we do not free it and just leave it there. + return NULL; } -nsfw_module_instance_t * -nsfw_module_create_instance (void) +nsfw_module_instance_t *nsfw_module_create_instance(void) { - /*Change module malloc to pool array */ - nsfw_module_instance_t *inst = nsfw_module_malloc_instance (); - if (NULL == inst) - return NULL; - - if (EOK != - MEMSET_S (inst, sizeof (nsfw_module_instance_t), 0, - sizeof (nsfw_module_instance_t))) + /*Change module malloc to pool array */ + nsfw_module_instance_t *inst = nsfw_module_malloc_instance(); + if (NULL == inst) + return NULL; + + if (EOK != + memset_s(inst, sizeof(nsfw_module_instance_t), 0, + sizeof(nsfw_module_instance_t))) { - // NOTE: if inst is not null, we do not free it and just leave it there. - return NULL; + // NOTE: if inst is not null, we do not free it and just leave it there. + return NULL; } - inst->stat = NSFW_INST_STAT_CHECKING; - return inst; + inst->stat = NSFW_INST_STAT_CHECKING; + return inst; } -void -nsfw_module_set_instance_name (nsfw_module_instance_t * inst, char *name) +void nsfw_module_set_instance_name(nsfw_module_instance_t * inst, char *name) { - if (NULL == inst || NULL == name || inst->name[0] != '\0') + if (NULL == inst || NULL == name || inst->name[0] != '\0') { - return; + return; } - /*change destMax from nameSize - 1 to sizeof(inst->name) */ - if (EOK != STRCPY_S (inst->name, sizeof (inst->name), name)) + /*change destMax from nameSize - 1 to sizeof(inst->name) */ + if (EOK != strcpy_s(inst->name, sizeof(inst->name), name)) { - return; + return; } - // Now we need to search if it's any instance's father - nsfw_module_instance_t *curInst = nsfw_module_getManager ()->inst; - while (curInst) + // Now we need to search if it's any instance's father + nsfw_module_instance_t *curInst = nsfw_module_get_manager()->inst; + while (curInst) { - if (curInst == inst) - goto next_loop; + if (curInst == inst) + goto next_loop; - if (0 == strcmp (curInst->fatherName, inst->name) - && NULL == curInst->father) + if (0 == strcmp(curInst->fatherName, inst->name) + && NULL == curInst->father) { - nsfw_module_setChildInstance (inst, curInst); + nsfw_module_set_child_instance(inst, curInst); } - next_loop: - curInst = curInst->next; + next_loop: /*usually use like this and no "space" */ + curInst = curInst->next; } - return; + return; } -void -nsfw_module_set_instance_father (nsfw_module_instance_t * inst, - char *fatherName) +void nsfw_module_set_instance_father(nsfw_module_instance_t * inst, + char *fatherName) { - if (NULL == inst || NULL == fatherName) + if (NULL == inst || NULL == fatherName) { - return; + return; } - if (EOK != - STRCPY_S (inst->fatherName, sizeof (inst->fatherName), fatherName)) + if (EOK != + strcpy_s(inst->fatherName, sizeof(inst->fatherName), fatherName)) { - return; + return; } - nsfw_module_instance_t *fatherInst = - nsfw_module_getModuleByName (fatherName); - if (fatherInst) + nsfw_module_instance_t *fatherInst = + nsfw_module_get_module_by_name(fatherName); + if (fatherInst) { - nsfw_module_setChildInstance (fatherInst, inst); + nsfw_module_set_child_instance(fatherInst, inst); } - return; + return; } -void -nsfw_module_set_instance_priority (nsfw_module_instance_t * inst, - int priority) +void nsfw_module_set_instance_priority(nsfw_module_instance_t * inst, + int priority) { - if (NULL == inst) - return; - inst->priority = priority; + if (NULL == inst) + return; + inst->priority = priority; - nsfw_module_del_instance (inst); - nsfw_module_add_instance (inst); - return; + nsfw_module_del_instance(inst); + nsfw_module_add_instance(inst); + return; } -void -nsfw_module_set_instance_initfn (nsfw_module_instance_t * inst, - nsfw_module_init_fn fn) +void nsfw_module_set_instance_initfn(nsfw_module_instance_t * inst, + nsfw_module_init_fn fn) { - if (NULL == inst) + if (NULL == inst) + return; + inst->fnInit = fn; return; - inst->fnInit = fn; - return; } -void -nsfw_module_set_instance_depends (nsfw_module_instance_t * inst, char *name) +void nsfw_module_set_instance_depends(nsfw_module_instance_t * inst, + char *name) { - if (NULL == inst || name == NULL) - return; + if (NULL == inst || name == NULL) + return; - // Check if depends already set - nsfw_module_depends_t *dep = inst->depends; - while (dep) + // Check if depends already set + nsfw_module_depends_t *dep = inst->depends; + while (dep) { - if (0 == strcmp (dep->name, name)) - return; - dep = dep->next; + if (0 == strcmp(dep->name, name)) + return; + dep = dep->next; } - dep = nsfw_module_create_depends (name); - if (NULL == dep) - return; + dep = nsfw_module_create_depends(name); + if (NULL == dep) + return; - if (NULL == inst->depends) - inst->depends = dep; - else - inst->depends->next = dep; + if (NULL == inst->depends) + inst->depends = dep; + else + inst->depends->next = dep; } -/* *INDENT-OFF* */ -nsfw_module_manager_t g_nsfw_module_manager = {.inst = NULL, .initMutex = - PTHREAD_MUTEX_INITIALIZER, .done = 0}; -/* *INDENT-ON* */ +nsfw_module_manager_t g_nsfw_module_manager = {.inst = NULL,.initMutex = + PTHREAD_MUTEX_INITIALIZER,.done = 0 +}; -void -nsfw_module_add_instance (nsfw_module_instance_t * inst) +void nsfw_module_add_instance(nsfw_module_instance_t * inst) { - nsfw_module_instance_t *curInst = nsfw_module_getManager ()->inst; + nsfw_module_instance_t *curInst = nsfw_module_get_manager()->inst; - if (NULL == curInst || curInst->priority > inst->priority) + if (NULL == curInst || curInst->priority > inst->priority) { - inst->next = curInst; - nsfw_module_getManager ()->inst = inst; + inst->next = curInst; + nsfw_module_get_manager()->inst = inst; } - else + else { - while (curInst->next && curInst->next->priority <= inst->priority) + while (curInst->next && curInst->next->priority <= inst->priority) { - curInst = curInst->next; + curInst = curInst->next; } - inst->next = curInst->next; - curInst->next = inst; + inst->next = curInst->next; + curInst->next = inst; } } -void -nsfw_module_del_instance (nsfw_module_instance_t * inst) +void nsfw_module_del_instance(nsfw_module_instance_t * inst) { - if (nsfw_module_getManager ()->inst == inst) + if (nsfw_module_get_manager()->inst == inst) { - nsfw_module_getManager ()->inst = inst->next; - return; + nsfw_module_get_manager()->inst = inst->next; + return; } - nsfw_module_instance_t *curInst = nsfw_module_getManager ()->inst; + nsfw_module_instance_t *curInst = nsfw_module_get_manager()->inst; - while (curInst->next && curInst->next != inst) - curInst = curInst->next; + while (curInst->next && curInst->next != inst) + curInst = curInst->next; - if (!curInst->next) - return; - curInst->next = inst->next; + if (!curInst->next) + return; + curInst->next = inst->next; } -nsfw_module_instance_t * -nsfw_module_getModuleByName (char *name) +nsfw_module_instance_t *nsfw_module_get_module_by_name(char *name) { - if (NULL == name) - return NULL; + if (NULL == name) + return NULL; - nsfw_module_instance_t *curInst = nsfw_module_getManager ()->inst; - while (curInst) + nsfw_module_instance_t *curInst = nsfw_module_get_manager()->inst; + while (curInst) { - if (0 == strcmp (curInst->name, name)) + if (0 == strcmp(curInst->name, name)) { - return curInst; + return curInst; } - curInst = curInst->next; + curInst = curInst->next; } - return NULL; + return NULL; } -int -nsfw_module_addDoneNode (nsfw_module_instance_t * inst) +int nsfw_module_add_done_node(nsfw_module_instance_t * inst) { - nsfw_module_doneNode_t *node = - (nsfw_module_doneNode_t *) malloc (sizeof (nsfw_module_doneNode_t)); - if (NULL == node) - return -1; - node->inst = inst; - node->next = NULL; - - nsfw_module_manager_t *manager = nsfw_module_getManager (); - if (NULL == manager->doneHead) + nsfw_module_done_node_t *node = (nsfw_module_done_node_t *) malloc(sizeof(nsfw_module_done_node_t)); /*malloc can use */ + if (NULL == node) + { + return -1; + } + node->inst = inst; + node->next = NULL; + + nsfw_module_manager_t *manager = nsfw_module_get_manager(); + if (NULL == manager->doneHead) { - manager->doneHead = node; + manager->doneHead = node; } - else + else { - nsfw_module_doneNode_t *tail = manager->doneHead; - while (tail->next) + nsfw_module_done_node_t *tail = manager->doneHead; + while (tail->next) { - tail = tail->next; + tail = tail->next; } - tail->next = node; + tail->next = node; } - return 0; + return 0; } #ifdef __cplusplus |