Cthulhu  0.2.10
Cthulhu compiler collection
view.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #include "arena/arena.h"
4 #include "io/impl.h"
5 #include "io/impl/view.h"
6 
7 #include "base/util.h"
8 #include "base/panic.h"
9 
10 #include "core/macros.h"
11 
12 static io_view_impl_t *view_data(io_t *self)
13 {
14  return io_data(self);
15 }
16 
17 static size_t view_read(io_t *self, void *dst, size_t size)
18 {
19  io_view_impl_t *mem = view_data(self);
20  size_t len = CT_MIN(size, mem->size - mem->offset);
21  ctu_memcpy(dst, mem->data + mem->offset, len);
22  mem->offset += len;
23  return len;
24 }
25 
26 static size_t view_size(io_t *self)
27 {
28  io_view_impl_t *mem = view_data(self);
29  return mem->size;
30 }
31 
32 static size_t view_seek(io_t *self, size_t offset)
33 {
34  io_view_impl_t *mem = view_data(self);
35  mem->offset = CT_MIN(offset, mem->size);
36  return mem->offset;
37 }
38 
39 static void *view_map(io_t *self, os_protect_t protect)
40 {
41  CTASSERTF(protect == eOsProtectRead, "cannot map view with protection %d", protect);
42  io_view_impl_t *mem = view_data(self);
43 
44  return (void*)mem->data;
45 }
46 
47 static const io_callbacks_t kViewCallbacks = {
48  .fn_read = view_read,
49 
50  .fn_get_size = view_size,
51  .fn_seek = view_seek,
52 
53  .fn_map = view_map,
54 
55  .size = sizeof(io_view_impl_t),
56 };
57 
58 static io_t *impl_view_init(void *buffer, const char *name, const void *data, size_t size, arena_t *arena)
59 {
60  CTASSERT(data != NULL);
61 
62  io_view_impl_t impl = {
63  .data = data,
64  .size = size,
65  .offset = 0
66  };
67 
68  return io_init(buffer, &kViewCallbacks, eOsAccessRead, name, &impl, arena);
69 }
70 
71 static io_t *impl_string_init(void *buffer, const char *name, const char *string, arena_t *arena)
72 {
73  CTASSERT(string != NULL);
74 
75  return impl_view_init(buffer, name, string, ctu_strlen(string), arena);
76 }
77 
81 
83 io_t *io_view(const char *name, const void *data, size_t size, arena_t *arena)
84 {
85  void *buffer = ARENA_MALLOC(IO_VIEW_SIZE, name, NULL, arena);
86  return impl_view_init(buffer, name, data, size, arena);
87 }
88 
90 io_t *io_string(const char *name, const char *string, arena_t *arena)
91 {
92  void *buffer = ARENA_MALLOC(IO_VIEW_SIZE, name, NULL, arena);
93  return impl_string_init(buffer, name, string, arena);
94 }
95 
99 
100 STA_DECL
101 io_t *io_view_init(void *buffer, const char *name, const void *data, size_t size)
102 {
103  return impl_view_init(buffer, name, data, size, NULL);
104 }
105 
106 STA_DECL
107 io_t *io_string_init(void *buffer, const char *name, const char *string)
108 {
109  return impl_string_init(buffer, name, string, NULL);
110 }
CT_NODISCARD size_t size
Definition: scan.h:128
os_protect_t
file mapping memory protection
Definition: core.h:47
#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
CT_NODISCARD CT_PUREFN CT_BASE_API size_t ctu_strlen(const char *str)
get the length of a string not including the null terminator equivalent to strlen but with safety che...
Definition: util.c:87
CT_PUREFN CT_IO_API void * io_data(io_t *io)
get the user data from an io object
Definition: common.c:10
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
#define CT_MIN(L, R)
Definition: macros.h:38
#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
io object implementation
Definition: impl.h:122
a non-owning, readonly view of a buffer
Definition: view.h:15
size_t offset
current offset in data
Definition: view.h:23
size_t size
size of data
Definition: view.h:20
const char * data
pointer to data
Definition: view.h:17
STA_DECL io_t * io_view(const char *name, const void *data, size_t size, arena_t *arena)
create a readonly IO object for a given view of memory
Definition: view.c:83
STA_DECL io_t * io_string_init(void *buffer, const char *name, const char *string)
Definition: view.c:107
STA_DECL io_t * io_view_init(void *buffer, const char *name, const void *data, size_t size)
Definition: view.c:101
STA_DECL io_t * io_string(const char *name, const char *string, arena_t *arena)
create an IO view of a string create a readonly IO view of a string
Definition: view.c:90
#define IO_VIEW_SIZE
Definition: view.h:26