/* Trims the size of 'b' to fit its actual content, reducing its tailroom to * 0. Its headroom, if any, is preserved. * * Buffers not obtained from malloc() are not resized, since that wouldn't save * any memory. */ void ofpbuf_trim(struct ofpbuf *b) { if (b->source == OFPBUF_MALLOC && (ofpbuf_headroom(b) || ofpbuf_tailroom(b))) { ofpbuf_resize__(b, 0, 0); } }
/* Reallocates 'b' so that it has exactly 'new_headroom' and 'new_tailroom' * bytes of headroom and tailroom, respectively. */ static void ofpbuf_resize__(struct ofpbuf *b, size_t new_headroom, size_t new_tailroom) { void *new_base, *new_data; size_t new_allocated; new_allocated = new_headroom + b->size + new_tailroom; switch (b->source) { case OFPBUF_MALLOC: if (new_headroom == ofpbuf_headroom(b)) { new_base = xrealloc(b->base, new_allocated); } else { new_base = xmalloc(new_allocated); ofpbuf_copy__(b, new_base, new_headroom, new_tailroom); free(b->base); } break; case OFPBUF_STACK: NOT_REACHED(); case OFPBUF_STUB: b->source = OFPBUF_MALLOC; new_base = xmalloc(new_allocated); ofpbuf_copy__(b, new_base, new_headroom, new_tailroom); break; default: NOT_REACHED(); } b->allocated = new_allocated; b->base = new_base; new_data = (char *) new_base + new_headroom; if (b->data != new_data) { uintptr_t data_delta = (char *) new_data - (char *) b->data; b->data = new_data; if (b->l2) { b->l2 = (char *) b->l2 + data_delta; } if (b->l2_5) { b->l2_5 = (char *) b->l2_5 + data_delta; } if (b->l3) { b->l3 = (char *) b->l3 + data_delta; } if (b->l4) { b->l4 = (char *) b->l4 + data_delta; } if (b->l7) { b->l7 = (char *) b->l7 + data_delta; } } }
/* 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=%"PRIu32", allocated=%"PRIu32", 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); return ds_cstr(&s); }
/* 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'). * * If used, user must make sure the 'header' and 'msg' pointers are updated * after shifting. */ 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; } }
/* 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 *) ofpbuf_data(b) + delta; memmove(dst, ofpbuf_data(b), ofpbuf_size(b)); ofpbuf_set_data(b, dst); } }
/* Reallocates 'b' so that it has exactly 'new_headroom' and 'new_tailroom' * bytes of headroom and tailroom, respectively. */ static void ofpbuf_resize__(struct ofpbuf *b, size_t new_headroom, size_t new_tailroom) { void *new_base, *new_data; size_t new_allocated; new_allocated = new_headroom + b->size + new_tailroom; switch (b->source) { case OFPBUF_MALLOC: if (new_headroom == ofpbuf_headroom(b)) { new_base = xrealloc(b->base, new_allocated); } else { new_base = xmalloc(new_allocated); ofpbuf_copy__(b, new_base, new_headroom, new_tailroom); free(b->base); } break; case OFPBUF_STACK: OVS_NOT_REACHED(); case OFPBUF_STUB: b->source = OFPBUF_MALLOC; new_base = xmalloc(new_allocated); ofpbuf_copy__(b, new_base, new_headroom, new_tailroom); break; default: OVS_NOT_REACHED(); } b->allocated = new_allocated; b->base = new_base; new_data = (char *) new_base + new_headroom; if (b->data != new_data) { if (b->header) { uintptr_t data_delta = (char *) b->header - (char *) b->data; b->header = (char *) new_data + data_delta; } if (b->msg) { uintptr_t data_delta = (char *) b->msg - (char *) b->data; b->msg = (char *) new_data + data_delta; } b->data = new_data; } }
static void ofpbuf_copy__(struct ofpbuf *b, uint8_t *new_base, size_t new_headroom, size_t new_tailroom) { const uint8_t *old_base = b->base; size_t old_headroom = ofpbuf_headroom(b); size_t old_tailroom = ofpbuf_tailroom(b); size_t copy_headroom = MIN(old_headroom, new_headroom); size_t copy_tailroom = MIN(old_tailroom, new_tailroom); memcpy(&new_base[new_headroom - copy_headroom], &old_base[old_headroom - copy_headroom], copy_headroom + b->size + copy_tailroom); }
/* Reallocates 'b' so that it has exactly 'new_headroom' and 'new_tailroom' * bytes of headroom and tailroom, respectively. */ static void ofpbuf_resize__(struct ofpbuf *b, size_t new_headroom, size_t new_tailroom) { void *new_base, *new_data; size_t new_allocated; new_allocated = new_headroom + ofpbuf_size(b) + new_tailroom; switch (b->source) { case OFPBUF_DPDK: OVS_NOT_REACHED(); case OFPBUF_MALLOC: if (new_headroom == ofpbuf_headroom(b)) { new_base = xrealloc(ofpbuf_base(b), new_allocated); } else { new_base = xmalloc(new_allocated); ofpbuf_copy__(b, new_base, new_headroom, new_tailroom); free(ofpbuf_base(b)); } break; case OFPBUF_STACK: OVS_NOT_REACHED(); case OFPBUF_STUB: b->source = OFPBUF_MALLOC; new_base = xmalloc(new_allocated); ofpbuf_copy__(b, new_base, new_headroom, new_tailroom); break; default: OVS_NOT_REACHED(); } b->allocated = new_allocated; ofpbuf_set_base(b, new_base); new_data = (char *) new_base + new_headroom; if (ofpbuf_data(b) != new_data) { if (b->frame) { uintptr_t data_delta = (char *) new_data - (char *) ofpbuf_data(b); b->frame = (char *) b->frame + data_delta; } ofpbuf_set_data(b, new_data); } }
void ofpbuf_prealloc_headroom(struct ofpbuf *b, size_t size) { assert(size <= ofpbuf_headroom(b)); }
/* Reallocates 'b' so that it has exactly 'new_tailroom' bytes of tailroom. */ static void ofpbuf_resize_tailroom__(struct ofpbuf *b, size_t new_tailroom) { b->allocated = ofpbuf_headroom(b) + b->size + new_tailroom; ofpbuf_rebase__(b, xrealloc(b->base, b->allocated)); }