Improving hooking methods

- Backward compatible with existing function-pointer based hooking method
- Using RT_USING_HOOK as an on/off switch
- Introducing a new low-overhead macro-based hooking method
This commit is contained in:
Gabriel Wang 2022-01-01 16:37:41 +00:00 committed by Bernard Xiong
parent 5e24acf7fa
commit b627414f67
1 changed files with 68 additions and 4 deletions

View File

@ -38,6 +38,7 @@
* 2021-05-10 armink change version number to v4.0.4
* 2021-11-19 Meco Man change version number to v4.1.0
* 2021-12-21 Meco Man re-implement RT_UNUSED
* 2022-01-01 Gabriel improve hooking method
*/
#ifndef __RT_DEF_H__
@ -437,11 +438,74 @@ struct rt_object_information
/**
* The hook function call macro
*/
#ifdef RT_USING_HOOK
#define RT_OBJECT_HOOK_CALL(func, argv) \
do { if ((func) != RT_NULL) func argv; } while (0)
#ifndef RT_USING_HOOK
#define __ON_HOOK_ARGS(__hook, argv)
#define RT_OBJECT_HOOK_CALL(func, argv)
#else
#define RT_OBJECT_HOOK_CALL(func, argv)
#define RT_OBJECT_HOOK_CALL(func, argv) __on_##func argv
#ifdef RT_HOOK_USING_FUNC_PTR
#define __ON_HOOK_ARGS(__hook, argv) do {if ((__hook) != RT_NULL) __hook argv; } while (0)
#else
#define __ON_HOOK_ARGS(__hook, argv)
#endif
#endif
#ifndef __on_rt_interrupt_enter_hook
#define __on_rt_interrupt_enter_hook() __ON_HOOK_ARGS(rt_interrupt_enter_hook, ())
#endif
#ifndef __on_rt_interrupt_leave_hook
#define __on_rt_interrupt_leave_hook() __ON_HOOK_ARGS(rt_interrupt_leave_hook, ())
#endif
#ifndef __on_rt_interrupt_switch_hook
#define __on_rt_interrupt_switch_hook() __ON_HOOK_ARGS(rt_interrupt_switch_hook, ())
#endif
#ifndef __on_rt_malloc_hook
#define __on_rt_malloc_hook(addr, size) __ON_HOOK_ARGS(rt_malloc_hook, (addr, size))
#endif
#ifndef __on_rt_free_hook
#define __on_rt_free_hook(rmem) __ON_HOOK_ARGS(rt_free_hook, (rmem))
#endif
#ifndef __on_rt_mp_alloc_hook
#define __on_rt_mp_alloc_hook(mp, block) __ON_HOOK_ARGS(rt_mp_alloc_hook, (mp, block))
#endif
#ifndef __on_rt_mp_free_hook
#define __on_rt_mp_free_hook(mp, block) __ON_HOOK_ARGS(rt_mp_free_hook, (mp, block))
#endif
#ifndef __on_rt_object_trytake_hook
#define __on_rt_object_trytake_hook(parent) __ON_HOOK_ARGS(rt_object_trytake_hook, (parent))
#endif
#ifndef __on_rt_object_take_hook
#define __on_rt_object_take_hook(parent) __ON_HOOK_ARGS(rt_object_take_hook, (parent))
#endif
#ifndef __on_rt_object_put_hook
#define __on_rt_object_put_hook(parent) __ON_HOOK_ARGS(rt_object_put_hook, (parent))
#endif
#ifndef __on_rt_scheduler_hook
#define __on_rt_scheduler_hook(from, to) __ON_HOOK_ARGS(rt_scheduler_hook, (from, to))
#endif
#ifndef __on_rt_scheduler_switch_hook
#define __on_rt_scheduler_switch_hook(tid) __ON_HOOK_ARGS(rt_scheduler_switch_hook, (tid))
#endif
#ifndef __on_rt_object_attach_hook
#define __on_rt_object_attach_hook(obj) __ON_HOOK_ARGS(rt_object_attach_hook, (obj))
#endif
#ifndef __on_rt_object_detach_hook
#define __on_rt_object_detach_hook(obj) __ON_HOOK_ARGS(rt_object_detach_hook, (obj))
#endif
#ifndef __on_rt_thread_inited_hook
#define __on_rt_thread_inited_hook(thread) __ON_HOOK_ARGS(rt_thread_inited_hook, (thread))
#endif
#ifndef __on_rt_thread_suspend_hook
#define __on_rt_thread_suspend_hook(thread) __ON_HOOK_ARGS(rt_thread_suspend_hook, (thread))
#endif
#ifndef __on_rt_thread_resume_hook
#define __on_rt_thread_resume_hook(thread) __ON_HOOK_ARGS(rt_thread_resume_hook, (thread))
#endif
#ifndef __on_rt_timer_enter_hook
#define __on_rt_timer_enter_hook(t) __ON_HOOK_ARGS(rt_timer_enter_hook, (t))
#endif
#ifndef __on_rt_timer_exit_hook
#define __on_rt_timer_exit_hook(t) __ON_HOOK_ARGS(rt_timer_exit_hook, (t))
#endif
/**@}*/