From a9a3782efc059744fd7e6b5bf29b1f7fb8da3c18 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Thu, 19 Mar 2009 10:39:32 -0700 Subject: [PATCH] New function ofpbuf_trim(), for freeing up wasted space in ofpbufs. The dpif code will start allocating 64 kB buffers in the next commit, since sometimes we get packets that big under virtualization. But we need to trim them down if we're going to store them for any time, so we need a function to do it. --- lib/ofpbuf.c | 15 ++++++++++++++- lib/ofpbuf.h | 3 ++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/ofpbuf.c b/lib/ofpbuf.c index ab469dbf..a2d2cb26 100644 --- a/lib/ofpbuf.c +++ b/lib/ofpbuf.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2008 The Board of Trustees of The Leland Stanford +/* Copyright (c) 2008, 2009 The Board of Trustees of The Leland Stanford * Junior University * * We are making the OpenFlow specification and associated documentation @@ -168,6 +168,19 @@ ofpbuf_prealloc_headroom(struct ofpbuf *b, size_t size) assert(size <= ofpbuf_headroom(b)); } +/* Trims the size of 'b' to fit its actual content. */ +void +ofpbuf_trim(struct ofpbuf *b) +{ + /* XXX These could be supported, but the current client doesn't care. */ + assert(b->data == b->base); + assert(b->l2 == NULL && b->l3 == NULL && b->l4 == NULL && b->l7 == NULL); + if (b->allocated > b->size) { + b->base = b->data = xrealloc(b->base, b->size); + b->allocated = b->size; + } +} + /* 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. */ diff --git a/lib/ofpbuf.h b/lib/ofpbuf.h index f3a94c2c..0f8f69c6 100644 --- a/lib/ofpbuf.h +++ b/lib/ofpbuf.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2008 The Board of Trustees of The Leland Stanford +/* Copyright (c) 2008, 2009 The Board of Trustees of The Leland Stanford * Junior University * * We are making the OpenFlow specification and associated documentation @@ -81,6 +81,7 @@ size_t ofpbuf_headroom(struct ofpbuf *); size_t ofpbuf_tailroom(struct ofpbuf *); void ofpbuf_prealloc_headroom(struct ofpbuf *, size_t); void ofpbuf_prealloc_tailroom(struct ofpbuf *, size_t); +void ofpbuf_trim(struct ofpbuf *); void ofpbuf_clear(struct ofpbuf *); void *ofpbuf_pull(struct ofpbuf *, size_t); -- 2.30.2