diff --git a/src/lua_plugin_manage.c b/src/lua_plugin_manage.c index 4fd6b8e..84170c6 100644 --- a/src/lua_plugin_manage.c +++ b/src/lua_plugin_manage.c @@ -79,7 +79,7 @@ struct lua_plugin *search_plugin_by_id(int plugin_id) { for (int i = 0; i < global_schema->model_count; ++i) { - struct lua_model *model = &global_schema->model[i]; + struct lua_model *model = global_schema->model[i]; struct lua_plugin *plugin = NULL; while ((plugin = utarray_next(model->plugin_array, plugin))) @@ -354,13 +354,13 @@ struct lua_plugin_manage_schema *lua_plugin_manage_init( /* 如果没有配置, 也应该算是创建成功 */ if (specific_count == 0) return new_schema; - new_schema->model = (struct lua_model *)calloc(specific_count, sizeof(struct lua_model)); + new_schema->model = (struct lua_model **)calloc(specific_count, sizeof(struct lua_model *)); if (__glibc_unlikely(!new_schema->model)) { lua_plugin_manage_exit(new_schema); return NULL; } - memset(new_schema->model, 0, specific_count * sizeof(struct lua_model)); + memset(new_schema->model, 0, specific_count * sizeof(struct lua_model *)); /* 依次初始化每个状态机 */ for (int thread_index = 0; thread_index < thread_count; ++thread_index) @@ -375,9 +375,16 @@ struct lua_plugin_manage_schema *lua_plugin_manage_init( for (int spec_index = 0; spec_index < specific_count; ++spec_index) { + /* BugFix: 由于追加model的函数, 拷贝动作可能导致model指针与注册至pluginmanage的指针不一致 */ + new_schema->model[spec_index] = (struct lua_model *)calloc(1, sizeof(struct lua_model)); + if (__glibc_unlikely(!new_schema->model[spec_index])) + { + lua_plugin_manage_exit(new_schema); + return NULL; + } /* 在状态机中加载specific中的配置 */ int load_ret = thread_state_load_specific(new_schema->thread_state[thread_index], - &new_schema->model[spec_index], + new_schema->model[spec_index], &specific[spec_index]); if (load_ret) { @@ -388,7 +395,7 @@ struct lua_plugin_manage_schema *lua_plugin_manage_init( /* 调用该模块中的load函数 */ int call_load_ret = thread_state_call_load(new_schema->thread_state[thread_index], - &new_schema->model[spec_index], + new_schema->model[spec_index], &specific[spec_index], st); if (call_load_ret) @@ -431,17 +438,23 @@ int lua_plugin_manage_load_one_specific( if (schema->model) { - schema->model = (struct lua_model *)realloc(schema->model, (schema->model_count + 1) * sizeof(struct lua_model)); + schema->model = (struct lua_model **)realloc(schema->model, (schema->model_count + 1) * sizeof(struct lua_model *)); schema->model_count += 1; } /* BugFix: 在某些情况下, 该内存未初始化会导致加载失败 */ - memset(&schema->model[schema->model_count - 1], 0, sizeof(struct lua_model)); + memset(&schema->model[schema->model_count - 1], 0, sizeof(struct lua_model *)); + schema->model[schema->model_count - 1] = (struct lua_model *)calloc(1, sizeof(struct lua_model)); + if (__glibc_unlikely(!schema->model[schema->model_count - 1])) + { + LOGERROR("calloc for new model failed\n"); + return -1; + } for (int thread_index = 0; thread_index < schema->state_count; ++thread_index) { /* 在状态机中加载specific中的配置 */ int load_ret = thread_state_load_specific(schema->thread_state[thread_index], - &schema->model[schema->model_count - 1], + schema->model[schema->model_count - 1], specific); if (load_ret) { @@ -451,7 +464,7 @@ int lua_plugin_manage_load_one_specific( /* 调用该模块中的load函数 */ int call_load_ret = thread_state_call_load(schema->thread_state[thread_index], - &schema->model[schema->model_count - 1], + schema->model[schema->model_count - 1], specific, schema->st); if (call_load_ret) @@ -496,7 +509,7 @@ void lua_plugin_manage_exit(struct lua_plugin_manage_schema *lua_plug_mgr) { /* 在状态机中对每一个模块调用对应的卸载函数 */ int call_unload_ret = thread_state_call_unload(lua_plug_mgr->thread_state[state_index], - &lua_plug_mgr->model[model_index]); + lua_plug_mgr->model[model_index]); if (call_unload_ret) { LOGERROR("call state unload function failed, ret is %d", call_unload_ret); @@ -510,7 +523,8 @@ void lua_plugin_manage_exit(struct lua_plugin_manage_schema *lua_plug_mgr) /* 释放所有加载的模块 */ for (int model_index = 0; model_index < lua_plug_mgr->model_count; ++model_index) { - utarray_free(lua_plug_mgr->model[model_index].plugin_array); + utarray_free(lua_plug_mgr->model[model_index]->plugin_array); + free(lua_plug_mgr->model[model_index]); } free(lua_plug_mgr->model); @@ -542,6 +556,33 @@ void lua_plugin_get_statistics(int plugin_id, int thread_id, int *new_success, i return; } +void debug_lua_plugin_manage_schema(struct lua_plugin_manage_schema *schema) +{ + printf("\n***** begin to debug one lua schema *****\n"); + printf("schema.st is %p\n", schema->st); + printf("schema state count is %d\n", schema->state_count); + for (int i = 0; i < schema->state_count; ++i) + { + printf("schema state[%d]pointer is %p\n", i, schema->thread_state[i]); + } + printf("schema model count is %d\n", schema->model_count); + for (int i = 0; i < schema->model_count; ++i) + { + printf("debug model[%d] %p\n", i, &schema->model[i]); + printf("array %p, load %d, unload %d, env %d, mark %04x, count %d\n", + schema->model[i]->plugin_array, schema->model[i]->load_ref, schema->model[i]->unload_ref, + schema->model[i]->private_env_ref, schema->model[i]->model_mark, schema->model[i]->plugin_count); + struct lua_plugin *plugin = NULL; + while ((plugin = utarray_next(schema->model[i]->plugin_array, plugin))) + { + printf("%d, %d, %d\n", plugin->plugin_id, plugin->ctx_new_ref, plugin->ctx_free_ref); + } + } + printf("schema total plugin count is %d\n", schema->plugin_count); + + printf("***** end of debug one lua schema *****\n\n"); +} + #ifdef LUAPLUGIN_BASIC_UNITTEST void debug_lua_state_stack(lua_State *state, int mod, const char *message) { @@ -586,31 +627,4 @@ void debug_lua_state_stack(lua_State *state, int mod, const char *message) } printf("***** end of debug one lua stack *****\n\n"); } - -void debug_lua_plugin_manage_schema(struct lua_plugin_manage_schema *schema) -{ - printf("\n***** begin to debug one lua schema *****\n"); - printf("schema.st is %p\n", schema->st); - printf("schema state count is %d\n", schema->state_count); - for (int i = 0; i < schema->state_count; ++i) - { - printf("schema state[%d]pointer is %p\n", i, schema->thread_state[i]); - } - printf("schema model count is %d\n", schema->model_count); - for (int i = 0; i < schema->model_count; ++i) - { - printf("debug model[%d]\n", i); - printf("array %p, load %d, unload %d, env %d, mark %04x, count %d\n", - schema->model[i].plugin_array, schema->model[i].load_ref, schema->model[i].unload_ref, - schema->model[i].private_env_ref, schema->model[i].model_mark, schema->model[i].plugin_count); - struct lua_plugin *plugin = NULL; - while ((plugin = utarray_next(schema->model[i].plugin_array, plugin))) - { - printf("%d, %d, %d\n", plugin->plugin_id, plugin->ctx_new_ref, plugin->ctx_free_ref); - } - } - printf("schema total plugin count is %d\n", schema->plugin_count); - - printf("***** end of debug one lua schema *****\n\n"); -} #endif \ No newline at end of file