From 37e237206c12af5773feb519e56c4534b750aeac Mon Sep 17 00:00:00 2001 From: Andy Zhou Date: Tue, 11 Aug 2015 14:04:55 -0700 Subject: [PATCH] lib/ofpbuf: add ofpbuf_use_ds() API Add an API to convert a dynamic string object into ofpbuf. Signed-off-by: Andy Zhou Acked-by: Ben Pfaff --- lib/ofpbuf.c | 10 ++++++++++ lib/ofpbuf.h | 1 + 2 files changed, 11 insertions(+) diff --git a/lib/ofpbuf.c b/lib/ofpbuf.c index 9b9d6b2d9e1..392f843aef7 100644 --- a/lib/ofpbuf.c +++ b/lib/ofpbuf.c @@ -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 diff --git a/lib/ofpbuf.h b/lib/ofpbuf.h index 9e82de2cb83..17257a0df10 100644 --- a/lib/ofpbuf.h +++ b/lib/ofpbuf.h @@ -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);