lib/ofpbuf: add ofpbuf_use_ds() API
authorAndy Zhou <azhou@nicira.com>
Tue, 11 Aug 2015 21:04:55 +0000 (14:04 -0700)
committerAndy Zhou <azhou@nicira.com>
Tue, 1 Sep 2015 22:17:14 +0000 (15:17 -0700)
Add an API to convert a dynamic string object into ofpbuf.

Signed-off-by: Andy Zhou <azhou@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
lib/ofpbuf.c
lib/ofpbuf.h

index 9b9d6b2..392f843 100644 (file)
@@ -53,6 +53,16 @@ ofpbuf_use(struct ofpbuf *b, void *base, size_t allocated)
     ofpbuf_use__(b, base, allocated, 0, OFPBUF_MALLOC);
 }
 
+/* Converts ds into ofpbuf 'b'. 'b' contains the 'ds->allocated' bytes of
+ * memory starting at 'ds->string'.  'ds' should not be modified any more.
+ * The memory allocated for 'ds' will be freed (with free()) if 'b' is
+ * resized or freed. */
+void
+ofpbuf_use_ds(struct ofpbuf *b, const struct ds *ds)
+{
+    ofpbuf_use__(b, ds->string, ds->allocated + 1, ds->length, OFPBUF_MALLOC);
+}
+
 /* Initializes 'b' as an empty ofpbuf that contains the 'allocated' bytes of
  * memory starting at 'base'.  'base' should point to a buffer on the stack.
  * (Nothing actually relies on 'base' being allocated on the stack.  It could
index 9e82de2..17257a0 100644 (file)
@@ -84,6 +84,7 @@ struct ofpbuf {
     }
 
 void ofpbuf_use(struct ofpbuf *, void *, size_t);
+void ofpbuf_use_ds(struct ofpbuf *, const struct ds *);
 void ofpbuf_use_stack(struct ofpbuf *, void *, size_t);
 void ofpbuf_use_stub(struct ofpbuf *, void *, size_t);
 void ofpbuf_use_const(struct ofpbuf *, const void *, size_t);