Cthulhu  0.2.10
Cthulhu compiler collection
common.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #include "base/util.h"
4 #include "io/impl.h"
5 
6 #include "base/panic.h"
7 #include "arena/arena.h"
8 
10 void *io_data(io_t *io)
11 {
12  CTASSERT(io != NULL);
13 
14  // return past the end of the object to get the user data region.
15  // we do this rather than have a flexible array member because
16  // C requires structs with flexible arrays are always the last
17  // member of a struct, but io_t is a header struct.
18  return io->data;
19 }
20 
22 io_t *io_new(const io_callbacks_t *cb, os_access_t flags, const char *name, const void *data, arena_t *arena)
23 {
24  void *buffer = ARENA_MALLOC(sizeof(io_t) + cb->size, name, NULL, arena);
25  return io_init(buffer, cb, flags, name, data, arena);
26 }
27 
29 io_t *io_init(void *buffer, const io_callbacks_t *cb, os_access_t flags, const char *name, const void *data, arena_t *arena)
30 {
31  CTASSERT(buffer != NULL);
32  CTASSERT(cb != NULL);
33  CTASSERT(name != NULL);
34 
35  if (flags & eOsAccessWrite)
36  CTASSERTF(cb->fn_write != NULL, "%s provided no `fn_write` function for a writable object",
37  name);
38 
39  if (flags & eOsAccessRead)
40  CTASSERTF(cb->fn_read != NULL, "%s provided no `fn_read` for a readable object", name);
41 
42  io_t *io = buffer;
43 
44  io->cb = cb;
45  io->name = name;
46  io->flags = flags;
47  io->error = 0;
48  io->arena = arena;
49 
50  if (cb->size > 0)
51  {
52  CTASSERT(data != NULL);
53  ctu_memcpy(io_data(io), data, cb->size);
54  }
55 
56  return io;
57 }
STA_DECL io_t * io_init(void *buffer, const io_callbacks_t *cb, os_access_t flags, const char *name, const void *data, arena_t *arena)
Definition: common.c:29
STA_DECL io_t * io_new(const io_callbacks_t *cb, os_access_t flags, const char *name, const void *data, arena_t *arena)
Definition: common.c:22
os_access_t
file access mode
Definition: core.h:38
#define STA_DECL
sal2 annotation on function implementations to copy annotations from the declaration
CT_NOALIAS CT_BASE_API void * ctu_memcpy(STA_WRITES(size) void *CT_RESTRICT dst, STA_READS(size) const void *CT_RESTRICT src, size_t size)
copy memory from one location to another equivalent to memcpy but with safety checks
STA_DECL void * io_data(io_t *io)
get the user data from an io object
Definition: common.c:10
#define ARENA_MALLOC(size, name, parent, arena)
allocate memory from a custom allocator
Definition: arena.h:392
#define CTASSERT(expr)
assert a condition, prints the condition as a message
Definition: panic.h:130
#define CTASSERTF(expr,...)
assert a condition with a message and optional format arguments
Definition: panic.h:116
an allocator object
Definition: arena.h:86
io callback interface
Definition: impl.h:86
io_read_t fn_read
read callback may be NULL on non-readable objects
Definition: impl.h:89
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 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