Skip to content

Commit 4f5b5fb

Browse files
committed
Fix IORing static Linux SDK
1 parent 41daa93 commit 4f5b5fb

File tree

2 files changed

+160
-5
lines changed

2 files changed

+160
-5
lines changed

Sources/CSystem/include/io_uring.h

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,164 @@
11
#include <unistd.h>
22
#include <sys/syscall.h>
33
#include <sys/uio.h>
4-
54
#include <signal.h>
5+
6+
#if __has_include(<linux/io_uring.h>)
67
#include <linux/io_uring.h>
8+
#else
9+
// Minimal fallback definitions when linux/io_uring.h is not available (e.g. static SDK)
10+
#include <stdint.h>
11+
12+
#define IORING_OFF_SQ_RING 0ULL
13+
#define IORING_OFF_CQ_RING 0x8000000ULL
14+
#define IORING_OFF_SQES 0x10000000ULL
15+
16+
#define IORING_ENTER_GETEVENTS (1U << 0)
17+
18+
#define IORING_FEAT_SINGLE_MMAP (1U << 0)
19+
#define IORING_FEAT_NODROP (1U << 1)
20+
#define IORING_FEAT_SUBMIT_STABLE (1U << 2)
21+
#define IORING_FEAT_RW_CUR_POS (1U << 3)
22+
#define IORING_FEAT_CUR_PERSONALITY (1U << 4)
23+
#define IORING_FEAT_FAST_POLL (1U << 5)
24+
#define IORING_FEAT_POLL_32BITS (1U << 6)
25+
#define IORING_FEAT_SQPOLL_NONFIXED (1U << 7)
26+
#define IORING_FEAT_EXT_ARG (1U << 8)
27+
#define IORING_FEAT_NATIVE_WORKERS (1U << 9)
28+
#define IORING_FEAT_RSRC_TAGS (1U << 10)
29+
#define IORING_FEAT_CQE_SKIP (1U << 11)
30+
#define IORING_FEAT_LINKED_FILE (1U << 12)
31+
#define IORING_FEAT_REG_REG_RING (1U << 13)
32+
#define IORING_FEAT_RECVSEND_BUNDLE (1U << 14)
33+
#define IORING_FEAT_MIN_TIMEOUT (1U << 15)
34+
#define IORING_FEAT_RW_ATTR (1U << 16)
35+
#define IORING_FEAT_NO_IOWAIT (1U << 17)
36+
37+
typedef uint8_t __u8;
38+
typedef uint16_t __u16;
39+
typedef uint32_t __u32;
40+
typedef uint64_t __u64;
41+
typedef int32_t __s32;
42+
43+
#ifndef __kernel_rwf_t
44+
typedef int __kernel_rwf_t;
45+
#endif
46+
47+
struct io_uring_sqe {
48+
__u8 opcode;
49+
__u8 flags;
50+
__u16 ioprio;
51+
__s32 fd;
52+
union {
53+
__u64 off;
54+
__u64 addr2;
55+
struct {
56+
__u32 cmd_op;
57+
__u32 __pad1;
58+
};
59+
};
60+
union {
61+
__u64 addr;
62+
__u64 splice_off_in;
63+
struct {
64+
__u32 level;
65+
__u32 optname;
66+
};
67+
};
68+
__u32 len;
69+
union {
70+
__kernel_rwf_t rw_flags;
71+
__u32 fsync_flags;
72+
__u16 poll_events;
73+
__u32 poll32_events;
74+
__u32 sync_range_flags;
75+
__u32 msg_flags;
76+
__u32 timeout_flags;
77+
__u32 accept_flags;
78+
__u32 cancel_flags;
79+
__u32 open_flags;
80+
__u32 statx_flags;
81+
__u32 fadvise_advice;
82+
__u32 splice_flags;
83+
__u32 rename_flags;
84+
__u32 unlink_flags;
85+
__u32 hardlink_flags;
86+
__u32 xattr_flags;
87+
__u32 msg_ring_flags;
88+
__u32 uring_cmd_flags;
89+
__u32 waitid_flags;
90+
__u32 futex_flags;
91+
__u32 install_fd_flags;
92+
__u32 nop_flags;
93+
};
94+
__u64 user_data;
95+
union {
96+
__u16 buf_index;
97+
__u16 buf_group;
98+
} __attribute__((packed));
99+
__u16 personality;
100+
union {
101+
__s32 splice_fd_in;
102+
__u32 file_index;
103+
__u32 optlen;
104+
struct {
105+
__u16 addr_len;
106+
__u16 __pad3[1];
107+
};
108+
};
109+
union {
110+
struct {
111+
__u64 addr3;
112+
__u64 __pad2[1];
113+
};
114+
__u64 optval;
115+
__u8 cmd[0];
116+
};
117+
};
118+
119+
struct io_uring_cqe {
120+
__u64 user_data;
121+
__s32 res;
122+
__u32 flags;
123+
};
124+
125+
struct io_sqring_offsets {
126+
__u32 head;
127+
__u32 tail;
128+
__u32 ring_mask;
129+
__u32 ring_entries;
130+
__u32 flags;
131+
__u32 dropped;
132+
__u32 array;
133+
__u32 resv1;
134+
__u64 user_addr;
135+
};
136+
137+
struct io_cqring_offsets {
138+
__u32 head;
139+
__u32 tail;
140+
__u32 ring_mask;
141+
__u32 ring_entries;
142+
__u32 overflow;
143+
__u32 cqes;
144+
__u32 flags;
145+
__u32 resv1;
146+
__u64 user_addr;
147+
};
148+
149+
struct io_uring_params {
150+
__u32 sq_entries;
151+
__u32 cq_entries;
152+
__u32 flags;
153+
__u32 sq_thread_cpu;
154+
__u32 sq_thread_idle;
155+
__u32 features;
156+
__u32 wq_fd;
157+
__u32 resv[3];
158+
struct io_sqring_offsets sq_off;
159+
struct io_cqring_offsets cq_off;
160+
};
161+
#endif // __has_include(<linux/io_uring.h>)
7162

8163
#ifndef SWIFT_IORING_C_WRAPPER
9164
#define SWIFT_IORING_C_WRAPPER

Sources/System/IORing/IORing.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ private func setUpRing(
223223
/* prot: */ PROT_READ | PROT_WRITE,
224224
/* flags: */ MAP_SHARED | MAP_POPULATE,
225225
/* fd: */ ringDescriptor,
226-
/* offset: */ __off_t(IORING_OFF_SQ_RING)
226+
/* offset: */ off_t(IORING_OFF_SQ_RING)
227227
)
228228

229229
if ringPtr == MAP_FAILED {
@@ -238,7 +238,7 @@ private func setUpRing(
238238
/* prot: */ PROT_READ | PROT_WRITE,
239239
/* flags: */ MAP_SHARED | MAP_POPULATE,
240240
/* fd: */ ringDescriptor,
241-
/* offset: */ __off_t(IORING_OFF_SQ_RING)
241+
/* offset: */ off_t(IORING_OFF_SQ_RING)
242242
)
243243

244244
if sqPtr == MAP_FAILED {
@@ -253,7 +253,7 @@ private func setUpRing(
253253
/* prot: */ PROT_READ | PROT_WRITE,
254254
/* flags: */ MAP_SHARED | MAP_POPULATE,
255255
/* fd: */ ringDescriptor,
256-
/* offset: */ __off_t(IORING_OFF_CQ_RING)
256+
/* offset: */ off_t(IORING_OFF_CQ_RING)
257257
)
258258

259259
if cqPtr == MAP_FAILED {
@@ -270,7 +270,7 @@ private func setUpRing(
270270
/* prot: */ PROT_READ | PROT_WRITE,
271271
/* flags: */ MAP_SHARED | MAP_POPULATE,
272272
/* fd: */ ringDescriptor,
273-
/* offset: */ __off_t(IORING_OFF_SQES)
273+
/* offset: */ off_t(IORING_OFF_SQES)
274274
)
275275

276276
if sqes == MAP_FAILED {

0 commit comments

Comments
 (0)