[PATCH usbredir 1/8] Abstract memory management calls.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This allows us to compile this code for use in the kernel
where we do not have libc functions available.

Signed-off-by: Jeremy White <jwhite@xxxxxxxxxxxxxxx>
---
 usbredirparser/usbredirfilter.c | 11 +++++-----
 usbredirparser/usbredirparser.c | 46 ++++++++++++++++++++---------------------
 usbredirparser/usbredirparser.h | 14 +++++++++++++
 3 files changed, 43 insertions(+), 28 deletions(-)

diff --git a/usbredirparser/usbredirfilter.c b/usbredirparser/usbredirfilter.c
index 02184ef..3fdc3e5 100644
--- a/usbredirparser/usbredirfilter.c
+++ b/usbredirparser/usbredirfilter.c
@@ -28,6 +28,7 @@
 #define strtok_r  glibc_strtok_r
 #endif
 
+#include "usbredirparser.h"
 #include "usbredirfilter.h"
 
 int usbredirfilter_string_to_rules(
@@ -56,12 +57,12 @@ int usbredirfilter_string_to_rules(
         rules_count++;
     }
 
-    rules = calloc(rules_count, sizeof(struct usbredirfilter_rule));
+    rules = usbredirparser_calloc(rules_count, sizeof(struct usbredirfilter_rule));
     if (!rules)
         return -ENOMEM;
 
     /* Make a copy since strtok mangles the string */
-    buf = strdup(filter_str);
+    buf = usbredirparser_strdup(filter_str);
     if (!buf) {
         ret = -ENOMEM;
         goto leave;
@@ -94,8 +95,8 @@ int usbredirfilter_string_to_rules(
 
 leave:
     if (ret)
-        free(rules);
-    free(buf);
+        usbredirparser_free(rules);
+    usbredirparser_free(buf);
     return ret;
 }
 
@@ -109,7 +110,7 @@ char *usbredirfilter_rules_to_string(const struct usbredirfilter_rule *rules,
         return NULL;
 
     /* We need 28 bytes per rule in the worst case */
-    str = malloc(28 * rules_count + 1);
+    str = usbredirparser_malloc(28 * rules_count + 1);
     if (!str)
         return NULL;
 
diff --git a/usbredirparser/usbredirparser.c b/usbredirparser/usbredirparser.c
index d1f9850..133f054 100644
--- a/usbredirparser/usbredirparser.c
+++ b/usbredirparser/usbredirparser.c
@@ -116,20 +116,20 @@ static void serialize_test(struct usbredirparser *parser_pub)
     wbuf = parser->write_buf;
     while (wbuf) {
         next_wbuf = wbuf->next;
-        free(wbuf->buf);
-        free(wbuf);
+        usbredirparser_free(wbuf->buf);
+        usbredirparser_free(wbuf);
         wbuf = next_wbuf;
     }
     parser->write_buf = NULL;
     parser->write_buf_count = 0;
 
-    free(parser->data);
+    usbredirparser_free(parser->data);
     parser->data = NULL;
 
     parser->type_header_len = parser->data_len = parser->have_peer_caps = 0;
 
     usbredirparser_unserialize(parser_pub, data, len);
-    free(data);
+    usbredirparser_free(data);
 }
 #endif
 
@@ -140,7 +140,7 @@ static int usbredirparser_caps_get_cap(struct usbredirparser_priv *parser,
 
 struct usbredirparser *usbredirparser_create(void)
 {
-    return calloc(1, sizeof(struct usbredirparser_priv));
+    return usbredirparser_calloc(1, sizeof(struct usbredirparser_priv));
 }
 
 static void usbredirparser_verify_caps(struct usbredirparser_priv *parser,
@@ -193,15 +193,15 @@ void usbredirparser_destroy(struct usbredirparser *parser_pub)
     wbuf = parser->write_buf;
     while (wbuf) {
         next_wbuf = wbuf->next;
-        free(wbuf->buf);
-        free(wbuf);
+        usbredirparser_free(wbuf->buf);
+        usbredirparser_free(wbuf);
         wbuf = next_wbuf;
     }
 
     if (parser->lock)
         parser->callb.free_lock_func(parser->lock);
 
-    free(parser);
+    usbredirparser_free(parser);
 }
 
 static int usbredirparser_caps_get_cap(struct usbredirparser_priv *parser,
@@ -278,7 +278,7 @@ static void usbredirparser_handle_hello(struct usbredirparser *parser_pub,
     }
     usbredirparser_verify_caps(parser, parser->peer_caps, "peer");
     parser->have_peer_caps = 1;
-    free(data);
+    usbredirparser_free(data);
 
     INFO("Peer version: %s, using %d-bits ids", buf,
          usbredirparser_using_32bits_ids(parser_pub) ? 32 : 64);
@@ -1006,7 +1006,7 @@ int usbredirparser_do_read(struct usbredirparser *parser_pub)
                 }
                 data_len = parser->header.length - type_header_len;
                 if (data_len) {
-                    parser->data = malloc(data_len);
+                    parser->data = usbredirparser_malloc(data_len);
                     if (!parser->data) {
                         ERROR("Out of memory allocating data buffer");
                         parser->to_skip = parser->header.length;
@@ -1079,8 +1079,8 @@ int usbredirparser_do_write(struct usbredirparser *parser_pub)
         if (wbuf->pos == wbuf->len) {
             parser->write_buf = wbuf->next;
             if (!(parser->flags & usbredirparser_fl_write_cb_owns_buffer))
-                free(wbuf->buf);
-            free(wbuf);
+                usbredirparser_free(wbuf->buf);
+            usbredirparser_free(wbuf);
             parser->write_buf_count--;
         }
     }
@@ -1091,13 +1091,13 @@ int usbredirparser_do_write(struct usbredirparser *parser_pub)
 void usbredirparser_free_write_buffer(struct usbredirparser *parser,
     uint8_t *data)
 {
-    free(data);
+    usbredirparser_free(data);
 }
 
 void usbredirparser_free_packet_data(struct usbredirparser *parser,
     uint8_t *data)
 {
-    free(data);
+    usbredirparser_free(data);
 }
 
 static void usbredirparser_queue(struct usbredirparser *parser_pub,
@@ -1124,11 +1124,11 @@ static void usbredirparser_queue(struct usbredirparser *parser_pub,
         return;
     }
 
-    new_wbuf = calloc(1, sizeof(*new_wbuf));
-    buf = malloc(header_len + type_header_len + data_len);
+    new_wbuf = usbredirparser_calloc(1, sizeof(*new_wbuf));
+    buf = usbredirparser_malloc(header_len + type_header_len + data_len);
     if (!new_wbuf || !buf) {
         ERROR("Out of memory allocating buffer to send packet, dropping!");
-        free(new_wbuf); free(buf);
+        usbredirparser_free(new_wbuf); usbredirparser_free(buf);
         return;
     }
 
@@ -1345,7 +1345,7 @@ void usbredirparser_send_filter_filter(struct usbredirparser *parser_pub,
     }
     usbredirparser_queue(parser_pub, usb_redir_filter_filter, 0, NULL,
                          (uint8_t *)str, strlen(str) + 1);
-    free(str);
+    usbredirparser_free(str);
 }
 
 void usbredirparser_send_start_bulk_receiving(struct usbredirparser *parser,
@@ -1456,9 +1456,9 @@ static int serialize_alloc(struct usbredirparser_priv *parser,
     size = (used + needed + USBREDIRPARSER_SERIALIZE_BUF_SIZE - 1) &
            ~(USBREDIRPARSER_SERIALIZE_BUF_SIZE - 1);
 
-    *state = realloc(*state, size);
+    *state = usbredirparser_realloc(*state, size);
     if (!*state) {
-        free(old_state);
+        usbredirparser_free(old_state);
         ERROR("Out of memory allocating serialization buffer");
         return -1;
     }
@@ -1548,7 +1548,7 @@ static int unserialize_data(struct usbredirparser_priv *parser,
         return -1;
     }
     if (*data == NULL && len > 0) {
-        *data = malloc(len);
+        *data = usbredirparser_malloc(len);
         if (!*data) {
             ERROR("Out of memory allocating unserialize buffer");
             return -1;
@@ -1744,7 +1744,7 @@ int usbredirparser_unserialize(struct usbredirparser *parser_pub,
     parser->type_header_read = i;
 
     if (parser->data_len) {
-        parser->data = malloc(parser->data_len);
+        parser->data = usbredirparser_malloc(parser->data_len);
         if (!parser->data) {
             ERROR("Out of memory allocating unserialize buffer");
             return -1;
@@ -1760,7 +1760,7 @@ int usbredirparser_unserialize(struct usbredirparser *parser_pub,
         return -1;
     next = &parser->write_buf;
     while (i) {
-        wbuf = calloc(1, sizeof(*wbuf));
+        wbuf = usbredirparser_calloc(1, sizeof(*wbuf));
         if (!wbuf) {
             ERROR("Out of memory allocating unserialize buffer");
             return -1;
diff --git a/usbredirparser/usbredirparser.h b/usbredirparser/usbredirparser.h
index ffeb1d7..59f3c1b 100644
--- a/usbredirparser/usbredirparser.h
+++ b/usbredirparser/usbredirparser.h
@@ -23,6 +23,20 @@
 
 #include "usbredirproto.h"
 
+#if defined(__KERNEL__)
+#define usbredirparser_calloc(a, b)    kcalloc((a), (b), GFP_KERNEL)
+#define usbredirparser_malloc(a)       kmalloc((a), GFP_KERNEL)
+#define usbredirparser_strdup(a)       kstrdup((a), GFP_KERNEL)
+#define usbredirparser_realloc(a, b)   krealloc((a), (b), GFP_KERNEL)
+#define usbredirparser_free kfree
+#else
+#define usbredirparser_calloc          calloc
+#define usbredirparser_malloc          malloc
+#define usbredirparser_strdup          strdup
+#define usbredirparser_realloc         realloc
+#define usbredirparser_free            free
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
-- 
2.1.4

_______________________________________________
Spice-devel mailing list
Spice-devel@xxxxxxxxxxxxxxxxxxxxx
http://lists.freedesktop.org/mailman/listinfo/spice-devel




[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]     [Monitors]