module-context.h revision c25356d5978632df6203437e1953bcb29e0c736f
cc77c1cfe8d072765f793474f49fecd897525466Stephan Bosch This is a bit complex to use, but it prevents using wrong module IDs
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen in module_contexts arrays.
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen The main structure is implemented like this:
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen struct STRUCT_NAME_module_register {
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen unsigned int id;
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen union STRUCT_NAME_module_context {
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen struct STRUCT_NAME_module_register *reg;
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen // it's allowed to have some structure here so it won't waste space.
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen // for example: struct STRUCT_NAME_vfuncs super;
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen struct STRUCT_NAME {
88b8c5db9f66bdbb70d274d8592947716f364c36Timo Sirainen ARRAY_DEFINE(module_contexts, union STRUCT_NAME_module_context *);
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen extern struct STRUCT_NAME_module_register STRUCT_NAME_module_register;
f86c599ead3602dc2d63a473cc13c1119978f538Timo Sirainen The usage in modules goes like:
f86c599ead3602dc2d63a473cc13c1119978f538Timo Sirainen static MODULE_CONTEXT_DEFINE(mymodule_STRUCT_NAME_module,
f86c599ead3602dc2d63a473cc13c1119978f538Timo Sirainen &STRUCT_NAME_module_register);
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen struct mymodule_STRUCT_NAME {
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen union STRUCT_NAME_module_context module_ctx;
f40e6ef5190f68c2fd0e94c0b001bdf7d160236aTimo Sirainen // module-specific data
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch struct mymodule_STRUCT_NAME *ctx = i_new(...);
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch MODULE_CONTEXT_SET(obj, mymodule_STRUCT_NAME_module, ctx);
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch struct mymodule_STRUCT_NAME *ctx =
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch MODULE_CONTEXT(obj, mymodule_STRUCT_NAME_module);
cc77c1cfe8d072765f793474f49fecd897525466Stephan Bosch COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE(OBJ_REGISTER(obj), (id_ctx).reg)
fbad2d10747ac76a29b3a9dcfb4e7e67236ab872Stephan Bosch (*((void **)array_idx_modifiable(&(obj)->module_contexts, \
} _name
struct _name { \
} _name
struct module_context_id {
unsigned int *module_id_register;
unsigned int module_id;
bool module_id_set;
} STMT_END