Luiz Capitulino | 6b8d1ec | 2009-08-28 15:27:05 -0300 | [diff] [blame] | 1 | /* |
Luiz Capitulino | 41836a9 | 2010-05-12 16:34:42 -0300 | [diff] [blame] | 2 | * QInt Module |
Luiz Capitulino | 6b8d1ec | 2009-08-28 15:27:05 -0300 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2009 Red Hat Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Luiz Capitulino <lcapitulino@redhat.com> |
| 8 | * |
Luiz Capitulino | 41836a9 | 2010-05-12 16:34:42 -0300 | [diff] [blame] | 9 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. |
| 10 | * See the COPYING.LIB file in the top-level directory. |
Luiz Capitulino | 6b8d1ec | 2009-08-28 15:27:05 -0300 | [diff] [blame] | 11 | */ |
Luiz Capitulino | 41836a9 | 2010-05-12 16:34:42 -0300 | [diff] [blame] | 12 | |
Peter Maydell | f2ad72b | 2016-01-29 17:50:01 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 14 | #include "qapi/qmp/qint.h" |
| 15 | #include "qapi/qmp/qobject.h" |
Luiz Capitulino | 6b8d1ec | 2009-08-28 15:27:05 -0300 | [diff] [blame] | 16 | #include "qemu-common.h" |
| 17 | |
Luiz Capitulino | 6b8d1ec | 2009-08-28 15:27:05 -0300 | [diff] [blame] | 18 | /** |
| 19 | * qint_from_int(): Create a new QInt from an int64_t |
| 20 | * |
| 21 | * Return strong reference. |
| 22 | */ |
| 23 | QInt *qint_from_int(int64_t value) |
| 24 | { |
| 25 | QInt *qi; |
| 26 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 27 | qi = g_malloc(sizeof(*qi)); |
Eric Blake | 55e1819 | 2015-12-01 22:20:45 -0700 | [diff] [blame] | 28 | qobject_init(QOBJECT(qi), QTYPE_QINT); |
Luiz Capitulino | 6b8d1ec | 2009-08-28 15:27:05 -0300 | [diff] [blame] | 29 | qi->value = value; |
Luiz Capitulino | 6b8d1ec | 2009-08-28 15:27:05 -0300 | [diff] [blame] | 30 | |
| 31 | return qi; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * qint_get_int(): Get the stored integer |
| 36 | */ |
| 37 | int64_t qint_get_int(const QInt *qi) |
| 38 | { |
| 39 | return qi->value; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * qobject_to_qint(): Convert a QObject into a QInt |
| 44 | */ |
| 45 | QInt *qobject_to_qint(const QObject *obj) |
| 46 | { |
Markus Armbruster | fcf73f6 | 2015-10-15 16:15:35 +0200 | [diff] [blame] | 47 | if (!obj || qobject_type(obj) != QTYPE_QINT) { |
Luiz Capitulino | 6b8d1ec | 2009-08-28 15:27:05 -0300 | [diff] [blame] | 48 | return NULL; |
Markus Armbruster | fcf73f6 | 2015-10-15 16:15:35 +0200 | [diff] [blame] | 49 | } |
Luiz Capitulino | 6b8d1ec | 2009-08-28 15:27:05 -0300 | [diff] [blame] | 50 | return container_of(obj, QInt, base); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * qint_destroy_obj(): Free all memory allocated by a |
| 55 | * QInt object |
| 56 | */ |
Eric Blake | 55e1819 | 2015-12-01 22:20:45 -0700 | [diff] [blame] | 57 | void qint_destroy_obj(QObject *obj) |
Luiz Capitulino | 6b8d1ec | 2009-08-28 15:27:05 -0300 | [diff] [blame] | 58 | { |
| 59 | assert(obj != NULL); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 60 | g_free(qobject_to_qint(obj)); |
Luiz Capitulino | 6b8d1ec | 2009-08-28 15:27:05 -0300 | [diff] [blame] | 61 | } |