Skip to content

Commit b627414

Browse files
GorgonMeducerBernardXiong
authored andcommitted
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
1 parent 5e24acf commit b627414

File tree

1 file changed

+68
-4
lines changed

1 file changed

+68
-4
lines changed

include/rtdef.h

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* 2021-05-10 armink change version number to v4.0.4
3939
* 2021-11-19 Meco Man change version number to v4.1.0
4040
* 2021-12-21 Meco Man re-implement RT_UNUSED
41+
* 2022-01-01 Gabriel improve hooking method
4142
*/
4243

4344
#ifndef __RT_DEF_H__
@@ -437,11 +438,74 @@ struct rt_object_information
437438
/**
438439
* The hook function call macro
439440
*/
440-
#ifdef RT_USING_HOOK
441-
#define RT_OBJECT_HOOK_CALL(func, argv) \
442-
do { if ((func) != RT_NULL) func argv; } while (0)
441+
#ifndef RT_USING_HOOK
442+
#define __ON_HOOK_ARGS(__hook, argv)
443+
#define RT_OBJECT_HOOK_CALL(func, argv)
443444
#else
444-
#define RT_OBJECT_HOOK_CALL(func, argv)
445+
#define RT_OBJECT_HOOK_CALL(func, argv) __on_##func argv
446+
#ifdef RT_HOOK_USING_FUNC_PTR
447+
#define __ON_HOOK_ARGS(__hook, argv) do {if ((__hook) != RT_NULL) __hook argv; } while (0)
448+
#else
449+
#define __ON_HOOK_ARGS(__hook, argv)
450+
#endif
451+
#endif
452+
453+
#ifndef __on_rt_interrupt_enter_hook
454+
#define __on_rt_interrupt_enter_hook() __ON_HOOK_ARGS(rt_interrupt_enter_hook, ())
455+
#endif
456+
#ifndef __on_rt_interrupt_leave_hook
457+
#define __on_rt_interrupt_leave_hook() __ON_HOOK_ARGS(rt_interrupt_leave_hook, ())
458+
#endif
459+
#ifndef __on_rt_interrupt_switch_hook
460+
#define __on_rt_interrupt_switch_hook() __ON_HOOK_ARGS(rt_interrupt_switch_hook, ())
461+
#endif
462+
#ifndef __on_rt_malloc_hook
463+
#define __on_rt_malloc_hook(addr, size) __ON_HOOK_ARGS(rt_malloc_hook, (addr, size))
464+
#endif
465+
#ifndef __on_rt_free_hook
466+
#define __on_rt_free_hook(rmem) __ON_HOOK_ARGS(rt_free_hook, (rmem))
467+
#endif
468+
#ifndef __on_rt_mp_alloc_hook
469+
#define __on_rt_mp_alloc_hook(mp, block) __ON_HOOK_ARGS(rt_mp_alloc_hook, (mp, block))
470+
#endif
471+
#ifndef __on_rt_mp_free_hook
472+
#define __on_rt_mp_free_hook(mp, block) __ON_HOOK_ARGS(rt_mp_free_hook, (mp, block))
473+
#endif
474+
#ifndef __on_rt_object_trytake_hook
475+
#define __on_rt_object_trytake_hook(parent) __ON_HOOK_ARGS(rt_object_trytake_hook, (parent))
476+
#endif
477+
#ifndef __on_rt_object_take_hook
478+
#define __on_rt_object_take_hook(parent) __ON_HOOK_ARGS(rt_object_take_hook, (parent))
479+
#endif
480+
#ifndef __on_rt_object_put_hook
481+
#define __on_rt_object_put_hook(parent) __ON_HOOK_ARGS(rt_object_put_hook, (parent))
482+
#endif
483+
#ifndef __on_rt_scheduler_hook
484+
#define __on_rt_scheduler_hook(from, to) __ON_HOOK_ARGS(rt_scheduler_hook, (from, to))
485+
#endif
486+
#ifndef __on_rt_scheduler_switch_hook
487+
#define __on_rt_scheduler_switch_hook(tid) __ON_HOOK_ARGS(rt_scheduler_switch_hook, (tid))
488+
#endif
489+
#ifndef __on_rt_object_attach_hook
490+
#define __on_rt_object_attach_hook(obj) __ON_HOOK_ARGS(rt_object_attach_hook, (obj))
491+
#endif
492+
#ifndef __on_rt_object_detach_hook
493+
#define __on_rt_object_detach_hook(obj) __ON_HOOK_ARGS(rt_object_detach_hook, (obj))
494+
#endif
495+
#ifndef __on_rt_thread_inited_hook
496+
#define __on_rt_thread_inited_hook(thread) __ON_HOOK_ARGS(rt_thread_inited_hook, (thread))
497+
#endif
498+
#ifndef __on_rt_thread_suspend_hook
499+
#define __on_rt_thread_suspend_hook(thread) __ON_HOOK_ARGS(rt_thread_suspend_hook, (thread))
500+
#endif
501+
#ifndef __on_rt_thread_resume_hook
502+
#define __on_rt_thread_resume_hook(thread) __ON_HOOK_ARGS(rt_thread_resume_hook, (thread))
503+
#endif
504+
#ifndef __on_rt_timer_enter_hook
505+
#define __on_rt_timer_enter_hook(t) __ON_HOOK_ARGS(rt_timer_enter_hook, (t))
506+
#endif
507+
#ifndef __on_rt_timer_exit_hook
508+
#define __on_rt_timer_exit_hook(t) __ON_HOOK_ARGS(rt_timer_exit_hook, (t))
445509
#endif
446510

447511
/**@}*/

0 commit comments

Comments
 (0)