[PATCH] [CU][RFC] Correct a few API issues

# HG changeset patch # User Dan Smith <danms@us.ibm.com> # Date 1196194806 28800 # Node ID 256b237ba19f9e178719ed657f2b9a8f0d44d433 # Parent 64e4f6355b52e4b7bcc984ec557610a1fa9a9786 [CU][RFC] Correct a few API issues that I think need to be resolved before we snap a release. Some of the cu_get_* functions returned values instead of a CMPIrc's like everything else, so this changes that. Also, return const char *'s from the broker memory so that the callers don't need to free the result. This will require some work in libvirt-cim to make sure the new API is adhered to properly. If this is okay with people, I'll follow up with a patch to libvirt-cim to make those changes. Any other discussion about things that should change in the libcmpiutil API is welcomed. Changes from last time: - Fix cu_get_str_prop() to return a const broker string as well Patch set to make associated changes to libvirt-cim to follow... Signed-off-by: Dan Smith <danms@us.ibm.com> diff -r 64e4f6355b52 -r 256b237ba19f args_util.c --- a/args_util.c Tue Nov 27 06:33:45 2007 -0800 +++ b/args_util.c Tue Nov 27 12:20:06 2007 -0800 @@ -32,7 +32,9 @@ #define CU_WEAK_TYPES 1 -char *cu_get_str_path(const CMPIObjectPath *reference, const char *key) +CMPIrc cu_get_str_path(const CMPIObjectPath *reference, + const char *key, + const char **val) { CMPIData data; CMPIStatus s; @@ -42,18 +44,20 @@ char *cu_get_str_path(const CMPIObjectPa if ((s.rc != CMPI_RC_OK) || CMIsNullValue(data) || CMIsNullObject(data.value.string)) - return NULL; + return CMPI_RC_ERR_FAILED; value = CMGetCharPtr(data.value.string); if ((value == NULL) || (*value == '\0')) - return NULL; - - return strdup(value); -} - -int cu_get_u16_path(const CMPIObjectPath *reference, - const char *key, - uint16_t *target) + return CMPI_RC_ERR_TYPE_MISMATCH; + + *val = value; + + return CMPI_RC_OK; +} + +CMPIrc cu_get_u16_path(const CMPIObjectPath *reference, + const char *key, + uint16_t *target) { CMPIData data; CMPIStatus s; @@ -61,11 +65,11 @@ int cu_get_u16_path(const CMPIObjectPath data = CMGetKey(reference, key, &s); if ((s.rc != CMPI_RC_OK) || CMIsNullValue(data)) - return 0; + return CMPI_RC_ERR_FAILED; *target = data.value.uint16; - return 1; + return CMPI_RC_OK; } const char *cu_check_args(const CMPIArgs *args, const char **names) @@ -85,92 +89,101 @@ const char *cu_check_args(const CMPIArgs return NULL; } -char *cu_get_str_arg(const CMPIArgs *args, const char *name) -{ - CMPIData argdata; - char *argval; +CMPIrc cu_get_str_arg(const CMPIArgs *args, const char *name, const char **val) +{ + CMPIData argdata; CMPIStatus s; argdata = CMGetArg(args, name, &s); if ((s.rc != CMPI_RC_OK) || (CMIsNullValue(argdata))) - return NULL; + return CMPI_RC_ERR_INVALID_PARAMETER; if ((argdata.type != CMPI_string) || CMIsNullObject(argdata.value.string)) - return NULL; - - argval = strdup(CMGetCharPtr(argdata.value.string)); - - return argval; -} - -CMPIObjectPath *cu_get_ref_arg(const CMPIArgs *args, const char *name) + return CMPI_RC_ERR_TYPE_MISMATCH; + + *val = CMGetCharPtr(argdata.value.string); + + return CMPI_RC_OK; +} + +CMPIrc cu_get_ref_arg(const CMPIArgs *args, + const char *name, + CMPIObjectPath **op) { CMPIData argdata; CMPIStatus s; argdata = CMGetArg(args, name, &s); if ((s.rc != CMPI_RC_OK) || (CMIsNullValue(argdata))) - return NULL; + return CMPI_RC_ERR_INVALID_PARAMETER; if ((argdata.type != CMPI_ref) || CMIsNullObject(argdata.value.ref)) - return NULL; - - return argdata.value.ref; -} - -CMPIInstance *cu_get_inst_arg(const CMPIArgs *args, const char *name) -{ - CMPIData argdata; - CMPIStatus s; - - argdata = CMGetArg(args, name, &s); - if ((s.rc != CMPI_RC_OK) || (CMIsNullValue(argdata))) { - return NULL; - } + return CMPI_RC_ERR_TYPE_MISMATCH; + + *op = argdata.value.ref; + + return CMPI_RC_OK; +} + +CMPIrc cu_get_inst_arg(const CMPIArgs *args, + const char *name, + CMPIInstance **inst) +{ + CMPIData argdata; + CMPIStatus s; + + argdata = CMGetArg(args, name, &s); + if ((s.rc != CMPI_RC_OK) || (CMIsNullValue(argdata))) + return CMPI_RC_ERR_INVALID_PARAMETER; if ((argdata.type != CMPI_instance) || - CMIsNullObject(argdata.value.inst)) { - return NULL; - } - - return argdata.value.inst; -} - -CMPIArray *cu_get_array_arg(const CMPIArgs *args, const char *name) + CMIsNullObject(argdata.value.inst)) + CMPI_RC_ERR_TYPE_MISMATCH; + + *inst = argdata.value.inst; + + return CMPI_RC_OK; +} + +CMPIrc cu_get_array_arg(const CMPIArgs *args, + const char *name, + CMPIArray **array) { CMPIData argdata; CMPIStatus s; argdata = CMGetArg(args, name, &s); if ((s.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) - return NULL; + return CMPI_RC_ERR_INVALID_PARAMETER; if (!CMIsArray(argdata) || CMIsNullObject(argdata.value.array)) - return NULL; - - return argdata.value.array; -} - -int cu_get_u16_arg(const CMPIArgs *args, const char *name, uint16_t *target) + return CMPI_RC_ERR_TYPE_MISMATCH; + + *array = argdata.value.array; + + return CMPI_RC_OK; +} + +CMPIrc cu_get_u16_arg(const CMPIArgs *args, const char *name, uint16_t *target) { CMPIData argdata; CMPIStatus s; argdata = CMGetArg(args, name, &s); if ((s.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) - return 0; + return CMPI_RC_ERR_INVALID_PARAMETER; #ifdef CU_WEAK_TYPES if (!(argdata.type & CMPI_INTEGER)) #else if (argdata.type != CMPI_uint16) #endif - return 0; + return CMPI_RC_ERR_TYPE_MISMATCH; *target = argdata.value.uint16; - return 1; + return CMPI_RC_OK; } #define REQUIRE_PROPERTY_DEFINED(i, p, pv, s) \ @@ -182,7 +195,7 @@ int cu_get_u16_arg(const CMPIArgs *args, CMPIrc cu_get_str_prop(const CMPIInstance *inst, const char *prop, - char **target) + const char **target) { CMPIData value; CMPIStatus s; @@ -198,8 +211,7 @@ CMPIrc cu_get_str_prop(const CMPIInstanc if ((prop_val = CMGetCharPtr(value.value.string)) == NULL) return CMPI_RC_ERROR; - if ((*target = strdup(prop_val)) == NULL) - return CMPI_RC_ERROR; + *target = prop_val; return CMPI_RC_OK; } diff -r 64e4f6355b52 -r 256b237ba19f libcmpiutil.h --- a/libcmpiutil.h Tue Nov 27 06:33:45 2007 -0800 +++ b/libcmpiutil.h Tue Nov 27 12:20:06 2007 -0800 @@ -91,18 +91,22 @@ const char *cu_check_args(const CMPIArgs * * @param args The argument list to search * @param name The name of the argument - * @returns The string argument's value, or NULL if error (must be free()'d) - */ -char *cu_get_str_arg(const CMPIArgs *args, const char *name); + * @param val The value of the argument (must be free()'d) + * @returns CMPI_RC_OK if successful + */ +CMPIrc cu_get_str_arg(const CMPIArgs *args, const char *name, const char **val); /** * Get a reference argument * * @param args The argument list to search * @param name The name of the argument - * @returns The reference argument's value, or NULL if error - */ -CMPIObjectPath *cu_get_ref_arg(const CMPIArgs *args, const char *name); + * @param op The reference argument's value + * @returns CMPI_RC_OK if successful + */ +CMPIrc cu_get_ref_arg(const CMPIArgs *args, + const char *name, + CMPIObjectPath **op); /** * Get an instance argument @@ -111,7 +115,9 @@ CMPIObjectPath *cu_get_ref_arg(const CMP * @param name The name of the argument * @returns The instance argument's value, or NULL if error */ -CMPIInstance *cu_get_inst_arg(const CMPIArgs *args, const char *name); +CMPIrc cu_get_inst_arg(const CMPIArgs *args, + const char *name, + CMPIInstance **inst); /** * Get an array argument @@ -120,7 +126,9 @@ CMPIInstance *cu_get_inst_arg(const CMPI * @param name The name of the argument * @returns The array argument's value, or NULL if error */ -CMPIArray *cu_get_array_arg(const CMPIArgs *args, const char *name); +CMPIrc cu_get_array_arg(const CMPIArgs *args, + const char *name, + CMPIArray **array); /** * Get a uint16 argument @@ -128,18 +136,22 @@ CMPIArray *cu_get_array_arg(const CMPIAr * @param args The argument list to search * @param name The name of the argument * @param target The uint16_t to reflect the argument value - * @returns nonzero on success, zero otherwise - */ -int cu_get_u16_arg(const CMPIArgs *args, const char *name, uint16_t *target); + * @returns CMPI_RC_OK if successful + */ +CMPIrc cu_get_u16_arg(const CMPIArgs *args, const char *name, uint16_t *target); /** * Get a string component of an object path * - * @param reference The object path + * @param ref The object path * @param key The name of the component to return - * @returns The value of the component, or NULL if error (must be free()'d) - */ -char *cu_get_str_path(const CMPIObjectPath *reference, const char *key); + * @param val The value of the component, or NULL if error (must be + * free()'d) + * @returns CMPI_RC_OK on success + */ +CMPIrc cu_get_str_path(const CMPIObjectPath *ref, + const char *key, + const char **val); /** * Get a uint16 component of an object path @@ -147,11 +159,11 @@ char *cu_get_str_path(const CMPIObjectPa * @param reference The object path * @param key The name of the component to return * @param target A pointer to the uint16 to set - * @returns nonzero on success, zero otherwise - */ -int cu_get_u16_path(const CMPIObjectPath *reference, - const char *key, - uint16_t *target); + * @returns CMPI_RC_OK if successful + */ +CMPIrc cu_get_u16_path(const CMPIObjectPath *reference, + const char *key, + uint16_t *target); /* Forward declaration */ struct inst_list; @@ -202,7 +214,7 @@ unsigned int cu_return_instance_names(co */ CMPIrc cu_get_str_prop(const CMPIInstance *inst, const char *prop, - char **target); + const char **target); /** * Get a boolean property of an instance diff -r 64e4f6355b52 -r 256b237ba19f std_indication.c --- a/std_indication.c Tue Nov 27 06:33:45 2007 -0800 +++ b/std_indication.c Tue Nov 27 12:20:06 2007 -0800 @@ -49,7 +49,8 @@ static CMPIStatus raise(struct std_indic if (ctx->handler->raise_fn == NULL) return (CMPIStatus){CMPI_RC_OK, NULL}; - inst = cu_get_inst_arg(argsin, "Indication"); + if (cu_get_inst_arg(argsin, "Indication", &inst) != CMPI_RC_OK) + return (CMPIStatus){CMPI_RC_ERR_FAILED, NULL}; return ctx->handler->raise_fn(context, inst); }

# HG changeset patch # User Dan Smith <danms@us.ibm.com> # Date 1196194806 28800 # Node ID 256b237ba19f9e178719ed657f2b9a8f0d44d433 # Parent 64e4f6355b52e4b7bcc984ec557610a1fa9a9786 [CU][RFC] Correct a few API issues that I think need to be resolved before we snap a release.
Some of the cu_get_* functions returned values instead of a CMPIrc's like everything else, so this changes that. Also, return const char *'s from the broker memory so that the callers don't need to free the result.
This will require some work in libvirt-cim to make sure the new API is adhered to properly. If this is okay with people, I'll follow up with a patch to libvirt-cim to make those changes. Any other discussion about things that should change in the libcmpiutil API is welcomed.
Changes from last time: - Fix cu_get_str_prop() to return a const broker string as well
Patch set to make associated changes to libvirt-cim to follow...
Signed-off-by: Dan Smith <danms@us.ibm.com>
CMPIrc cu_get_str_prop(const CMPIInstance *inst, const char *prop, - char **target) + const char **target) { CMPIData value; CMPIStatus s; @@ -198,8 +211,7 @@ CMPIrc cu_get_str_prop(const CMPIInstanc if ((prop_val = CMGetCharPtr(value.value.string)) == NULL) return CMPI_RC_ERROR;
- if ((*target = strdup(prop_val)) == NULL) - return CMPI_RC_ERROR; + *target = prop_val;
return CMPI_RC_OK; } The "callers don't need to free the result" part was not immediately obvious to me so I want to make sure I understand it. My guess is that adding the const makes it so that callers can't modify the resulting string, which means that we can just hand them all pointers to the same string, so there is no need to strdup, so they don't have to free? Is
Dan Smith wrote: that close? -- -Jay

JG> My guess is that adding the const makes it so that callers can't JG> modify the resulting string, Right, which makes free() choke on it, thus indicating all the free()'s that I needed to remove in this case :) JG> which means that we can just hand them all pointers to the same JG> string, so there is no need to strdup, so they don't have to free? No. The const doesn't really have anything to do with not needing to free() it. The reason it doesn't need to be free()'d is because it's memory from a broker-managed object, which will be freed when the current provider returns back to the CIMOM. The same reason you don't need to free() a CMPIInstance or CMPIObjectPath. -- Dan Smith IBM Linux Technology Center Open Hypervisor Team email: danms@us.ibm.com
participants (2)
-
Dan Smith
-
Jay Gagnon