回收系统_对内存管理的提出新算法.

--[[
 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 then
			local len = table.getn( listen_sign )
			for i=1, len do
				G_res_custodian.action_map.remove_listener( listen_sign[i], act )
			end
			_obj.delete()
			_obj = nil
			listen_sign = nil
			act = nil
			public = nil
		end
	end
	function public.add_listener( _lst_id )
		listen_sign[ table.getn( listen_sign ) + 1 ] = _lst_id
		G_res_custodian.action_map.add_listener( _lst_id, act )
	end
	local len = table.getn( _t_id )
	for i=1, len do
		public.add_listener( _t_id[i] )
	end
end


 

--[[
 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 then
		return _t
	end
	local t = {}
	for i,v in pairs(_t) do
		t[table.getn(t) + 1] = i
	end
	return t
end

--取出对象值
function get_t_set_by_v( _t )
	if nil == _t then
		return _t
	end
	local t = {}
	for i,v in pairs(_t) do
		t[table.getn(t) + 1] = v
	end
	return t
end


--获得有效值
function get_valid_t( _t )
	local t = {}
	local len = table.getn(_t)
	for i=1, len do
		if nil ~= _t[i] then
			t[ table.getn(t) + 1] = _t[i]
		end
	end
	return t
end

--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 then
			local ret = t[1]
			t[1] = t[1] + 1
			if t[1] > t[2] then
				res[1] = res[ table.getn( res ) ]
				res[ table.getn( res ) ] = nil
			end
			return ret
		end
		return nil
	end
	function public.take_back_index( _index )
		local t = {_index, _index}
		res[ table.getn( res ) + 1] = t
		if table.getn( res ) > 50 then
			public.tidy()
		end
	end
	function public.tidy()
		local reflash = true
		--有新合并发生则继续进行合并检测
		while reflash do
			reflash = false
			--进行合并
			local len = table.getn( res )
			for i=1, len do
				for j=i+1, len do
					if table.getn(res[i]) > 1 and table.getn(res[j]) > 1 then
						local be_mixed = true
						--判断交集情况
						if res[i][2]+1 < res[j][1] or res[j][2]+1 < res[i][1] then
							be_mixed = false
						end
						--合并
						if be_mixed then
							local t = {}
							if res[i][1] < res[j][1] then
								t[1] = res[i][1]
							else
								t[1] = res[j][1]
							end
							if res[i][2] > res[j][2] then
								t[2] = res[i][2]
							else
								t[2] = res[j][2]
							end
							res[i] = t
							res[j] = {}
							reflash = true
						end
					end
				end
			end
			--刷新数据
			local t = {}
			local len = table.getn( res )
			for i=1, len do
				if table.getn(res[i]) > 1 then
					t[ table.getn(t) + 1 ] = res[i]
				end
			end
			res = t
		end
	end
	function public.show()
		local len = table.getn( res )
		for i=1, len do
			print(res[i][1], res[i][2])
		end
	end
	return public
end


--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] then
			left_set[_left] = {}
		end
		local len = table.getn(left_set[_left])
		for i=1, len do
			if left_set[_left][i] == _right then
				return
			end
		end
		left_set[_left][table.getn(left_set[_left])+1] = _right
		right_set[_right] = _left
	end
	--清除绑定
	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 do
			right_set[ t_right[i] ] = nil
		end
		left_set[_left] = nil
	end
	--清除绑定
	function public.clear_right( _left, _right )
		right_set[_right] = nil
		local t_right = left_set[_left]
		local len = table.getn( t_right )
		for i=1, len do
			if t_right[i] == _right then
				t_right[i] = nil
			end
		end
	end
	--通过left获得rigth表
	function public.get_t_right_by_left( _left )
		return get_valid_t( left_set[_left] )
	end
	--通过right获得left
	function public.get_left_by_right( _right )
		return right_set[ _right ]
	end
	return public
end

--buf绑定器(用于把类的实现内容弱耦合的分拆成多个模块独立完成)
function new_map_for_index_to_buf( _sign )
	local index_set = {}
	local public = {}
	--获得绑定者标识
	function public.get_sign()
		return _sign
	end
	--绑定buf
	function public.bind_index_to_buf( _index, _buf )
		index_set[ _index ] = _buf
	end
	--清除绑定
	function public.clear_index_to_buf( _index )
		index_set[_index] = nil
	end
	--通过left获得rigth表
	function public.get_buf( _index )
		return index_set[ _index ]
	end
	return public
end

--常用绑定者
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 _uid
end
function 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 do
		if g_type_binder.get_buf( t_right[i] ) == _buf_type then
			ret[ table.getn(ret) + 1] = t_right[i]
		end
	end
	return ret
end


--table转字符串
function sz_T2S(_table)
	local szLua = ""
	local t = type(_table)
	if t == "number" then
		szLua = szLua .. _table
	elseif t == "boolean" then
		szLua = szLua .. tostring(_table)
	elseif t == "string" then
		szLua = szLua .. string.format("%q", _table)
	elseif t == "table" then
		szLua = szLua .. "{"
		for k, v in pairs(_table) do
			szLua = szLua .. "[" .. sz_T2S(k) .. "]=" .. sz_T2S(v) .. ","
		end
		local metatable = getmetatable(_table)
			if metatable ~= nil and type(metatable.__index) == "table" then
			for k, v in pairs(metatable.__index) do
				szLua = szLua .. "[" .. sz_T2S(k) .. "]=" .. sz_T2S(v) .. ","
			end
		end
		szLua = szLua .. "}"
	elseif t == "nil" then
		return {}
	end
	return szLua
end

--提供绑定数据的函数
function clk_bind_data( t_par )
	local function do_clk_bind_data()
		if nil == t_par then
			return nil
		end
		local len = table.getn( t_par )
		if 0 == len then
			return nil
		elseif 1 == len then
			return t_par[1]
		elseif 2 == len then
			return t_par[1], t_par[2]
		elseif 3 == len then
			return t_par[1], t_par[2], t_par[3]
		elseif 4 == len then
			return t_par[1], t_par[2], t_par[3], t_par[4]
		elseif 5 == len then
			return t_par[1], t_par[2], t_par[3], t_par[4], t_par[5]
		elseif 6 == len then
			return t_par[1], t_par[2], t_par[3], t_par[4], t_par[5], t_par[6]
		elseif 7 == len then
			return t_par[1], t_par[2], t_par[3], t_par[4], t_par[5], t_par[6], t_par[7]
		elseif 8 == len then
			return 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 then
			return 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 then
			return 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 then
			return 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 then
			return 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 then
			return 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]
		else
			print("clk_bind_data",1)	--------------------------------------------------------
			return nil
		end
	end
	return do_clk_bind_data
end

--提供绑定函数和相关参数的
function clk_bind_fun_data( _fun, t_par )
	local function do_clk_bind_fun_data()
		if nil == _fun then
			print("clk_bind_fun_data fun is nil", 1)
			return true
		end
		if nil == t_par then
			return _fun()
		end
		local len = table.getn( t_par )
		if 0 == len then
			return _fun()
		elseif 1 == len then
			return _fun(t_par[1])
		elseif 2 == len then
			return _fun(t_par[1], t_par[2])
		elseif 3 == len then
			return _fun(t_par[1], t_par[2], t_par[3])
		elseif 4 == len then
			return _fun(t_par[1], t_par[2], t_par[3], t_par[4])
		elseif 5 == len then
			return _fun(t_par[1], t_par[2], t_par[3], t_par[4], t_par[5])
		elseif 6 == len then
			return _fun(t_par[1], t_par[2], t_par[3], t_par[4], t_par[5], t_par[6])
		elseif 7 == len then
			return _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 then
			return _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 then
			return _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 then
			return _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 then
			return _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 then
			return _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 then
			return _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])
		else
			print("clk_bind_fun_data实现",1)	--------------------------------------------------------
			return true
		end
	end
	return do_clk_bind_fun_data
end


 

--[[
 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 )
		end
		return public
	end
	trace_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} )
	end

	return public
end

G_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 ] = t
		return uid
	end
	--删除时间触发器
	function public.delete_timer_tgr( _uid )
		timer_tgr_res[ _uid ] = nil
		uid_manager.take_back_index( _uid )
		PACK_Trace.G_Trace.trace_text( TIMER_MODULE_NAME, "trace  删除定时器[" .. _uid .. "]" )
	end
	local sta_time = os.time()
	--模糊触发
	function public.on_vague_time()
		local time_unit = 1
		local cur_time = os.time()
		local dt = cur_time - sta_time
		if dt > time_unit then
			sta_time = cur_time
			local 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 - dt
				if t[i].left_time <= 0 then
					t[i].left_time = t[i].need_time
					t[i].callback( t[i].tgr_uid )
				end
			end
		end
	end
	--精确触发
	function public.on_exact_time()
		local time_unit = 1
		local cur_time = os.time()
		local dt = cur_time - sta_time
		if dt > time_unit then
			sta_time = cur_time
			local 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 - 1
				if t[i].left_time <= 0 then
					t[i].left_time = t[i].need_time
					t[i].callback( t[i].tgr_uid )
				end
			end
		end
	end
	return public
end
G_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 false
	end
	return public
end

--执行消息监听
function new_action_evt_listener()
	local public = {}
	function public.on_action( _t_evt )
		print("未实现 new_action_evt_listener", 1)
	end
	return public
end

--完毕消息监听
function new_response_evt_listener()
	local public = {}
	function public.on_response( _t_evt )
		print("未实现 new_response_evt_listener", 1)
	end
	return public
end

--消息处理机
function new_evt_server()
	--查找obj位置
	local function find_obj_pos(_t, _p)
		local len = table.getn( _t )
		for i=1, len do
			if _t[i] == _p then
				return i
			end
		end
		return nil
	end
	--映射图
	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] then
				this_public.map[_evt_iid] = {}
			end
			local t = this_public.map[_evt_iid]
			if nil == find_obj_pos(t, _p_recv) then
				t[ table.getn(t) + 1 ] = _p_recv
			end
		end
		function this_public.remove_listener(_evt_iid, _p_recv)
			remove_temp[ table.getn( remove_temp ) + 1 ] = {_evt_iid, _p_recv}
		end
		function this_public.erase()
			local len = table.getn( remove_temp )
			for i=1, len do
				local _evt_iid = remove_temp[i][1]
				local _p_recv = remove_temp[i][2]
				if nil ~= this_public.map[_evt_iid] then
					local t = this_public.map[_evt_iid]
					local id = find_obj_pos(t, _p_recv)
					if nil ~= id then
						local len = table.getn(t)
						t[id] = t[len]
						t[len] = nil
					end
					if table.getn(t) <= 0 then
						this_public.map[_evt_iid] = nil
					end
				end
			end
			remove_temp = {}
		end
		function this_public.clear()
			this_public.map = {}
		end
		return this_public
	end
	--事件机
	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()
	end
	function 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 ] then
			local t = public.vote_map.map[ _evt_iid ]
			local len = table.getn( t )
			for i=1, len do
				if t[i].on_vote( _t_evt ) then
					return
				end
			end
		end

		--触发
		if nil ~= public.action_map.map[ _evt_iid ] then
			local t = public.action_map.map[ _evt_iid ]
			local len = table.getn( t )
			for i=1, len do
				t[i].on_action( _t_evt )
			end
		end

		--完毕
		if nil ~= public.response_map.map[ _evt_iid ] then
			local t = public.response_map.map[ _evt_iid ]
			local len = table.getn( t )
			for i=1, len do
				t[i].on_response( _t_evt )
			end
		end
	end

	return public
end


 

--[[
 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 = nil
	end
	return public
end




 

--[[
 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 )
end

function test()
	print("")
	print("致晕")
	print("")
end


function 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 do
		PACK_Timer.G_timer_manager.on_vague_time()
	end
end
main()


 

你可能感兴趣的:(回收系统_对内存管理的提出新算法.)