ofpbuf: New function ofpbuf_steal_data().
authorBen Pfaff <blp@nicira.com>
Fri, 11 Mar 2011 19:52:49 +0000 (11:52 -0800)
committerBen Pfaff <blp@nicira.com>
Fri, 18 Mar 2011 21:40:55 +0000 (14:40 -0700)
This will have its first use in an upcoming commit.

lib/ofpbuf.c
lib/ofpbuf.h

index 34aad93..7e96536 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -462,6 +462,25 @@ ofpbuf_try_pull(struct ofpbuf *b, size_t size)
     return b->size >= size ? ofpbuf_pull(b, size) : NULL;
 }
 
+/* Returns the data in 'b' as a block of malloc()'d memory and frees the buffer
+ * within 'b'.  (If 'b' itself was dynamically allocated, e.g. with
+ * ofpbuf_new(), then it should still be freed with, e.g., ofpbuf_delete().) */
+void *
+ofpbuf_steal_data(struct ofpbuf *b)
+{
+    void *p;
+    if (b->source == OFPBUF_MALLOC && b->data == b->base) {
+        p = b->data;
+    } else {
+        p = xmemdup(b->data, b->size);
+        if (b->source == OFPBUF_MALLOC) {
+            free(b->base);
+        }
+    }
+    b->base = b->data = NULL;
+    return p;
+}
+
 /* Returns a string that describes some of 'b''s metadata plus a hex dump of up
  * to 'maxbytes' from the start of the buffer. */
 char *
index 442c90c..b8e3670 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -97,6 +97,8 @@ void ofpbuf_clear(struct ofpbuf *);
 void *ofpbuf_pull(struct ofpbuf *, size_t);
 void *ofpbuf_try_pull(struct ofpbuf *, size_t);
 
+void *ofpbuf_steal_data(struct ofpbuf *);
+
 char *ofpbuf_to_string(const struct ofpbuf *, size_t maxbytes);
 
 static inline struct ofpbuf *ofpbuf_from_list(const struct list *list)