2. Keyword arguments¶
Some functions in the MPS interface take keyword arguments in
order to pass values that might be optional, or are only required in
some circumstances. For example, the function
mps_arena_create_k() creates any class of arena, but
client arenas require you to specify a base address. These
arguments are passed in a keyword argument array, like this:
mps_res_t res;
mps_arena_t arena;
mps_arg_s args[3];
args[0].key = MPS_KEY_ARENA_SIZE;
args[0].val.size = 6553600;
args[1].key = MPS_KEY_ARENA_CL_BASE;
args[1].val.addr = base_address;
args[2].key = MPS_KEY_ARGS_END;
res = mps_arena_create_k(&arena, mps_arena_class_cl(), args);
Each keyword argument in the array is a structure of type
mps_arg_s.
For convenience and robustness, the MPS interface includes macros to help with forming keyword argument lists:
MPS_ARGS_BEGIN(args) {
MPS_ARGS_ADD(args, MPS_KEY_ARENA_SIZE, 6553600);
MPS_ARGS_ADD(args, MPS_KEY_ARENA_CL_BASE, base_address);
res = mps_arena_create_k(&arena, mps_arena_class_cl(), args);
} MPS_ARGS_END(args);
The argument array must not be NULL, and must end with
MPS_KEY_ARGS_END. If you don’t want to pass any arguments,
you can pass mps_args_none.
When a function that takes keyword arguments returns, the keyword
argument array has been modified to remove any arguments that have
been used. If all arguments have been used, the first element key is
now MPS_KEY_ARGS_END.
-
mps_arg_s¶ The type of the structure used to represent a single keyword argument to a function.
typedef struct mps_arg_s { mps_key_t key; union { /* many fields; see table below */ } val; } mps_arg_s;
keyidentifies the key. It must be one of the values listed in the documentation for the typemps_key_t.valis the corresponding value. This union contains many fields: one for each keyword argument type. The table given in the documentation formps_key_tbelow indicates which structure field is used by each keyword.Note
If you use the convenience macro
MPS_ARGS_ADD()then you don’t need to know the name of the field.
-
mps_args_none¶ An array of
mps_arg_srepresenting the empty list of keyword arguments. Equivalent to:mps_arg_s mps_args_none[] = {{MPS_KEY_ARGS_END}};
-
mps_key_t¶ The type of keyword argument keys. Must take one of the following values:
Keyword
Type & field in
arg.valSee
MPS_KEY_ARGS_ENDnone
see above
MPS_KEY_ALIGNmps_align_talignMPS_KEY_AMS_SUPPORT_AMBIGUOUSMPS_KEY_ARENA_CL_BASEmps_addr_taddrMPS_KEY_ARENA_SIZEsize_tsizeMPS_KEY_ARENA_GRAIN_SIZEsize_tsizeMPS_KEY_AWL_FIND_DEPENDENTvoid *(*)(void *)addr_methodMPS_KEY_CHAINmps_chain_tchainmps_class_amc(),mps_class_amcz(),mps_class_ams(),mps_class_awl(),mps_class_lo()MPS_KEY_EXTEND_BYsize_tsizemps_class_amc(),mps_class_amcz(),mps_class_mfs(),mps_class_mv(),mps_class_mvff()MPS_KEY_FMT_ALIGNmps_align_talignMPS_KEY_FMT_CLASSmps_fmt_class_tfmt_classMPS_KEY_FMT_FWDmps_fmt_fwd_tfmt_fwdMPS_KEY_FMT_HEADER_SIZEsize_tsizeMPS_KEY_FMT_ISFWDmps_fmt_isfwd_tfmt_isfwdMPS_KEY_FMT_PADmps_fmt_pad_tfmt_padMPS_KEY_FMT_SCANmps_fmt_scan_tfmt_scanMPS_KEY_FMT_SKIPmps_fmt_skip_tfmt_skipMPS_KEY_FORMATmps_fmt_tformatmps_class_amc(),mps_class_amcz(),mps_class_ams(),mps_class_awl(),mps_class_lo(),mps_class_snc()MPS_KEY_GENunsigneduMPS_KEY_INTERIORMPS_KEY_MAX_SIZEsize_tsizeMPS_KEY_MEAN_SIZEsize_tsizeMPS_KEY_MFS_UNIT_SIZEsize_tsizeMPS_KEY_MIN_SIZEsize_tsizeMPS_KEY_MVFF_ARENA_HIGHMPS_KEY_MVFF_FIRST_FITMPS_KEY_MVFF_SLOT_HIGHMPS_KEY_MVT_FRAG_LIMITmps_count_tcountMPS_KEY_MVT_RESERVE_DEPTHmps_count_tcountMPS_KEY_POOL_DEBUG_OPTIONSmps_pool_debug_options_s*pool_debug_optionsmps_class_ams_debug(),mps_class_mv_debug(),mps_class_mvff_debug()MPS_KEY_RANKmps_rank_trankMPS_KEY_SPAREdoubledMPS_KEY_VMW3_TOP_DOWN
-
MPS_ARGS_BEGIN(args)¶ Start construction of a list of keyword arguments. This macro must be used like this:
MPS_ARGS_BEGIN(args) { MPS_ARGS_ADD(args, MPS_KEY_ARENA_SIZE, 6553600); MPS_ARGS_ADD(args, MPS_KEY_ARENA_CL_BASE, base_address); res = mps_arena_create_k(&arena, mps_arena_class_cl(), args); } MPS_ARGS_END(args);
That is, you must call
MPS_ARGS_ADD()(orMPS_ARGS_ADD_FIELD()) zero or more times, and then pass the arguments to a function.argsis the name of the array that contains the keyword arguments. The array is stack-allocated, and exists betweenMPS_ARGS_BEGINandMPS_ARGS_END.It is safe to nest blocks created by
MPS_ARGS_BEGINandMPS_ARGS_END.
-
MPS_ARGS_ADD(mps_arg_s args[], mps_key_t key, value)¶ Add an argument to a list of keyword arguments. This macro must be used only between
MPS_ARGS_BEGINandMPS_ARGS_END.argsis the name of array that contains the keyword arguments. It must match the argument to the preceding call toMPS_ARGS_BEGIN().keyis the keyword identifying this argument. It must be one of the key names starting withMPS_KEY_that are listed in the table in the documentation formps_key_t.valueis the value for this argument.
-
MPS_ARGS_ADD_FIELD(mps_arg_s args[], mps_key_t key, field, value)¶ Add an argument to a list of keyword arguments. This macro must be used only between
MPS_ARGS_BEGINandMPS_ARGS_END.argsis the name of array that contains the keyword arguments. It must match the argument to the preceding call toMPS_ARGS_BEGIN().keyis the keyword identifying this argument.fieldis the name of the field in thevalunion in the structuremps_args_s.valueis the value for this argument.Note
You should prefer to use
MPS_ARGS_ADD(), because then you don’t need to look up the name of the field.
-
MPS_ARGS_END(args)¶ Finish using a list of keyword arguments whose construction was started by
MPS_ARGS_BEGIN().argsis the name of array that contains the keyword arguments. It must match the argument to the preceding call toMPS_ARGS_BEGIN().
-
MPS_ARGS_DONE(args)¶ Deprecated
starting with version 1.113.
Formerly this was used to finalize a list of keyword arguments before passing it to a function. It is no longer needed.