Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass capacity as max_events to epoll_wait #17

Closed
wants to merge 1 commit into from

Conversation

ibraheemdev
Copy link

Up to maxevents are returned by epoll_wait(). The maxevents argument must be greater than zero.

Passing the length here doesn't really make sense. Passing Vec::with_capacity(1024) currently causes in invalid argument exception because the length is still 0.

@nathansizemore
Copy link
Owner

nathansizemore commented Jun 17, 2021

Passing a Vec would be a bad idea. Epoll will coerce to a pointer with a length it can write into. If you passed a Vec instead of an actual array you'd need to unsafely set the length. Because when the Vec comes back out, it will have no idea how many elements it actually has in it (what the len really is).

@nathansizemore
Copy link
Owner

That is because their underlying type is Vec. They can safely do this afterwards, because they know it's their Vec

.map(|n_events| {
     // This is safe because `epoll_wait` ensures that `n_events` are
     // assigned.
    unsafe { events.set_len(n_events as usize) };  

You cannot just do that to say, a mutable reference to an actual array that was passed in, which is what should be used.

@ibraheemdev
Copy link
Author

ibraheemdev commented Jun 17, 2021

Okay got it, so I should just pass an array of dummy events?

@nathansizemore
Copy link
Owner

nathansizemore commented Jun 17, 2021

If you wanna use a vec, you can do it like so...

let mut buf = Vec::<epoll::Event>::with_capacity(100);
unsafe { buf.set_len(100); }
epoll::wait(123, -1, &buf[0..100]);

You can then just take the slice of whatever wait returned. So, you'd use [0..num_returned]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants