diff --git a/util/buffer.c b/util/buffer.c deleted file mode 100644 index 9133a5ea5f..0000000000 --- a/util/buffer.c +++ /dev/null @@ -1,84 +0,0 @@ -#include -#include "util/buffer.h" -#include "kernel/errno.h" - -int buf_init(struct buffer *buf, size_t capacity) { - buf->data = malloc(capacity); - if (buf->data == NULL) - return _ENOMEM; - buf->capacity = capacity; - buf->start = buf->unread = 0; - pthread_mutex_init(&buf->lock, NULL); - pthread_cond_init(&buf->changed, NULL); - return 0; -} - -void buf_free(struct buffer *buf) { - free(buf->data); -} - -size_t buf_unread(struct buffer *buf) { - lock(buf); - size_t unread = buf->unread; - unlock(buf); - return unread; -} - -size_t buf_remaining(struct buffer *buf) { - lock(buf); - size_t remaining = buf->capacity - buf->unread; - unlock(buf); - return remaining; -} - -size_t buf_read(struct buffer *buf, char *str, size_t len, int flags) { - lock(buf); - while (len > buf_unread(buf)) { - if (flags & BUF_BLOCK) - wait_for(buf, changed); - else - return 0; - } - - // read up to the end of the buffer - size_t len1 = len; - if (buf->start + len > buf->capacity) - len1 = buf->capacity - buf->start; - memcpy(str, buf->data + buf->start, len1); - // wrap around if necessary - memcpy(str + len1, buf->data, len - len1); - - buf->start += len; - if (buf->start >= buf->capacity) - buf->start -= buf->capacity; - buf->unread -= len; - notify(buf, changed); - unlock(buf); - return len; -} - -size_t buf_write(struct buffer *buf, char *str, size_t len, int flags) { - lock(buf); - while (len > buf_remaining(buf)) { - if (flags & BUF_BLOCK) - wait_for(buf, changed); - else - return 0; - } - - size_t end = buf->start + buf->unread; - - // copy data up to the end of the buffer - size_t len1 = len; - if (end + len > buf->capacity) - len1 = buf->capacity - end; - memcpy(buf->data + end, str, len1); - // if we have to wrap around, do so - memcpy(buf->data, str + len1, len - len1); - - buf->unread += len; - notify(buf, changed); - unlock(buf); - return len; -} - diff --git a/util/buffer.h b/util/buffer.h deleted file mode 100644 index f6d62c0c98..0000000000 --- a/util/buffer.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef BUFFER_H -#define BUFFER_H - -#include -#include "misc.h" - -// A circular buffer. -struct buffer { - char *data; - size_t capacity; - size_t start; // first byte of data - size_t unread; - - lock_t lock; - pthread_cond_t changed; -}; - -int buf_init(struct buffer *buf, size_t capacity); -void buf_free(struct buffer *buf); - -// Returns how many bytes of data are currently in the buffer. -size_t buf_unread(struct buffer *buf); -// Returns how much more room there is in the buffer. -size_t buf_remaining(struct buffer *buf); - -// If flags is specified as BUF_BLOCK, will block if there's not enough data or -// not enough space. Otherwise, just return 0. -#define BUF_BLOCK 1 -size_t buf_read(struct buffer *buf, char *str, size_t len, int flags); -size_t buf_write(struct buffer *buf, char *str, size_t len, int flags); - -#endif