Skip to content

Commit aded9ff

Browse files
authored
Updates for recent SHM API changes (#537)
- updates for recent SHM API changes - support SHM Provider and Client interface changes - up zenohc
1 parent f7831fe commit aded9ff

File tree

9 files changed

+116
-43
lines changed

9 files changed

+116
-43
lines changed

include/zenoh/api/shm/client/shm_client.hxx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace zenoh {
2727
class CppShmClient {
2828
public:
2929
virtual std::unique_ptr<CppShmSegment> attach(SegmentId segment_id) = 0;
30+
virtual ProtocolId id() const = 0;
3031
virtual ~CppShmClient() = default;
3132
};
3233

@@ -43,6 +44,8 @@ inline bool _z_cpp_shm_client_attach_fn(struct z_shm_segment_t* out_segment, z_s
4344
return false;
4445
}
4546

47+
inline ProtocolId _z_cpp_shm_client_id_fn(void* context) { return static_cast<CppShmClient*>(context)->id(); }
48+
4649
inline void _z_cpp_shm_client_drop_fn(void* context) { delete static_cast<CppShmClient*>(context); }
4750
}
4851
} // namespace shm::client::closures
@@ -59,7 +62,10 @@ class ShmClient : public Owned<::z_owned_shm_client_t> {
5962
ShmClient(std::unique_ptr<CppShmClient>&& cpp_interface) : Owned(nullptr) {
6063
zc_threadsafe_context_t context = {{cpp_interface.release()},
6164
&shm::client::closures::_z_cpp_shm_client_drop_fn};
62-
zc_shm_client_callbacks_t callbacks = {&shm::client::closures::_z_cpp_shm_client_attach_fn};
65+
zc_shm_client_callbacks_t callbacks = {
66+
&shm::client::closures::_z_cpp_shm_client_attach_fn,
67+
&shm::client::closures::_z_cpp_shm_client_id_fn,
68+
};
6369
z_shm_client_new(&this->_0, context, callbacks);
6470
}
6571
};

include/zenoh/api/shm/client_storage/client_storage.hxx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ class ShmClientStorage : public Owned<::z_owned_shm_client_storage_t> {
4747
/// clients
4848
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
4949
/// thrown in case of error.
50-
template <class Container, typename _T = std::enable_if<
51-
std::is_same<typename std::iterator_traits<typename Container::iterator>::value_type,
52-
std::pair<ProtocolId, ShmClient>>::value>>
50+
template <class Container,
51+
typename _T = std::enable_if<std::is_same<
52+
typename std::iterator_traits<typename Container::iterator>::value_type, ShmClient>::value>>
5353
ShmClientStorage(Container&& container, bool add_default_client_set, ZResult* err = nullptr)
5454
: ShmClientStorage(std::make_move_iterator(container.begin()), std::make_move_iterator(container.end()),
5555
add_default_client_set, err) {}
@@ -61,8 +61,8 @@ class ShmClientStorage : public Owned<::z_owned_shm_client_storage_t> {
6161
/// clients
6262
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
6363
/// thrown in case of error.
64-
template <class I, typename _T = std::enable_if<std::is_same<typename std::iterator_traits<I>::value_type,
65-
std::pair<ProtocolId, ShmClient>>::value>>
64+
template <class I, typename _T =
65+
std::enable_if<std::is_same<typename std::iterator_traits<I>::value_type, ShmClient>::value>>
6666
ShmClientStorage(std::move_iterator<I> begin, std::move_iterator<I> end, bool add_default_client_set,
6767
ZResult* err = nullptr)
6868
: Owned(nullptr) {
@@ -71,9 +71,8 @@ class ShmClientStorage : public Owned<::z_owned_shm_client_storage_t> {
7171

7272
// fill list with clients
7373
for (std::move_iterator<I> it = begin; it != end; ++it) {
74-
__ZENOH_RESULT_CHECK(
75-
zc_shm_client_list_add_client(interop::as_loaned_c_ptr(list), it->first, z_move(it->second._0)), err,
76-
"Failed to form list of SHM clients");
74+
__ZENOH_RESULT_CHECK(zc_shm_client_list_add_client(interop::as_loaned_c_ptr(list), z_move(it->_0)), err,
75+
"Failed to form list of SHM clients");
7776
}
7877

7978
// create client storage from the list

include/zenoh/api/shm/protocol_implementations/posix/posix_shm_provider.hxx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,20 @@ class PosixShmProvider : public ShmProvider {
2626

2727
/// @name Constructors
2828

29+
/// @brief Create a new PosixShmProvider.
30+
/// @param size size of POSIX shared memory segment to be allocated and used by the provider
31+
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
32+
/// thrown in case of error.
33+
PosixShmProvider(std::size_t size, ZResult* err = nullptr) : ShmProvider(zenoh::detail::null_object) {
34+
__ZENOH_RESULT_CHECK(::z_posix_shm_provider_new(&this->_0, size), err, "Failed to create POSIX SHM provider");
35+
}
36+
2937
/// @brief Create a new PosixShmProvider.
3038
/// @param layout layout for POSIX shared memory segment to be allocated and used by the provider
3139
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
3240
/// thrown in case of error.
3341
PosixShmProvider(const MemoryLayout& layout, ZResult* err = nullptr) : ShmProvider(zenoh::detail::null_object) {
34-
__ZENOH_RESULT_CHECK(::z_posix_shm_provider_new(&this->_0, interop::as_loaned_c_ptr(layout)), err,
42+
__ZENOH_RESULT_CHECK(::z_posix_shm_provider_with_layout_new(&this->_0, interop::as_loaned_c_ptr(layout)), err,
3543
"Failed to create POSIX SHM provider");
3644
}
3745
};

include/zenoh/api/shm/provider/alloc_layout.hxx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,18 @@ class AllocLayout : public Owned<::z_owned_alloc_layout_t> {
5151
public:
5252
/// @name Constructors
5353

54+
/// @brief Create a new Alloc Layout for SHM Provider.
55+
AllocLayout(const ShmProvider& owner_provider, std::size_t size, ZResult* err = nullptr) : Owned(nullptr) {
56+
__ZENOH_RESULT_CHECK(::z_alloc_layout_new(&this->_0, interop::as_loaned_c_ptr(owner_provider), size), err,
57+
"Failed to create SHM Alloc Layout");
58+
}
59+
5460
/// @brief Create a new Alloc Layout for SHM Provider.
5561
AllocLayout(const ShmProvider& owner_provider, std::size_t size, AllocAlignment alignment, ZResult* err = nullptr)
5662
: Owned(nullptr) {
57-
__ZENOH_RESULT_CHECK(::z_alloc_layout_new(&this->_0, interop::as_loaned_c_ptr(owner_provider), size, alignment),
58-
err, "Failed to create SHM Alloc Layout");
63+
__ZENOH_RESULT_CHECK(
64+
::z_alloc_layout_with_alignment_new(&this->_0, interop::as_loaned_c_ptr(owner_provider), size, alignment),
65+
err, "Failed to create SHM Alloc Layout");
5966
}
6067

6168
/// @name Methods

include/zenoh/api/shm/provider/shm_provider.hxx

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,45 +50,82 @@ class ShmProvider : public Owned<::z_owned_shm_provider_t> {
5050
friend class AllocLayout;
5151

5252
protected:
53-
ShmProvider(zenoh::detail::null_object_t) : Owned(nullptr){};
53+
ShmProvider(zenoh::detail::null_object_t) : Owned(nullptr) {}
5454

5555
public:
56+
BufLayoutAllocResult alloc(size_t size) const {
57+
z_buf_layout_alloc_result_t result;
58+
::z_shm_provider_alloc(&result, interop::as_loaned_c_ptr(*this), size);
59+
return Converters::from(result);
60+
}
61+
5662
BufLayoutAllocResult alloc(size_t size, AllocAlignment alignment) const {
5763
z_buf_layout_alloc_result_t result;
58-
::z_shm_provider_alloc(&result, interop::as_loaned_c_ptr(*this), size, alignment);
64+
::z_shm_provider_alloc_aligned(&result, interop::as_loaned_c_ptr(*this), size, alignment);
65+
return Converters::from(result);
66+
}
67+
68+
BufLayoutAllocResult alloc_gc(size_t size) const {
69+
z_buf_layout_alloc_result_t result;
70+
::z_shm_provider_alloc_gc(&result, interop::as_loaned_c_ptr(*this), size);
5971
return Converters::from(result);
6072
}
6173

6274
BufLayoutAllocResult alloc_gc(size_t size, AllocAlignment alignment) const {
6375
z_buf_layout_alloc_result_t result;
64-
::z_shm_provider_alloc_gc(&result, interop::as_loaned_c_ptr(*this), size, alignment);
76+
::z_shm_provider_alloc_gc_aligned(&result, interop::as_loaned_c_ptr(*this), size, alignment);
77+
return Converters::from(result);
78+
}
79+
80+
BufLayoutAllocResult alloc_gc_defrag(size_t size) const {
81+
z_buf_layout_alloc_result_t result;
82+
::z_shm_provider_alloc_gc_defrag(&result, interop::as_loaned_c_ptr(*this), size);
6583
return Converters::from(result);
6684
}
6785

6886
BufLayoutAllocResult alloc_gc_defrag(size_t size, AllocAlignment alignment) const {
6987
z_buf_layout_alloc_result_t result;
70-
::z_shm_provider_alloc_gc_defrag(&result, interop::as_loaned_c_ptr(*this), size, alignment);
88+
::z_shm_provider_alloc_gc_defrag_aligned(&result, interop::as_loaned_c_ptr(*this), size, alignment);
89+
return Converters::from(result);
90+
}
91+
92+
BufLayoutAllocResult alloc_gc_defrag_dealloc(size_t size) const {
93+
z_buf_layout_alloc_result_t result;
94+
::z_shm_provider_alloc_gc_defrag_dealloc(&result, interop::as_loaned_c_ptr(*this), size);
7195
return Converters::from(result);
7296
}
7397

7498
BufLayoutAllocResult alloc_gc_defrag_dealloc(size_t size, AllocAlignment alignment) const {
7599
z_buf_layout_alloc_result_t result;
76-
::z_shm_provider_alloc_gc_defrag_dealloc(&result, interop::as_loaned_c_ptr(*this), size, alignment);
100+
::z_shm_provider_alloc_gc_defrag_dealloc_aligned(&result, interop::as_loaned_c_ptr(*this), size, alignment);
101+
return Converters::from(result);
102+
}
103+
104+
BufLayoutAllocResult alloc_gc_defrag_blocking(size_t size) const {
105+
z_buf_layout_alloc_result_t result;
106+
::z_shm_provider_alloc_gc_defrag_blocking(&result, interop::as_loaned_c_ptr(*this), size);
77107
return Converters::from(result);
78108
}
79109

80110
BufLayoutAllocResult alloc_gc_defrag_blocking(size_t size, AllocAlignment alignment) const {
81111
z_buf_layout_alloc_result_t result;
82-
::z_shm_provider_alloc_gc_defrag_blocking(&result, interop::as_loaned_c_ptr(*this), size, alignment);
112+
::z_shm_provider_alloc_gc_defrag_blocking_aligned(&result, interop::as_loaned_c_ptr(*this), size, alignment);
83113
return Converters::from(result);
84114
}
85115

116+
ZResult alloc_gc_defrag_async(size_t size, std::unique_ptr<ShmProviderAsyncInterface> receiver) const {
117+
auto rcv = receiver.release();
118+
::zc_threadsafe_context_t context = {{rcv}, &ShmProviderAsyncInterface::drop};
119+
return ::z_shm_provider_alloc_gc_defrag_async(&rcv->_result, interop::as_loaned_c_ptr(*this), size, context,
120+
ShmProviderAsyncInterface::result);
121+
}
122+
86123
ZResult alloc_gc_defrag_async(size_t size, AllocAlignment alignment,
87124
std::unique_ptr<ShmProviderAsyncInterface> receiver) const {
88125
auto rcv = receiver.release();
89126
::zc_threadsafe_context_t context = {{rcv}, &ShmProviderAsyncInterface::drop};
90-
return ::z_shm_provider_alloc_gc_defrag_async(&rcv->_result, interop::as_loaned_c_ptr(*this), size, alignment,
91-
context, ShmProviderAsyncInterface::result);
127+
return ::z_shm_provider_alloc_gc_defrag_aligned_async(&rcv->_result, interop::as_loaned_c_ptr(*this), size,
128+
alignment, context, ShmProviderAsyncInterface::result);
92129
}
93130

94131
void defragment() const { ::z_shm_provider_defragment(interop::as_loaned_c_ptr(*this)); }
@@ -114,8 +151,7 @@ class CppShmProvider : public ShmProvider {
114151
/// @name Constructors
115152

116153
/// @brief Create a new CPP-defined ShmProvider.
117-
CppShmProvider(ProtocolId id, std::unique_ptr<CppShmProviderBackend> backend)
118-
: ShmProvider(zenoh::detail::null_object) {
154+
CppShmProvider(std::unique_ptr<CppShmProviderBackend> backend) : ShmProvider(zenoh::detail::null_object) {
119155
// init context
120156
zc_context_t context = {backend.release(),
121157
&shm::provider_backend::closures::_z_cpp_shm_provider_backend_drop_fn};
@@ -126,15 +162,16 @@ class CppShmProvider : public ShmProvider {
126162
&shm::provider_backend::closures::_z_cpp_shm_provider_backend_free_fn,
127163
&shm::provider_backend::closures::_z_cpp_shm_provider_backend_defragment_fn,
128164
&shm::provider_backend::closures::_z_cpp_shm_provider_backend_available_fn,
129-
&shm::provider_backend::closures::_z_cpp_shm_provider_backend_layout_for_fn};
165+
&shm::provider_backend::closures::_z_cpp_shm_provider_backend_layout_for_fn,
166+
&shm::provider_backend::closures::_z_cpp_shm_provider_backend_id_fn,
167+
};
130168

131169
// create provider
132-
::z_shm_provider_new(&this->_0, id, context, callbacks);
170+
::z_shm_provider_new(&this->_0, context, callbacks);
133171
}
134172

135173
/// @brief Create a new CPP-defined threadsafe ShmProvider.
136-
CppShmProvider(ProtocolId id, std::unique_ptr<CppShmProviderBackendThreadsafe> backend)
137-
: ShmProvider(zenoh::detail::null_object) {
174+
CppShmProvider(std::unique_ptr<CppShmProviderBackendThreadsafe> backend) : ShmProvider(zenoh::detail::null_object) {
138175
// init context
139176
::zc_threadsafe_context_t context = {{backend.release()},
140177
&shm::provider_backend::closures::_z_cpp_shm_provider_backend_drop_fn};
@@ -145,10 +182,12 @@ class CppShmProvider : public ShmProvider {
145182
&shm::provider_backend::closures::_z_cpp_shm_provider_backend_free_fn,
146183
&shm::provider_backend::closures::_z_cpp_shm_provider_backend_defragment_fn,
147184
&shm::provider_backend::closures::_z_cpp_shm_provider_backend_available_fn,
148-
&shm::provider_backend::closures::_z_cpp_shm_provider_backend_layout_for_fn};
185+
&shm::provider_backend::closures::_z_cpp_shm_provider_backend_layout_for_fn,
186+
&shm::provider_backend::closures::_z_cpp_shm_provider_backend_id_fn,
187+
};
149188

150189
// create provider
151-
::z_shm_provider_threadsafe_new(&this->_0, id, context, callbacks);
190+
::z_shm_provider_threadsafe_new(&this->_0, context, callbacks);
152191
}
153192
};
154193

include/zenoh/api/shm/provider/shm_provider_backend.hxx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#pragma once
1616

17+
#include <memory>
18+
1719
#include "../../base.hxx"
1820
#include "../../interop.hxx"
1921
#include "chunk.hxx"
@@ -28,6 +30,7 @@ class CppShmProviderBackendIface {
2830
virtual size_t defragment() = 0;
2931
virtual size_t available() const = 0;
3032
virtual void layout_for(MemoryLayout &layout) = 0;
33+
virtual ProtocolId id() const = 0;
3134
virtual ~CppShmProviderBackendIface() = default;
3235
};
3336

@@ -56,6 +59,9 @@ inline size_t _z_cpp_shm_provider_backend_available_fn(void *context) {
5659
inline void _z_cpp_shm_provider_backend_layout_for_fn(struct z_owned_memory_layout_t *layout, void *context) {
5760
static_cast<CppShmProviderBackend *>(context)->layout_for(interop::as_owned_cpp_ref<MemoryLayout>(layout));
5861
}
62+
inline ProtocolId _z_cpp_shm_provider_backend_id_fn(void *context) {
63+
return static_cast<CppShmProviderBackend *>(context)->id();
64+
}
5965
}
6066
} // namespace shm::provider_backend::closures
6167

tests/zenohc/config.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void test_config_to_string() {
3232
Config config = Config::create_default();
3333
auto s = config.to_string();
3434
assert(s.size() > 0);
35-
assert(s.find("{\"id\":\"") == 0);
35+
assert(s.find("{\"id\":") != std::string::npos);
3636
}
3737

3838
int main(int argc, char** argv) {

tests/zenohc/shm_api.cxx

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ class TestShmProviderBackend : public CppShmProviderBackend {
160160
delete[] busy_flags;
161161
}
162162

163+
private:
164+
static void deref_segemnt_fn(void* context) {}
165+
163166
private:
164167
virtual ChunkAllocResult alloc(const MemoryLayout& layout) override {
165168
assert(interop::detail::check(layout));
@@ -176,12 +179,16 @@ class TestShmProviderBackend : public CppShmProviderBackend {
176179
this->busy_flags[i] = true;
177180
this->bytes_available--;
178181

182+
z_owned_ptr_in_segment_t ptr;
183+
zc_threadsafe_context_t segment = {{NULL}, &TestShmProviderBackend::deref_segemnt_fn};
184+
z_ptr_in_segment_new(&ptr, &this->bytes[i], segment);
185+
179186
AllocatedChunk chunk;
180-
chunk.data = &this->bytes[i];
181-
uint64_t ptr = (uint64_t)(chunk.data);
182-
chunk.descriptpr.chunk = ptr & 0xFFFFFFFF;
187+
chunk.ptr = z_move(ptr);
188+
uint64_t data_ptr = (uint64_t)(&this->bytes[i]);
189+
chunk.descriptpr.chunk = data_ptr & 0xFFFFFFFF;
183190
chunk.descriptpr.len = 1;
184-
chunk.descriptpr.segment = (ptr >> 32) & 0xFFFFFFFF;
191+
chunk.descriptpr.segment = (data_ptr >> 32) & 0xFFFFFFFF;
185192

186193
return ChunkAllocResult(chunk);
187194
}
@@ -209,6 +216,8 @@ class TestShmProviderBackend : public CppShmProviderBackend {
209216

210217
virtual size_t available() const override { return this->bytes_available; }
211218

219+
virtual ProtocolId id() const override { return 100500; }
220+
212221
virtual void layout_for(MemoryLayout& layout) override {
213222
assert(interop::detail::check(layout));
214223

@@ -224,14 +233,13 @@ class TestShmProviderBackend : public CppShmProviderBackend {
224233
};
225234

226235
int run_c_provider() {
227-
const ProtocolId id = 100500;
228236
const size_t size = 1024;
229237

230238
// create test backend
231239
auto backend = std::make_unique<TestShmProviderBackend>(size);
232240

233241
// create provider
234-
CppShmProvider provider(id, std::move(backend));
242+
CppShmProvider provider(std::move(backend));
235243
ASSERT_VALID(provider);
236244

237245
// test provider
@@ -287,7 +295,7 @@ int run_global_client_storage() {
287295
}
288296

289297
template <bool list_api>
290-
int run_client_storage_for_list(std::vector<std::pair<ProtocolId, ShmClient>>&& list) {
298+
int run_client_storage_for_list(std::vector<ShmClient>&& list) {
291299
// create storage
292300
auto storage = [list = std::move(list)]() mutable {
293301
if constexpr (list_api) {
@@ -307,14 +315,14 @@ int run_client_storage_for_list(std::vector<std::pair<ProtocolId, ShmClient>>&&
307315
template <bool list_api>
308316
int run_client_storage_impl() {
309317
// create client list
310-
std::vector<std::pair<ProtocolId, ShmClient>> list;
318+
std::vector<ShmClient> list;
311319

312320
// create POSIX SHM Client
313321
PosixShmClient client;
314322
ASSERT_VALID(client);
315323

316324
// add client to the list
317-
list.push_back(std::make_pair(Z_SHM_POSIX_PROTOCOL_ID, std::move(client)));
325+
list.push_back(std::move(client));
318326

319327
return run_client_storage_for_list<list_api>(std::move(list));
320328
}
@@ -341,20 +349,20 @@ class TestShmClient : public CppShmClient {
341349
virtual std::unique_ptr<CppShmSegment> attach(SegmentId segment_id) override {
342350
return std::make_unique<TestShmSegment>(segment_id);
343351
}
352+
353+
virtual ProtocolId id() const override { return 100500; }
344354
};
345355

346356
int run_c_client() {
347-
const ProtocolId id = 100500;
348-
349357
// create client list
350-
std::vector<std::pair<ProtocolId, ShmClient>> list;
358+
std::vector<ShmClient> list;
351359

352360
// create C SHM Client
353361
auto client = ShmClient(std::make_unique<TestShmClient>());
354362
ASSERT_VALID(client);
355363

356364
// add client to the list
357-
list.push_back(std::make_pair(id, std::move(client)));
365+
list.push_back(std::move(client));
358366
ASSERT_NULL(client);
359367

360368
// create client storage from the list

zenoh-c

Submodule zenoh-c updated 71 files

0 commit comments

Comments
 (0)