MOS Source Code
Loading...
Searching...
No Matches
io.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
6
7#include <mos/io/io_types.h>
8#include <mos/mm/mm_types.h>
9#include <mos/types.h>
10
11typedef struct _io io_t;
12typedef struct _vmap vmap_t; // forward declaration
13
14typedef enum
15{
16 IO_NULL, // null io port
17 IO_FILE, // a file
18 IO_DIR, // a directory (i.e. readdir())
19 IO_IPC, // an IPC channel
20 IO_PIPE, // an end of a pipe
21 IO_CONSOLE, // a console
22} io_type_t;
23
24typedef enum
25{
30 IO_SEEKABLE = 1 << 3,
31 IO_MMAPABLE = 1 << 4,
33
34typedef struct
35{
36 size_t (*read)(io_t *io, void *buf, size_t count);
37 size_t (*write)(io_t *io, const void *buf, size_t count);
38 void (*close)(io_t *io);
39 off_t (*seek)(io_t *io, off_t offset, io_seek_whence_t whence);
40 bool (*mmap)(io_t *io, vmap_t *vmap, off_t offset);
41 bool (*munmap)(io_t *io, vmap_t *vmap, bool *unmapped);
42 void (*get_name)(const io_t *io, char *buf, size_t size);
43} io_op_t;
44
45typedef struct _io
46{
47 bool closed;
48 atomic_t refcount;
51 const io_op_t *ops;
52} io_t;
53
54extern io_t *const io_null;
55
56void io_init(io_t *io, io_type_t type, io_flags_t flags, const io_op_t *ops);
57
58io_t *io_ref(io_t *io);
59io_t *io_unref(io_t *io);
60__nodiscard bool io_valid(const io_t *io);
61
62size_t io_read(io_t *io, void *buf, size_t count);
63size_t io_pread(io_t *io, void *buf, size_t count, off_t offset);
64size_t io_write(io_t *io, const void *buf, size_t count);
65off_t io_seek(io_t *io, off_t offset, io_seek_whence_t whence);
66off_t io_tell(io_t *io);
67bool io_mmap_perm_check(io_t *io, vm_flags flags, bool is_private);
68bool io_mmap(io_t *io, vmap_t *vmap, off_t offset);
69bool io_munmap(io_t *io, vmap_t *vmap, bool *unmapped);
70void io_get_name(const io_t *io, char *buf, size_t size);
MOSAPI void(1, 2) fatal_abort(const char *fmt
bool io_mmap_perm_check(io_t *io, vm_flags flags, bool is_private)
Definition io.c:224
off_t io_tell(io_t *io)
Definition io.c:218
size_t io_pread(io_t *io, void *buf, size_t count, off_t offset)
Definition io.c:151
bool io_munmap(io_t *io, vmap_t *vmap, bool *unmapped)
Definition io.c:273
struct _io io_t
Definition io.h:11
void io_init(io_t *io, io_type_t type, io_flags_t flags, const io_op_t *ops)
Definition io.c:44
io_t * io_ref(io_t *io)
Definition io.c:73
io_flags_t
Definition io.h:25
@ IO_MMAPABLE
Definition io.h:31
@ IO_EXECUTABLE
Definition io.h:29
@ IO_READABLE
Definition io.h:27
@ IO_SEEKABLE
Definition io.h:30
@ IO_WRITABLE
Definition io.h:28
@ IO_NONE
Definition io.h:26
bool io_mmap(io_t *io, vmap_t *vmap, off_t offset)
Definition io.c:254
__nodiscard bool io_valid(const io_t *io)
Definition io.c:127
off_t io_seek(io_t *io, off_t offset, io_seek_whence_t whence)
Definition io.c:199
io_t * io_unref(io_t *io)
Definition io.c:92
io_type_t
Definition io.h:15
@ IO_IPC
Definition io.h:19
@ IO_PIPE
Definition io.h:20
@ IO_CONSOLE
Definition io.h:21
@ IO_FILE
Definition io.h:17
@ IO_NULL
Definition io.h:16
@ IO_DIR
Definition io.h:18
io_t *const io_null
Definition io.c:42
size_t io_write(io_t *io, const void *buf, size_t count)
Definition io.c:180
size_t io_read(io_t *io, void *buf, size_t count)
Definition io.c:132
void io_get_name(const io_t *io, char *buf, size_t size)
Definition io.c:307
io_seek_whence_t
Definition io_types.h:6
@ MEM_PERM_NONE
Definition mm_types.h:8
@ MEM_PERM_EXEC
Definition mm_types.h:11
@ MEM_PERM_READ
Definition mm_types.h:9
@ MEM_PERM_WRITE
Definition mm_types.h:10
bool munmap(ptr_t addr, size_t size)
Unmap a page from the current process's address space.
Definition mmap.c:111
#define __nodiscard
Definition mos_global.h:35
int bool
Definition pb_syshdr.h:57
uint32_t size_t
Definition pb_syshdr.h:42
vm_flags
Definition platform.h:40
size_t size
Definition slab.c:30
Definition io.h:35
Definition io.h:46
io_type_t type
Definition io.h:50
io_flags_t flags
Definition io.h:49
bool closed
Definition io.h:47
atomic_t refcount
Definition io.h:48
const io_op_t * ops
Definition io.h:51
Definition mm.h:58
ssize_t off_t
Definition types.h:84