Implement QoS framework.
[sliver-openvswitch.git] / lib / ofpbuf.c
index d1407e7..1621bcc 100644 (file)
@@ -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 <assert.h>
 #include <stdlib.h>
 #include <string.h>
+#include "dynamic-string.h"
 #include "util.h"
 
 /* Initializes 'b' as an empty ofpbuf that contains the 'allocated' bytes of
@@ -36,7 +37,7 @@ ofpbuf_use(struct ofpbuf *b, void *base, size_t allocated)
     b->size = 0;
     b->l2 = b->l3 = b->l4 = b->l7 = NULL;
     b->next = NULL;
-    b->private = NULL;
+    b->private_p = NULL;
 }
 
 /* Initializes 'b' as an empty ofpbuf with an initial capacity of 'size'
@@ -103,7 +104,7 @@ ofpbuf_delete(struct ofpbuf *b)
  * commonly, the data in a ofpbuf is at its beginning, and thus the ofpbuf's
  * headroom is 0.) */
 size_t
-ofpbuf_headroom(struct ofpbuf *b) 
+ofpbuf_headroom(const struct ofpbuf *b)
 {
     return (char*)b->data - (char*)b->base;
 }
@@ -111,7 +112,7 @@ ofpbuf_headroom(struct ofpbuf *b)
 /* Returns the number of bytes that may be appended to the tail end of ofpbuf
  * 'b' before the ofpbuf must be reallocated. */
 size_t
-ofpbuf_tailroom(struct ofpbuf *b) 
+ofpbuf_tailroom(const struct ofpbuf *b)
 {
     return (char*)ofpbuf_end(b) - (char*)ofpbuf_tail(b);
 }
@@ -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);
+}