X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Fofpbuf.c;h=1621bcc198128e9140323a32891ab9d99e9cef82;hb=15c93f417f8dd87f1655448b7c4c59e3fb8b5aba;hp=fd01ea8d0227b5af4e8062ff17ee367feccc4c0d;hpb=5019f688d4e3117960503d57978ff2e0fb3771e0;p=openvswitch diff --git a/lib/ofpbuf.c b/lib/ofpbuf.c index fd01ea8d..1621bcc1 100644 --- a/lib/ofpbuf.c +++ b/lib/ofpbuf.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2009 Nicira Networks. + * Copyright (c) 2008, 2009, 2010 Nicira Networks. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ #include #include #include +#include "dynamic-string.h" #include "util.h" /* Initializes 'b' as an empty ofpbuf that contains the 'allocated' bytes of @@ -218,6 +219,17 @@ ofpbuf_push_uninit(struct ofpbuf *b, size_t size) return b->data; } +/* Prefixes 'size' zeroed bytes to the head end of 'b'. 'b' must have at least + * 'size' bytes of headroom. Returns a pointer to the first byte of the data's + * location in the ofpbuf. */ +void * +ofpbuf_push_zeros(struct ofpbuf *b, size_t size) +{ + void *dst = ofpbuf_push_uninit(b, size); + memset(dst, 0, size); + return dst; +} + void * ofpbuf_push(struct ofpbuf *b, const void *p, size_t size) { @@ -286,3 +298,18 @@ ofpbuf_try_pull(struct ofpbuf *b, size_t size) { return b->size >= size ? ofpbuf_pull(b, size) : NULL; } + +/* 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 * +ofpbuf_to_string(const struct ofpbuf *b, size_t maxbytes) +{ + struct ds s; + + ds_init(&s); + ds_put_format(&s, "size=%zu, allocated=%zu, head=%zu, tail=%zu\n", + b->size, b->allocated, + ofpbuf_headroom(b), ofpbuf_tailroom(b)); + ds_put_hex_dump(&s, b->data, MIN(b->size, maxbytes), 0, false); + return ds_cstr(&s); +}