回收系统_对内存管理的提出新算法.
--[[ file name : 技能模块第一版 author : Clark/陈泽丹 created : 2012-11-07 purpose : 由回收系统的new出来的物体, 它自身和它的被依赖者是没有对其进行死亡操作的, 它和它的被依赖者, 只能通过向回收系统发事件让"上天"来解放资源. 回收系统实现了与引用计数管理内存相反的效果: 引用计数是最后者生效, 这里实现最早者生效. 那为什么不采用直接删除,然后后来者的操作无效即可的方案呢? 答案是如果那样做, 会有回收后再分配出去的ID被误删的可能. 而采用这种方式, 再删除时,是删除监听者, 即删除"关系", 而不是删除"目标",所以不用担心 目标回收后再分配出去的问题. 因为虽然是同一个目标,但是他们的之间的关系已不存在了. --]]RECOVER_MODULE_NAME = "工具库_堆内存管理"module(RECOVER_MODULE_NAME, package.seeall)PACK_EvtSvr = require"工具库_事件机"PACK_GameAPI = require"工具库_插件"PACK_Timer = require"工具库_定时器"PACK_Trace= require"工具库_调试工具"G_res_custodian= PACK_EvtSvr.new_evt_server()--死亡信号function send_delete_evt( _id )G_res_custodian.dispatch_evt( _id, {"释放"} )end--托管员function newEx( _obj, _t_id )local listen_sign = {}local act = PACK_EvtSvr.new_action_evt_listener()local public = {}function act.on_action( _evt )if nil ~= _obj thenlocal len = table.getn( listen_sign )for i=1, len doG_res_custodian.action_map.remove_listener( listen_sign[i], act )end_obj.delete()_obj = nillisten_sign = nilact = nilpublic = nilendendfunction public.add_listener( _lst_id )listen_sign[ table.getn( listen_sign ) + 1 ] = _lst_idG_res_custodian.action_map.add_listener( _lst_id, act )endlocal len = table.getn( _t_id )for i=1, len dopublic.add_listener( _t_id[i] )endend
--[[ file name : 技能模块第一版 author : Clark/陈泽丹 created : 2012-11-07 purpose : --]]GameAPI_MODULE_NAME = "工具库_插件"module(GameAPI_MODULE_NAME, package.seeall)--取出序例值function get_t_set_by_i( _t )if nil == _t thenreturn _tendlocal t = {}for i,v in pairs(_t) dot[table.getn(t) + 1] = iendreturn tend--取出对象值function get_t_set_by_v( _t )if nil == _t thenreturn _tendlocal t = {}for i,v in pairs(_t) dot[table.getn(t) + 1] = vendreturn tend--获得有效值function get_valid_t( _t )local t = {}local len = table.getn(_t)for i=1, len doif nil ~= _t[i] thent[ table.getn(t) + 1] = _t[i]endendreturn tend--ID管理器function new_index_manager()local res = {{1,99999999}}local public = {}function public.take_away_index()local t = res[1]if table.getn(t) >= 1 thenlocal ret = t[1]t[1] = t[1] + 1if t[1] > t[2] thenres[1] = res[ table.getn( res ) ]res[ table.getn( res ) ] = nilendreturn retendreturn nilendfunction public.take_back_index( _index )local t = {_index, _index}res[ table.getn( res ) + 1] = tif table.getn( res ) > 50 thenpublic.tidy()endendfunction public.tidy()local reflash = true--有新合并发生则继续进行合并检测while reflash doreflash = false--进行合并local len = table.getn( res )for i=1, len dofor j=i+1, len doif table.getn(res[i]) > 1 and table.getn(res[j]) > 1 thenlocal be_mixed = true--判断交集情况if res[i][2]+1 < res[j][1] or res[j][2]+1 < res[i][1] thenbe_mixed = falseend--合并if be_mixed thenlocal t = {}if res[i][1] < res[j][1] thent[1] = res[i][1]elset[1] = res[j][1]endif res[i][2] > res[j][2] thent[2] = res[i][2]elset[2] = res[j][2]endres[i] = tres[j] = {}reflash = trueendendendend--刷新数据local t = {}local len = table.getn( res )for i=1, len doif table.getn(res[i]) > 1 thent[ table.getn(t) + 1 ] = res[i]endendres = tendendfunction public.show()local len = table.getn( res )for i=1, len doprint(res[i][1], res[i][2])endendreturn publicend--1:N绑定器function new_map_for_1_and_N()local left_set = {}local right_set = {}local public = {}--绑定索引和UID( 1:N )function public.bind_left_and_right( _left, _right )if nil == left_set[_left] thenleft_set[_left] = {}endlocal len = table.getn(left_set[_left])for i=1, len doif left_set[_left][i] == _right thenreturnendendleft_set[_left][table.getn(left_set[_left])+1] = _rightright_set[_right] = _leftend--清除绑定function public.clear_left_and_right( _left )local t_right = public.get_t_map_by_fb_buf_index( _left )local len = table.getn( t_right )for i=1, len doright_set[ t_right[i] ] = nilendleft_set[_left] = nilend--清除绑定function public.clear_right( _left, _right )right_set[_right] = nillocal t_right = left_set[_left]local len = table.getn( t_right )for i=1, len doif t_right[i] == _right thent_right[i] = nilendendend--通过left获得rigth表function public.get_t_right_by_left( _left )return get_valid_t( left_set[_left] )end--通过right获得leftfunction public.get_left_by_right( _right )return right_set[ _right ]endreturn publicend--buf绑定器(用于把类的实现内容弱耦合的分拆成多个模块独立完成)function new_map_for_index_to_buf( _sign )local index_set = {}local public = {}--获得绑定者标识function public.get_sign()return _signend--绑定buffunction public.bind_index_to_buf( _index, _buf )index_set[ _index ] = _bufend--清除绑定function public.clear_index_to_buf( _index )index_set[_index] = nilend--通过left获得rigth表function public.get_buf( _index )return index_set[ _index ]endreturn publicend--常用绑定者g_type_binder = new_map_for_index_to_buf( "type" )function new_binder(_uid, _buf_type_binder, _buf_obj)--绑定操作类型g_type_binder.bind_index_to_buf(_uid, _buf_type_binder.get_uid() )--绑定操作对象_buf_type_binder.bind_index_to_buf(_uid, _buf_obj)return _uidendfunction find_t_buf_by_type( _container, _index, _buf_type )local t_right = _container.get_t_right_by_left( _index )local ret = {}local len = table.getn( t_right )for i=1, len doif g_type_binder.get_buf( t_right[i] ) == _buf_type thenret[ table.getn(ret) + 1] = t_right[i]endendreturn retend--table转字符串function sz_T2S(_table)local szLua = ""local t = type(_table)if t == "number" thenszLua = szLua .. _tableelseif t == "boolean" thenszLua = szLua .. tostring(_table)elseif t == "string" thenszLua = szLua .. string.format("%q", _table)elseif t == "table" thenszLua = szLua .. "{"for k, v in pairs(_table) doszLua = szLua .. "[" .. sz_T2S(k) .. "]=" .. sz_T2S(v) .. ","endlocal metatable = getmetatable(_table)if metatable ~= nil and type(metatable.__index) == "table" thenfor k, v in pairs(metatable.__index) doszLua = szLua .. "[" .. sz_T2S(k) .. "]=" .. sz_T2S(v) .. ","endendszLua = szLua .. "}"elseif t == "nil" thenreturn {}endreturn szLuaend--提供绑定数据的函数function clk_bind_data( t_par )local function do_clk_bind_data()if nil == t_par thenreturn nilendlocal len = table.getn( t_par )if 0 == len thenreturn nilelseif 1 == len thenreturn t_par[1]elseif 2 == len thenreturn t_par[1], t_par[2]elseif 3 == len thenreturn t_par[1], t_par[2], t_par[3]elseif 4 == len thenreturn t_par[1], t_par[2], t_par[3], t_par[4]elseif 5 == len thenreturn t_par[1], t_par[2], t_par[3], t_par[4], t_par[5]elseif 6 == len thenreturn t_par[1], t_par[2], t_par[3], t_par[4], t_par[5], t_par[6]elseif 7 == len thenreturn t_par[1], t_par[2], t_par[3], t_par[4], t_par[5], t_par[6], t_par[7]elseif 8 == len thenreturn t_par[1], t_par[2], t_par[3], t_par[4], t_par[5], t_par[6], t_par[7], t_par[8]elseif 9 == len thenreturn t_par[1], t_par[2], t_par[3], t_par[4], t_par[5], t_par[6], t_par[7], t_par[8], t_par[9]elseif 10 == len thenreturn t_par[1], t_par[2], t_par[3], t_par[4], t_par[5], t_par[6], t_par[7], t_par[8], t_par[9], t_par[10]elseif 11 == len thenreturn t_par[1], t_par[2], t_par[3], t_par[4], t_par[5], t_par[6], t_par[7], t_par[8], t_par[9], t_par[10], t_par[11]elseif 12 == len thenreturn t_par[1], t_par[2], t_par[3], t_par[4], t_par[5], t_par[6], t_par[7], t_par[8], t_par[9], t_par[10], t_par[11], t_par[12]elseif 13 == len thenreturn t_par[1], t_par[2], t_par[3], t_par[4], t_par[5], t_par[6], t_par[7], t_par[8], t_par[9], t_par[10], t_par[11], t_par[12], t_par[13]elseprint("clk_bind_data",1)--------------------return nilendendreturn do_clk_bind_dataend--提供绑定函数和相关参数的function clk_bind_fun_data( _fun, t_par )local function do_clk_bind_fun_data()if nil == _fun thenprint("clk_bind_fun_data fun is nil", 1)return trueendif nil == t_par thenreturn _fun()endlocal len = table.getn( t_par )if 0 == len thenreturn _fun()elseif 1 == len thenreturn _fun(t_par[1])elseif 2 == len thenreturn _fun(t_par[1], t_par[2])elseif 3 == len thenreturn _fun(t_par[1], t_par[2], t_par[3])elseif 4 == len thenreturn _fun(t_par[1], t_par[2], t_par[3], t_par[4])elseif 5 == len thenreturn _fun(t_par[1], t_par[2], t_par[3], t_par[4], t_par[5])elseif 6 == len thenreturn _fun(t_par[1], t_par[2], t_par[3], t_par[4], t_par[5], t_par[6])elseif 7 == len thenreturn _fun(t_par[1], t_par[2], t_par[3], t_par[4], t_par[5], t_par[6], t_par[7])elseif 8 == len thenreturn _fun(t_par[1], t_par[2], t_par[3], t_par[4], t_par[5], t_par[6], t_par[7], t_par[8])elseif 9 == len thenreturn _fun(t_par[1], t_par[2], t_par[3], t_par[4], t_par[5], t_par[6], t_par[7], t_par[8], t_par[9])elseif 10 == len thenreturn _fun(t_par[1], t_par[2], t_par[3], t_par[4], t_par[5], t_par[6], t_par[7], t_par[8], t_par[9], t_par[10])elseif 11 == len thenreturn _fun(t_par[1], t_par[2], t_par[3], t_par[4], t_par[5], t_par[6], t_par[7], t_par[8], t_par[9], t_par[10], t_par[11])elseif 12 == len thenreturn _fun(t_par[1], t_par[2], t_par[3], t_par[4], t_par[5], t_par[6], t_par[7], t_par[8], t_par[9], t_par[10], t_par[11], t_par[12])elseif 13 == len thenreturn _fun(t_par[1], t_par[2], t_par[3], t_par[4], t_par[5], t_par[6], t_par[7], t_par[8], t_par[9], t_par[10], t_par[11], t_par[12], t_par[13])elseprint("clk_bind_fun_data实现",1)--------------------return trueendendreturn do_clk_bind_fun_dataend
--[[ file name : 技能模块第一版 author : Clark/陈泽丹 created : 2012-11-07 purpose : --]]TRACE_MODULE_NAME = "工具库_调试工具"module(TRACE_MODULE_NAME, package.seeall)PACK_EvtSvr = require"工具库_事件机"--Trace服function new_Game_Trace()local trace_svr = PACK_EvtSvr.new_evt_server()--输出local function do_print_on_trace_svr()local public = PACK_EvtSvr.new_action_evt_listener()function public.on_action( _evt )print( _evt.evt_iid, _evt.text )endreturn publicendtrace_obj = do_print_on_trace_svr()local public = {}--打开输出开关function public.open_trace( _evt_iid )trace_svr.action_map.add_listener( _evt_iid, trace_obj )end--关闭输出开关function public.close_trace( _evt_iid )trace_svr.action_map.remove_listener( _evt_iid, trace_obj )end--输出调试语句function public.trace_text( _evt_iid, _text )trace_svr.dispatch_evt( _evt_iid, { evt_iid = _evt_iid, text = _text} )endreturn publicendG_Trace = new_Game_Trace()
--[[ file name : 技能模块第一版 author : Clark/陈泽丹 created : 2012-11-07 purpose : --]]TIMER_MODULE_NAME = "工具库_定时器"module(TIMER_MODULE_NAME, package.seeall)PACK_GameAPI = require"工具库_插件"PACK_Trace= require"工具库_调试工具"--时间触发器管理者function new_timer_manager()local uid_manager = PACK_GameAPI.new_index_manager()local timer_tgr_res = {}local public = {}--创建时间触发器function public.new_timer_tgr( _time, _callback )local uid = uid_manager.take_away_index()local t ={tgr_uid = uid,need_time = _time,left_time = _time,callback = _callback,}timer_tgr_res[ uid ] = treturn uidend--删除时间触发器function public.delete_timer_tgr( _uid )timer_tgr_res[ _uid ] = niluid_manager.take_back_index( _uid )PACK_Trace.G_Trace.trace_text( TIMER_MODULE_NAME, "trace 删除定时器[" .. _uid .. "]" )endlocal sta_time = os.time()--模糊触发function public.on_vague_time()local time_unit = 1local cur_time = os.time()local dt = cur_time - sta_timeif dt > time_unit thensta_time = cur_timelocal t = PACK_GameAPI.get_t_set_by_v(timer_tgr_res)local len = table.getn( t )for i=1, len do--模糊触发t[i].left_time = t[i].left_time - dtif t[i].left_time <= 0 thent[i].left_time = t[i].need_timet[i].callback( t[i].tgr_uid )endendendend--精确触发function public.on_exact_time()local time_unit = 1local cur_time = os.time()local dt = cur_time - sta_timeif dt > time_unit thensta_time = cur_timelocal t = PACK_GameAPI.get_t_set_by_v(timer_tgr_res)local len = table.getn( t )for i=1, len do--精确触发t[i].left_time = t[i].left_time - 1if t[i].left_time <= 0 thent[i].left_time = t[i].need_timet[i].callback( t[i].tgr_uid )endendendendreturn publicendG_timer_manager = new_timer_manager()
--[[ file name : 技能模块第一版 author : Clark/陈泽丹 created : 2012-11-07 purpose : --]]EVTSVR_MODULE_NAME = "工具库_事件机"module(EVTSVR_MODULE_NAME, package.seeall)--否决事件监听function new_vote_evt_listener()local public = {}function public.on_vote( _t_evt )print("未实现 new_vote_evt_listener", 1)return falseendreturn publicend--执行消息监听function new_action_evt_listener()local public = {}function public.on_action( _t_evt )print("未实现 new_action_evt_listener", 1)endreturn publicend--完毕消息监听function new_response_evt_listener()local public = {}function public.on_response( _t_evt )print("未实现 new_response_evt_listener", 1)endreturn publicend--消息处理机function new_evt_server()--查找obj位置local function find_obj_pos(_t, _p)local len = table.getn( _t )for i=1, len doif _t[i] == _p thenreturn iendendreturn nilend--映射图local function new_event_map()local this_public = {}this_public.map = {}local remove_temp = {}function this_public.add_listener(_evt_iid, _p_recv)if nil == this_public.map[_evt_iid] thenthis_public.map[_evt_iid] = {}endlocal t = this_public.map[_evt_iid]if nil == find_obj_pos(t, _p_recv) thent[ table.getn(t) + 1 ] = _p_recvendendfunction this_public.remove_listener(_evt_iid, _p_recv)remove_temp[ table.getn( remove_temp ) + 1 ] = {_evt_iid, _p_recv}endfunction this_public.erase()local len = table.getn( remove_temp )for i=1, len dolocal _evt_iid = remove_temp[i][1]local _p_recv = remove_temp[i][2]if nil ~= this_public.map[_evt_iid] thenlocal t = this_public.map[_evt_iid]local id = find_obj_pos(t, _p_recv)if nil ~= id thenlocal len = table.getn(t)t[id] = t[len]t[len] = nilendif table.getn(t) <= 0 thenthis_public.map[_evt_iid] = nilendendendremove_temp = {}endfunction this_public.clear()this_public.map = {}endreturn this_publicend--事件机local public = {}public.vote_map = new_event_map()public.action_map = new_event_map()public.response_map = new_event_map()function public.clear()public.vote_map.clear()public.action_map.clear()public.response_map.clear()endfunction public.dispatch_evt( _evt_iid, _t_evt )public.vote_map.erase()public.action_map.erase()public.response_map.erase()--否决if nil ~= public.vote_map.map[ _evt_iid ] thenlocal t = public.vote_map.map[ _evt_iid ]local len = table.getn( t )for i=1, len doif t[i].on_vote( _t_evt ) thenreturnendendend--触发if nil ~= public.action_map.map[ _evt_iid ] thenlocal t = public.action_map.map[ _evt_iid ]local len = table.getn( t )for i=1, len dot[i].on_action( _t_evt )endend--完毕if nil ~= public.response_map.map[ _evt_iid ] thenlocal t = public.response_map.map[ _evt_iid ]local len = table.getn( t )for i=1, len dot[i].on_response( _t_evt )endendendreturn publicend
--[[ file name : 技能模块第一版 author : Clark/陈泽丹 created : 2012-11-07 purpose : --]]RCV_ETT_MODULE_NAME = "中间件_定时触发器"module(RCV_ETT_MODULE_NAME, package.seeall)PACK_EvtSvr = require"工具库_事件机"PACK_GameAPI = require"工具库_插件"PACK_Timer = require"工具库_定时器"PACK_Trace= require"工具库_调试工具"PACK_Recover= require"工具库_堆内存管理"--定时器function new_timer_tgr( _dt, _callback )local public = {}public.tm_tgr_uid = PACK_Timer.G_timer_manager.new_timer_tgr(_dt, _callback)function public.delete()PACK_Timer.G_timer_manager.delete_timer_tgr(public.tm_tgr_uid)public.tm_tgr_uid = nilendreturn publicend
--[[ file name : 技能模块第一版 author : Clark/陈泽丹 created : 2012-11-07 purpose : --]]module("测试", package.seeall)PACK_GameAPI = require"工具库_插件"PACK_Timer = require"工具库_定时器"PACK_Trace= require"工具库_调试工具"PACK_Recover= require"工具库_堆内存管理"PACK_TimerEx= require"中间件_定时触发器"function send_delete_evt()PACK_Recover.send_delete_evt( 1078 )endfunction test()print("")print("致晕")print("")endfunction main()--初始化模块PACK_Trace.G_Trace.open_trace(PACK_TimerEx.RCV_ETT_MODULE_NAME)PACK_Trace.G_Trace.open_trace(PACK_Recover.RECOVER_MODULE_NAME)PACK_Trace.G_Trace.open_trace(PACK_Timer.TIMER_MODULE_NAME)PACK_TimerEx.new_timer_tgr( 6, send_delete_evt )PACK_Recover.newEx( PACK_TimerEx.new_timer_tgr( 2, test ), {1078} )while true doPACK_Timer.G_timer_manager.on_vague_time()endendmain()