summaryrefslogtreecommitdiff
path: root/lib/ofpbuf.c
diff options
context:
space:
mode:
authorBen Pfaff <blp@nicira.com>2011-03-11 11:52:49 -0800
committerBen Pfaff <blp@nicira.com>2011-03-18 14:40:55 -0700
commit933369b1194bce82bffa8cc1ccfdf82782cc5cde (patch)
tree656ace3c33a089ef6a69136b745a763d87e03917 /lib/ofpbuf.c
parentc7981f8ca285147fa315fde62d06107d54f4acac (diff)
downloadopenvswitch-933369b1194bce82bffa8cc1ccfdf82782cc5cde.tar.gz
ofpbuf: New function ofpbuf_steal_data().
This will have its first use in an upcoming commit.
Diffstat (limited to 'lib/ofpbuf.c')
-rw-r--r--lib/ofpbuf.c21
1 files changed, 20 insertions, 1 deletions
diff --git a/lib/ofpbuf.c b/lib/ofpbuf.c
index 34aad9382..7e9653600 100644
--- a/lib/ofpbuf.c
+++ b/lib/ofpbuf.c
@@ -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 *