[ovs-dev] [ovs-ofctl 06/10] ofpbuf: New function ofpbuf_steal_data().
Ben Pfaff
blp at nicira.com
Fri Mar 11 21:20:32 UTC 2011
This will have its first use in an upcoming commit.
---
lib/ofpbuf.c | 21 ++++++++++++++++++++-
lib/ofpbuf.h | 4 +++-
2 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/lib/ofpbuf.c b/lib/ofpbuf.c
index 34aad93..7e96536 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 *
diff --git a/lib/ofpbuf.h b/lib/ofpbuf.h
index 442c90c..b8e3670 100644
--- a/lib/ofpbuf.h
+++ b/lib/ofpbuf.h
@@ -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)
--
1.7.1
More information about the dev
mailing list