Luiz Capitulino | 6b8d1ec | 2009-08-28 15:27:05 -0300 | [diff] [blame] | 1 | /* |
| 2 | * QInt data type. |
| 3 | * |
| 4 | * Copyright (C) 2009 Red Hat Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Luiz Capitulino <lcapitulino@redhat.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 10 | * the COPYING file in the top-level directory. |
| 11 | */ |
| 12 | #include "qint.h" |
| 13 | #include "qobject.h" |
| 14 | #include "qemu-common.h" |
| 15 | |
Blue Swirl | aa43d9c | 2009-09-04 17:43:37 +0000 | [diff] [blame] | 16 | static void qint_destroy_obj(QObject *obj); |
| 17 | |
| 18 | static const QType qint_type = { |
| 19 | .code = QTYPE_QINT, |
| 20 | .destroy = qint_destroy_obj, |
| 21 | }; |
Luiz Capitulino | 6b8d1ec | 2009-08-28 15:27:05 -0300 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * qint_from_int(): Create a new QInt from an int64_t |
| 25 | * |
| 26 | * Return strong reference. |
| 27 | */ |
| 28 | QInt *qint_from_int(int64_t value) |
| 29 | { |
| 30 | QInt *qi; |
| 31 | |
| 32 | qi = qemu_malloc(sizeof(*qi)); |
| 33 | qi->value = value; |
| 34 | QOBJECT_INIT(qi, &qint_type); |
| 35 | |
| 36 | return qi; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * qint_get_int(): Get the stored integer |
| 41 | */ |
| 42 | int64_t qint_get_int(const QInt *qi) |
| 43 | { |
| 44 | return qi->value; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * qobject_to_qint(): Convert a QObject into a QInt |
| 49 | */ |
| 50 | QInt *qobject_to_qint(const QObject *obj) |
| 51 | { |
| 52 | if (qobject_type(obj) != QTYPE_QINT) |
| 53 | return NULL; |
| 54 | |
| 55 | return container_of(obj, QInt, base); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * qint_destroy_obj(): Free all memory allocated by a |
| 60 | * QInt object |
| 61 | */ |
| 62 | static void qint_destroy_obj(QObject *obj) |
| 63 | { |
| 64 | assert(obj != NULL); |
| 65 | qemu_free(qobject_to_qint(obj)); |
| 66 | } |