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 <geekos/ring_buffer.h>
21 #include <geekos/malloc.h>
22 #include <geekos/ktypes.h>
23 #include <geekos/debug.h>
26 void init_ring_buffer(struct ring_buffer * ring, uint_t size) {
27 ring->buf = Malloc(size);
32 ring->current_len = 0;
36 struct ring_buffer * create_ring_buffer(uint_t size) {
37 struct ring_buffer * ring = (struct ring_buffer *)Malloc(sizeof(struct ring_buffer));
39 init_ring_buffer(ring, size);
44 void free_ring_buffer(struct ring_buffer * ring) {
53 static inline uchar_t * get_read_ptr(struct ring_buffer * ring) {
54 return (uchar_t *)(ring->buf + ring->start);
57 static inline uchar_t * get_write_ptr(struct ring_buffer * ring) {
58 return (uchar_t *)(ring->buf + ring->end);
62 static inline int get_read_section_size(struct ring_buffer * ring) {
63 return ring->size - ring->start;
66 static inline int get_write_section_size(struct ring_buffer * ring) {
67 return ring->size - ring->end;
70 static inline int is_read_loop(struct ring_buffer * ring, uint_t len) {
71 if ((ring->start >= ring->end) && (ring->current_len > 0)) {
72 // end is past the end of the buffer
73 if (get_read_section_size(ring) < len) {
81 static inline int is_write_loop(struct ring_buffer * ring, uint_t len) {
82 if ((ring->end >= ring->start) && (ring->current_len < ring->size)) {
83 // end is past the end of the buffer
84 if (get_write_section_size(ring) < len) {
92 int rb_data_len(struct ring_buffer * ring) {
93 return ring->current_len;
96 int rb_capacity(struct ring_buffer * ring) {
100 int rb_read(struct ring_buffer * ring, char * dst, uint_t len) {
102 int ring_data_len = ring->current_len;
104 read_len = (len > ring_data_len) ? ring_data_len : len;
106 if (is_read_loop(ring, read_len)) {
107 int section_len = get_read_section_size(ring);
109 memcpy(dst, get_read_ptr(ring), section_len);
110 memcpy(dst + section_len, ring->buf, read_len - section_len);
112 ring->start = read_len - section_len;
114 memcpy(dst, get_read_ptr(ring), read_len);
116 ring->start += read_len;
119 ring->current_len -= read_len;
127 int rb_peek(struct ring_buffer * ring, char * dst, uint_t len) {
129 int ring_data_len = ring->current_len;
131 read_len = (len > ring_data_len) ? ring_data_len : len;
133 if (is_read_loop(ring, read_len)) {
134 int section_len = get_read_section_size(ring);
136 memcpy(dst, get_read_ptr(ring), section_len);
137 memcpy(dst + section_len, ring->buf, read_len - section_len);
139 memcpy(dst, get_read_ptr(ring), read_len);
146 int rb_delete(struct ring_buffer * ring, uint_t len) {
148 int ring_data_len = ring->current_len;
150 del_len = (len > ring_data_len) ? ring_data_len : len;
152 if (is_read_loop(ring, del_len)) {
153 int section_len = get_read_section_size(ring);
154 ring->start = del_len - section_len;
156 ring->start += del_len;
159 ring->current_len -= del_len;
164 int rb_write(struct ring_buffer * ring, char * src, uint_t len) {
166 int ring_avail_space = ring->size - ring->current_len;
168 write_len = (len > ring_avail_space) ? ring_avail_space : len;
171 if (is_write_loop(ring, write_len)) {
172 int section_len = get_write_section_size(ring);
174 memcpy(get_write_ptr(ring), src, section_len);
177 memcpy(get_write_ptr(ring), src + section_len, write_len - section_len);
179 ring->end += write_len - section_len;
181 memcpy(get_write_ptr(ring), src, write_len);
183 ring->end += write_len;
186 ring->current_len += write_len;
192 void print_ring_buffer(struct ring_buffer * ring) {
195 for (ctr = 0; ctr < ring->current_len; ctr++) {
196 int index = (ctr + ring->start) % ring->size;
198 PrintBoth("Entry %d (index=%d): %c\n", ctr, index, ring->buf[index]);