Skip to content

Commit 26a7766

Browse files
committed
refactor(epoll): move libc wrapper functions
1 parent 0c47ca8 commit 26a7766

File tree

2 files changed

+58
-59
lines changed

2 files changed

+58
-59
lines changed

src/timer/epoll.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,61 @@ impl Timer {
158158
Ok(())
159159
}
160160
}
161+
162+
/// Wrapper for libc functions.
163+
///
164+
/// Error wrapper for some libc functions used by the library. This only does
165+
/// Error (-1 return) wrapping. Alternatively, the nix crate could be used
166+
/// instead of expanding this wrappers (if more functions and types are used
167+
/// from libc)
168+
169+
/// Error Wrapper for libc return. Only check for errors.
170+
fn check_err<T: Ord + Default>(num: T) -> Result<T> {
171+
if num < T::default() {
172+
return Err(PyroscopeError::from(std::io::Error::last_os_error()));
173+
}
174+
Ok(num)
175+
}
176+
177+
/// libc::timerfd wrapper
178+
pub fn timerfd_create(clockid: libc::clockid_t, clock_flags: libc::c_int) -> Result<i32> {
179+
check_err(unsafe { libc::timerfd_create(clockid, clock_flags) }).map(|timer_fd| timer_fd as i32)
180+
}
181+
182+
/// libc::timerfd_settime wrapper
183+
pub fn timerfd_settime(
184+
timer_fd: i32, set_flags: libc::c_int, new_value: &mut libc::itimerspec,
185+
old_value: &mut libc::itimerspec,
186+
) -> Result<()> {
187+
check_err(unsafe { libc::timerfd_settime(timer_fd, set_flags, new_value, old_value) })?;
188+
Ok(())
189+
}
190+
191+
/// libc::epoll_create1 wrapper
192+
pub fn epoll_create1(epoll_flags: libc::c_int) -> Result<i32> {
193+
check_err(unsafe { libc::epoll_create1(epoll_flags) }).map(|epoll_fd| epoll_fd as i32)
194+
}
195+
196+
/// libc::epoll_ctl wrapper
197+
pub fn epoll_ctl(epoll_fd: i32, epoll_flags: libc::c_int, timer_fd: i32, event: &mut libc::epoll_event) -> Result<()> {
198+
check_err(unsafe {
199+
libc::epoll_ctl(epoll_fd, epoll_flags, timer_fd, event)
200+
})?;
201+
Ok(())
202+
}
203+
204+
/// libc::epoll_wait wrapper
205+
pub fn epoll_wait(epoll_fd: i32, events: *mut libc::epoll_event, maxevents: libc::c_int, timeout: libc::c_int) -> Result<()> {
206+
check_err(unsafe {
207+
libc::epoll_wait(epoll_fd, events, maxevents, timeout)
208+
})?;
209+
Ok(())
210+
}
211+
212+
/// libc::read wrapper
213+
pub fn read(timer_fd: i32, bufptr: *mut libc::c_void, count: libc::size_t) -> Result<()> {
214+
check_err(unsafe {
215+
libc::read(timer_fd, bufptr, count)
216+
})?;
217+
Ok(())
218+
}

src/utils.rs

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// except according to those terms.
66

77
use crate::error::Result;
8-
use crate::PyroscopeError;
98

109
use std::collections::HashMap;
1110

@@ -55,61 +54,3 @@ mod tests {
5554
)
5655
}
5756
}
58-
59-
/// Wrapper for libc functions.
60-
///
61-
/// Error wrapper for some libc functions used by the library. This only does
62-
/// Error (-1 return) wrapping. Alternatively, the nix crate could be used
63-
/// instead of expanding this wrappers (if more functions and types are used
64-
/// from libc)
65-
66-
/// Error Wrapper for libc return. Only check for errors.
67-
fn check_err<T: Ord + Default>(num: T) -> Result<T> {
68-
if num < T::default() {
69-
return Err(PyroscopeError::from(std::io::Error::last_os_error()));
70-
}
71-
Ok(num)
72-
}
73-
74-
/// libc::timerfd wrapper
75-
pub fn timerfd_create(clockid: libc::clockid_t, clock_flags: libc::c_int) -> Result<i32> {
76-
check_err(unsafe { libc::timerfd_create(clockid, clock_flags) }).map(|timer_fd| timer_fd as i32)
77-
}
78-
79-
/// libc::timerfd_settime wrapper
80-
pub fn timerfd_settime(
81-
timer_fd: i32, set_flags: libc::c_int, new_value: &mut libc::itimerspec,
82-
old_value: &mut libc::itimerspec,
83-
) -> Result<()> {
84-
check_err(unsafe { libc::timerfd_settime(timer_fd, set_flags, new_value, old_value) })?;
85-
Ok(())
86-
}
87-
88-
/// libc::epoll_create1 wrapper
89-
pub fn epoll_create1(epoll_flags: libc::c_int) -> Result<i32> {
90-
check_err(unsafe { libc::epoll_create1(epoll_flags) }).map(|epoll_fd| epoll_fd as i32)
91-
}
92-
93-
/// libc::epoll_ctl wrapper
94-
pub fn epoll_ctl(epoll_fd: i32, epoll_flags: libc::c_int, timer_fd: i32, event: &mut libc::epoll_event) -> Result<()> {
95-
check_err(unsafe {
96-
libc::epoll_ctl(epoll_fd, epoll_flags, timer_fd, event)
97-
})?;
98-
Ok(())
99-
}
100-
101-
/// libc::epoll_wait wrapper
102-
pub fn epoll_wait(epoll_fd: i32, events: *mut libc::epoll_event, maxevents: libc::c_int, timeout: libc::c_int) -> Result<()> {
103-
check_err(unsafe {
104-
libc::epoll_wait(epoll_fd, events, maxevents, timeout)
105-
})?;
106-
Ok(())
107-
}
108-
109-
/// libc::read wrapper
110-
pub fn read(timer_fd: i32, bufptr: *mut libc::c_void, count: libc::size_t) -> Result<()> {
111-
check_err(unsafe {
112-
libc::read(timer_fd, bufptr, count)
113-
})?;
114-
Ok(())
115-
}

0 commit comments

Comments
 (0)