From patchwork Thu Dec 17 16:44:54 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Molton X-Patchwork-Id: 41332 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 6DF36B6F05 for ; Fri, 18 Dec 2009 03:52:57 +1100 (EST) Received: from localhost ([127.0.0.1]:48725 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NLJaw-0001ep-56 for incoming@patchwork.ozlabs.org; Thu, 17 Dec 2009 11:52:54 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NLJVG-00076I-JD for qemu-devel@nongnu.org; Thu, 17 Dec 2009 11:47:02 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NLJVF-00075P-7K for qemu-devel@nongnu.org; Thu, 17 Dec 2009 11:47:02 -0500 Received: from [199.232.76.173] (port=44961 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NLJVE-00074v-Qm for qemu-devel@nongnu.org; Thu, 17 Dec 2009 11:47:00 -0500 Received: from bhuna.collabora.co.uk ([93.93.128.226]:33983) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1NLJV9-0000G0-K4 for qemu-devel@nongnu.org; Thu, 17 Dec 2009 11:46:56 -0500 Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: ian) with ESMTPSA id 621086017EF From: Ian Molton To: qemu-devel@nongnu.org Date: Thu, 17 Dec 2009 16:44:54 +0000 Message-Id: <1261068295-25831-4-git-send-email-ian.molton@collabora.co.uk> X-Mailer: git-send-email 1.6.5.4 In-Reply-To: <1261068295-25831-3-git-send-email-ian.molton@collabora.co.uk> References: <1261068295-25831-1-git-send-email-ian.molton@collabora.co.uk> <1261068295-25831-2-git-send-email-ian.molton@collabora.co.uk> <1261068295-25831-3-git-send-email-ian.molton@collabora.co.uk> X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2) Cc: Ian Molton Subject: [Qemu-devel] [PATCH 3/4] Add SIZE type to qdev properties X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This patch adds a 'SIZE' type property to those available to qdevs. It is the analogue of the OPT_SIZE property for options. Signed-off-by: Ian Molton --- hw/qdev-properties.c | 34 ++++++++++++++++++++++++++++++++++ hw/qdev.h | 4 ++++ parse_common.h | 40 ++++++++++++++++++++++++++++++++++++++++ qemu-option.c | 23 +++-------------------- 4 files changed, 81 insertions(+), 20 deletions(-) create mode 100644 parse_common.h diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c index fb07279..44d14ef 100644 --- a/hw/qdev-properties.c +++ b/hw/qdev-properties.c @@ -1,6 +1,7 @@ #include "sysemu.h" #include "net.h" #include "qdev.h" +#include "parse_common.h" void *qdev_get_prop_ptr(DeviceState *dev, Property *prop) { @@ -194,6 +195,39 @@ PropertyInfo qdev_prop_hex64 = { .print = print_hex64, }; +/* --- 64bit unsigned int 'size' type --- */ + +static int parse_size(DeviceState *dev, Property *prop, const char *str) +{ + uint64_t *ptr = qdev_get_prop_ptr(dev, prop); + + if(str != NULL) + return parse_common_size(str, ptr); + + return -1; +} + +static int print_size(DeviceState *dev, Property *prop, char *dest, size_t len) +{ + uint64_t *ptr = qdev_get_prop_ptr(dev, prop); + char suffixes[] = {'T', 'G', 'M', 'K', 'B'}; + int i; + uint64_t div; + + for(div = (long int)1 << 40 ; + !(*ptr / div) ; div >>= 10, i++); + + return snprintf(dest, len, "%0.03f%c", (double)*ptr/div, suffixes[i]); +} + +PropertyInfo qdev_prop_size = { + .name = "size", + .type = PROP_TYPE_SIZE, + .size = sizeof(uint64_t), + .parse = parse_size, + .print = print_size, +}; + /* --- string --- */ static int parse_string(DeviceState *dev, Property *prop, const char *str) diff --git a/hw/qdev.h b/hw/qdev.h index bbcdba1..0f985f5 100644 --- a/hw/qdev.h +++ b/hw/qdev.h @@ -74,6 +74,7 @@ enum PropertyType { PROP_TYPE_UINT32, PROP_TYPE_INT32, PROP_TYPE_UINT64, + PROP_TYPE_SIZE, PROP_TYPE_TADDR, PROP_TYPE_MACADDR, PROP_TYPE_DRIVE, @@ -180,6 +181,7 @@ extern PropertyInfo qdev_prop_int32; extern PropertyInfo qdev_prop_uint64; extern PropertyInfo qdev_prop_hex32; extern PropertyInfo qdev_prop_hex64; +extern PropertyInfo qdev_prop_size; extern PropertyInfo qdev_prop_string; extern PropertyInfo qdev_prop_chr; extern PropertyInfo qdev_prop_ptr; @@ -217,6 +219,8 @@ extern PropertyInfo qdev_prop_pci_devfn; DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t) #define DEFINE_PROP_HEX64(_n, _s, _f, _d) \ DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t) +#define DEFINE_PROP_SIZE(_n, _s, _f, _d) \ + DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_size, uint64_t) #define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d) \ DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, uint32_t) diff --git a/parse_common.h b/parse_common.h new file mode 100644 index 0000000..93b1fc2 --- /dev/null +++ b/parse_common.h @@ -0,0 +1,40 @@ +/* + * Common parsing routines + * + * Copyright Collabora 2009 + * + * Author: + * Ian Molton + * + * Derived from the parser in qemu-option.c + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + * + */ + +static inline int parse_common_size(const char *value, uint64_t *ptr) { + char *postfix; + double sizef; + + sizef = strtod(value, &postfix); + switch (*postfix) { + case 'T': + sizef *= 1024; + case 'G': + sizef *= 1024; + case 'M': + sizef *= 1024; + case 'K': + case 'k': + sizef *= 1024; + case 'B': + case 'b': + case '\0': + *ptr = (uint64_t) sizef; + return 0; + } + + return -1; +} + diff --git a/qemu-option.c b/qemu-option.c index 24392fc..91772c5 100644 --- a/qemu-option.c +++ b/qemu-option.c @@ -28,6 +28,7 @@ #include "qemu-common.h" #include "qemu-option.h" +#include "parse_common.h" /* * Extracts the name of an option from the parameter string (p points at the @@ -203,28 +204,10 @@ static int parse_option_number(const char *name, const char *value, uint64_t *re static int parse_option_size(const char *name, const char *value, uint64_t *ret) { - char *postfix; - double sizef; - if (value != NULL) { - sizef = strtod(value, &postfix); - switch (*postfix) { - case 'T': - sizef *= 1024; - case 'G': - sizef *= 1024; - case 'M': - sizef *= 1024; - case 'K': - case 'k': - sizef *= 1024; - case 'b': - case '\0': - *ret = (uint64_t) sizef; - break; - default: + if(parse_common_size(value, ret) == -1) { fprintf(stderr, "Option '%s' needs size as parameter\n", name); - fprintf(stderr, "You may use k, M, G or T suffixes for " + fprintf(stderr, "You may use B, K, M, G or T suffixes for bytes " "kilobytes, megabytes, gigabytes and terabytes.\n"); return -1; }