blob: 86b9b04f0b6214a88c223e6a3420ed7085016231 [file] [log] [blame]
Luiz Capitulino6b8d1ec2009-08-28 15:27:05 -03001/*
Luiz Capitulino41836a92010-05-12 16:34:42 -03002 * QInt Module
Luiz Capitulino6b8d1ec2009-08-28 15:27:05 -03003 *
4 * Copyright (C) 2009 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 *
Luiz Capitulino41836a92010-05-12 16:34:42 -03009 * 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 Capitulino6b8d1ec2009-08-28 15:27:05 -030011 */
Luiz Capitulino41836a92010-05-12 16:34:42 -030012
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010013#include "qapi/qmp/qint.h"
14#include "qapi/qmp/qobject.h"
Luiz Capitulino6b8d1ec2009-08-28 15:27:05 -030015#include "qemu-common.h"
16
Blue Swirlaa43d9c2009-09-04 17:43:37 +000017static void qint_destroy_obj(QObject *obj);
18
19static const QType qint_type = {
20 .code = QTYPE_QINT,
21 .destroy = qint_destroy_obj,
22};
Luiz Capitulino6b8d1ec2009-08-28 15:27:05 -030023
24/**
25 * qint_from_int(): Create a new QInt from an int64_t
26 *
27 * Return strong reference.
28 */
29QInt *qint_from_int(int64_t value)
30{
31 QInt *qi;
32
Anthony Liguori7267c092011-08-20 22:09:37 -050033 qi = g_malloc(sizeof(*qi));
Luiz Capitulino6b8d1ec2009-08-28 15:27:05 -030034 qi->value = value;
35 QOBJECT_INIT(qi, &qint_type);
36
37 return qi;
38}
39
40/**
41 * qint_get_int(): Get the stored integer
42 */
43int64_t qint_get_int(const QInt *qi)
44{
45 return qi->value;
46}
47
48/**
49 * qobject_to_qint(): Convert a QObject into a QInt
50 */
51QInt *qobject_to_qint(const QObject *obj)
52{
53 if (qobject_type(obj) != QTYPE_QINT)
54 return NULL;
55
56 return container_of(obj, QInt, base);
57}
58
59/**
60 * qint_destroy_obj(): Free all memory allocated by a
61 * QInt object
62 */
63static void qint_destroy_obj(QObject *obj)
64{
65 assert(obj != NULL);
Anthony Liguori7267c092011-08-20 22:09:37 -050066 g_free(qobject_to_qint(obj));
Luiz Capitulino6b8d1ec2009-08-28 15:27:05 -030067}