fix[mpp_singleton]: fix init order issue

Platform: General
Spec: all

Error case:
Constructors may be added after 65535(mpp_singleton_init)
ex: mpp_platform added after 65535 when mpp_soc is
    refactored to c

Signed-off-by: Hongjin Li <vic.hong@rock-chips.com>
Signed-off-by: xiaoxu.chen <xiaoxu.chen@rock-chips.com>
Change-Id: If736904beb0cd64a3e4ae3b20fd72e1198646ac7
This commit is contained in:
Hongjin Li
2025-05-17 10:40:03 +08:00
committed by Herman Chen
parent 63e1e88b0b
commit dee9bced95
4 changed files with 23 additions and 17 deletions

View File

@@ -313,7 +313,7 @@ void CONCAT_US(KMPP_OBJ_NAME, unregister)(void)
KMPP_OBJ_DBG_LOG("unregister leave\n");
}
MPP_SINGLETON(KMPP_OBJ_SGLN_ID, TO_STR(KMPP_OBJ_NAME), CONCAT_US(KMPP_OBJ_NAME, register), CONCAT_US(KMPP_OBJ_NAME, unregister));
MPP_SINGLETON(KMPP_OBJ_SGLN_ID, KMPP_OBJ_NAME, CONCAT_US(KMPP_OBJ_NAME, register), CONCAT_US(KMPP_OBJ_NAME, unregister));
rk_s32 CONCAT_US(KMPP_OBJ_NAME, size)(void)
{

View File

@@ -14,7 +14,8 @@
#include "kmpp_obj.h"
#define TO_STR(x) #x
#define _TO_STR(x) #x
#define TO_STR(x) _TO_STR(x)
/* concat by underscore */
#define CONCAT_US1(a) a

View File

@@ -42,26 +42,31 @@ typedef struct MppSingletonInfo_t {
void (*deinit)(void);
} MppSingletonInfo;
#define SNGL_TO_STR(x) #x
#define SNGL_TO_FUNC(x) __mpp_singleton_add_##x
/* warning: constructor priorities from 0 to 100 are reserved for the implementation */
#define SNGL_BASE_ID 101
#define MPP_SINGLETON(id, name, init, deinit) \
__attribute__((constructor)) \
static void __mpp_singleton_add(void) { \
/* increase id from base id to avoid compiler warning */ \
__attribute__((constructor(SNGL_BASE_ID + id))) \
static void SNGL_TO_FUNC(name)(void) { \
MppSingletonInfo info = { \
id, \
name, \
SNGL_TO_STR(name), \
init, \
deinit, \
}; \
mpp_singleton_add(&info); \
mpp_singleton_add(&info, __FUNCTION__); \
}
#ifdef __cplusplus
extern "C" {
#endif
rk_s32 mpp_singleton_add(MppSingletonInfo *info);
rk_s32 mpp_singleton_add(MppSingletonInfo *info, const char *caller);
#ifdef __cplusplus
}
#endif
#endif /* __MPP_SINGLETON_H__ */
#endif /* __MPP_SINGLETON_H__ */

View File

@@ -21,29 +21,29 @@ static MppSingletonInfo sgln_info[MPP_SGLN_MAX_CNT] = {0};
static rk_u64 sgln_mask = 0;
static rk_u32 sgln_debug = 0;
rk_s32 mpp_singleton_add(MppSingletonInfo *info)
rk_s32 mpp_singleton_add(MppSingletonInfo *info, const char *caller)
{
mpp_env_get_u32("mpp_sgln_debug", &sgln_debug, 0);
if (!info) {
sgln_dbg("can not add NULL info\n");
sgln_dbg("can not add NULL info at %s\n", caller);
return rk_nok;
}
if (info->id >= MPP_SGLN_MAX_CNT) {
sgln_dbg("id %d larger than max %d\n", info->id, MPP_SGLN_MAX_CNT);
sgln_dbg("id %d larger than max %d at %s\n", info->id, MPP_SGLN_MAX_CNT, caller);
return rk_nok;
}
if (sgln_mask & (1 << info->id)) {
sgln_dbg("info %d has been registered\n", info->id);
sgln_dbg("info %d has been registered at %s\n", info->id, caller);
return rk_nok;
}
sgln_info[info->id] = *info;
sgln_mask |= (1 << info->id);
sgln_dbg("info %d %s registered\n", info->id, info->name);
sgln_dbg("info %2d %-12s registered at %s\n", info->id, info->name, caller);
return rk_ok;
}
@@ -60,9 +60,9 @@ static void mpp_singleton_deinit(void)
MppSingletonInfo *info = &sgln_info[i];
if (info->deinit) {
sgln_dbg("info %d %s deinit start\n", info->id, info->name);
sgln_dbg("info %2d %-12s deinit start\n", info->id, info->name);
info->deinit();
sgln_dbg("info %d %s deinit finish\n", info->id, info->name);
sgln_dbg("info %2d %-12s deinit finish\n", info->id, info->name);
}
}
}
@@ -84,9 +84,9 @@ __attribute__((constructor(65535))) static void mpp_singleton_init(void)
MppSingletonInfo *info = &sgln_info[i];
if (info->init) {
sgln_dbg("info %d %s init start\n", info->id, info->name);
sgln_dbg("info %2d %-12s init start\n", info->id, info->name);
info->init();
sgln_dbg("info %d %s init finish\n", info->id, info->name);
sgln_dbg("info %2d %-12s init finish\n", info->id, info->name);
}
}
}