[v2,08/24] Use C++ style for cursor message initialization instead of C style

Submitted by Christophe de Dinechin on Feb. 21, 2018, 5:46 p.m.

Details

Message ID 20180221174636.37758-9-christophe@dinechin.org
State New
Headers show
Series "WIP: Refactor the streaming agent towards a more standard C++ style" ( rev: 2 ) in Spice

Not browsing as part of any series.

Commit Message

Christophe de Dinechin Feb. 21, 2018, 5:46 p.m.
From: Christophe de Dinechin <dinechin@redhat.com>

Signed-off-by: Christophe de Dinechin <dinechin@redhat.com>
---
 src/spice-streaming-agent.cpp | 47 +++++++++++++++++++++++++------------------
 1 file changed, 27 insertions(+), 20 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/spice-streaming-agent.cpp b/src/spice-streaming-agent.cpp
index 6599028..d02b873 100644
--- a/src/spice-streaming-agent.cpp
+++ b/src/spice-streaming-agent.cpp
@@ -324,38 +324,45 @@  static void usage(const char *progname)
 }
 
 static void
-send_cursor(int streamfd, unsigned width, unsigned height, int hotspot_x, int hotspot_y,
+send_cursor(int streamfd,
+            uint16_t width, uint16_t height,
+            uint16_t hotspot_x, uint16_t hotspot_y,
             std::function<void(uint32_t *)> fill_cursor)
 {
     if (width >= STREAM_MSG_CURSOR_SET_MAX_WIDTH ||
         height >= STREAM_MSG_CURSOR_SET_MAX_HEIGHT)
         return;
 
-    size_t cursor_size =
-        sizeof(StreamDevHeader) + sizeof(StreamMsgCursorSet) +
-        width * height * sizeof(uint32_t);
-    std::unique_ptr<uint8_t[]> msg(new uint8_t[cursor_size]);
+    const uint32_t cursor_msgsize =
+        sizeof(SpiceStreamCursorMessage) + width * height * sizeof(uint32_t);
+    const uint32_t hdrsize  = sizeof(StreamDevHeader);
 
-    StreamDevHeader &dev_hdr(*reinterpret_cast<StreamDevHeader*>(msg.get()));
-    memset(&dev_hdr, 0, sizeof(dev_hdr));
-    dev_hdr.protocol_version = STREAM_DEVICE_PROTOCOL;
-    dev_hdr.type = STREAM_TYPE_CURSOR_SET;
-    dev_hdr.size = cursor_size - sizeof(StreamDevHeader);
+    std::unique_ptr<uint8_t[]> storage(new uint8_t[cursor_msgsize]);
 
-    StreamMsgCursorSet &cursor_msg(*reinterpret_cast<StreamMsgCursorSet *>(msg.get() + sizeof(StreamDevHeader)));
-    memset(&cursor_msg, 0, sizeof(cursor_msg));
-
-    cursor_msg.type = SPICE_CURSOR_TYPE_ALPHA;
-    cursor_msg.width = width;
-    cursor_msg.height = height;
-    cursor_msg.hot_spot_x = hotspot_x;
-    cursor_msg.hot_spot_y = hotspot_y;
+    SpiceStreamCursorMessage *cursor_msg =
+        new(storage.get()) SpiceStreamCursorMessage {
+        .hdr = {
+            .protocol_version = STREAM_DEVICE_PROTOCOL,
+            .padding = 0,       // Workaround GCC internal / not implemented compiler error
+            .type = STREAM_TYPE_CURSOR_SET,
+            .size = cursor_msgsize - hdrsize
+        },
+        .msg = {
+            .width = width,
+            .height = height,
+            .hot_spot_x = hotspot_x,
+            .hot_spot_y = hotspot_y,
+            .type = SPICE_CURSOR_TYPE_ALPHA,
+            .padding1 = { },
+            .data = { }
+        }
+    };
 
-    uint32_t *pixels = reinterpret_cast<uint32_t *>(cursor_msg.data);
+    uint32_t *pixels = reinterpret_cast<uint32_t *>(cursor_msg->msg.data);
     fill_cursor(pixels);
 
     std::lock_guard<std::mutex> stream_guard(stream_mtx);
-    write_all(streamfd, msg.get(), cursor_size);
+    write_all(streamfd, storage.get(), cursor_msgsize);
 }
 
 static void cursor_changes(int streamfd, Display *display, int event_base)

Comments

> 
> From: Christophe de Dinechin <dinechin@redhat.com>
> 
> Signed-off-by: Christophe de Dinechin <dinechin@redhat.com>
> ---
>  src/spice-streaming-agent.cpp | 47
>  +++++++++++++++++++++++++------------------
>  1 file changed, 27 insertions(+), 20 deletions(-)
> 
> diff --git a/src/spice-streaming-agent.cpp b/src/spice-streaming-agent.cpp
> index 6599028..d02b873 100644
> --- a/src/spice-streaming-agent.cpp
> +++ b/src/spice-streaming-agent.cpp
> @@ -324,38 +324,45 @@ static void usage(const char *progname)
>  }
>  
>  static void
> -send_cursor(int streamfd, unsigned width, unsigned height, int hotspot_x,
> int hotspot_y,
> +send_cursor(int streamfd,
> +            uint16_t width, uint16_t height,
> +            uint16_t hotspot_x, uint16_t hotspot_y,
>              std::function<void(uint32_t *)> fill_cursor)
>  {
>      if (width >= STREAM_MSG_CURSOR_SET_MAX_WIDTH ||
>          height >= STREAM_MSG_CURSOR_SET_MAX_HEIGHT)
>          return;
>  
> -    size_t cursor_size =
> -        sizeof(StreamDevHeader) + sizeof(StreamMsgCursorSet) +
> -        width * height * sizeof(uint32_t);
> -    std::unique_ptr<uint8_t[]> msg(new uint8_t[cursor_size]);
> +    const uint32_t cursor_msgsize =
> +        sizeof(SpiceStreamCursorMessage) + width * height *
> sizeof(uint32_t);
> +    const uint32_t hdrsize  = sizeof(StreamDevHeader);
>  
> -    StreamDevHeader
> &dev_hdr(*reinterpret_cast<StreamDevHeader*>(msg.get()));
> -    memset(&dev_hdr, 0, sizeof(dev_hdr));
> -    dev_hdr.protocol_version = STREAM_DEVICE_PROTOCOL;
> -    dev_hdr.type = STREAM_TYPE_CURSOR_SET;
> -    dev_hdr.size = cursor_size - sizeof(StreamDevHeader);
> +    std::unique_ptr<uint8_t[]> storage(new uint8_t[cursor_msgsize]);
>  
> -    StreamMsgCursorSet &cursor_msg(*reinterpret_cast<StreamMsgCursorSet
> *>(msg.get() + sizeof(StreamDevHeader)));
> -    memset(&cursor_msg, 0, sizeof(cursor_msg));
> -
> -    cursor_msg.type = SPICE_CURSOR_TYPE_ALPHA;
> -    cursor_msg.width = width;
> -    cursor_msg.height = height;
> -    cursor_msg.hot_spot_x = hotspot_x;
> -    cursor_msg.hot_spot_y = hotspot_y;
> +    SpiceStreamCursorMessage *cursor_msg =
> +        new(storage.get()) SpiceStreamCursorMessage {
> +        .hdr = {
> +            .protocol_version = STREAM_DEVICE_PROTOCOL,
> +            .padding = 0,       // Workaround GCC internal / not implemented
> compiler error
> +            .type = STREAM_TYPE_CURSOR_SET,
> +            .size = cursor_msgsize - hdrsize
> +        },
> +        .msg = {
> +            .width = width,
> +            .height = height,
> +            .hot_spot_x = hotspot_x,
> +            .hot_spot_y = hotspot_y,
> +            .type = SPICE_CURSOR_TYPE_ALPHA,
> +            .padding1 = { },
> +            .data = { }
> +        }
> +    };
>  
> -    uint32_t *pixels = reinterpret_cast<uint32_t *>(cursor_msg.data);
> +    uint32_t *pixels = reinterpret_cast<uint32_t *>(cursor_msg->msg.data);
>      fill_cursor(pixels);
>  
>      std::lock_guard<std::mutex> stream_guard(stream_mtx);
> -    write_all(streamfd, msg.get(), cursor_size);
> +    write_all(streamfd, storage.get(), cursor_msgsize);
>  }
>  
>  static void cursor_changes(int streamfd, Display *display, int event_base)

It would be kind if you consider and reply to my previous comments.

Frediano