[Spice-devel] [PATCH spice 16/17] spicec-x11: use malloc / free / realloc for clipboard data

Hans de Goede hdegoede at redhat.com
Mon Oct 4 03:35:07 PDT 2010


As we need a realloc function it is better to use malloc / free /
realloc then to diy, esp. as realloc can grow the buffer without
needing a memcpy.
---
 client/x11/platform.cpp |   26 +++++++-------------------
 1 files changed, 7 insertions(+), 19 deletions(-)

diff --git a/client/x11/platform.cpp b/client/x11/platform.cpp
index 0af3c8c..836561e 100644
--- a/client/x11/platform.cpp
+++ b/client/x11/platform.cpp
@@ -2265,28 +2265,13 @@ void Platform::path_append(std::string& path, const std::string& partial_path)
 static void ensure_clipboard_data_space(uint32_t size)
 {
     if (size > clipboard_data_space) {
-        delete clipboard_data;
-        clipboard_data = NULL;
-        clipboard_data = new uint8_t[size];
+        free(clipboard_data);
+        clipboard_data = (uint8_t *)malloc(size);
         assert(clipboard_data);
         clipboard_data_space = size;
     }
 }
 
-static void realloc_clipboard_data_space(uint32_t size)
-{
-    if (size <= clipboard_data_space) {
-        return;
-    }
-    uint32_t old_alloc = clipboard_data_space;
-    clipboard_data_space = size;
-    uint8_t *newbuf = new uint8_t[clipboard_data_space];
-    assert(newbuf);
-    memcpy(newbuf, clipboard_data, old_alloc);
-    delete[] clipboard_data;
-    clipboard_data = newbuf;
-}
-
 static void send_selection_notify(Atom prop, int process_next_req)
 {
     XEvent res, *event = &next_selection_request->event;
@@ -2425,8 +2410,11 @@ static int get_selection(XEvent &event, Atom type, Atom prop, int format,
 
     if (incr) {
         if (len) {
-            if (clipboard_data_size + len > clipboard_data_space)
-                realloc_clipboard_data_space(clipboard_data_size + len);
+            if (clipboard_data_size + len > clipboard_data_space) {
+                clipboard_data_space = clipboard_data_size + len;
+                clipboard_data = (uint8_t *)realloc(clipboard_data, clipboard_data_space);
+                assert(clipboard_data);
+            }
             memcpy(clipboard_data + clipboard_data_size, data, len);
             clipboard_data_size += len;
             LOG_INFO("Appended %d bytes to buffer", len);
-- 
1.7.2.2



More information about the Spice-devel mailing list