Skip to content

Commit

Permalink
Implement Copy for MapData
Browse files Browse the repository at this point in the history
Implement Copy for MapData so that
when `take_map` is used we create a
1 to 1 mapping of MapData to internal
FileDescriptor.  This will ensure
that when MapData is used in multiple
tasks that we don't drop the FD before
all tasks are done using it.

Signed-off-by: Andrew Stoycos <astoycos@redhat.com>
  • Loading branch information
Andrew Stoycos committed Oct 17, 2022
1 parent 898a14d commit 893f9f4
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion aya/src/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ impl_try_from_map_generic_key_and_value!(HashMap, PerCpuHashMap, LpmTrie);
/// A generic handle to a BPF map.
///
/// You should never need to use this unless you're implementing a new map type.
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct MapData {
pub(crate) obj: obj::Map,
pub(crate) fd: Option<RawFd>,
Expand Down Expand Up @@ -577,6 +577,22 @@ impl Drop for MapData {
}
}

impl Clone for MapData {
fn clone(&self) -> MapData {
MapData {
obj: self.obj.clone(),
fd: {
if let Some(fd) = self.fd {
unsafe { Some(libc::dup(fd)) };
}
None
},
btf_fd: self.btf_fd,
pinned: self.pinned,
}
}
}

/// An iterable map
pub trait IterableMap<K: Pod, V> {
/// Get a generic map handle
Expand Down

0 comments on commit 893f9f4

Please sign in to comment.