Skip to content

Commit 4e65477

Browse files
committed
lower the minimum C++ standard to C++20
1 parent eee7d19 commit 4e65477

9 files changed

Lines changed: 85 additions & 78 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ FetchContent_Declare(
4545

4646
add_library(ublkcpp INTERFACE)
4747
target_include_directories(ublkcpp INTERFACE include)
48-
target_compile_features(ublkcpp INTERFACE cxx_std_23) # TODO: cxx_std_26
48+
target_compile_features(ublkcpp INTERFACE cxx_std_20)
4949
if(UBLKCPP_USE_URING_CMD128)
5050
target_compile_definitions(ublkcpp INTERFACE UBLKCPP_USE_URING_CMD128)
5151
endif()

‎bin/ublkctl.cpp‎

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
#include <cstdlib>
1212
#include <fcntl.h>
1313
#include <filesystem>
14+
#include <format>
1415
#include <iostream>
15-
#include <print>
1616
#include <sched.h>
17+
#include <stdexcept>
1718
#include <string>
1819
#include <ublk.hpp>
1920
#include <unistd.h>
@@ -127,18 +128,20 @@ std::string flags_str(uint64_t flags) noexcept {
127128

128129
void dump_dev_info(uint32_t dev_id, const ublksrv_ctrl_dev_info &info,
129130
const ublk_params &params) {
130-
std::println("dev id {}: nr_hw_queues {} queue_depth {} block size {} "
131-
"dev_capacity {}",
132-
dev_id, info.nr_hw_queues, info.queue_depth,
133-
1U << params.basic.logical_bs_shift, params.basic.dev_sectors);
134-
std::println("\tmax rq size {} daemon pid {} state {}",
135-
info.max_io_buf_bytes, info.ublksrv_pid,
136-
state_str(info.state));
137-
std::println("\tflags 0x{:x} [{}]", info.flags, flags_str(info.flags));
138-
std::println("\tublkc: {}:{} ublkb: {}:{} owner: {}:{}",
139-
params.devt.char_major, params.devt.char_minor,
140-
params.devt.disk_major, params.devt.disk_minor, info.owner_uid,
141-
info.owner_gid);
131+
std::cout << std::format("dev id {}: nr_hw_queues {} queue_depth {} block "
132+
"size {} dev_capacity {}\n",
133+
dev_id, info.nr_hw_queues, info.queue_depth,
134+
1U << params.basic.logical_bs_shift,
135+
params.basic.dev_sectors);
136+
std::cout << std::format("\tmax rq size {} daemon pid {} state {}\n",
137+
info.max_io_buf_bytes, info.ublksrv_pid,
138+
state_str(info.state));
139+
std::cout << std::format("\tflags 0x{:x} [{}]\n", info.flags,
140+
flags_str(info.flags));
141+
std::cout << std::format("\tublkc: {}:{} ublkb: {}:{} owner: {}:{}\n",
142+
params.devt.char_major, params.devt.char_minor,
143+
params.devt.disk_major, params.devt.disk_minor,
144+
info.owner_uid, info.owner_gid);
142145
}
143146

144147
std::string cpu_str(const cpu_set_t &cpuset) noexcept {
@@ -171,7 +174,8 @@ ex::task<void> dump_queue_affinity(int ctrl_fd,
171174
for (uint16_t q_id = 0; q_id < info.nr_hw_queues; q_id++) {
172175
cpu_set_t cpuset;
173176
co_await ublk::get_queue_affinity(ctrl_fd, info.dev_id, q_id, &cpuset);
174-
std::println("\tqueue {}: affinity({})", q_id, cpu_str(cpuset));
177+
std::cout << std::format("\tqueue {}: affinity({})\n", q_id,
178+
cpu_str(cpuset));
175179
}
176180
}
177181

@@ -253,10 +257,11 @@ ex::task<void> run_cmd(int ctrl_fd, const FeaturesCmd &) {
253257
uint64_t features = 0;
254258
co_await ublk::get_features(ctrl_fd, &features);
255259

256-
std::println("ublk_drv features: 0x{:x}", features);
260+
std::cout << std::format("ublk_drv features: 0x{:x}\n", features);
257261
for (const auto &e : FLAG_TABLE) {
258262
if (features & e.flag)
259-
std::println("\t{:<20s}: 0x{:x}", e.short_name, e.flag);
263+
std::cout << std::format("\t{:<20s}: 0x{:x}\n", e.short_name,
264+
e.flag);
260265
}
261266
}
262267

@@ -367,7 +372,7 @@ Cmd parse_args(int argc, char *argv[]) {
367372
} else if (app.got_subcommand("quiesce")) {
368373
return quiesce_opts;
369374
} else {
370-
std::unreachable();
375+
throw std::runtime_error("unhandled subcommand");
371376
}
372377
}
373378

@@ -391,10 +396,10 @@ int main(int argc, char *argv[]) noexcept(false) {
391396
std::visit([&](const auto &c) { return run_cmd(ctrl_fd, c); },
392397
cmd)));
393398
} catch (const std::system_error &e) {
394-
std::println(std::cerr, "ublkctl: {}", e.what());
399+
std::cerr << std::format("ublkctl: {}\n", e.what());
395400
return e.code().value();
396401
} catch (const std::exception &e) {
397-
std::println(std::cerr, "ublkctl: {}", e.what());
402+
std::cerr << std::format("ublkctl: {}\n", e.what());
398403
return 1;
399404
}
400405
return 0;

‎examples/loop.cpp‎

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
#include <cstring>
1414
#include <exception>
1515
#include <fcntl.h>
16+
#include <format>
1617
#include <getopt.h>
1718
#include <iostream>
1819
#include <linux/fs.h>
19-
#include <print>
2020
#include <string>
2121
#include <sys/ioctl.h>
2222
#include <sys/signalfd.h>
@@ -163,35 +163,36 @@ static_assert(ublk::QueueHandler<LoopHandler>);
163163

164164
ex::task<void> wait_signal(int ctrl_fd, int signal_fd, uint32_t dev_id,
165165
ex::inplace_stop_source &source) {
166-
std::println("ublk-loop: ublk device {} is running...", dev_id);
166+
std::cout << std::format("ublk-loop: ublk device {} is running...\n",
167+
dev_id);
167168
auto parent_token = co_await ex::read_env(ex::get_stop_token);
168-
auto stop_request = [&] noexcept { source.request_stop(); };
169+
auto stop_request = [&]() noexcept { source.request_stop(); };
169170
ex::inplace_stop_callback<decltype(stop_request)> cb{
170171
parent_token, std::move(stop_request)};
171172
signalfd_siginfo si;
172173
co_await (condy::async_read(signal_fd, condy::buffer(&si, sizeof(si)), 0) |
173174
ex::write_env(ex::prop{ex::get_stop_token, source.get_token()}));
174-
std::println("ublk-loop: received signal {}, shutting down...",
175-
si.ssi_signo);
175+
std::cout << std::format(
176+
"ublk-loop: received signal {}, shutting down...\n", si.ssi_signo);
176177
co_await ublk::stop_dev(ctrl_fd, dev_id);
177178
}
178179

179180
void print_usage(const char *prog) {
180-
std::println("Usage: {} [OPTIONS] -f <backing_file>", prog);
181-
std::println(
182-
"Run a ublk loop device backed by a regular file or block device.");
183-
std::println();
184-
std::println("Options:");
185-
std::println(" -f <file> backing file or block device (required)");
186-
std::println(
187-
" -n <dev_id> ublk device id, -1: auto-allocation (default)");
188-
std::println(" -q <queues> nr_hw_queues (default: 1)");
189-
std::println(
190-
" -d <depth> queue depth, max in-flight io commands (default: 64)");
191-
std::println(" -b <bytes> io buffer size (default: 524288)");
192-
std::println(
193-
" -p enable UBLK_F_UNPRIVILEGED_DEV (unprivileged mode)");
194-
std::println(" -h show this help and exit");
181+
std::cout << std::format("Usage: {} [OPTIONS] -f <backing_file>\n", prog);
182+
std::cout << "Run a ublk loop device backed by a regular file or block "
183+
"device.\n";
184+
std::cout << '\n';
185+
std::cout << "Options:\n";
186+
std::cout << " -f <file> backing file or block device (required)\n";
187+
std::cout << " -n <dev_id> ublk device id, -1: auto-allocation "
188+
"(default)\n";
189+
std::cout << " -q <queues> nr_hw_queues (default: 1)\n";
190+
std::cout << " -d <depth> queue depth, max in-flight io commands "
191+
"(default: 64)\n";
192+
std::cout << " -b <bytes> io buffer size (default: 524288)\n";
193+
std::cout << " -p enable UBLK_F_UNPRIVILEGED_DEV "
194+
"(unprivileged mode)\n";
195+
std::cout << " -h show this help and exit\n";
195196
}
196197

197198
} // namespace
@@ -245,24 +246,24 @@ int main(int argc, char *argv[]) noexcept(false) {
245246
"open backing file");
246247
}
247248
auto d_backing =
248-
ublk::detail::defer([&] noexcept { close(backing_fd); });
249+
ublk::detail::defer([&]() noexcept { close(backing_fd); });
249250

250251
int flags = fcntl(backing_fd, F_GETFL);
251252
if (flags >= 0 && fcntl(backing_fd, F_SETFL, flags | O_DIRECT) < 0) {
252-
std::println(
253-
std::cerr,
254-
"ublk-loop: failed to set O_DIRECT on backing file: {}",
253+
std::cerr << std::format(
254+
"ublk-loop: failed to set O_DIRECT on backing "
255+
"file: {}\n",
255256
std::strerror(errno));
256257
}
257258

258259
FileInfo file_info;
259260
lo_file_size(backing_fd, file_info);
260261
uint64_t dev_sectors = file_info.size / SECTOR_SIZE;
261-
std::println("ublk-loop: backing file {} ({} bytes, {} sectors, "
262-
"logical {}B, physical {}B)",
263-
backing_path, file_info.size, dev_sectors,
264-
1u << file_info.logical_bs_shift,
265-
1u << file_info.physical_bs_shift);
262+
std::cout << std::format("ublk-loop: backing file {} ({} bytes, {}"
263+
" sectors, logical {}B, physical {}B)\n",
264+
backing_path, file_info.size, dev_sectors,
265+
1u << file_info.logical_bs_shift,
266+
1u << file_info.physical_bs_shift);
266267

267268
sigset_t mask;
268269
sigemptyset(&mask);
@@ -274,20 +275,20 @@ int main(int argc, char *argv[]) noexcept(false) {
274275
if (signal_fd < 0) {
275276
throw std::system_error(errno, std::generic_category(), "signalfd");
276277
}
277-
auto d = ublk::detail::defer([&] noexcept { close(signal_fd); });
278+
auto d = ublk::detail::defer([&]() noexcept { close(signal_fd); });
278279

279280
int ctrl_fd = open("/dev/ublk-control", O_RDWR | O_CLOEXEC);
280281
if (ctrl_fd < 0) {
281282
throw std::system_error(errno, std::generic_category(),
282283
"open /dev/ublk-control");
283284
}
284-
auto d2 = ublk::detail::defer([&] noexcept { close(ctrl_fd); });
285+
auto d2 = ublk::detail::defer([&]() noexcept { close(ctrl_fd); });
285286

286287
condy::RuntimeOptions options;
287288
options.enable_sqe128();
288289
condy::Runtime runtime(options);
289290
std::jthread loop([&]() { runtime.run(); });
290-
auto d3 = ublk::detail::defer([&] noexcept { runtime.allow_exit(); });
291+
auto d3 = ublk::detail::defer([&]() noexcept { runtime.allow_exit(); });
291292
ex::scheduler auto sched = condy::get_scheduler(runtime);
292293

293294
uint64_t features;
@@ -334,10 +335,10 @@ int main(int argc, char *argv[]) noexcept(false) {
334335

335336
ex::sync_wait(ex::starts_on(sched, s));
336337
} catch (const std::system_error &e) {
337-
std::println(std::cerr, "ublk-loop: {}", e.what());
338+
std::cerr << std::format("ublk-loop: {}\n", e.what());
338339
return e.code().value();
339340
} catch (const std::exception &e) {
340-
std::println(std::cerr, "ublk-loop: {}", e.what());
341+
std::cerr << std::format("ublk-loop: {}\n", e.what());
341342
return 1;
342343
}
343344

‎examples/nop.cpp‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
#include <cstdint>
1212
#include <exception>
1313
#include <fcntl.h>
14+
#include <format>
1415
#include <getopt.h>
1516
#include <iostream>
16-
#include <print>
1717
#include <sys/signalfd.h>
1818
#include <system_error>
1919
#include <thread>
@@ -46,26 +46,27 @@ void prep_params(ublk_params &params,
4646
}
4747

4848
void print_usage(const char *prog) {
49-
std::println("Usage: {} -n <dev_id>", prog);
50-
std::println("Run a no-op ublk block device.");
51-
std::println();
52-
std::println("Options:");
53-
std::println(" -n <dev_id> ublk device id to use");
54-
std::println(" -h show this help and exit");
49+
std::cout << std::format("Usage: {} -n <dev_id>\n", prog);
50+
std::cout << "Run a no-op ublk block device.\n";
51+
std::cout << '\n';
52+
std::cout << "Options:\n";
53+
std::cout << " -n <dev_id> ublk device id to use\n";
54+
std::cout << " -h show this help and exit\n";
5555
}
5656

5757
ex::task<void> wait_signal(int ctrl_fd, int signal_fd, uint32_t dev_id,
5858
ex::inplace_stop_source &source) {
59-
std::println("ublk-nop: ublk device {} is running...", dev_id);
59+
std::cout << std::format("ublk-nop: ublk device {} is running...\n",
60+
dev_id);
6061
auto parent_token = co_await ex::read_env(ex::get_stop_token);
61-
auto stop_request = [&] noexcept { source.request_stop(); };
62+
auto stop_request = [&]() noexcept { source.request_stop(); };
6263
ex::inplace_stop_callback<decltype(stop_request)> cb{
6364
parent_token, std::move(stop_request)};
6465
signalfd_siginfo si;
6566
co_await (condy::async_read(signal_fd, condy::buffer(&si, sizeof(si)), 0) |
6667
ex::write_env(ex::prop{ex::get_stop_token, source.get_token()}));
67-
std::println("ublk-nop: received signal {}, shutting down...",
68-
si.ssi_signo);
68+
std::cout << std::format("ublk-nop: received signal {}, shutting down...\n",
69+
si.ssi_signo);
6970
co_await ublk::stop_dev(ctrl_fd, dev_id);
7071
}
7172

@@ -96,20 +97,20 @@ int main(int argc, char *argv[]) noexcept(false) {
9697
if (signal_fd < 0) {
9798
throw std::system_error(errno, std::generic_category(), "signalfd");
9899
}
99-
auto d = ublk::detail::defer([&] noexcept { close(signal_fd); });
100+
auto d = ublk::detail::defer([&]() noexcept { close(signal_fd); });
100101

101102
int ctrl_fd = open("/dev/ublk-control", O_RDWR | O_CLOEXEC);
102103
if (ctrl_fd < 0) {
103104
throw std::system_error(errno, std::generic_category(),
104105
"open /dev/ublk-control");
105106
}
106-
auto d2 = ublk::detail::defer([&] noexcept { close(ctrl_fd); });
107+
auto d2 = ublk::detail::defer([&]() noexcept { close(ctrl_fd); });
107108

108109
condy::RuntimeOptions options;
109110
options.enable_sqe128();
110111
condy::Runtime runtime(options);
111112
std::jthread loop([&]() { runtime.run(); });
112-
auto d3 = ublk::detail::defer([&] noexcept { runtime.allow_exit(); });
113+
auto d3 = ublk::detail::defer([&]() noexcept { runtime.allow_exit(); });
113114
ex::scheduler auto sched = condy::get_scheduler(runtime);
114115

115116
uint64_t features;
@@ -154,10 +155,10 @@ int main(int argc, char *argv[]) noexcept(false) {
154155

155156
ex::sync_wait(ex::starts_on(sched, s));
156157
} catch (const std::system_error &e) {
157-
std::println(std::cerr, "ublk-nop: {}", e.what());
158+
std::cerr << std::format("ublk-nop: {}\n", e.what());
158159
return e.code().value();
159160
} catch (const std::exception &e) {
160-
std::println(std::cerr, "ublk-nop: {}", e.what());
161+
std::cerr << std::format("ublk-nop: {}\n", e.what());
161162
return 1;
162163
}
163164

‎include/ublk/detail/daemon.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ struct daemon_run_t {
137137

138138
std::string path = dev_path(dev_id);
139139
int ublkc_fd = co_await condy::async_open(path.c_str(), O_RDWR, 0);
140-
auto d = defer([&] noexcept { close(ublkc_fd); });
140+
auto d = defer([&]() noexcept { close(ublkc_fd); });
141141

142142
ex::simple_counting_scope scope;
143143
AllocVector<std::exception_ptr, decltype(alloc)> errs(info.nr_hw_queues,

‎include/ublk/detail/io.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct io_run_dev_t {
4242
constexpr int UBLKC_FD = 0;
4343
auto &fd_table = condy::current_runtime().fd_table();
4444
auto r = fd_table.update(UBLKC_FD, &ublkc_fd, 1);
45-
auto d = defer([&] noexcept {
45+
auto d = defer([&]() noexcept {
4646
if (r >= 0) {
4747
int fd = -1;
4848
fd_table.update(UBLKC_FD, &fd, 1); // unregister

‎include/ublk/detail/io_loop.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class IoLoop {
3333
"buffer_table init");
3434
}
3535
}
36-
thread_ = std::jthread([this, cpuset] { run_(cpuset); });
36+
thread_ = std::jthread([this, cpuset]() { run_(cpuset); });
3737
}
3838

3939
IoLoop(const IoLoop &) = delete;

‎include/ublk/detail/queue.hpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ template <typename Fd, IoHandler Handler, typename Alloc> class IoQueue {
116116
queue_depth_(queue_depth), max_io_buf_bytes_(max_io_buf_bytes),
117117
handler_(handler), alloc_(alloc) {
118118
bool ok = false;
119-
auto d = defer([&] noexcept {
119+
auto d = defer([&]() noexcept {
120120
if (!ok) {
121121
cleanup_();
122122
}
@@ -303,7 +303,7 @@ template <typename Fd, IoHandler Handler, typename Alloc> class BatchIoQueue {
303303
queue_depth_(queue_depth), max_io_buf_bytes_(max_io_buf_bytes),
304304
handler_(handler), alloc_(alloc) {
305305
bool ok = false;
306-
auto d = defer([&] noexcept {
306+
auto d = defer([&]() noexcept {
307307
if (!ok) {
308308
cleanup_();
309309
}
@@ -413,7 +413,7 @@ template <typename Fd, IoHandler Handler, typename Alloc> class BatchIoQueue {
413413
ex::upon_error([&, tag](const std::exception_ptr &ep) noexcept {
414414
worker_errs[tag] = ep;
415415
}) |
416-
ex::then([&] noexcept {
416+
ex::then([&]() noexcept {
417417
if (--running_workers == 0) {
418418
flusher.futex.notify_one();
419419
}

‎include/ublk/detail/shm.hpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ template <typename Sched, typename Alloc, typename Session>
107107
inline ex::task<void, TaskEnv<Sched, Alloc>>
108108
shm_server_run(std::string_view path, Session session) {
109109
int server_fd = co_await condy::async_socket(AF_UNIX, SOCK_STREAM, 0, 0);
110-
auto d = defer([&] noexcept { close(server_fd); });
110+
auto d = defer([&]() noexcept { close(server_fd); });
111111

112112
sockaddr_un addr = {};
113113
addr.sun_family = AF_UNIX;
@@ -127,7 +127,7 @@ shm_server_run(std::string_view path, Session session) {
127127

128128
co_await condy::async_bind(server_fd, reinterpret_cast<sockaddr *>(&addr),
129129
sizeof(addr));
130-
auto d2 = defer([&] noexcept { unlink(addr.sun_path); });
130+
auto d2 = defer([&]() noexcept { unlink(addr.sun_path); });
131131
co_await condy::async_listen(server_fd, 128);
132132

133133
ex::counting_scope scope;

0 commit comments

Comments
 (0)