From mboxrd@z Thu Jan 1 00:00:00 1970 From: Vincent Hanquez Subject: [PATCH 2/3] remove some duplicated code by having helper add/del functions Date: Tue, 22 Jun 2010 10:26:57 +0100 Message-ID: <1277198818-27090-3-git-send-email-vincent.hanquez@eu.citrix.com> References: <1277198818-27090-1-git-send-email-vincent.hanquez@eu.citrix.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------1.7.1" Return-path: In-Reply-To: <1277198818-27090-1-git-send-email-vincent.hanquez@eu.citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Xen Devel Cc: Vincent Hanquez List-Id: xen-devel@lists.xenproject.org --------------1.7.1 Content-Type: text/plain; charset="UTF-8"; format=fixed Content-Transfer-Encoding: quoted-printable Signed-off-by: Vincent Hanquez --- tools/libxc/xc_flask.c | 227 +++++++++---------------------------------= ----- 1 files changed, 44 insertions(+), 183 deletions(-) --------------1.7.1 Content-Type: text/x-patch; name="0002-remove-some-duplicated-code-by-having-helper-add-del.patch" Content-Disposition: attachment; filename="0002-remove-some-duplicated-code-by-having-helper-add-del.patch" Content-Transfer-Encoding: quoted-printable diff --git a/tools/libxc/xc_flask.c b/tools/libxc/xc_flask.c index 3b733e5..12cb001 100644 --- a/tools/libxc/xc_flask.c +++ b/tools/libxc/xc_flask.c @@ -141,237 +141,98 @@ int xc_flask_setenforce(xc_interface *xc_handle, i= nt mode) return 0; } =20 -int xc_flask_add_pirq(xc_interface *xc_handle, unsigned int pirq, char *= scontext) +static int xc_flask_add(xc_interface *xc_handle, char *cat, char *arg, c= har *scontext) { - int err; + char buf[512]; flask_op_t op; - char *buf; - char *pirq_s =3D OCON_PIRQ_STR; - int size =3D INITCONTEXTLEN + strlen(pirq_s) + (sizeof(unsigned int)= ) + - (sizeof(char) * 3); - - if ( (buf =3D (char *) malloc(size)) =3D=3D NULL ) - return -ENOMEM; - memset(buf, 0, size); =20 + memset(buf, 0, 512); + snprintf(buf, 512, "%s %255s %s", cat, scontext, arg); op.cmd =3D FLASK_ADD_OCONTEXT; - snprintf(buf, size, "%s %255s %u", pirq_s, scontext, pirq); op.buf =3D buf; - op.size =3D size; - - if ( (err =3D xc_flask_op(xc_handle, &op)) !=3D 0 ) - { - free(buf); - return err; - } + op.size =3D 512; + =20 + return xc_flask_op(xc_handle, &op); +} =20 - free(buf); - return 0; +int xc_flask_add_pirq(xc_interface *xc_handle, unsigned int pirq, char *= scontext) +{ + char arg[16]; =20 + snprintf(arg, 16, "%u", pirq); + return xc_flask_add(xc_handle, OCON_PIRQ_STR, arg, scontext); } =20 int xc_flask_add_ioport(xc_interface *xc_handle, unsigned long low, unsi= gned long high, char *scontext) { - int err; - flask_op_t op; - char *buf; - char *ioport =3D OCON_IOPORT_STR; - int size =3D INITCONTEXTLEN + strlen(ioport) + - (sizeof(unsigned long) * 2) + (sizeof(char) * 4); - - if ( (buf =3D (char *) malloc(size)) =3D=3D NULL ) - return -ENOMEM; - memset(buf, 0, size); - - op.cmd =3D FLASK_ADD_OCONTEXT; - snprintf(buf, size, "%s %255s %lu %lu", ioport, scontext, low, high)= ; - op.buf =3D buf; - op.size =3D size; - - if ( (err =3D xc_flask_op(xc_handle, &op)) !=3D 0 ) - { - free(buf); - return err; - } - - free(buf); - return 0; + char arg[64]; =20 + snprintf(arg, 64, "%lu %lu", low, high); + return xc_flask_add(xc_handle, OCON_IOPORT_STR, arg, scontext); } =20 int xc_flask_add_iomem(xc_interface *xc_handle, unsigned long low, unsig= ned long high, char *scontext) { - int err; - flask_op_t op; - char *buf; - char *iomem =3D OCON_IOMEM_STR; - int size =3D INITCONTEXTLEN + strlen(iomem) + - (sizeof(unsigned long) * 2) + (sizeof(char) * 4); - - if ( (buf =3D (char *) malloc(size)) =3D=3D NULL ) - return -ENOMEM; - memset(buf, 0, size); - - op.cmd =3D FLASK_ADD_OCONTEXT; - snprintf(buf, size, "%s %255s %lu %lu", iomem, scontext, low, high); - op.buf =3D buf; - op.size =3D size; - - if ( (err =3D xc_flask_op(xc_handle, &op)) !=3D 0 ) - { - free(buf); - return err; - } - - free(buf); - return 0; + char arg[64]; =20 + snprintf(arg, 64, "%lu %lu", low, high); + return xc_flask_add(xc_handle, OCON_IOMEM_STR, arg, scontext); } =20 int xc_flask_add_device(xc_interface *xc_handle, unsigned long device, c= har *scontext) { - int err; - flask_op_t op; - char *buf; - char *dev =3D OCON_DEVICE_STR; - int size =3D INITCONTEXTLEN + strlen(dev) + (sizeof(unsigned long)) = + - (sizeof(char) * 3); - - if ( (buf =3D (char *) malloc(size)) =3D=3D NULL ) - return -ENOMEM; - memset(buf, 0, size); - - op.cmd =3D FLASK_ADD_OCONTEXT; - snprintf(buf, size, "%s %255s %lu", dev, scontext, device); - op.buf =3D buf; - op.size =3D size; - - if ( (err =3D xc_flask_op(xc_handle, &op)) !=3D 0 ) - { - free(buf); - return err; - } - - free(buf); - return 0; + char arg[32]; =20 + snprintf(arg, 32, "%lu", device); + return xc_flask_add(xc_handle, OCON_DEVICE_STR, arg, scontext); } =20 -int xc_flask_del_pirq(xc_interface *xc_handle, unsigned int pirq) +static int xc_flask_del(xc_interface *xc_handle, char *cat, char *arg) { - int err; + char buf[256]; flask_op_t op; - char *buf; - char *pirq_s =3D OCON_PIRQ_STR; - int size =3D strlen(pirq_s) + (sizeof(unsigned int)) + - (sizeof(char) * 2); - - if ( (buf =3D (char *) malloc(size)) =3D=3D NULL ) - return -ENOMEM; - memset(buf, 0, size); =20 + memset(buf, 0, 256); + snprintf(buf, 256, "%s %s", cat, arg); op.cmd =3D FLASK_DEL_OCONTEXT; - snprintf(buf, size, "%s %u", pirq_s, pirq); op.buf =3D buf; - op.size =3D size; - - if ( (err =3D xc_flask_op(xc_handle, &op)) !=3D 0 ) - { - free(buf); - return err; - } + op.size =3D 256; + =20 + return xc_flask_op(xc_handle, &op); +} =20 - free(buf); - return 0; +int xc_flask_del_pirq(xc_interface *xc_handle, unsigned int pirq) +{ + char arg[16]; =20 + snprintf(arg, 16, "%u", pirq); + return xc_flask_del(xc_handle, OCON_PIRQ_STR, arg); } =20 int xc_flask_del_ioport(xc_interface *xc_handle, unsigned long low, unsi= gned long high) { - int err; - flask_op_t op; - char *buf; - char *ioport =3D OCON_IOPORT_STR; - int size =3D strlen(ioport) + (sizeof(unsigned long) * 2) + - (sizeof(char) * 3); - - if ( (buf =3D (char *) malloc(size)) =3D=3D NULL ) - return -ENOMEM; - memset(buf, 0, size); - - op.cmd =3D FLASK_DEL_OCONTEXT; - snprintf(buf, size, "%s %lu %lu", ioport, low, high); - op.buf =3D buf; - op.size =3D size; - - if ( (err =3D xc_flask_op(xc_handle, &op)) !=3D 0 ) - { - free(buf); - return err; - } - - free(buf); - return 0; + char arg[64]; =20 + snprintf(arg, 64, "%lu %lu", low, high); + return xc_flask_del(xc_handle, OCON_IOPORT_STR, arg); } =20 int xc_flask_del_iomem(xc_interface *xc_handle, unsigned long low, unsig= ned long high) { - int err; - flask_op_t op; - char *buf; - char *iomem =3D OCON_IOMEM_STR; - int size =3D strlen(iomem) + (sizeof(unsigned long) * 2) + - (sizeof(char) * 3); - - if ( (buf =3D (char *) malloc(size)) =3D=3D NULL ) - return -ENOMEM; - memset(buf, 0, size); - - op.cmd =3D FLASK_DEL_OCONTEXT; - snprintf(buf, size, "%s %lu %lu", iomem, low, high); - op.buf =3D buf; - op.size =3D size; - - if ( (err =3D xc_flask_op(xc_handle, &op)) !=3D 0 ) - { - free(buf); - return err; - } - - free(buf); - return 0; + char arg[64]; =20 + snprintf(arg, 64, "%lu %lu", low, high); + return xc_flask_del(xc_handle, OCON_IOMEM_STR, arg); } =20 int xc_flask_del_device(xc_interface *xc_handle, unsigned long device) { - int err; - flask_op_t op; - char *buf; - char *dev =3D OCON_DEVICE_STR; - int size =3D strlen(dev) + (sizeof(unsigned long)) + (sizeof(char) *= 2); - - if ( (buf =3D (char *) malloc(size)) =3D=3D NULL ) - return -ENOMEM; - memset(buf, 0, size); - - op.cmd =3D FLASK_DEL_OCONTEXT; - snprintf(buf, size, "%s %lu", dev, device); - op.buf =3D buf; - op.size =3D size; - - if ( (err =3D xc_flask_op(xc_handle, &op)) !=3D 0 ) - { - free(buf); - return err; - } - - free(buf); - return 0; + char arg[32]; =20 + snprintf(arg, 32, "%lu", device); + return xc_flask_del(xc_handle, OCON_DEVICE_STR, arg); } =20 int xc_flask_access(xc_interface *xc_handle, const char *scon, const cha= r *tcon, --------------1.7.1 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --------------1.7.1--