Skip to content

Commit 26ee6b9

Browse files
committed
Experiment with changing Buffer::parts_mut to Buffer::as_mut_ptr.
Based on an idea suggested [here], experiment with changing from a `parts_mut` that returns `(*mut T, usize)` to a `as_mut_ptr` which returns a `*mut [MaybeUninit<T>]`. [here]: https://hachyderm.io/deck/@wffl@im-in.space/114145162248844972
1 parent 04e2a5a commit 26ee6b9

File tree

21 files changed

+245
-169
lines changed

21 files changed

+245
-169
lines changed

src/backend/libc/event/syscalls.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use crate::backend::c;
44
#[cfg(any(linux_kernel, solarish, target_os = "redox"))]
55
use crate::backend::conv::ret;
66
use crate::backend::conv::ret_c_int;
7-
#[cfg(any(linux_kernel, target_os = "illumos", target_os = "redox"))]
8-
use crate::backend::conv::ret_u32;
97
#[cfg(solarish)]
108
use crate::event::port::Event;
119
#[cfg(any(
@@ -21,7 +19,13 @@ use crate::event::{PollFd, Timespec};
2119
use crate::io;
2220
#[cfg(any(linux_kernel, target_os = "illumos", target_os = "redox"))]
2321
use crate::utils::as_ptr;
24-
#[cfg(solarish)]
22+
#[cfg(any(
23+
bsd,
24+
linux_kernel,
25+
solarish,
26+
target_os = "illumos",
27+
target_os = "redox"
28+
))]
2529
use core::mem::MaybeUninit;
2630
#[cfg(any(
2731
bsd,
@@ -47,6 +51,8 @@ use {crate::backend::conv::borrowed_fd, crate::fd::BorrowedFd};
4751
target_os = "redox"
4852
))]
4953
use {crate::backend::conv::ret_owned_fd, crate::fd::OwnedFd};
54+
#[cfg(any(linux_kernel, target_os = "illumos", target_os = "redox"))]
55+
use {crate::backend::conv::ret_u32, crate::event::epoll::Event};
5056
#[cfg(bsd)]
5157
use {crate::event::kqueue::Event, crate::utils::as_ptr};
5258

@@ -95,7 +101,7 @@ pub(crate) fn kqueue() -> io::Result<OwnedFd> {
95101
pub(crate) unsafe fn kevent(
96102
kq: BorrowedFd<'_>,
97103
changelist: &[Event],
98-
eventlist: (*mut Event, usize),
104+
eventlist: *mut [MaybeUninit<Event>],
99105
timeout: Option<&c::timespec>,
100106
) -> io::Result<c::c_int> {
101107
ret_c_int(c::kevent(
@@ -105,8 +111,11 @@ pub(crate) unsafe fn kevent(
105111
.len()
106112
.try_into()
107113
.map_err(|_| io::Errno::OVERFLOW)?,
108-
eventlist.0.cast(),
109-
eventlist.1.try_into().map_err(|_| io::Errno::OVERFLOW)?,
114+
eventlist.cast::<c::kevent>(),
115+
eventlist
116+
.len()
117+
.try_into()
118+
.map_err(|_| io::Errno::OVERFLOW)?,
110119
timeout.map_or(null(), as_ptr),
111120
))
112121
}
@@ -395,7 +404,7 @@ pub(crate) fn port_get(port: BorrowedFd<'_>, timeout: Option<&Timespec>) -> io::
395404
#[cfg(solarish)]
396405
pub(crate) unsafe fn port_getn(
397406
port: BorrowedFd<'_>,
398-
events: (*mut Event, usize),
407+
events: *mut [MaybeUninit<Eventt>],
399408
mut nget: u32,
400409
timeout: Option<&Timespec>,
401410
) -> io::Result<usize> {
@@ -422,15 +431,15 @@ pub(crate) unsafe fn port_getn(
422431

423432
// `port_getn` special-cases a max value of 0 to be a query that returns
424433
// the number of events, so bail out early if needed.
425-
if events.1 == 0 {
434+
if events.is_empty() {
426435
return Ok(0);
427436
}
428437

429438
// In Rust ≥ 1.65, the `as _` can be `.cast_mut()`.
430439
ret(c::port_getn(
431440
borrowed_fd(port),
432-
events.0.cast(),
433-
events.1.try_into().unwrap_or(u32::MAX),
441+
events.cast::<c::port_event_t>(),
442+
events.len().try_into().unwrap_or(u32::MAX),
434443
&mut nget,
435444
timeout as _,
436445
))?;
@@ -484,7 +493,7 @@ pub(crate) fn epoll_create(flags: super::epoll::CreateFlags) -> io::Result<Owned
484493
pub(crate) fn epoll_add(
485494
epoll: BorrowedFd<'_>,
486495
source: BorrowedFd<'_>,
487-
event: &crate::event::epoll::Event,
496+
event: &Event,
488497
) -> io::Result<()> {
489498
// We use our own `Event` struct instead of libc's because
490499
// ours preserves pointer provenance instead of just using a `u64`,
@@ -505,7 +514,7 @@ pub(crate) fn epoll_add(
505514
pub(crate) fn epoll_mod(
506515
epoll: BorrowedFd<'_>,
507516
source: BorrowedFd<'_>,
508-
event: &crate::event::epoll::Event,
517+
event: &Event,
509518
) -> io::Result<()> {
510519
unsafe {
511520
ret(c::epoll_ctl(
@@ -535,7 +544,7 @@ pub(crate) fn epoll_del(epoll: BorrowedFd<'_>, source: BorrowedFd<'_>) -> io::Re
535544
#[cfg(any(linux_kernel, target_os = "illumos", target_os = "redox"))]
536545
pub(crate) unsafe fn epoll_wait(
537546
epoll: BorrowedFd<'_>,
538-
events: (*mut crate::event::epoll::Event, usize),
547+
events: *mut [MaybeUninit<Event>],
539548
timeout: Option<&Timespec>,
540549
) -> io::Result<usize> {
541550
// If we're on Linux ≥ 5.11 and a libc that has an `epoll_pwait2`
@@ -560,8 +569,8 @@ pub(crate) unsafe fn epoll_wait(
560569
if let Some(epoll_pwait2_func) = epoll_pwait2.get() {
561570
return ret_u32(epoll_pwait2_func(
562571
borrowed_fd(epoll),
563-
events.0.cast::<c::epoll_event>(),
564-
events.1.try_into().unwrap_or(i32::MAX),
572+
events.cast::<c::epoll_event>(),
573+
events.len().try_into().unwrap_or(i32::MAX),
565574
crate::utils::option_as_ptr(timeout).cast(),
566575
null(),
567576
))
@@ -584,8 +593,8 @@ pub(crate) unsafe fn epoll_wait(
584593

585594
ret_u32(epoll_pwait2(
586595
borrowed_fd(epoll),
587-
events.0.cast::<c::epoll_event>(),
588-
events.1.try_into().unwrap_or(i32::MAX),
596+
events.cast::<c::epoll_event>(),
597+
events.len().try_into().unwrap_or(i32::MAX),
589598
crate::utils::option_as_ptr(timeout).cast(),
590599
null(),
591600
))
@@ -602,8 +611,8 @@ pub(crate) unsafe fn epoll_wait(
602611

603612
ret_u32(c::epoll_wait(
604613
borrowed_fd(epoll),
605-
events.0.cast::<c::epoll_event>(),
606-
events.1.try_into().unwrap_or(i32::MAX),
614+
events.cast::<c::epoll_event>(),
615+
events.len().try_into().unwrap_or(i32::MAX),
607616
timeout,
608617
))
609618
.map(|i| i as usize)

src/backend/libc/fs/syscalls.rs

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,14 @@ pub(crate) fn readlink(path: &CStr, buf: &mut [u8]) -> io::Result<usize> {
292292
pub(crate) unsafe fn readlinkat(
293293
dirfd: BorrowedFd<'_>,
294294
path: &CStr,
295-
buf: (*mut u8, usize),
295+
buf: *mut [MaybeUninit<u8>],
296296
) -> io::Result<usize> {
297-
ret_usize(c::readlinkat(borrowed_fd(dirfd), c_str(path), buf.0.cast(), buf.1) as isize)
297+
ret_usize(c::readlinkat(
298+
borrowed_fd(dirfd),
299+
c_str(path),
300+
buf.cast::<ffi::c_char>(),
301+
buf.len(),
302+
) as isize)
298303
}
299304

300305
pub(crate) fn mkdir(path: &CStr, mode: Mode) -> io::Result<()> {
@@ -2349,32 +2354,32 @@ struct Attrlist {
23492354
pub(crate) unsafe fn getxattr(
23502355
path: &CStr,
23512356
name: &CStr,
2352-
value: (*mut u8, usize),
2357+
value: *mut [MaybeUninit<u8>],
23532358
) -> io::Result<usize> {
23542359
#[cfg(not(apple))]
23552360
{
23562361
ret_usize(c::getxattr(
23572362
path.as_ptr(),
23582363
name.as_ptr(),
2359-
value.0.cast::<c::c_void>(),
2360-
value.1,
2364+
value.cast::<c::c_void>(),
2365+
value.len(),
23612366
))
23622367
}
23632368

23642369
#[cfg(apple)]
23652370
{
23662371
// Passing an empty to slice to `getxattr` leads to `ERANGE` on macOS.
23672372
// Pass null instead.
2368-
let ptr = if value.1 == 0 {
2373+
let ptr = if value.len() == 0 {
23692374
core::ptr::null_mut()
23702375
} else {
2371-
value.0.cast::<c::c_void>()
2376+
value.cast::<c::c_void>()
23722377
};
23732378
ret_usize(c::getxattr(
23742379
path.as_ptr(),
23752380
name.as_ptr(),
23762381
ptr,
2377-
value.1,
2382+
value.len(),
23782383
0,
23792384
0,
23802385
))
@@ -2385,33 +2390,33 @@ pub(crate) unsafe fn getxattr(
23852390
pub(crate) unsafe fn lgetxattr(
23862391
path: &CStr,
23872392
name: &CStr,
2388-
value: (*mut u8, usize),
2393+
value: *mut [MaybeUninit<u8>],
23892394
) -> io::Result<usize> {
23902395
#[cfg(not(apple))]
23912396
{
23922397
ret_usize(c::lgetxattr(
23932398
path.as_ptr(),
23942399
name.as_ptr(),
2395-
value.0.cast::<c::c_void>(),
2396-
value.1,
2400+
value.cast::<c::c_void>(),
2401+
value.len(),
23972402
))
23982403
}
23992404

24002405
#[cfg(apple)]
24012406
{
24022407
// Passing an empty to slice to `getxattr` leads to `ERANGE` on macOS.
24032408
// Pass null instead.
2404-
let ptr = if value.1 == 0 {
2409+
let ptr = if value.len() == 0 {
24052410
core::ptr::null_mut()
24062411
} else {
2407-
value.0.cast::<c::c_void>()
2412+
value.cast::<c::c_void>()
24082413
};
24092414

24102415
ret_usize(c::getxattr(
24112416
path.as_ptr(),
24122417
name.as_ptr(),
24132418
ptr,
2414-
value.1,
2419+
value.len(),
24152420
0,
24162421
c::XATTR_NOFOLLOW,
24172422
))
@@ -2422,32 +2427,32 @@ pub(crate) unsafe fn lgetxattr(
24222427
pub(crate) unsafe fn fgetxattr(
24232428
fd: BorrowedFd<'_>,
24242429
name: &CStr,
2425-
value: (*mut u8, usize),
2430+
value: *mut [MaybeUninit<u8>],
24262431
) -> io::Result<usize> {
24272432
#[cfg(not(apple))]
24282433
{
24292434
ret_usize(c::fgetxattr(
24302435
borrowed_fd(fd),
24312436
name.as_ptr(),
2432-
value.0.cast::<c::c_void>(),
2433-
value.1,
2437+
value.cast::<c::c_void>(),
2438+
value.len(),
24342439
))
24352440
}
24362441

24372442
#[cfg(apple)]
24382443
{
24392444
// Passing an empty to slice to `getxattr` leads to `ERANGE` on macOS.
24402445
// Pass null instead.
2441-
let ptr = if value.1 == 0 {
2446+
let ptr = if value.len() == 0 {
24422447
core::ptr::null_mut()
24432448
} else {
2444-
value.0.cast::<c::c_void>()
2449+
value.cast::<c::c_void>()
24452450
};
24462451
ret_usize(c::fgetxattr(
24472452
borrowed_fd(fd),
24482453
name.as_ptr(),
24492454
ptr,
2450-
value.1,
2455+
value.len(),
24512456
0,
24522457
0,
24532458
))
@@ -2548,61 +2553,64 @@ pub(crate) fn fsetxattr(
25482553
}
25492554

25502555
#[cfg(any(apple, linux_kernel, target_os = "hurd"))]
2551-
pub(crate) unsafe fn listxattr(path: &CStr, list: (*mut u8, usize)) -> io::Result<usize> {
2556+
pub(crate) unsafe fn listxattr(path: &CStr, list: *mut [MaybeUninit<u8>]) -> io::Result<usize> {
25522557
#[cfg(not(apple))]
25532558
{
25542559
ret_usize(c::listxattr(
25552560
path.as_ptr(),
2556-
list.0.cast::<ffi::c_char>(),
2557-
list.1,
2561+
list.cast::<ffi::c_char>(),
2562+
list.len(),
25582563
))
25592564
}
25602565

25612566
#[cfg(apple)]
25622567
{
25632568
ret_usize(c::listxattr(
25642569
path.as_ptr(),
2565-
list.0.cast::<ffi::c_char>(),
2566-
list.1,
2570+
list.cast::<ffi::c_char>(),
2571+
list.len(),
25672572
0,
25682573
))
25692574
}
25702575
}
25712576

25722577
#[cfg(any(apple, linux_kernel, target_os = "hurd"))]
2573-
pub(crate) unsafe fn llistxattr(path: &CStr, list: (*mut u8, usize)) -> io::Result<usize> {
2578+
pub(crate) unsafe fn llistxattr(path: &CStr, list: *mut [MaybeUninit<u8>]) -> io::Result<usize> {
25742579
#[cfg(not(apple))]
25752580
{
25762581
ret_usize(c::llistxattr(
25772582
path.as_ptr(),
2578-
list.0.cast::<ffi::c_char>(),
2579-
list.1,
2583+
list.cast::<ffi::c_char>(),
2584+
list.len(),
25802585
))
25812586
}
25822587

25832588
#[cfg(apple)]
25842589
{
25852590
ret_usize(c::listxattr(
25862591
path.as_ptr(),
2587-
list.0.cast::<ffi::c_char>(),
2588-
list.1,
2592+
list.cast::<ffi::c_char>(),
2593+
list.len(),
25892594
c::XATTR_NOFOLLOW,
25902595
))
25912596
}
25922597
}
25932598

25942599
#[cfg(any(apple, linux_kernel, target_os = "hurd"))]
2595-
pub(crate) unsafe fn flistxattr(fd: BorrowedFd<'_>, list: (*mut u8, usize)) -> io::Result<usize> {
2600+
pub(crate) unsafe fn flistxattr(
2601+
fd: BorrowedFd<'_>,
2602+
list: *mut [MaybeUninit<u8>],
2603+
) -> io::Result<usize> {
25962604
let fd = borrowed_fd(fd);
25972605

25982606
#[cfg(not(apple))]
25992607
{
2600-
ret_usize(c::flistxattr(fd, list.0.cast::<ffi::c_char>(), list.1))
2608+
ret_usize(c::flistxattr(fd, list.cast::<ffi::c_char>(), list.len()))
26012609
}
26022610

26032611
#[cfg(apple)]
26042612
{
2605-
ret_usize(c::flistxattr(fd, list.0.cast::<ffi::c_char>(), list.1, 0))
2613+
ret_usize(c::flistxattr(fd, list.cast::<ffi::c_char>(), list.len(), 0))
26062614
}
26072615
}
26082616

0 commit comments

Comments
 (0)