From f8009e6f38f97b2ae27036862fd5ef0cb7800be8 Mon Sep 17 00:00:00 2001 From: raster Date: Fri, 2 Apr 2010 11:05:53 +0000 Subject: [PATCH] aaah well... 1. fix segv with edje_match stuff is deep child cleans up pattersn - ref them. 2. edje lua... beating it into shape. slowly bit by bit. git-svn-id: http://svn.enlightenment.org/svn/e/trunk/edje@47685 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33 --- src/lib/edje_cache.c | 13 ++++++------- src/lib/edje_load.c | 8 +------- src/lib/edje_lua.c | 32 +++++++++++++++++++++++++++----- src/lib/edje_lua_script_only.c | 23 +++++++++++------------ src/lib/edje_match.c | 25 +++++++++++++++++++++---- src/lib/edje_message_queue.c | 2 +- src/lib/edje_private.h | 18 +++++++++++------- src/lib/edje_program.c | 4 +--- 8 files changed, 79 insertions(+), 46 deletions(-) diff --git a/src/lib/edje_cache.c b/src/lib/edje_cache.c index 06778ca..f5384da 100644 --- a/src/lib/edje_cache.c +++ b/src/lib/edje_cache.c @@ -99,18 +99,17 @@ _edje_file_coll_open(Edje_File *edf, const char *coll) int err_code; //printf("lua chunk size: %d\n", size); - edc->L = _edje_lua_new_thread(_edje_lua_state_get()); // gets freed in 'edje_load::_edje_collection_free' - _edje_lua_new_reg(edc->L, -1, edc); // gets freed in 'edje_load::_edje_collectoin_free' + _edje_lua_new_reg(_edje_lua_state_get(), -1, edc); // gets freed in 'edje_load::_edje_collectoin_free' - if ((err_code = luaL_loadbuffer(edc->L, data, size, "edje_lua_script"))) + if ((err_code = luaL_loadbuffer(_edje_lua_state_get(), data, size, "edje_lua_script"))) { if (err_code == LUA_ERRSYNTAX) - ERR("lua load syntax error: %s", lua_tostring(edc->L, -1)); + ERR("lua load syntax error: %s", lua_tostring(_edje_lua_state_get(), -1)); else if (err_code == LUA_ERRMEM) - ERR("lua load memory allocation error: %s", lua_tostring(edc->L, -1)); + ERR("lua load memory allocation error: %s", lua_tostring(_edje_lua_state_get(), -1)); } - if (lua_pcall(edc->L, 0, 0, 0)) - ERR("lua call error: %s", lua_tostring(edc->L, -1)); + if (lua_pcall(_edje_lua_state_get(), 0, 0, 0)) + ERR("lua call error: %s", lua_tostring(_edje_lua_state_get(), -1)); free(data); } diff --git a/src/lib/edje_load.c b/src/lib/edje_load.c index 68a3eb0..a20eac4 100644 --- a/src/lib/edje_load.c +++ b/src/lib/edje_load.c @@ -951,7 +951,7 @@ _edje_file_del(Edje *ed) { _edje_lua_free_reg(ed->L, ed); // created in edje_lua.c::_edje_lua_script_fn_new/_edje_lua_group_fn_new _edje_lua_free_reg(ed->L, ed->L); // created in edje_program.c::_edje_program_run/edje_lua_script_only.c::_edje_lua_script_only_init - _edje_lua_free_thread(ed->L); // created in edje_program.c::_edje_program_run/edje_lua_script_only.c::_edje_lua_script_only_init + _edje_lua_free_thread(ed, ed->L); // created in edje_program.c::_edje_program_run/edje_lua_script_only.c::_edje_lua_script_only_init ed->L = NULL; } if (ed->table_parts) free(ed->table_parts); @@ -1154,12 +1154,6 @@ _edje_collection_free(Edje_File *edf, Edje_Part_Collection *ec) } #endif if (ec->script) embryo_program_free(ec->script); - if (ec->L) - { - _edje_lua_free_reg(ec->L, ec); // created in edje_cache.c::_edje_file_coll_open - _edje_lua_free_thread(ec->L); // created in edje_cache.c::_edje_file_coll_open - ec->L = NULL; - } free(ec); } diff --git a/src/lib/edje_lua.c b/src/lib/edje_lua.c index 6315024..6730ee0 100644 --- a/src/lib/edje_lua.c +++ b/src/lib/edje_lua.c @@ -270,8 +270,19 @@ __edje_lua_error(const char *file, const char *fnc, int line, lua_State *L, int } lua_State * -_edje_lua_new_thread(lua_State *L) +_edje_lua_new_thread(Edje *ed, lua_State *L) { +#if 1 // newlua + lua_newtable(L); + ed->lua_ref = luaL_ref(L, LUA_REGISTRYINDEX); + /* inherit new environment from global environment */ + lua_createtable(L, 1, 0); + lua_pushvalue(L, LUA_GLOBALSINDEX); + lua_setfield(L, -2, "__index"); + lua_setmetatable(L, -2); + lua_setfenv(L, -2); + return L; +#else /* create new thread */ lua_State *thread = lua_newthread(L); //printf ("new thread %d->%d\n", L, thread); @@ -284,11 +295,16 @@ _edje_lua_new_thread(lua_State *L) lua_setmetatable(L, -2); lua_setfenv(L, -2); return thread; +#endif } void -_edje_lua_free_thread(lua_State *L) +_edje_lua_free_thread(Edje *ed, lua_State *L) { +#if 1 // newlua + luaL_unref(L, LUA_REGISTRYINDEX, ed->lua_ref); + lua_gc(L, LUA_GCCOLLECT, 0); +#else lua_pushthread(L); lua_getfenv(L, -1); lua_pushnil(L); @@ -302,6 +318,7 @@ _edje_lua_free_thread(lua_State *L) } lua_settop(L, 0); lua_gc(L, LUA_GCCOLLECT, 0); +#endif } /* @@ -1459,6 +1476,7 @@ static int _edje_lua_object_fn_del(lua_State *L) { Edje_Lua_Evas_Object *obj = _edje_lua_checkudata(L, 1, &mObject); + if (obj->eo) { evas_object_del(obj->eo); @@ -5549,6 +5567,7 @@ static void * _edje_lua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) { Edje_Lua_Alloc *ela = ud; + void *ptr2; ela->cur += nsize - osize; if (ela->cur > ela->max) @@ -5562,9 +5581,12 @@ _edje_lua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) free(ptr); /* ANSI requires that free(NULL) has no effect */ return NULL; } - else - /* ANSI requires that realloc(NULL, size) == malloc(size) */ - return realloc(ptr, nsize); + + /* ANSI requires that realloc(NULL, size) == malloc(size) */ + ptr2 = realloc(ptr, nsize); + if (ptr2) return ptr2; + ERR("Edje Lua cannot re-allocate %i bytes\n", nsize); + return ptr2; } void diff --git a/src/lib/edje_lua_script_only.c b/src/lib/edje_lua_script_only.c index 8ba7b49..0c81dfe 100644 --- a/src/lib/edje_lua_script_only.c +++ b/src/lib/edje_lua_script_only.c @@ -28,8 +28,7 @@ void *alloca(size_t); Eina_Bool _edje_lua_script_only(Edje * ed) { - if ((ed->collection) && (ed->collection->L) && - (ed->collection->lua_script_only)) + if ((ed->collection) && (ed->collection->lua_script_only)) return EINA_TRUE; return EINA_FALSE; } @@ -37,11 +36,11 @@ _edje_lua_script_only(Edje * ed) void _edje_lua_script_only_init(Edje * ed) { - if (ed->collection && ed->collection->L) + if (ed->collection) { - ed->L = _edje_lua_new_thread(ed->collection->L); // freed in _edje_lua_script_only_shutdown - _edje_lua_new_reg(ed->collection->L, -1, ed->L); // freed in _edje_lua_script_only_shutdown - lua_pop(ed->collection->L, 1); /* thread */ + ed->L = _edje_lua_new_thread(ed, _edje_lua_state_get()); // freed in _edje_lua_script_only_shutdown + _edje_lua_new_reg(ed->L, -1, ed->L); // freed in _edje_lua_script_only_shutdown + lua_pop(ed->L, 1); /* thread */ lua_State *L = ed->L; _edje_lua_script_fn_new(ed); @@ -67,7 +66,7 @@ _edje_lua_script_only_init(Edje * ed) void _edje_lua_script_only_shutdown(Edje * ed) { - if (ed->collection && ed->collection->L && ed->L) + if (ed->collection && ed->L) { lua_State *L = ed->L; lua_getglobal(L, "shutdown"); @@ -90,7 +89,7 @@ _edje_lua_script_only_shutdown(Edje * ed) void _edje_lua_script_only_show(Edje * ed) { - if (ed->collection && ed->collection->L && ed->L) + if (ed->collection && ed->L) { lua_State *L = ed->L; lua_getglobal(L, "show"); @@ -113,7 +112,7 @@ _edje_lua_script_only_show(Edje * ed) void _edje_lua_script_only_hide(Edje * ed) { - if (ed->collection && ed->collection->L && ed->L) + if (ed->collection && ed->L) { lua_State *L = ed->L; lua_getglobal(L, "hide"); @@ -136,7 +135,7 @@ _edje_lua_script_only_hide(Edje * ed) void _edje_lua_script_only_move(Edje * ed) { - if (ed->collection && ed->collection->L && ed->L) + if (ed->collection && ed->L) { lua_State *L = ed->L; lua_getglobal(L, "move"); @@ -161,7 +160,7 @@ _edje_lua_script_only_move(Edje * ed) void _edje_lua_script_only_resize(Edje * ed) { - if (ed->collection && ed->collection->L && ed->L) + if (ed->collection && ed->L) { lua_State *L = ed->L; lua_getglobal(L, "resize"); @@ -186,7 +185,7 @@ _edje_lua_script_only_resize(Edje * ed) void _edje_lua_script_only_message(Edje * ed, Edje_Message * em) { - if (ed->collection && ed->collection->L && ed->L) + if (ed->collection && ed->L) { lua_State *L = ed->L; lua_getglobal(L, "message"); diff --git a/src/lib/edje_match.c b/src/lib/edje_match.c index 9f5c8bd..b7752dd 100644 --- a/src/lib/edje_match.c +++ b/src/lib/edje_match.c @@ -266,6 +266,8 @@ _edje_match_patterns_exec_init_states(Edje_States *states, * sizeof(*r->patterns)); \ if (!r) return NULL; \ \ + r->ref = 1; \ + r->delete_me = 0; \ r->patterns_size = eina_list_count(lst); \ r->max_length = 0; \ r->patterns = (const char **) r->finals + r->patterns_size + 1; \ @@ -370,7 +372,9 @@ edje_match_programs_exec_check_finals(const size_t *signal_finals, } static int -edje_match_callback_exec_check_finals(const size_t *signal_finals, +edje_match_callback_exec_check_finals(const Edje_Patterns *singal_ppat, + const Edje_Patterns *source_ppat, + const size_t *signal_finals, const size_t *source_finals, const Edje_States *signal_states, const Edje_States *source_states, @@ -402,6 +406,8 @@ edje_match_callback_exec_check_finals(const size_t *signal_finals, } if (_edje_block_break(ed)) return 0; + if ((singal_ppat->delete_me) || (source_ppat->delete_me)) + return 0; } } @@ -511,8 +517,8 @@ edje_match_programs_exec(const Edje_Patterns *ppat_signal, } int -edje_match_callback_exec(const Edje_Patterns *ppat_signal, - const Edje_Patterns *ppat_source, +edje_match_callback_exec(Edje_Patterns *ppat_signal, + Edje_Patterns *ppat_source, const char *signal, const char *source, Eina_List *callbacks, @@ -522,6 +528,8 @@ edje_match_callback_exec(const Edje_Patterns *ppat_signal, Edje_States *source_result; int r = 0; + ppat_signal->ref++; + ppat_source->ref++; _edje_match_patterns_exec_init_states(ppat_signal->states, ppat_signal->patterns_size, ppat_signal->max_length); @@ -533,7 +541,9 @@ edje_match_callback_exec(const Edje_Patterns *ppat_signal, source_result = _edje_match_fn(ppat_source, source, ppat_source->states); if (signal_result && source_result) - r = edje_match_callback_exec_check_finals(ppat_signal->finals, + r = edje_match_callback_exec_check_finals(ppat_signal, + ppat_source, + ppat_signal->finals, ppat_source->finals, signal_result, source_result, @@ -541,12 +551,19 @@ edje_match_callback_exec(const Edje_Patterns *ppat_signal, source, callbacks, ed); + ppat_signal->ref--; + ppat_source->ref--; + if (ppat_signal->ref <= 0) edje_match_patterns_free(ppat_signal); + if (ppat_source->ref <= 0) edje_match_patterns_free(ppat_source); return r; } void edje_match_patterns_free(Edje_Patterns *ppat) { + ppat->delete_me = 1; + ppat->ref--; + if (ppat->ref > 0) return; _edje_match_states_free(ppat->states, 2); free(ppat); } diff --git a/src/lib/edje_message_queue.c b/src/lib/edje_message_queue.c index 12a2833..f207be3 100644 --- a/src/lib/edje_message_queue.c +++ b/src/lib/edje_message_queue.c @@ -629,7 +629,7 @@ _edje_message_process(Edje_Message *em) _edje_script_only_message(em->edje, em); return; } - if (em->edje->collection->L) + if (em->edje->L) { _edje_lua_script_only_message(em->edje, em); return; diff --git a/src/lib/edje_private.h b/src/lib/edje_private.h index e99b8ab..b7b994b 100644 --- a/src/lib/edje_private.h +++ b/src/lib/edje_private.h @@ -313,7 +313,7 @@ struct _Edje_File Eina_Hash *data_cache; Eet_File *ef; - + unsigned int free_strings : 1; unsigned int dangling : 1; }; @@ -533,7 +533,6 @@ struct _Edje_Part_Collection unsigned char script_only; unsigned char lua_script_only; - lua_State *L; }; struct _Edje_Part @@ -844,7 +843,9 @@ struct _Edje unsigned int all_part_change : 1; #endif unsigned int have_mapped_part : 1; - lua_State *L; + + lua_State *L; + int lua_ref; }; struct _Edje_Calc_Params @@ -1150,6 +1151,9 @@ struct _Edje_Patterns Edje_States *states; + int ref; + Eina_Bool delete_me : 1; + size_t patterns_size; size_t max_length; size_t finals[]; @@ -1170,8 +1174,8 @@ Eina_Bool edje_match_programs_exec(const Edje_Patterns *ppat_signal, Eina_List *programs, Eina_Bool (*func)(Edje_Program *pr, void *data), void *data); -int edje_match_callback_exec(const Edje_Patterns *ppat_signal, - const Edje_Patterns *ppat_source, +int edje_match_callback_exec(Edje_Patterns *ppat_signal, + Edje_Patterns *ppat_source, const char *signal, const char *source, Eina_List *callbacks, @@ -1479,8 +1483,8 @@ extern jmp_buf _edje_lua_panic_jmp; #define _edje_lua_panic_here() setjmp(_edje_lua_panic_jmp) lua_State *_edje_lua_state_get(); -lua_State *_edje_lua_new_thread(lua_State *L); -void _edje_lua_free_thread(lua_State *L); +lua_State *_edje_lua_new_thread(Edje *ed, lua_State *L); +void _edje_lua_free_thread(Edje *ed, lua_State *L); void _edje_lua_new_reg(lua_State *L, int index, void *ptr); void _edje_lua_get_reg(lua_State *L, void *ptr); void _edje_lua_free_reg(lua_State *L, void *ptr); diff --git a/src/lib/edje_program.c b/src/lib/edje_program.c index f9122f9..8f9872b 100644 --- a/src/lib/edje_program.c +++ b/src/lib/edje_program.c @@ -847,9 +847,7 @@ _edje_program_run(Edje *ed, Edje_Program *pr, Eina_Bool force, const char *ssig, if (ed->L == NULL) /* private state does not yet exist, create it */ { - ed->L = _edje_lua_new_thread(ed->collection->L); - _edje_lua_new_reg(ed->collection->L, -1, ed->L); // freed in edje_load.c::_edje_file_del - lua_pop(ed->collection->L, 1); + ed->L = _edje_lua_new_thread(ed, _edje_lua_state_get()); } lua_State *L = ed->L; lua_pushnumber(L, pr->id); -- 2.7.4