2 * This file is part of the Palacios Virtual Machine Monitor developed
\r
3 * by the V3VEE Project with funding from the United States National
\r
4 * Science Foundation and the Department of Energy.
\r
6 * The V3VEE Project is a joint project between Northwestern University
\r
7 * and the University of New Mexico. You can find out more at
\r
8 * http://www.v3vee.org
\r
10 * Copyright (c) 2010, Lei Xia <lxia@northwestern.edu>
\r
11 * Copyright (c) 2010, The V3VEE Project <http://www.v3vee.org>
\r
12 * All rights reserved.
\r
14 * This is free software. You are permitted to use, redistribute,
\r
15 * and modify it under the terms of the GNU General Public License
\r
16 * Version 2 (GPLv2). The accompanying COPYING file contains the
\r
17 * full text of the license.
\r
20 #include <linux/errno.h>
\r
21 #include <linux/percpu.h>
\r
22 #include <linux/sched.h>
\r
24 #include "palacios.h"
\r
25 #include "util-ringbuffer.h"
\r
27 void init_ringbuf(struct ringbuf * ring, unsigned int size) {
\r
28 ring->buf = kmalloc(size, GFP_KERNEL);
\r
33 ring->current_len = 0;
\r
36 struct ringbuf * create_ringbuf(unsigned int size) {
\r
37 struct ringbuf * ring = (struct ringbuf *)kmalloc(sizeof(struct ringbuf), GFP_KERNEL);
\r
38 init_ringbuf(ring, size);
\r
43 void free_ringbuf(struct ringbuf * ring) {
\r
48 static inline unsigned char * get_read_ptr(struct ringbuf * ring) {
\r
49 return (unsigned char *)(ring->buf + ring->start);
\r
53 static inline unsigned char * get_write_ptr(struct ringbuf * ring) {
\r
54 return (unsigned char *)(ring->buf + ring->end);
\r
57 static inline int get_read_section_size(struct ringbuf * ring) {
\r
58 return ring->size - ring->start;
\r
62 static inline int get_write_section_size(struct ringbuf * ring) {
\r
63 return ring->size - ring->end;
\r
67 static inline int is_read_loop(struct ringbuf * ring, unsigned int len) {
\r
68 if ((ring->start >= ring->end) && (ring->current_len > 0)) {
\r
69 // end is past the end of the buffer
\r
70 if (get_read_section_size(ring) < len) {
\r
78 static inline int is_write_loop(struct ringbuf * ring, unsigned int len) {
\r
79 if ((ring->end >= ring->start) && (ring->current_len < ring->size)) {
\r
80 // end is past the end of the buffer
\r
81 if (get_write_section_size(ring) < len) {
\r
89 static inline int ringbuf_avail_space(struct ringbuf * ring) {
\r
90 return ring->size - ring->current_len;
\r
94 int ringbuf_data_len(struct ringbuf * ring) {
\r
95 return ring->current_len;
\r
99 static inline int ringbuf_capacity(struct ringbuf * ring) {
\r
104 int ringbuf_read(struct ringbuf * ring, unsigned char * dst, unsigned int len) {
\r
106 int ring_data_len = ring->current_len;
\r
108 read_len = (len > ring_data_len) ? ring_data_len : len;
\r
110 if (is_read_loop(ring, read_len)) {
\r
111 int section_len = get_read_section_size(ring);
\r
113 memcpy(dst, get_read_ptr(ring), section_len);
\r
114 memcpy(dst + section_len, ring->buf, read_len - section_len);
\r
116 ring->start = read_len - section_len;
\r
118 memcpy(dst, get_read_ptr(ring), read_len);
\r
120 ring->start += read_len;
\r
123 ring->current_len -= read_len;
\r
131 static int ringbuf_peek(struct ringbuf * ring, unsigned char * dst, unsigned int len) {
\r
133 int ring_data_len = ring->current_len;
\r
135 read_len = (len > ring_data_len) ? ring_data_len : len;
\r
137 if (is_read_loop(ring, read_len)) {
\r
138 int section_len = get_read_section_size(ring);
\r
140 memcpy(dst, get_read_ptr(ring), section_len);
\r
141 memcpy(dst + section_len, ring->buf, read_len - section_len);
\r
143 memcpy(dst, get_read_ptr(ring), read_len);
\r
150 static int ringbuf_delete(struct ringbuf * ring, unsigned int len) {
\r
152 int ring_data_len = ring->current_len;
\r
154 del_len = (len > ring_data_len) ? ring_data_len : len;
\r
156 if (is_read_loop(ring, del_len)) {
\r
157 int section_len = get_read_section_size(ring);
\r
158 ring->start = del_len - section_len;
\r
160 ring->start += del_len;
\r
163 ring->current_len -= del_len;
\r
168 int ringbuf_write(struct ringbuf * ring, unsigned char * src, unsigned int len) {
\r
170 int ring_avail_space = ring->size - ring->current_len;
\r
172 write_len = (len > ring_avail_space) ? ring_avail_space : len;
\r
174 if (is_write_loop(ring, write_len)) {
\r
175 int section_len = get_write_section_size(ring);
\r
177 memcpy(get_write_ptr(ring), src, section_len);
\r
180 memcpy(get_write_ptr(ring), src + section_len, write_len - section_len);
\r
182 ring->end += write_len - section_len;
\r
184 memcpy(get_write_ptr(ring), src, write_len);
\r
186 ring->end += write_len;
\r
189 ring->current_len += write_len;
\r