From ff434c288f87a1a08a2cf1e58260daea68914769 Mon Sep 17 00:00:00 2001 From: Hans de Goede Date: Mon, 4 Oct 2010 12:10:47 +0200 Subject: spicec-x11: use malloc / free / realloc for clipboard data 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 file changed, 7 insertions(+), 19 deletions(-) (limited to 'client/x11/platform.cpp') diff --git a/client/x11/platform.cpp b/client/x11/platform.cpp index 981bdc0b..dc0787f0 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); -- cgit