[Spice-devel] [spice-server v4 06/11] qxl: Add red_message_{new, ref, unref} helpers
Christophe Fergeau
cfergeau at redhat.com
Thu Nov 29 12:50:08 UTC 2018
Currently, RedMessage are allocated on the stack, and then
initialized/uninitialized with red_{get,put}_message
This makes the API inconsistent with what is being done for RedDrawable
and RedCursor. Since QXLMessage is just a (mostly unused/unsecure) debugging tool,
we can dynamically allocate it instead, and get a consistent API.
---
server/red-parse-qxl.c | 37 ++++++++++++++++++++++++++++++++++---
server/red-parse-qxl.h | 7 ++++---
server/red-worker.c | 8 ++++----
3 files changed, 42 insertions(+), 10 deletions(-)
diff --git a/server/red-parse-qxl.c b/server/red-parse-qxl.c
index 4be53d3e5..e88e75096 100644
--- a/server/red-parse-qxl.c
+++ b/server/red-parse-qxl.c
@@ -1261,8 +1261,8 @@ void red_put_update_cmd(RedUpdateCmd *red)
/* nothing yet */
}
-bool red_get_message(QXLInstance *qxl_instance, RedMemSlotInfo *slots, int group_id,
- RedMessage *red, QXLPHYSICAL addr)
+static bool red_get_message(QXLInstance *qxl_instance, RedMemSlotInfo *slots, int group_id,
+ RedMessage *red, QXLPHYSICAL addr)
{
QXLMessage *qxl;
int memslot_id;
@@ -1294,13 +1294,44 @@ bool red_get_message(QXLInstance *qxl_instance, RedMemSlotInfo *slots, int group
return true;
}
-void red_put_message(RedMessage *red)
+static void red_put_message(RedMessage *red)
{
if (red->qxl != NULL) {
red_qxl_release_resource(red->qxl, red->release_info_ext);
}
}
+RedMessage *red_message_new(QXLInstance *qxl, RedMemSlotInfo *slots, int group_id, QXLPHYSICAL addr)
+{
+ RedMessage *red;
+
+ red = g_new0(RedMessage, 1);
+
+ red->refs = 1;
+
+ if (!red_get_message(qxl, slots, group_id, red, addr)) {
+ red_message_unref(red);
+ return NULL;
+ }
+
+ return red;
+}
+
+RedMessage *red_message_ref(RedMessage *red)
+{
+ red->refs++;
+ return red;
+}
+
+void red_message_unref(RedMessage *red)
+{
+ if (--red->refs) {
+ return;
+ }
+ red_put_message(red);
+ g_free(red);
+}
+
static unsigned int surface_format_to_bpp(uint32_t format)
{
switch (format) {
diff --git a/server/red-parse-qxl.h b/server/red-parse-qxl.h
index ecf7b1577..fad144071 100644
--- a/server/red-parse-qxl.h
+++ b/server/red-parse-qxl.h
@@ -69,6 +69,7 @@ typedef struct RedUpdateCmd {
typedef struct RedMessage {
QXLInstance *qxl;
QXLReleaseInfoExt release_info_ext;
+ int refs;
int len;
uint8_t *data;
} RedMessage;
@@ -122,9 +123,9 @@ bool red_get_update_cmd(RedMemSlotInfo *slots, int group_id,
RedUpdateCmd *red, QXLPHYSICAL addr);
void red_put_update_cmd(RedUpdateCmd *red);
-bool red_get_message(QXLInstance *qxl, RedMemSlotInfo *slots, int group_id,
- RedMessage *red, QXLPHYSICAL addr);
-void red_put_message(RedMessage *red);
+RedMessage *red_message_new(QXLInstance *qxl, RedMemSlotInfo *slots, int group_id, QXLPHYSICAL addr);
+RedMessage *red_message_ref(RedMessage *red);
+void red_message_unref(RedMessage *red);
bool red_validate_surface(uint32_t width, uint32_t height,
int32_t stride, uint32_t format);
diff --git a/server/red-worker.c b/server/red-worker.c
index aa54dcd85..70d596dd6 100644
--- a/server/red-worker.c
+++ b/server/red-worker.c
@@ -230,16 +230,16 @@ static int red_process_display(RedWorker *worker, int *ring_is_empty)
break;
}
case QXL_CMD_MESSAGE: {
- RedMessage message;
+ RedMessage *message;
- if (!red_get_message(worker->qxl, &worker->mem_slots, ext_cmd.group_id,
- &message, ext_cmd.cmd.data)) {
+ message = red_message_new(worker->qxl, &worker->mem_slots, ext_cmd.group_id, ext_cmd.cmd.data);
+ if (message == NULL) {
break;
}
#ifdef DEBUG
spice_warning("MESSAGE: %.*s", message.len, message.data);
#endif
- red_put_message(&message);
+ red_message_unref(message);
break;
}
case QXL_CMD_SURFACE:
--
2.19.1
More information about the Spice-devel
mailing list