Cthulhu  0.2.10
Cthulhu compiler collection
impl.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #pragma once
4 
5 #include "io/io.h"
6 
7 #include "os/core.h"
8 
9 #include <stddef.h>
10 #include <stdarg.h>
11 
13 
19 
21 typedef struct io_t io_t;
22 
30 typedef size_t (*io_read_t)(io_t *self, void *dst, size_t size);
31 
39 typedef size_t (*io_write_t)(io_t *self, const void *src, size_t size);
40 
50 typedef size_t (*io_fwrite_t)(io_t *self, const char *fmt, va_list args);
51 
58 typedef size_t (*io_size_t)(io_t *self);
59 
67 typedef size_t (*io_seek_t)(io_t *self, size_t offset);
68 
76 typedef void *(*io_map_t)(io_t *self, os_protect_t protect);
77 
82 typedef os_error_t (*io_close_t)(io_t *self);
83 
85 typedef struct io_callbacks_t
86 {
90 
94 
99 
103 
107 
111 
115 
117  size_t size;
119 
121 typedef struct io_t
122 {
125 
127  os_error_t error;
128 
131 
134 
136  const char *name;
137 
139  char data[];
140 } io_t;
141 
148 CT_PUREFN
149 CT_IO_API void *io_data(IN_NOTNULL io_t *io);
150 
164 CT_IO_API io_t *io_new(
165  IN_NOTNULL const io_callbacks_t *cb,
166  os_access_t flags,
167  IN_STRING const char *name,
168  STA_READS(cb->size) const void *data,
169  IN_NOTNULL arena_t *arena);
170 
187 CT_IO_API io_t *io_init(
188  STA_WRITES(sizeof(io_t) + cb->size) void *buffer,
189  IN_NOTNULL const io_callbacks_t *cb,
190  os_access_t flags,
191  IN_STRING const char *name,
192  STA_READS(cb->size) const void *data,
193  arena_t *arena);
194 
196 
CT_NODISCARD STA_WRITES(size) void *dst
CT_NODISCARD size_t size
Definition: scan.h:128
os_protect_t
file mapping memory protection
Definition: core.h:47
os_access_t
file access mode
Definition: core.h:38
#define CT_PUREFN
mark a function as pure, always returns the same value for the same arguments
Definition: analyze.h:228
#define IN_NOTNULL
annotate a parameter as not being null
#define STA_READS(size)
annotate a parameter as reading expr elements
#define IN_STRING
annotate a parameter as being a null terminated string
#define CT_BEGIN_API
Definition: compiler.h:129
#define CT_END_API
Definition: compiler.h:130
size_t(* io_fwrite_t)(io_t *self, const char *fmt, va_list args)
io write format callback seperate from io_write_t to allow for more efficient implementations if this...
Definition: impl.h:50
size_t(* io_seek_t)(io_t *self, size_t offset)
io seek callback seek from start callback
Definition: impl.h:67
CT_PUREFN CT_IO_API void * io_data(io_t *io)
get the user data from an io object
Definition: common.c:10
size_t(* io_read_t)(io_t *self, void *dst, size_t size)
io object read callback
Definition: impl.h:30
void *(* io_map_t)(io_t *self, os_protect_t protect)
io map callback map an io objects backing data into memory
Definition: impl.h:76
CT_IO_API io_t * io_init(STA_WRITES(sizeof(io_t)+cb->size) void *buffer, const io_callbacks_t *cb, os_access_t flags, const char *name, STA_READS(cb->size) const void *data, arena_t *arena)
initialize an IO object for a given interface
size_t(* io_write_t)(io_t *self, const void *src, size_t size)
io write callback
Definition: impl.h:39
os_error_t(* io_close_t)(io_t *self)
io close callback destroy an io objects backing data and any associated resources
Definition: impl.h:82
typedefCT_BEGIN_API struct io_t io_t
an io object
Definition: console.h:11
CT_IO_API io_t * io_new(const io_callbacks_t *cb, os_access_t flags, const char *name, STA_READS(cb->size) const void *data, arena_t *arena)
create a new IO object for a given interface
size_t(* io_size_t)(io_t *self)
io size callback get the total size of an io objects backing data
Definition: impl.h:58
CT_NODISCARD STA_FORMAT_STRING const char * fmt
Definition: str.h:68
an allocator object
Definition: arena.h:86
io callback interface
Definition: impl.h:86
io_seek_t fn_seek
absolute seek callback must always be provided
Definition: impl.h:106
io_map_t fn_map
file map callback must always be provided
Definition: impl.h:110
io_close_t fn_close
close callback optional if backing data does not require lifetime management
Definition: impl.h:114
io_read_t fn_read
read callback may be NULL on non-readable objects
Definition: impl.h:89
io_size_t fn_get_size
total size callback must always be provided
Definition: impl.h:102
size_t size
the size of the io objects private data
Definition: impl.h:117
io_write_t fn_write
write callback may be NULL on non-writable objects
Definition: impl.h:93
io_fwrite_t fn_fwrite
write format callback may be NULL on non-writable objects
Definition: impl.h:98
io object implementation
Definition: impl.h:122
char data[]
user data region
Definition: impl.h:139
arena_t * arena
the arena this object was allocated from
Definition: impl.h:133
const char * name
the name of this object
Definition: impl.h:136
os_error_t error
the last error set on this object
Definition: impl.h:127
const io_callbacks_t * cb
callback struct
Definition: impl.h:124
os_access_t flags
the access flags for this object
Definition: impl.h:130