qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Alexander Graf <agraf@suse.de>
To: Alexey Kardashevskiy <aik@ozlabs.ru>, qemu-devel@nongnu.org
Cc: qemu-ppc@nongnu.org
Subject: Re: [Qemu-devel] [PATCH 1/2] vmstate: Add helper to enable GHashTable migration
Date: Thu, 22 May 2014 12:57:31 +0200	[thread overview]
Message-ID: <537DD81B.4080007@suse.de> (raw)
In-Reply-To: <1400756006-26297-2-git-send-email-aik@ozlabs.ru>


On 22.05.14 12:53, Alexey Kardashevskiy wrote:
> This adds a VMSTATE_HASH_V macro. This implements put/get callbacks for it.
> This implements a qemu_hash_init() wrapper to save key/value sizes.
>
> Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
> ---
>   include/migration/vmstate.h | 10 +++++++++
>   include/qemu-common.h       | 13 +++++++++++
>   vmstate.c                   | 54 +++++++++++++++++++++++++++++++++++++++++++++
>   3 files changed, 77 insertions(+)
>
> diff --git a/include/migration/vmstate.h b/include/migration/vmstate.h
> index 7e45048..6af599d 100644
> --- a/include/migration/vmstate.h
> +++ b/include/migration/vmstate.h
> @@ -166,6 +166,7 @@ extern const VMStateInfo vmstate_info_timer;
>   extern const VMStateInfo vmstate_info_buffer;
>   extern const VMStateInfo vmstate_info_unused_buffer;
>   extern const VMStateInfo vmstate_info_bitmap;
> +static const VMStateInfo vmstate_info_hash;
>   
>   #define type_check_2darray(t1,t2,n,m) ((t1(*)[n][m])0 - (t2*)0)
>   #define type_check_array(t1,t2,n) ((t1(*)[n])0 - (t2*)0)
> @@ -740,6 +741,15 @@ extern const VMStateInfo vmstate_info_bitmap;
>   #define VMSTATE_BUFFER_UNSAFE(_field, _state, _version, _size)        \
>       VMSTATE_BUFFER_UNSAFE_INFO(_field, _state, _version, vmstate_info_buffer, _size)
>   
> +#define VMSTATE_HASH_V(_field, _state, _version) {                      \
> +    .name       = (stringify(_field)),                                  \
> +    .version_id = (_version),                                           \
> +    .size       = sizeof(qemu_hash),                                    \
> +    .info       = &vmstate_info_hash,                                   \
> +    .flags      = VMS_SINGLE,                                           \
> +    .offset     = vmstate_offset_value(_state, _field, qemu_hash),      \
> +}
> +
>   #define VMSTATE_UNUSED_V(_v, _size)                                   \
>       VMSTATE_UNUSED_BUFFER(NULL, _v, _size)
>   
> diff --git a/include/qemu-common.h b/include/qemu-common.h
> index 3f3fd60..ee973e7 100644
> --- a/include/qemu-common.h
> +++ b/include/qemu-common.h
> @@ -462,4 +462,17 @@ int parse_debug_env(const char *name, int max, int initial);
>   
>   const char *qemu_ether_ntoa(const MACAddr *mac);
>   
> +typedef struct qemu_hash {
> +    GHashTable *hash;
> +    int key_size;
> +    int value_size;
> +} qemu_hash;
> +
> +static inline void qemu_hash_init(qemu_hash *h, int key_size, int value_size)
> +{
> +    h->key_size = key_size;
> +    h->value_size = value_size;
> +    h->hash = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
> +}
> +
>   #endif
> diff --git a/vmstate.c b/vmstate.c
> index b5882fa..2148e73 100644
> --- a/vmstate.c
> +++ b/vmstate.c
> @@ -667,3 +667,57 @@ const VMStateInfo vmstate_info_bitmap = {
>       .get = get_bitmap,
>       .put = put_bitmap,
>   };
> +
> +/* Save restore for qemu_hash which is a wrapper over GHashTable */
> +static int get_hash(QEMUFile *f, void *pv, size_t size)
> +{
> +    qemu_hash *h = pv;
> +    uint32_t num = g_hash_table_size(h->hash);
> +    gpointer key, value;
> +
> +    num = qemu_get_be32(f);
> +
> +    for ( ; num; --num) {
> +        int i;
> +
> +        key = g_malloc0(h->key_size);
> +        for (i = 0; i < h->key_size; ++i) {
> +            ((uint8_t *)key)[i] = qemu_get_byte(f);
> +        }
> +        value = g_malloc0(h->value_size);
> +        for (i = 0; i < h->value_size; ++i) {
> +            ((uint8_t *)value)[i] = qemu_get_byte(f);
> +        }
> +        g_hash_table_insert(h->hash, key, value);
> +    }
> +
> +    return 0;
> +}
> +
> +static void put_hash(QEMUFile *f, void *pv, size_t size)
> +{
> +    qemu_hash *h = pv;
> +    uint32_t num = g_hash_table_size(h->hash);
> +    GHashTableIter iter;
> +    gpointer key, value;
> +
> +    qemu_put_be32(f, num);
> +
> +    g_hash_table_iter_init(&iter, h->hash);
> +    while (g_hash_table_iter_next (&iter, &key, &value)) {
> +        int i;
> +
> +        for (i = 0; i < h->key_size; ++i) {
> +            qemu_put_byte(f, ((uint8_t *)key)[i]);
> +        }
> +        for (i = 0; i < h->value_size; ++i) {
> +            qemu_put_byte(f, ((uint8_t *)value)[i]);

I think the only way to do this safely would be to do a recursive 
introspective walk through the hash table's key and value properties. 
Every key and every property can be an object again, right?

We would basically impose a limit onto ourselves that every member of 
the hash table would have to be a GObject again.


Alex

  reply	other threads:[~2014-05-22 10:57 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-15  9:59 [Qemu-devel] [PATCH v2 0/8] Move interrupts from spapr to xics Alexey Kardashevskiy
2014-05-15  9:59 ` [Qemu-devel] [PATCH v2 1/8] xics: Add flags for interrupts Alexey Kardashevskiy
2014-05-21  6:30   ` Alexey Kardashevskiy
2014-05-21  8:40     ` Alexander Graf
2014-05-15  9:59 ` [Qemu-devel] [PATCH v2 2/8] xics: Add xics_find_source() Alexey Kardashevskiy
2014-05-15  9:59 ` [Qemu-devel] [PATCH v2 3/8] xics: Disable flags reset on xics reset Alexey Kardashevskiy
2014-05-15  9:59 ` [Qemu-devel] [PATCH v2 4/8] spapr: Move interrupt allocator to xics Alexey Kardashevskiy
2014-05-21  8:34   ` Alexander Graf
2014-05-21  8:46     ` Alexey Kardashevskiy
2014-05-21  8:47       ` Alexander Graf
2014-05-15  9:59 ` [Qemu-devel] [PATCH v2 5/8] xics: Remove obsolete xics_set_irq_type() Alexey Kardashevskiy
2014-05-15  9:59 ` [Qemu-devel] [PATCH v2 6/8] spapr: Remove @next_irq Alexey Kardashevskiy
2014-05-15  9:59 ` [Qemu-devel] [PATCH v2 7/8] xics: Implement xics_ics_free() Alexey Kardashevskiy
2014-05-15  9:59 ` [Qemu-devel] [PATCH v2 8/8] spapr_pci: Use XICS interrupt allocator and do not cache interrupts in PHB Alexey Kardashevskiy
2014-05-21  8:40   ` Alexander Graf
2014-05-21  8:52     ` Alexey Kardashevskiy
2014-05-21  9:06       ` Alexander Graf
2014-05-21  9:11         ` Michael S. Tsirkin
2014-05-21  9:13           ` Alexander Graf
2014-05-21  9:33             ` Alexey Kardashevskiy
2014-05-21  9:38               ` Michael S. Tsirkin
2014-05-21 10:03                 ` Alexey Kardashevskiy
2014-05-21  9:50               ` Alexander Graf
2014-05-21 10:13                 ` Alexey Kardashevskiy
2014-05-21 10:35                   ` Alexander Graf
2014-05-21 12:42                     ` Alexey Kardashevskiy
2014-05-22  6:53                       ` Alexey Kardashevskiy
2014-05-22  7:16                         ` Alexander Graf
2014-05-22 10:53                           ` Alexey Kardashevskiy
2014-05-22 10:53                             ` [Qemu-devel] [PATCH 1/2] vmstate: Add helper to enable GHashTable migration Alexey Kardashevskiy
2014-05-22 10:57                               ` Alexander Graf [this message]
2014-05-22 11:04                                 ` Alexey Kardashevskiy
2014-05-22 10:53                             ` [Qemu-devel] [PATCH 2/2] spapr_pci: Use XICS interrupt allocator and do not cache interrupts in PHB Alexey Kardashevskiy
2014-05-22 10:57                             ` [Qemu-devel] [PATCH v2 8/8] " Alexander Graf
2014-05-22 14:25                               ` Alexey Kardashevskiy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=537DD81B.4080007@suse.de \
    --to=agraf@suse.de \
    --cc=aik@ozlabs.ru \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).