#include <stdlib.h>
#include <string.h>
#include "dynamic-string.h"
+#include "netdev-dpdk.h"
#include "util.h"
static void
void
ofpbuf_uninit(struct ofpbuf *b)
{
- if (b && b->source == OFPBUF_MALLOC) {
- free(b->base);
+ if (b) {
+ if (b->source == OFPBUF_MALLOC) {
+ free(b->base);
+ }
+ if (b->source == OFPBUF_DPDK) {
+ free_dpdk_buf(b);
+ }
}
}
new_allocated = new_headroom + b->size + new_tailroom;
switch (b->source) {
+ case OFPBUF_DPDK:
+ OVS_NOT_REACHED();
+
case OFPBUF_MALLOC:
if (new_headroom == ofpbuf_headroom(b)) {
new_base = xrealloc(b->base, new_allocated);
break;
case OFPBUF_STACK:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OFPBUF_STUB:
b->source = OFPBUF_MALLOC;
break;
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
b->allocated = new_allocated;
void
ofpbuf_trim(struct ofpbuf *b)
{
+ ovs_assert(b->source != OFPBUF_DPDK);
+
if (b->source == OFPBUF_MALLOC
&& (ofpbuf_headroom(b) || ofpbuf_tailroom(b))) {
ofpbuf_resize__(b, 0, 0);
}
}
+/* Shifts all of the data within the allocated space in 'b' by 'delta' bytes.
+ * For example, a 'delta' of 1 would cause each byte of data to move one byte
+ * forward (from address 'p' to 'p+1'), and a 'delta' of -1 would cause each
+ * byte to move one byte backward (from 'p' to 'p-1'). */
+void
+ofpbuf_shift(struct ofpbuf *b, int delta)
+{
+ ovs_assert(delta > 0 ? delta <= ofpbuf_tailroom(b)
+ : delta < 0 ? -delta <= ofpbuf_headroom(b)
+ : true);
+
+ if (delta != 0) {
+ char *dst = (char *) b->data + delta;
+ memmove(dst, b->data, b->size);
+ b->data = dst;
+ }
+}
+
/* Appends 'size' bytes of data to the tail end of 'b', reallocating and
* copying its data if necessary. Returns a pointer to the first byte of the
* new data, which is left uninitialized. */
b->data = (char*)b->data + size;
}
+/* Reserves 'size' bytes of headroom so that they can be later allocated with
+ * ofpbuf_push_uninit() without reallocating the ofpbuf. */
+void
+ofpbuf_reserve_with_tailroom(struct ofpbuf *b, size_t headroom,
+ size_t tailroom)
+{
+ ovs_assert(!b->size);
+ ofpbuf_prealloc_tailroom(b, headroom + tailroom);
+ b->data = (char*)b->data + headroom;
+}
+
/* Prefixes 'size' bytes to the head end of 'b', reallocating and copying its
* data if necessary. Returns a pointer to the first byte of the data's
* location in the ofpbuf. The new data is left uninitialized. */
ofpbuf_steal_data(struct ofpbuf *b)
{
void *p;
+ ovs_assert(b->source != OFPBUF_DPDK);
+
if (b->source == OFPBUF_MALLOC && b->data == b->base) {
p = b->data;
} else {
struct ds s;
ds_init(&s);
- ds_put_format(&s, "size=%zu, allocated=%zu, head=%zu, tail=%zu\n",
+ ds_put_format(&s, "size=%"PRIuSIZE", allocated=%"PRIuSIZE", head=%"PRIuSIZE", tail=%"PRIuSIZE"\n",
b->size, b->allocated,
ofpbuf_headroom(b), ofpbuf_tailroom(b));
ds_put_hex_dump(&s, b->data, MIN(b->size, maxbytes), 0, false);