Skip to content

Commit 62337fc

Browse files
authored
Merge pull request #2330 from mazunki/bump-cxx-23
bump to c++23
2 parents e3f6160 + a157c87 commit 62337fc

File tree

24 files changed

+118
-56
lines changed

24 files changed

+118
-56
lines changed

api/kernel/rng.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include <cstdint>
2525
#include <delegate>
2626
#include <smp_utils>
27+
#ifdef INCLUDEOS_SMP_ENABLE
28+
#include <mutex>
29+
#endif
2730

2831
// Incorporate seed data into the system RNG state
2932
extern void rng_absorb(const void* input, size_t bytes);

api/net/buffer_store.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#include <vector>
2424
#include <smp>
2525
#include <likely>
26+
#ifdef INCLUDEOS_SMP_ENABLE
27+
#include <mutex>
28+
#endif
2629

2730
namespace net
2831
{

api/posix/tcp_fd.hpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,30 @@
2121

2222
#include "sockfd.hpp"
2323

24-
struct TCP_FD_Conn;
2524
struct TCP_FD_Listen;
2625

26+
struct TCP_FD_Conn
27+
{
28+
TCP_FD_Conn(net::tcp::Connection_ptr c);
29+
~TCP_FD_Conn() = default;
30+
31+
void retrieve_buffer();
32+
void set_default_read();
33+
34+
ssize_t send(const void *, size_t, int fl);
35+
ssize_t recv(void*, size_t, int fl);
36+
int close();
37+
int shutdown(int);
38+
39+
std::string to_string() const { return conn->to_string(); }
40+
41+
net::tcp::Connection_ptr conn;
42+
net::tcp::buffer_t buffer;
43+
size_t buf_offset;
44+
bool recv_disc = false;
45+
};
46+
47+
2748
class TCP_FD : public SockFD {
2849
public:
2950
using id_t = int;
@@ -68,25 +89,6 @@ class TCP_FD : public SockFD {
6889
friend struct TCP_FD_Listen;
6990
};
7091

71-
struct TCP_FD_Conn
72-
{
73-
TCP_FD_Conn(net::tcp::Connection_ptr c);
74-
75-
void retrieve_buffer();
76-
void set_default_read();
77-
78-
ssize_t send(const void *, size_t, int fl);
79-
ssize_t recv(void*, size_t, int fl);
80-
int close();
81-
int shutdown(int);
82-
83-
std::string to_string() const { return conn->to_string(); }
84-
85-
net::tcp::Connection_ptr conn;
86-
net::tcp::buffer_t buffer;
87-
size_t buf_offset;
88-
bool recv_disc = false;
89-
};
9092

9193
struct TCP_FD_Listen
9294
{

api/util/delegate.hpp

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <type_traits>
2222
#include <functional>
2323
#include <memory>
24+
#include <new> // std::launder
2425

2526
// ----- SYNOPSIS -----
2627

@@ -68,6 +69,20 @@ class empty_delegate_error : public std::bad_function_call
6869
}
6970
};
7071

72+
template<std::size_t Size, std::size_t Align>
73+
struct sbo_storage {
74+
alignas(Align) std::byte data[Size];
75+
76+
template<class T>
77+
constexpr T& as() noexcept {
78+
return *std::launder(reinterpret_cast<T*>(data));
79+
}
80+
template<class T>
81+
constexpr const T& as() const noexcept {
82+
return *std::launder(reinterpret_cast<const T*>(data));
83+
}
84+
};
85+
7186
// ----- IMPLEMENTATION -----
7287

7388
namespace detail
@@ -163,7 +178,7 @@ template<
163178
> class inplace_triv
164179
{
165180
public:
166-
using storage_t = std::aligned_storage_t<size, align>;
181+
using storage_t = sbo_storage<size, align>;
167182
using invoke_ptr_t = R(*)(storage_t&, Args&&...);
168183

169184
explicit inplace_triv() noexcept :
@@ -178,7 +193,10 @@ template<
178193
> explicit inplace_triv(T&& closure) :
179194
invoke_ptr_{ static_cast<invoke_ptr_t>(
180195
[](storage_t& storage, Args&&... args) -> R
181-
{ return reinterpret_cast<C&>(storage)(std::forward<Args>(args)...); }
196+
{
197+
auto& closure = storage.template as<C>();
198+
return closure(std::forward<Args>(args)...);
199+
}
182200
)}
183201
{
184202
static_assert(sizeof(C) <= size,
@@ -211,12 +229,12 @@ template<
211229

212230
bool empty() const noexcept
213231
{
214-
return reinterpret_cast<std::nullptr_t&>(storage_) == nullptr;
232+
return storage_.template as <std::nullptr_t&>() == nullptr;
215233
}
216234

217235
template<typename T> T* target() const noexcept
218236
{
219-
return reinterpret_cast<T*>(&storage_);
237+
return &storage_.template as<T*>();
220238
}
221239

222240
private:
@@ -233,7 +251,7 @@ template<
233251
> class inplace
234252
{
235253
public:
236-
using storage_t = std::aligned_storage_t<size, align>;
254+
using storage_t = sbo_storage<size, align>;
237255

238256
using invoke_ptr_t = R(*)(storage_t&, Args&&...);
239257
using copy_ptr_t = void(*)(storage_t&, storage_t&);
@@ -251,12 +269,18 @@ template<
251269
> explicit inplace(T&& closure) noexcept :
252270
invoke_ptr_{ static_cast<invoke_ptr_t>(
253271
[](storage_t& storage, Args&&... args) -> R
254-
{ return reinterpret_cast<C&>(storage)(std::forward<Args>(args)...); }
272+
{
273+
auto& closure = storage.template as<C>();
274+
return closure(std::forward<Args>(args)...);
275+
}
255276
) },
256277
copy_ptr_{ copy_op<C, storage_t>() },
257278
destructor_ptr_{ static_cast<destructor_ptr_t>(
258279
[](storage_t& storage) noexcept -> void
259-
{ reinterpret_cast<C&>(storage).~C(); }
280+
{
281+
auto& closure = storage.template as<C>();
282+
closure.~C();
283+
}
260284
) }
261285
{
262286
static_assert(sizeof(C) <= size,
@@ -337,7 +361,7 @@ template<
337361

338362
template<typename T> T* target() const noexcept
339363
{
340-
return reinterpret_cast<T*>(&storage_);
364+
return &storage_.template as <T>();
341365
}
342366

343367
private:
@@ -357,7 +381,7 @@ template<
357381
{
358382
return [](S& dst, S& src) noexcept -> void
359383
{
360-
new(&dst)T{ reinterpret_cast<T&>(src) };
384+
new(&dst)T{ src.template as<T>() };
361385
};
362386
}
363387

api/util/fixed_vector.hpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <cassert>
3030
#include <type_traits>
3131
#include <iterator>
32+
#include <new>
3233

3334
enum class Fixedvector_Init {
3435
UNINIT
@@ -54,14 +55,14 @@ class Fixed_vector {
5455
T& push_back(const T& e) noexcept {
5556
assert(count < N);
5657
(*this)[count] = e;
57-
return (*this)[count++];
58+
return reinterpret_raw()[count++];
5859
}
5960
// construct into
6061
template <typename... Args>
6162
T& emplace_back(Args&&... args) noexcept {
6263
assert(count < N);
63-
new (&element[count]) T(args...);
64-
return (*this)[count++];
64+
new (static_cast<void*>(reinterpret_raw() + count)) T(std::forward<Args>(args)...);
65+
return reinterpret_raw()[count++];
6566
}
6667

6768
/**
@@ -108,36 +109,36 @@ class Fixed_vector {
108109
{ return capacity() - size(); }
109110

110111
T& operator[] (uint32_t i) noexcept {
111-
return *(T*) (element + i);
112+
return reinterpret_raw()[i];
112113
}
113114
T* at (uint32_t i) noexcept {
114115
if (i >= size()) return nullptr;
115-
return (T*) (element + i);
116+
return reinterpret_raw() + i;
116117
}
117118

118119
T* data() noexcept {
119-
return (T*) &element[0];
120+
return reinterpret_raw();
120121
}
121122
T* begin() noexcept {
122-
return (T*) &element[0];
123+
return reinterpret_raw();
123124
}
124125
T* end() noexcept {
125-
return (T*) &element[count];
126+
return reinterpret_raw() + count;
126127
}
127128

128129
const T* data() const noexcept {
129-
return (T*) &element[0];
130+
return reinterpret_raw();
130131
}
131132
const T* begin() const noexcept {
132-
return (T*) &element[0];
133+
return reinterpret_raw();
133134
}
134135
const T* end() const noexcept {
135-
return (T*) &element[count];
136+
return reinterpret_raw() + count;
136137
}
137138

138139
T& back() noexcept {
139140
assert(not empty());
140-
return (T&)element[count-1];
141+
return reinterpret_raw()[count-1];
141142
}
142143

143144
constexpr int capacity() const noexcept {
@@ -151,7 +152,7 @@ class Fixed_vector {
151152
// source of the same type T, with @size elements
152153
// Note: size and capacity are not related, and they don't have to match
153154
void copy(T* src, uint32_t size) {
154-
memcpy(element, src, size * sizeof(T));
155+
memcpy(reinterpret_raw(), src, size * sizeof(T));
155156
count = size;
156157
}
157158

@@ -163,7 +164,10 @@ class Fixed_vector {
163164

164165
private:
165166
uint32_t count;
166-
typename std::aligned_storage<sizeof(T), alignof(T)>::type element[N];
167+
alignas(T) std::byte storage[sizeof(T) * N];
168+
169+
T* reinterpret_raw() noexcept { return std::launder(reinterpret_cast< T*>(storage)); }
170+
const T* reinterpret_raw() const noexcept { return std::launder(reinterpret_cast<const T*>(storage)); }
167171
};
168172

169173

cmake/includeos.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#includeos standard settings for compilation and linkers
22

3-
set(CMAKE_CXX_STANDARD 20)
3+
set(CMAKE_CXX_STANDARD 23)
44
set(CMAKE_CXX_STANDARD_REQUIRED ON)
55
set(CMAKE_CXX_EXTENSIONS OFF)
66

cmake/library.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ if (debug)
5757
endif()
5858

5959
if (CMAKE_COMPILER_IS_GNUCC)
60-
set(CMAKE_CXX_FLAGS "-m32 -MMD ${CAPABS} ${WARNS} -nostdlib -fno-omit-frame-pointer -c -std=c++20 -D_LIBCPP_HAS_NO_THREADS=1")
60+
set(CMAKE_CXX_FLAGS "-m32 -MMD ${CAPABS} ${WARNS} -nostdlib -fno-omit-frame-pointer -c -std=c++23 -D_LIBCPP_HAS_NO_THREADS=1")
6161
set(CMAKE_C_FLAGS "-m32 -MMD ${CAPABS} ${WARNS} -nostdlib -fno-omit-frame-pointer -c")
6262
else()
6363
# these kinda work with llvm
64-
set(CMAKE_CXX_FLAGS "-MMD ${CAPABS} ${OPTIMIZE} ${WARNS} -nostdlib -nostdlibinc -fno-omit-frame-pointer -c -std=c++20 -fno-threadsafe-statics -D_LIBCPP_HAS_NO_THREADS=1")
64+
set(CMAKE_CXX_FLAGS "-MMD ${CAPABS} ${OPTIMIZE} ${WARNS} -nostdlib -nostdlibinc -fno-omit-frame-pointer -c -std=c++23 -fno-threadsafe-statics -D_LIBCPP_HAS_NO_THREADS=1")
6565
set(CMAKE_C_FLAGS "-MMD ${CAPABS} ${OPTIMIZE} ${WARNS} -nostdlib -nostdlibinc -fno-omit-frame-pointer -c")
6666
endif()
6767

cmake/linux.service.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
# Linux Userspace CMake script #
33
####################################
44

5-
#set(CMAKE_CXX_STANDARD 20)
5+
#set(CMAKE_CXX_STANDARD 23)
66
set(COMMON "-g -O2 -Wall -Wextra")
7-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 ${COMMON}")
7+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++23 ${COMMON}")
88
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMMON}")
99

1010
option(BUILD_PLUGINS "Build all plugins as libraries" OFF)

cmake/os.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ if (NOT CMAKE_BUILD_TYPE)
22
set(CMAKE_BUILD_TYPE "Release")
33
endif()
44

5-
set (CMAKE_CXX_STANDARD 20)
5+
set (CMAKE_CXX_STANDARD 23)
66
set (CMAKE_CXX_STANDARD_REQUIRED ON)
77

88

lib/LiveUpdate/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
44

55
project(includeos C CXX)
66

7-
set(CMAKE_CXX_STANDARD 20)
7+
set(CMAKE_CXX_STANDARD 23)
88
set(CMAKE_CXX_STANDARD_REQUIRED ON)
99
set(CMAKE_CXX_EXTENSIONS OFF)
1010

0 commit comments

Comments
 (0)