Skip to content

Commit c9133c1

Browse files
authored
Remove libc dependencies (#284)
1 parent 67a8d85 commit c9133c1

File tree

11 files changed

+49
-92
lines changed

11 files changed

+49
-92
lines changed

rclrs/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ path = "src/lib.rs"
1414
# Please keep the list of dependencies alphabetically sorted,
1515
# and also state why each dependency is needed.
1616
[dependencies]
17-
# Needed for FFI
18-
libc = "0.2.43"
1917
# Needed for the Message trait, among others
2018
rosidl_runtime_rs = "0.3"
2119
# Needed for clients

rclrs/src/arguments.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use std::ffi::CString;
22
use std::os::raw::c_char;
3+
use std::os::raw::c_void;
34
use std::ptr::null_mut;
45

5-
use libc::c_void;
6-
76
use crate::error::*;
87
use crate::rcl_bindings::*;
98

rclrs/src/node.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ mod graph;
33
use std::cmp::PartialEq;
44
use std::ffi::CStr;
55
use std::fmt;
6+
use std::os::raw::c_char;
67
use std::sync::{Arc, Mutex, Weak};
78
use std::vec::Vec;
89

9-
use libc::c_char;
1010
use rosidl_runtime_rs::Message;
1111

1212
pub use self::builder::*;

rclrs/src/parameter/override_map.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::collections::BTreeMap;
22
use std::ffi::CStr;
3-
4-
use libc::c_char;
3+
use std::os::raw::c_char;
54

65
use crate::rcl_bindings::*;
76
use crate::{ParameterValue, RclrsError, ToResult};

rosidl_generator_rs/resource/Cargo.toml.em

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ version = "@(package_version)"
44
edition = "2021"
55

66
[dependencies]
7-
libc = "0.2"
87
rosidl_runtime_rs = "0.3"
98
serde = { version = "1", optional = true, features = ["derive"] }
109
@[for dep in dependency_packages]@

rosidl_generator_rs/resource/msg_rmw.rs.em

+9-9
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ type_name = msg_spec.structure.namespaced_type.name
1919

2020
#[link(name = "@(package_name)__rosidl_typesupport_c")]
2121
extern "C" {
22-
fn rosidl_typesupport_c__get_message_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() -> libc::uintptr_t;
22+
fn rosidl_typesupport_c__get_message_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() -> *const std::os::raw::c_void;
2323
}
2424

2525
#[link(name = "@(package_name)__rosidl_generator_c")]
2626
extern "C" {
2727
fn @(package_name)__@(subfolder)__@(type_name)__init(msg: *mut @(type_name)) -> bool;
28-
fn @(package_name)__@(subfolder)__@(type_name)__Sequence__init(seq: *mut rosidl_runtime_rs::Sequence<@(type_name)>, size: libc::size_t) -> bool;
28+
fn @(package_name)__@(subfolder)__@(type_name)__Sequence__init(seq: *mut rosidl_runtime_rs::Sequence<@(type_name)>, size: usize) -> bool;
2929
fn @(package_name)__@(subfolder)__@(type_name)__Sequence__fini(seq: *mut rosidl_runtime_rs::Sequence<@(type_name)>);
30+
fn @(package_name)__@(subfolder)__@(type_name)__Sequence__copy(in_seq: &rosidl_runtime_rs::Sequence<@(type_name)>, out_seq: *mut rosidl_runtime_rs::Sequence<@(type_name)>) -> bool;
3031
}
3132

3233
@# Drop is not needed, since the default drop glue does the same as fini here:
@@ -80,18 +81,17 @@ impl Default for @(type_name) {
8081
}
8182
8283
impl rosidl_runtime_rs::SequenceAlloc for @(type_name) {
83-
fn sequence_init(seq: &mut rosidl_runtime_rs::Sequence<Self>, size: libc::size_t) -> bool {
84-
// SAFETY: This is safe since a the point is guaranteed to be valid/initialized.
84+
fn sequence_init(seq: &mut rosidl_runtime_rs::Sequence<Self>, size: usize) -> bool {
85+
// SAFETY: This is safe since the pointer is guaranteed to be valid/initialized.
8586
unsafe { @(package_name)__@(subfolder)__@(type_name)__Sequence__init(seq as *mut _, size) }
8687
}
8788
fn sequence_fini(seq: &mut rosidl_runtime_rs::Sequence<Self>) {
88-
// SAFETY: This is safe since a the point is guaranteed to be valid/initialized.
89+
// SAFETY: This is safe since the pointer is guaranteed to be valid/initialized.
8990
unsafe { @(package_name)__@(subfolder)__@(type_name)__Sequence__fini(seq as *mut _) }
9091
}
9192
fn sequence_copy(in_seq: &rosidl_runtime_rs::Sequence<Self>, out_seq: &mut rosidl_runtime_rs::Sequence<Self>) -> bool {
92-
out_seq.resize_to_at_least(in_seq.len());
93-
out_seq.clone_from_slice(in_seq.as_slice());
94-
true
93+
// SAFETY: This is safe since the pointer is guaranteed to be valid/initialized.
94+
unsafe { @(package_name)__@(subfolder)__@(type_name)__Sequence__copy(in_seq, out_seq as *mut _) }
9595
}
9696
}
9797
@@ -103,7 +103,7 @@ impl rosidl_runtime_rs::Message for @(type_name) {
103103
104104
impl rosidl_runtime_rs::RmwMessage for @(type_name) where Self: Sized {
105105
const TYPE_NAME: &'static str = "@(package_name)/@(subfolder)/@(type_name)";
106-
fn get_type_support() -> libc::uintptr_t {
106+
fn get_type_support() -> *const std::os::raw::c_void {
107107
// SAFETY: No preconditions for this function.
108108
unsafe { rosidl_typesupport_c__get_message_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() }
109109
}

rosidl_generator_rs/resource/srv.rs.em

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type_name = srv_spec.namespaced_type.name
2424

2525
#[link(name = "@(package_name)__rosidl_typesupport_c")]
2626
extern "C" {
27-
fn rosidl_typesupport_c__get_service_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() -> libc::uintptr_t;
27+
fn rosidl_typesupport_c__get_service_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() -> *const std::os::raw::c_void;
2828
}
2929

3030
// Corresponds to @(package_name)__@(subfolder)__@(type_name)
@@ -34,7 +34,7 @@ impl rosidl_runtime_rs::Service for @(type_name) {
3434
type Request = crate::@(subfolder)::@(type_name)_Request;
3535
type Response = crate::@(subfolder)::@(type_name)_Response;
3636

37-
fn get_type_support() -> libc::uintptr_t {
37+
fn get_type_support() -> *const std::os::raw::c_void {
3838
// SAFETY: No preconditions for this function.
3939
unsafe { rosidl_typesupport_c__get_service_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() }
4040
}
@@ -61,7 +61,7 @@ type_name = srv_spec.namespaced_type.name
6161

6262
#[link(name = "@(package_name)__rosidl_typesupport_c")]
6363
extern "C" {
64-
fn rosidl_typesupport_c__get_service_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() -> libc::uintptr_t;
64+
fn rosidl_typesupport_c__get_service_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() -> *const std::os::raw::c_void;
6565
}
6666

6767
// Corresponds to @(package_name)__@(subfolder)__@(type_name)
@@ -71,7 +71,7 @@ type_name = srv_spec.namespaced_type.name
7171
type Request = crate::@(subfolder)::rmw::@(type_name)_Request;
7272
type Response = crate::@(subfolder)::rmw::@(type_name)_Response;
7373

74-
fn get_type_support() -> libc::uintptr_t {
74+
fn get_type_support() -> *const std::os::raw::c_void {
7575
// SAFETY: No preconditions for this function.
7676
unsafe { rosidl_typesupport_c__get_service_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() }
7777
}

rosidl_runtime_rs/Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ path = "src/lib.rs"
1313
# Please keep the list of dependencies alphabetically sorted,
1414
# and also state why each dependency is needed.
1515
[dependencies]
16-
# Needed for FFI
17-
libc = "0.2"
1816
# Optional dependency for making it possible to convert messages to and from
1917
# formats such as JSON, YAML, Pickle, etc.
2018
serde = { version = "1", optional = true }
@@ -24,4 +22,3 @@ serde = { version = "1", optional = true }
2422
quickcheck = "1"
2523
# Needed for testing serde support
2624
serde_json = "1"
27-

rosidl_runtime_rs/src/sequence.rs

+10-48
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ use crate::traits::SequenceAlloc;
3737
#[repr(C)]
3838
pub struct Sequence<T: SequenceAlloc> {
3939
data: *mut T,
40-
size: libc::size_t,
41-
capacity: libc::size_t,
40+
size: usize,
41+
capacity: usize,
4242
}
4343

4444
/// A bounded sequence.
@@ -274,30 +274,6 @@ where
274274
}
275275
}
276276

277-
impl<T: Default + SequenceAlloc> Sequence<T> {
278-
/// Internal function for the sequence_copy impl. To be removed when rosidl#650 is backported and released.
279-
pub fn resize_to_at_least(&mut self, len: usize) {
280-
let allocation_size = std::mem::size_of::<Self>() * len;
281-
if self.capacity < len {
282-
// SAFETY: The memory in self.data is owned by C.
283-
let data = unsafe { libc::realloc(self.data as *mut _, allocation_size) } as *mut T;
284-
if data.is_null() {
285-
panic!("realloc failed");
286-
}
287-
// Initialize the new memory
288-
for i in self.capacity..len {
289-
// SAFETY: i is in bounds, and write() is appropriate for initializing uninitialized memory
290-
unsafe {
291-
data.add(i).write(T::default());
292-
}
293-
}
294-
self.data = data;
295-
self.size = len;
296-
self.capacity = len;
297-
}
298-
}
299-
}
300-
301277
// ========================= impl for BoundedSequence =========================
302278

303279
impl<T: Debug + SequenceAlloc, const N: usize> Debug for BoundedSequence<T, N> {
@@ -518,12 +494,16 @@ macro_rules! impl_sequence_alloc_for_primitive_type {
518494
($rust_type:ty, $init_func:ident, $fini_func:ident, $copy_func:ident) => {
519495
#[link(name = "rosidl_runtime_c")]
520496
extern "C" {
521-
fn $init_func(seq: *mut Sequence<$rust_type>, size: libc::size_t) -> bool;
497+
fn $init_func(seq: *mut Sequence<$rust_type>, size: usize) -> bool;
522498
fn $fini_func(seq: *mut Sequence<$rust_type>);
499+
fn $copy_func(
500+
in_seq: *const Sequence<$rust_type>,
501+
out_seq: *mut Sequence<$rust_type>,
502+
) -> bool;
523503
}
524504

525505
impl SequenceAlloc for $rust_type {
526-
fn sequence_init(seq: &mut Sequence<Self>, size: libc::size_t) -> bool {
506+
fn sequence_init(seq: &mut Sequence<Self>, size: usize) -> bool {
527507
// SAFETY: There are no special preconditions to the sequence_init function.
528508
unsafe {
529509
// This allocates space and sets seq.size and seq.capacity to size
@@ -538,26 +518,8 @@ macro_rules! impl_sequence_alloc_for_primitive_type {
538518
unsafe { $fini_func(seq as *mut _) }
539519
}
540520
fn sequence_copy(in_seq: &Sequence<Self>, out_seq: &mut Sequence<Self>) -> bool {
541-
let allocation_size = std::mem::size_of::<Self>() * in_seq.size;
542-
if out_seq.capacity < in_seq.size {
543-
// SAFETY: The memory in out_seq.data is owned by C.
544-
let data = unsafe { libc::realloc(out_seq.data as *mut _, allocation_size) };
545-
if data.is_null() {
546-
return false;
547-
}
548-
out_seq.data = data as *mut _;
549-
out_seq.capacity = in_seq.size;
550-
}
551-
// SAFETY: The memory areas don't overlap.
552-
unsafe {
553-
libc::memcpy(
554-
out_seq.data as *mut _,
555-
in_seq.data as *const _,
556-
allocation_size,
557-
);
558-
}
559-
out_seq.size = in_seq.size;
560-
true
521+
// SAFETY: There are no special preconditions to the sequence_copy function.
522+
unsafe { $copy_func(in_seq as *const _, out_seq as *mut _) }
561523
}
562524
}
563525
};

rosidl_runtime_rs/src/string.rs

+20-17
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ use crate::traits::SequenceAlloc;
2929
pub struct String {
3030
/// Dynamic memory in this type is allocated and deallocated by C, but this is a detail that is managed by
3131
/// the relevant functions and trait impls.
32-
data: *mut libc::c_char,
33-
size: libc::size_t,
34-
capacity: libc::size_t,
32+
data: *mut std::os::raw::c_char,
33+
size: usize,
34+
capacity: usize,
3535
}
3636

3737
/// A zero-terminated string of 16-bit characters.
@@ -50,9 +50,9 @@ pub struct String {
5050
/// ```
5151
#[repr(C)]
5252
pub struct WString {
53-
data: *mut libc::c_ushort,
54-
size: libc::size_t,
55-
capacity: libc::size_t,
53+
data: *mut std::os::raw::c_ushort,
54+
size: usize,
55+
capacity: usize,
5656
}
5757

5858
/// A zero-terminated string of 8-bit characters with a length limit.
@@ -117,9 +117,13 @@ macro_rules! string_impl {
117117
extern "C" {
118118
fn $init(s: *mut $string) -> bool;
119119
fn $fini(s: *mut $string);
120-
fn $assignn(s: *mut $string, value: *const $char_type, n: libc::size_t) -> bool;
121-
fn $sequence_init(seq: *mut Sequence<$string>, size: libc::size_t) -> bool;
120+
fn $assignn(s: *mut $string, value: *const $char_type, n: usize) -> bool;
121+
fn $sequence_init(seq: *mut Sequence<$string>, size: usize) -> bool;
122122
fn $sequence_fini(seq: *mut Sequence<$string>);
123+
fn $sequence_copy(
124+
in_seq: *const Sequence<$string>,
125+
out_seq: *mut Sequence<$string>,
126+
) -> bool;
123127
}
124128

125129
impl Default for $string {
@@ -226,7 +230,7 @@ macro_rules! string_impl {
226230
unsafe impl Sync for $string {}
227231

228232
impl SequenceAlloc for $string {
229-
fn sequence_init(seq: &mut Sequence<Self>, size: libc::size_t) -> bool {
233+
fn sequence_init(seq: &mut Sequence<Self>, size: usize) -> bool {
230234
// SAFETY: There are no special preconditions to the sequence_init function.
231235
unsafe { $sequence_init(seq as *mut _, size) }
232236
}
@@ -235,17 +239,16 @@ macro_rules! string_impl {
235239
unsafe { $sequence_fini(seq as *mut _) }
236240
}
237241
fn sequence_copy(in_seq: &Sequence<Self>, out_seq: &mut Sequence<Self>) -> bool {
238-
out_seq.resize_to_at_least(in_seq.len());
239-
out_seq.clone_from_slice(in_seq.as_slice());
240-
true
242+
// SAFETY: There are no special preconditions to the sequence_copy function.
243+
unsafe { $sequence_copy(in_seq as *const _, out_seq as *mut _) }
241244
}
242245
}
243246
};
244247
}
245248

246249
string_impl!(
247250
String,
248-
libc::c_char,
251+
std::os::raw::c_char,
249252
u8,
250253
from_utf8_lossy,
251254
rosidl_runtime_c__String__init,
@@ -257,7 +260,7 @@ string_impl!(
257260
);
258261
string_impl!(
259262
WString,
260-
libc::c_ushort,
263+
std::os::raw::c_ushort,
261264
u16,
262265
from_utf16_lossy,
263266
rosidl_runtime_c__U16String__init,
@@ -330,7 +333,7 @@ impl<const N: usize> Debug for BoundedString<N> {
330333
}
331334

332335
impl<const N: usize> Deref for BoundedString<N> {
333-
type Target = [libc::c_char];
336+
type Target = [std::os::raw::c_char];
334337
fn deref(&self) -> &Self::Target {
335338
self.inner.deref()
336339
}
@@ -349,7 +352,7 @@ impl<const N: usize> Display for BoundedString<N> {
349352
}
350353

351354
impl<const N: usize> SequenceAlloc for BoundedString<N> {
352-
fn sequence_init(seq: &mut Sequence<Self>, size: libc::size_t) -> bool {
355+
fn sequence_init(seq: &mut Sequence<Self>, size: usize) -> bool {
353356
// SAFETY: There are no special preconditions to the rosidl_runtime_c__String__Sequence__init function.
354357
unsafe {
355358
rosidl_runtime_c__String__Sequence__init(seq as *mut Sequence<Self> as *mut _, size)
@@ -415,7 +418,7 @@ impl<const N: usize> Display for BoundedWString<N> {
415418
}
416419

417420
impl<const N: usize> SequenceAlloc for BoundedWString<N> {
418-
fn sequence_init(seq: &mut Sequence<Self>, size: libc::size_t) -> bool {
421+
fn sequence_init(seq: &mut Sequence<Self>, size: usize) -> bool {
419422
// SAFETY: There are no special preconditions to the rosidl_runtime_c__U16String__Sequence__init function.
420423
unsafe {
421424
rosidl_runtime_c__U16String__Sequence__init(seq as *mut Sequence<Self> as *mut _, size)

rosidl_runtime_rs/src/traits.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::fmt::Debug;
2424
/// User code never needs to call these trait methods, much less implement this trait.
2525
pub trait SequenceAlloc: Sized {
2626
/// Wraps the corresponding init function generated by `rosidl_generator_c`.
27-
fn sequence_init(seq: &mut crate::Sequence<Self>, size: libc::size_t) -> bool;
27+
fn sequence_init(seq: &mut crate::Sequence<Self>, size: usize) -> bool;
2828
/// Wraps the corresponding fini function generated by `rosidl_generator_c`.
2929
fn sequence_fini(seq: &mut crate::Sequence<Self>);
3030
/// Wraps the corresponding copy function generated by `rosidl_generator_c`.
@@ -42,7 +42,7 @@ pub trait RmwMessage: Clone + Debug + Default + Send + Sync + Message {
4242
const TYPE_NAME: &'static str;
4343

4444
/// Get a pointer to the correct `rosidl_message_type_support_t` structure.
45-
fn get_type_support() -> libc::uintptr_t;
45+
fn get_type_support() -> *const std::os::raw::c_void;
4646
}
4747

4848
/// Trait for types that can be used in a `rclrs::Subscription` and a `rclrs::Publisher`.
@@ -158,5 +158,5 @@ pub trait Service: 'static {
158158
type Response: Message;
159159

160160
/// Get a pointer to the correct `rosidl_service_type_support_t` structure.
161-
fn get_type_support() -> libc::uintptr_t;
161+
fn get_type_support() -> *const std::os::raw::c_void;
162162
}

0 commit comments

Comments
 (0)