📨 chic 0.0.0
Realtime-safe channels in C
Loading...
Searching...
No Matches
claim.h
Go to the documentation of this file.
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
9
10#pragma once
11#include <stddef.h>
12#ifdef __cplusplus
13 extern "C" {
14#elif __STDC_VERSION__ < 202311L
15 #include <stdbool.h>
16#endif
17
26struct claim {
37 size_t seq;
38
40 size_t len[2];
41
43 char *ptr[2];
44};
45
51#define claim_cast(C, T, I) ((T)*)((C)->ptr[I])
52
54static inline bool claim_is_empty(const struct claim *it) {
55 return it->len[0] == 0;
56}
57
59static inline bool claim_is_split(const struct claim *it) {
60 return it->len[1] != 0;
61}
62
64static inline size_t claim_len(const struct claim *it) {
65 return it->len[0] + it->len[1];
66}
67
69static inline void claim_consume(struct claim *it) {
70 #ifdef NDEBUG
71 // Sufficient to ensure claim_is_empty(it) == true
72 it->len[0] = 0;
73 #else
74 *it = (struct claim){0};
75 #endif
76}
77
78#ifdef __cplusplus
79 }
80#endif
static void claim_consume(struct claim *it)
Consumes a claim, such that committing it has no effect.
Definition claim.h:69
static bool claim_is_empty(const struct claim *it)
Checks if a claim is empty.
Definition claim.h:54
static bool claim_is_split(const struct claim *it)
Checks if a claim is split between two contiguous regions.
Definition claim.h:59
static size_t claim_len(const struct claim *it)
Gets the total length of the claimed space in a claim.
Definition claim.h:64
Claimed space for reading or writing directly into the internal ringbuffer of a channel.
Definition claim.h:26
char * ptr[2]
The pointers to the start of the two slices.
Definition claim.h:43
size_t seq
The sequence number of this claim.
Definition claim.h:37
size_t len[2]
The lengths of the two slices.
Definition claim.h:40