[lib][libc] move the io hooks into a separate struct that the handle points to

This commit is contained in:
Travis Geiselbrecht
2015-12-17 13:08:17 -08:00
parent c75ca2aa09
commit 8d199a5db5
3 changed files with 50 additions and 25 deletions

View File

@@ -41,25 +41,24 @@ void register_print_callback(print_callback_t *cb);
void unregister_print_callback(print_callback_t *cb);
/* the underlying handle to talk to io devices */
typedef struct io_handle {
ssize_t (*write)(void *ctx, const char *buf, size_t len);
ssize_t (*read)(void *ctx, char *buf, size_t len);
struct io_handle;
typedef struct io_handle_hooks {
ssize_t (*write)(struct io_handle *handle, const char *buf, size_t len);
ssize_t (*read)(struct io_handle *handle, char *buf, size_t len);
} io_handle_hooks_t;
void *ctx;
#define IO_HANDLE_MAGIC 'ioh '
typedef struct io_handle {
uint32_t magic;
const io_handle_hooks_t *hooks;
} io_handle_t;
/* the main console io handle */
extern io_handle_t console_io;
/* convenience routines for the above */
static inline ssize_t io_write(const io_handle_t *io, const char *buf, size_t len)
{
return io->write(io->ctx, buf, len);
}
static inline ssize_t io_read(const io_handle_t *io, char *buf, size_t len)
{
return io->read(io->ctx, buf, len);
}
/* routines to call through the io handle */
ssize_t io_write(io_handle_t *io, const char *buf, size_t len);
ssize_t io_read(io_handle_t *io, char *buf, size_t len);
__END_CDECLS

View File

@@ -65,7 +65,7 @@ static int __panic_stdio_fgetc(void *ctx)
return (unsigned char)c;
}
static ssize_t __panic_stdio_read(void *ctx, char *s, size_t len)
static ssize_t __panic_stdio_read(io_handle_t *io, char *s, size_t len)
{
if (len == 0)
return 0;
@@ -77,7 +77,7 @@ static ssize_t __panic_stdio_read(void *ctx, char *s, size_t len)
return 1;
}
static ssize_t __panic_stdio_write(void *ctx, const char *s, size_t len)
static ssize_t __panic_stdio_write(io_handle_t *io, const char *s, size_t len)
{
for (size_t i = 0; i < len; i++) {
platform_pputc(s[i]);
@@ -87,10 +87,13 @@ static ssize_t __panic_stdio_write(void *ctx, const char *s, size_t len)
FILE *get_panic_fd(void)
{
static io_handle_t panic_io = {
static const io_handle_hooks_t panic_hooks = {
.write = __panic_stdio_write,
.read = __panic_stdio_read,
.ctx = 0
};
static io_handle_t panic_io = {
.magic = IO_HANDLE_MAGIC,
.hooks = &panic_hooks
};
static FILE panic_fd = {
.io = &panic_io

View File

@@ -22,11 +22,10 @@
*/
#include <sys/io.h>
#include <err.h>
#include <ctype.h>
#include <debug.h>
#include <stdlib.h>
#include <printf.h>
#include <stdio.h>
#include <assert.h>
#include <list.h>
#include <string.h>
#include <arch/ops.h>
@@ -90,13 +89,13 @@ void unregister_print_callback(print_callback_t *cb)
spin_unlock_restore(&print_spin_lock, state, PRINT_LOCK_FLAGS);
}
static ssize_t __debug_stdio_write(void *ctx, const char *s, size_t len)
static ssize_t __debug_stdio_write(io_handle_t *io, const char *s, size_t len)
{
out_count(s, len);
return len;
}
static ssize_t __debug_stdio_read(void *ctx, char *s, size_t len)
static ssize_t __debug_stdio_read(io_handle_t *io, char *s, size_t len)
{
if (len == 0)
return 0;
@@ -109,9 +108,33 @@ static ssize_t __debug_stdio_read(void *ctx, char *s, size_t len)
}
/* global console io handle */
io_handle_t console_io = {
static const io_handle_hooks_t console_io_hooks = {
.write = __debug_stdio_write,
.read = __debug_stdio_read,
.ctx = 0
};
io_handle_t console_io = {
.magic = IO_HANDLE_MAGIC,
.hooks = &console_io_hooks
};
ssize_t io_write(io_handle_t *io, const char *buf, size_t len)
{
DEBUG_ASSERT(io->magic == IO_HANDLE_MAGIC);
if (!io->hooks->write)
return ERR_NOT_SUPPORTED;
return io->hooks->write(io, buf, len);
}
ssize_t io_read(io_handle_t *io, char *buf, size_t len)
{
DEBUG_ASSERT(io->magic == IO_HANDLE_MAGIC);
if (!io->hooks->read)
return ERR_NOT_SUPPORTED;
return io->hooks->read(io, buf, len);
}