2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmm_ringbuffer.h>
21 #include <palacios/vmm.h>
26 void NO_INST v3_init_ringbuf(struct v3_ringbuf * ring, uint_t size) {
27 ring->buf = V3_Malloc(size);
32 ring->current_len = 0;
36 struct v3_ringbuf * v3_create_ringbuf(uint_t size) {
37 struct v3_ringbuf * ring = (struct v3_ringbuf *)V3_Malloc(sizeof(struct v3_ringbuf));
39 v3_init_ringbuf(ring, size);
45 void v3_free_ringbuf(struct v3_ringbuf * ring) {
52 static inline uchar_t * get_read_ptr(struct v3_ringbuf * ring) {
53 return (uchar_t *)(ring->buf + ring->start);
57 static inline uchar_t * get_write_ptr(struct v3_ringbuf * ring) {
58 return (uchar_t *)(ring->buf + ring->end);
62 static inline int get_read_section_size(struct v3_ringbuf * ring) {
63 return ring->size - ring->start;
67 static inline int get_write_section_size(struct v3_ringbuf * ring) {
68 return ring->size - ring->end;
72 static inline int is_read_loop(struct v3_ringbuf * ring, uint_t len) {
73 if ((ring->start >= ring->end) && (ring->current_len > 0)) {
74 // end is past the end of the buffer
75 if (get_read_section_size(ring) < len) {
83 static inline int is_write_loop(struct v3_ringbuf * ring, uint_t len) {
84 if ((ring->end >= ring->start) && (ring->current_len < ring->size)) {
85 // end is past the end of the buffer
86 if (get_write_section_size(ring) < len) {
94 int v3_ringbuf_avail_space(struct v3_ringbuf * ring) {
95 return ring->size - ring->current_len;
99 int v3_ringbuf_data_len(struct v3_ringbuf * ring) {
100 return ring->current_len;
104 int v3_ringbuf_capacity(struct v3_ringbuf * ring) {
109 int v3_ringbuf_read(struct v3_ringbuf * ring, uchar_t * dst, uint_t len) {
111 int ring_data_len = ring->current_len;
113 read_len = (len > ring_data_len) ? ring_data_len : len;
115 if (is_read_loop(ring, read_len)) {
116 int section_len = get_read_section_size(ring);
118 memcpy(dst, get_read_ptr(ring), section_len);
119 memcpy(dst + section_len, ring->buf, read_len - section_len);
121 ring->start = read_len - section_len;
123 memcpy(dst, get_read_ptr(ring), read_len);
125 ring->start += read_len;
128 ring->current_len -= read_len;
135 int v3_ringbuf_peek(struct v3_ringbuf * ring, uchar_t * dst, uint_t len) {
137 int ring_data_len = ring->current_len;
139 read_len = (len > ring_data_len) ? ring_data_len : len;
141 if (is_read_loop(ring, read_len)) {
142 int section_len = get_read_section_size(ring);
144 memcpy(dst, get_read_ptr(ring), section_len);
145 memcpy(dst + section_len, ring->buf, read_len - section_len);
147 memcpy(dst, get_read_ptr(ring), read_len);
155 int v3_ringbuf_delete(struct v3_ringbuf * ring, uint_t len) {
157 int ring_data_len = ring->current_len;
159 del_len = (len > ring_data_len) ? ring_data_len : len;
161 if (is_read_loop(ring, del_len)) {
162 int section_len = get_read_section_size(ring);
163 ring->start = del_len - section_len;
165 ring->start += del_len;
168 ring->current_len -= del_len;
174 int v3_ringbuf_write(struct v3_ringbuf * ring, uchar_t * src, uint_t len) {
176 int ring_avail_space = ring->size - ring->current_len;
178 write_len = (len > ring_avail_space) ? ring_avail_space : len;
181 if (is_write_loop(ring, write_len)) {
182 int section_len = get_write_section_size(ring);
184 // PrintDebug("Write loop: write_ptr=%p, src=%p, sec_len=%d\n",
185 // (void *)get_write_ptr(ring),(void*)src, section_len);
187 memcpy(get_write_ptr(ring), src, section_len);
190 memcpy(get_write_ptr(ring), src + section_len, write_len - section_len);
192 ring->end += write_len - section_len;
194 // PrintDebug("Writing: write_ptr=%p, src=%p, write_len=%d\n",
195 // (void *)get_write_ptr(ring),(void*)src, write_len);
197 memcpy(get_write_ptr(ring), src, write_len);
199 ring->end += write_len;
202 ring->current_len += write_len;
209 void v3_print_ringbuf(struct v3_ringbuf * ring) {
212 for (ctr = 0; ctr < ring->current_len; ctr++) {
213 int index = (ctr + ring->start) % ring->size;
215 PrintDebug("Entry %d (index=%d): %c\n", ctr, index, ring->buf[index]);