Kaynağa Gözat

Implement Copy for MapData

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>
Andrew Stoycos 2 yıl önce
ebeveyn
işleme
893f9f44a2
1 değiştirilmiş dosya ile 17 ekleme ve 1 silme
  1. 17 1
      aya/src/maps/mod.rs

+ 17 - 1
aya/src/maps/mod.rs

@@ -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>,
@@ -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