From: Jack Lange Date: Mon, 9 Feb 2009 17:01:51 +0000 (-0600) Subject: added ring buffer X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=commitdiff_plain;h=ed814ce69c64a1f24670f21950f3da646f7921df;p=palacios.git added ring buffer --- diff --git a/palacios/build/Makefile b/palacios/build/Makefile index 457f078..e93fd63 100644 --- a/palacios/build/Makefile +++ b/palacios/build/Makefile @@ -269,6 +269,7 @@ VMM_OBJS := \ palacios/vmm_rbtree.o \ palacios/vmm_profiler.o \ palacios/vmm_direct_paging.o \ + palacios/vmm_ringbuffer.o \ # vmx.c vmcs_gen.c vmcs.c diff --git a/palacios/include/palacios/vmm_ringbuffer.h b/palacios/include/palacios/vmm_ringbuffer.h new file mode 100644 index 0000000..29e892f --- /dev/null +++ b/palacios/include/palacios/vmm_ringbuffer.h @@ -0,0 +1,56 @@ +/* + * This file is part of the Palacios Virtual Machine Monitor developed + * by the V3VEE Project with funding from the United States National + * Science Foundation and the Department of Energy. + * + * The V3VEE Project is a joint project between Northwestern University + * and the University of New Mexico. You can find out more at + * http://www.v3vee.org + * + * Copyright (c) 2008, Jack Lange + * Copyright (c) 2008, The V3VEE Project + * All rights reserved. + * + * Author: Jack Lange + * + * This is free software. You are permitted to use, + * redistribute, and modify it as specified in the file "V3VEE_LICENSE". + */ + +#ifndef __VMM_RING_BUFFER_H__ +#define __VMM_RING_BUFFER_H__ + +#ifdef __V3VEE__ + +#include + + +struct v3_ringbuf { + uchar_t * buf; + uint_t size; + + uint_t start; + uint_t end; + uint_t current_len; +}; + + +void v3_init_ringbuf(struct v3_ringbuf * ring, uint_t size); +struct v3_ringbuf * v3_create_ringbuf(uint_t size); +void v3_free_ringbuf(struct v3_ringbuf * ring); + + +int v3_ringbuf_read(struct v3_ringbuf * ring, uchar_t * dst, uint_t len); +int v3_ringbuf_peek(struct v3_ringbuf * ring, uchar_t * dst, uint_t len); +int v3_ringbuf_delete(struct v3_ringbuf * ring, uint_t len); +int v3_ringbuf_write(struct v3_ringbuf * ring, uchar_t * src, uint_t len); +int v3_ringbuf_data_len(struct v3_ringbuf * ring); +int v3_ringbuf_capacity(struct v3_ringbuf * ring); + + +void v3_print_ringbuf(struct v3_ringbuf * ring); + + +#endif + +#endif // ! __VMM_RING_BUFFER_H__ diff --git a/palacios/src/palacios/vmm_ringbuffer.c b/palacios/src/palacios/vmm_ringbuffer.c new file mode 100644 index 0000000..bbde628 --- /dev/null +++ b/palacios/src/palacios/vmm_ringbuffer.c @@ -0,0 +1,198 @@ +/* + * This file is part of the Palacios Virtual Machine Monitor developed + * by the V3VEE Project with funding from the United States National + * Science Foundation and the Department of Energy. + * + * The V3VEE Project is a joint project between Northwestern University + * and the University of New Mexico. You can find out more at + * http://www.v3vee.org + * + * Copyright (c) 2008, Jack Lange + * Copyright (c) 2008, The V3VEE Project + * All rights reserved. + * + * Author: Jack Lange + * + * This is free software. You are permitted to use, + * redistribute, and modify it as specified in the file "V3VEE_LICENSE". + */ + +#include +#include + + +void v3_init_ringbuf(struct v3_ringbuf * ring, uint_t size) { + ring->buf = V3_Malloc(size); + ring->size = size; + + ring->start = 0; + ring->end = 0; + ring->current_len = 0; +} + + +struct v3_ringbuf * v3_create_ringbuf(uint_t size) { + struct v3_ringbuf * ring = (struct v3_ringbuf *)V3_Malloc(sizeof(struct v3_ringbuf)); + + v3_init_ringbuf(ring, size); + + return ring; +} + +void v3_free_ringbuf(struct v3_ringbuf * ring) { + V3_Free(ring->buf); + V3_Free(ring); +} + + + + + +static inline uchar_t * get_read_ptr(struct v3_ringbuf * ring) { + return (uchar_t *)(ring->buf + ring->start); +} + +static inline uchar_t * get_write_ptr(struct v3_ringbuf * ring) { + return (uchar_t *)(ring->buf + ring->end); +} + + +static inline int get_read_section_size(struct v3_ringbuf * ring) { + return ring->size - ring->start; +} + +static inline int get_write_section_size(struct v3_ringbuf * ring) { + return ring->size - ring->end; +} + +static inline int is_read_loop(struct v3_ringbuf * ring, uint_t len) { + if ((ring->start >= ring->end) && (ring->current_len > 0)) { + // end is past the end of the buffer + if (get_read_section_size(ring) < len) { + return 1; + } + } + return 0; +} + + +static inline int is_write_loop(struct v3_ringbuf * ring, uint_t len) { + if ((ring->end >= ring->start) && (ring->current_len < ring->size)) { + // end is past the end of the buffer + if (get_write_section_size(ring) < len) { + return 1; + } + } + return 0; +} + + +int v3_ringbuf_data_len(struct v3_ringbuf * ring) { + return ring->current_len; +} + +int v3_ringbuf_capacity(struct v3_ringbuf * ring) { + return ring->size; +} + +int v3_ringbuf_read(struct v3_ringbuf * ring, uchar_t * dst, uint_t len) { + int read_len = 0; + int ring_data_len = ring->current_len; + + read_len = (len > ring_data_len) ? ring_data_len : len; + + if (is_read_loop(ring, read_len)) { + int section_len = get_read_section_size(ring); + + memcpy(dst, get_read_ptr(ring), section_len); + memcpy(dst + section_len, ring->buf, read_len - section_len); + + ring->start = read_len - section_len; + } else { + memcpy(dst, get_read_ptr(ring), read_len); + + ring->start += read_len; + } + + ring->current_len -= read_len; + + return read_len; +} + + + + +int v3_ringbuf_peek(struct v3_ringbuf * ring, uchar_t * dst, uint_t len) { + int read_len = 0; + int ring_data_len = ring->current_len; + + read_len = (len > ring_data_len) ? ring_data_len : len; + + if (is_read_loop(ring, read_len)) { + int section_len = get_read_section_size(ring); + + memcpy(dst, get_read_ptr(ring), section_len); + memcpy(dst + section_len, ring->buf, read_len - section_len); + } else { + memcpy(dst, get_read_ptr(ring), read_len); + } + + return read_len; +} + + +int v3_ringbuf_delete(struct v3_ringbuf * ring, uint_t len) { + int del_len = 0; + int ring_data_len = ring->current_len; + + del_len = (len > ring_data_len) ? ring_data_len : len; + + if (is_read_loop(ring, del_len)) { + int section_len = get_read_section_size(ring); + ring->start = del_len - section_len; + } else { + ring->start += del_len; + } + + ring->current_len -= del_len; + return del_len; +} + + +int v3_ringbuf_write(struct v3_ringbuf * ring, uchar_t * src, uint_t len) { + int write_len = 0; + int ring_avail_space = ring->size - ring->current_len; + + write_len = (len > ring_avail_space) ? ring_avail_space : len; + + + if (is_write_loop(ring, write_len)) { + int section_len = get_write_section_size(ring); + + memcpy(get_write_ptr(ring), src, section_len); + ring->end = 0; + + memcpy(get_write_ptr(ring), src + section_len, write_len - section_len); + + ring->end += write_len - section_len; + } else { + memcpy(get_write_ptr(ring), src, write_len); + + ring->end += write_len; + } + + ring->current_len += write_len; + + return write_len; +} + + +void v3_print_ringbuf(struct v3_ringbuf * ring) { + int ctr = 0; + + for (ctr = 0; ctr < ring->current_len; ctr++) { + int index = (ctr + ring->start) % ring->size; + + PrintDebug("Entry %d (index=%d): %c\n", ctr, index, ring->buf[index]); + } +}