Pārlūkot izejas kodu

Merge pull request #116 from eero-thia/thia/close

aya: close file descriptors on Map drop.
Alessandro Decina 3 gadi atpakaļ
vecāks
revīzija
98b36b2
1 mainītis faili ar 9 papildinājumiem un 0 dzēšanām
  1. 9 0
      aya/src/maps/mod.rs

+ 9 - 0
aya/src/maps/mod.rs

@@ -232,6 +232,15 @@ impl Map {
     }
 }
 
+impl Drop for Map {
+    fn drop(&mut self) {
+        // TODO: Replace this with an OwnedFd once that is stabilized.
+        if let Some(fd) = self.fd.take() {
+            unsafe { libc::close(fd) };
+        }
+    }
+}
+
 pub(crate) trait IterableMap<K: Pod, V> {
     fn map(&self) -> &Map;