summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordzwdz2022-08-23 19:15:59 +0200
committerdzwdz2022-08-23 19:15:59 +0200
commit38cf66edaacc4e58b561bea8a77abfd3facf59fe (patch)
tree2e9ff33e195078fdcd454607082064e949b29bd4 /src
parent03c5dd9462492e291c6a49b88e1cd9ab34d86b6f (diff)
shared/ring: rename ring_size to ring_used, add ring_avail
Diffstat (limited to 'src')
-rw-r--r--src/kernel/arch/amd64/driver/ps2.c2
-rw-r--r--src/kernel/arch/amd64/driver/serial.c2
-rw-r--r--src/shared/container/ring.c6
-rw-r--r--src/shared/container/ring.h5
-rw-r--r--src/user/app/drawmouse/drawmouse.c2
-rw-r--r--src/user/app/init/driver/ps2.c2
-rw-r--r--src/user/app/tests/shared/ringbuf.c16
7 files changed, 22 insertions, 13 deletions
diff --git a/src/kernel/arch/amd64/driver/ps2.c b/src/kernel/arch/amd64/driver/ps2.c
index 35165d9..9148c98 100644
--- a/src/kernel/arch/amd64/driver/ps2.c
+++ b/src/kernel/arch/amd64/driver/ps2.c
@@ -91,7 +91,7 @@ enum {
};
static void read_backlog(struct vfs_request *req, ring_t *r, struct vfs_request **queue) {
- if (ring_size(r) == 0) {
+ if (ring_used(r) == 0) {
/* nothing to read, join queue */
assert(!req->postqueue_next);
while (*queue) queue = &(*queue)->postqueue_next;
diff --git a/src/kernel/arch/amd64/driver/serial.c b/src/kernel/arch/amd64/driver/serial.c
index b3bfe29..cf21b61 100644
--- a/src/kernel/arch/amd64/driver/serial.c
+++ b/src/kernel/arch/amd64/driver/serial.c
@@ -72,7 +72,7 @@ static void accept(struct vfs_request *req) {
vfsreq_finish_short(req, valid ? 0 : -1);
break;
case VFSOP_READ:
- if (ring_size((void*)&backlog) == 0) {
+ if (ring_used((void*)&backlog) == 0) {
/* nothing to read, join queue */
assert(!req->postqueue_next);
struct vfs_request **slot = &blocked_on;
diff --git a/src/shared/container/ring.c b/src/shared/container/ring.c
index 67944b0..21c63a1 100644
--- a/src/shared/container/ring.c
+++ b/src/shared/container/ring.c
@@ -7,13 +7,17 @@ static bool at_end(ring_t *r) {
|| (r->_head + 1 == r->capacity && r->_tail == 0);
}
-size_t ring_size(ring_t *r) {
+size_t ring_used(ring_t *r) {
if (r->_head >= r->_tail)
return r->_head - r->_tail;
else
return r->_head + r->capacity - r->_tail;
}
+size_t ring_avail(ring_t *r) {
+ return r->capacity - ring_used(r);
+}
+
void ring_put(ring_t *r, void *buf, size_t len) {
// TODO do something similar to ring_get
for (size_t i = 0; i < len; i++)
diff --git a/src/shared/container/ring.h b/src/shared/container/ring.h
index bc978ce..4f0d310 100644
--- a/src/shared/container/ring.h
+++ b/src/shared/container/ring.h
@@ -8,7 +8,10 @@ typedef struct {
size_t _head, _tail;
} ring_t;
-size_t ring_size(ring_t*);
+/** Returns amount of bytes stored in the buffer. */
+size_t ring_used(ring_t*);
+/** Returns amount of space left in the buffer. */
+size_t ring_avail(ring_t*);
void ring_put(ring_t*, void*, size_t);
void ring_put1b(ring_t*, uint8_t);
diff --git a/src/user/app/drawmouse/drawmouse.c b/src/user/app/drawmouse/drawmouse.c
index 7e7c55b..47e8e8a 100644
--- a/src/user/app/drawmouse/drawmouse.c
+++ b/src/user/app/drawmouse/drawmouse.c
@@ -66,7 +66,7 @@ int main(void) {
int len = _syscall_read(fd, buf, sizeof buf, 0);
if (len == 0) break;
ring_put(&r, buf, len);
- while (ring_size(&r) >= 3) {
+ while (ring_used(&r) >= 3) {
struct packet p;
ring_get(&r, &p, sizeof p);
p.dy *= -1;
diff --git a/src/user/app/init/driver/ps2.c b/src/user/app/init/driver/ps2.c
index 961ea53..85b83b7 100644
--- a/src/user/app/init/driver/ps2.c
+++ b/src/user/app/init/driver/ps2.c
@@ -61,7 +61,7 @@ static void main_loop(void) {
break;
case VFSOP_READ:
- while (ring_size((void*)&backlog) == 0) {
+ while (ring_used((void*)&backlog) == 0) {
/* read raw input until we have something to output */
int len = _syscall_read(fd, buf, sizeof buf, 0);
if (len == 0) break;
diff --git a/src/user/app/tests/shared/ringbuf.c b/src/user/app/tests/shared/ringbuf.c
index 7330bbe..d4e8dc9 100644
--- a/src/user/app/tests/shared/ringbuf.c
+++ b/src/user/app/tests/shared/ringbuf.c
@@ -11,34 +11,36 @@ static void test_ringbuf(void) {
/* aliasing */
for (size_t i = 0; i < 16; i++) {
- test(ring_size(&r) == 0);
+ test(ring_used(&r) == 0);
+ test(ring_avail(&r) == 16);
ring_put(&r, "11 bytes...", 11);
- test(ring_size(&r) == 11);
+ test(ring_used(&r) == 11);
+ test(ring_avail(&r) == 5);
memset(cmpbuf, 0, sizeof cmpbuf);
test(ring_get(&r, cmpbuf, 16) == 11);
test(memcmp(cmpbuf, "11 bytes...", 11) == 0);
}
- test(ring_size(&r) == 0);
+ test(ring_used(&r) == 0);
for (size_t i = 0; i < 7; i++)
ring_put1b(&r, num_written++);
- test(ring_size(&r) == 7);
+ test(ring_used(&r) == 7);
for (size_t i = 0; i < 3; i++) {
ring_get(&r, &c, 1);
test(num_read++ == c);
}
- test(ring_size(&r) == 4);
+ test(ring_used(&r) == 4);
for (size_t j = 0; j < 40; j++) {
for (size_t i = 0; i < 7; i++)
ring_put1b(&r, num_written++ & 0xff);
- test(ring_size(&r) == 11);
+ test(ring_used(&r) == 11);
for (size_t i = 0; i < 7; i++) {
ring_get(&r, &c, 1);
test((num_read++ & 0xff) == c);
}
- test(ring_size(&r) == 4);
+ test(ring_used(&r) == 4);
}
}