From fc49559b7800bd13652102b779adcdecccd520e3 Mon Sep 17 00:00:00 2001 From: Davide Grohmann Date: Mon, 25 Apr 2022 15:27:52 +0200 Subject: Fix: always close fd when destructing buffers Also always rethrow exceptions not swallow them Change-Id: I7a4b1e408858aa0d0128ca7bd6d6a7715662f9fb --- driver_library/include/ethosu.hpp | 8 ++++---- driver_library/src/ethosu.cpp | 13 +++++++++---- 2 files changed, 13 insertions(+), 8 deletions(-) (limited to 'driver_library') diff --git a/driver_library/include/ethosu.hpp b/driver_library/include/ethosu.hpp index 547e346..a12d668 100644 --- a/driver_library/include/ethosu.hpp +++ b/driver_library/include/ethosu.hpp @@ -129,7 +129,7 @@ public: class Device { public: Device(const char *device = "/dev/ethosu0"); - virtual ~Device(); + virtual ~Device() noexcept(false); int ioctl(unsigned long cmd, void *data = nullptr) const; Capabilities capabilities() const; @@ -141,7 +141,7 @@ private: class Buffer { public: Buffer(const Device &device, const size_t capacity); - virtual ~Buffer(); + virtual ~Buffer() noexcept(false); size_t capacity() const; void clear() const; @@ -162,7 +162,7 @@ class Network { public: Network(const Device &device, std::shared_ptr &buffer); Network(const Device &device, const unsigned index); - virtual ~Network(); + virtual ~Network() noexcept(false); int ioctl(unsigned long cmd, void *data = nullptr); std::shared_ptr getBuffer(); @@ -215,7 +215,7 @@ public: create(counterConfigs, enableCycleCounter); } - virtual ~Inference(); + virtual ~Inference() noexcept(false); int wait(int64_t timeoutNanos = -1) const; const std::vector getPmuCounters() const; diff --git a/driver_library/src/ethosu.cpp b/driver_library/src/ethosu.cpp index 01631b3..2b1da45 100644 --- a/driver_library/src/ethosu.cpp +++ b/driver_library/src/ethosu.cpp @@ -148,7 +148,7 @@ Device::Device(const char *device) { fd = eopen(device, O_RDWR | O_NONBLOCK); } -Device::~Device() { +Device::~Device() noexcept(false) { eclose(fd); } @@ -185,18 +185,21 @@ Buffer::Buffer(const Device &device, const size_t capacity) : fd(-1), dataPtr(nu try { eclose(fd); } catch (...) { std::throw_with_nested(e); } + throw; } dataPtr = reinterpret_cast(d); } -Buffer::~Buffer() { +Buffer::~Buffer() noexcept(false) { try { emunmap(dataPtr, dataCapacity); } catch (std::exception &e) { try { eclose(fd); } catch (...) { std::throw_with_nested(e); } + throw; } + eclose(fd); } size_t Buffer::capacity() const { @@ -250,6 +253,7 @@ Network::Network(const Device &device, shared_ptr &buffer) : fd(-1), buf try { eclose(fd); } catch (...) { std::throw_with_nested(e); } + throw; } } @@ -265,6 +269,7 @@ Network::Network(const Device &device, const unsigned index) : fd(-1) { try { eclose(fd); } catch (...) { std::throw_with_nested(e); } + throw; } } @@ -281,7 +286,7 @@ void Network::collectNetworkInfo() { } } -Network::~Network() { +Network::~Network() noexcept(false) { eclose(fd); } @@ -325,7 +330,7 @@ size_t Network::getOfmSize() const { * Inference ****************************************************************************/ -Inference::~Inference() { +Inference::~Inference() noexcept(false) { eclose(fd); } -- cgit v1.2.1