enabling automatic device cleanup for bad applications not releasing the device on exit
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
#if defined(USE_FPGA) || defined(USE_ASE)
|
#if defined(USE_FPGA) || defined(USE_ASE)
|
||||||
#include <opae/fpga.h>
|
#include <opae/fpga.h>
|
||||||
@@ -78,6 +79,34 @@ inline bool is_aligned(size_t addr, size_t alignment) {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
class AutoDeviceCleanup {
|
||||||
|
private:
|
||||||
|
std::list<vx_device_h> devices_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
AutoDeviceCleanup() {}
|
||||||
|
|
||||||
|
~AutoDeviceCleanup() {
|
||||||
|
for (auto it = devices_.begin(), it_end = devices_.end(); it != it_end;) {
|
||||||
|
auto device = *it;
|
||||||
|
it = devices_.erase(it);
|
||||||
|
vx_dev_close(device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void add_device(vx_device_h device) {
|
||||||
|
devices_.push_back(device);
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove_device(vx_device_h device) {
|
||||||
|
devices_.remove(device);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
AutoDeviceCleanup gAutoDeviceCleanup;
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
extern int vx_dev_caps(vx_device_h hdevice, unsigned caps_id, unsigned *value) {
|
extern int vx_dev_caps(vx_device_h hdevice, unsigned caps_id, unsigned *value) {
|
||||||
if (nullptr == hdevice)
|
if (nullptr == hdevice)
|
||||||
return -1;
|
return -1;
|
||||||
@@ -223,6 +252,8 @@ extern int vx_dev_open(vx_device_h* hdevice) {
|
|||||||
|
|
||||||
*hdevice = device;
|
*hdevice = device;
|
||||||
|
|
||||||
|
gAutoDeviceCleanup.add_device(device);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,6 +261,8 @@ extern int vx_dev_close(vx_device_h hdevice) {
|
|||||||
if (nullptr == hdevice)
|
if (nullptr == hdevice)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
gAutoDeviceCleanup.remove_device(hdevice);
|
||||||
|
|
||||||
vx_device_t *device = ((vx_device_t*)hdevice);
|
vx_device_t *device = ((vx_device_t*)hdevice);
|
||||||
|
|
||||||
#ifdef SCOPE
|
#ifdef SCOPE
|
||||||
|
|||||||
@@ -154,6 +154,34 @@ private:
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
class AutoDeviceCleanup {
|
||||||
|
private:
|
||||||
|
std::list<vx_device_h> devices_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
AutoDeviceCleanup() {}
|
||||||
|
|
||||||
|
~AutoDeviceCleanup() {
|
||||||
|
for (auto it = devices_.begin(), it_end = devices_.end(); it != it_end;) {
|
||||||
|
auto device = *it;
|
||||||
|
it = devices_.erase(it);
|
||||||
|
vx_dev_close(device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void add_device(vx_device_h device) {
|
||||||
|
devices_.push_back(device);
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove_device(vx_device_h device) {
|
||||||
|
devices_.remove(device);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
AutoDeviceCleanup gAutoDeviceCleanup;
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
extern int vx_dev_caps(vx_device_h hdevice, unsigned caps_id, unsigned *value) {
|
extern int vx_dev_caps(vx_device_h hdevice, unsigned caps_id, unsigned *value) {
|
||||||
if (nullptr == hdevice)
|
if (nullptr == hdevice)
|
||||||
return -1;
|
return -1;
|
||||||
@@ -198,6 +226,8 @@ extern int vx_dev_open(vx_device_h* hdevice) {
|
|||||||
|
|
||||||
*hdevice = new vx_device();
|
*hdevice = new vx_device();
|
||||||
|
|
||||||
|
gAutoDeviceCleanup.add_device(*hdevice);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,6 +235,8 @@ extern int vx_dev_close(vx_device_h hdevice) {
|
|||||||
if (nullptr == hdevice)
|
if (nullptr == hdevice)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
gAutoDeviceCleanup.remove_device(hdevice);
|
||||||
|
|
||||||
vx_device *device = ((vx_device*)hdevice);
|
vx_device *device = ((vx_device*)hdevice);
|
||||||
|
|
||||||
#ifdef DUMP_PERF_STATS
|
#ifdef DUMP_PERF_STATS
|
||||||
|
|||||||
@@ -223,11 +223,41 @@ private:
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
class AutoDeviceCleanup {
|
||||||
|
private:
|
||||||
|
std::list<vx_device_h> devices_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
AutoDeviceCleanup() {}
|
||||||
|
|
||||||
|
~AutoDeviceCleanup() {
|
||||||
|
for (auto it = devices_.begin(), it_end = devices_.end(); it != it_end;) {
|
||||||
|
auto device = *it;
|
||||||
|
it = devices_.erase(it);
|
||||||
|
vx_dev_close(device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void add_device(vx_device_h device) {
|
||||||
|
devices_.push_back(device);
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove_device(vx_device_h device) {
|
||||||
|
devices_.remove(device);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
AutoDeviceCleanup gAutoDeviceCleanup;
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
extern int vx_dev_open(vx_device_h* hdevice) {
|
extern int vx_dev_open(vx_device_h* hdevice) {
|
||||||
if (nullptr == hdevice)
|
if (nullptr == hdevice)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
*hdevice = new vx_device();
|
*hdevice = new vx_device();
|
||||||
|
|
||||||
|
gAutoDeviceCleanup.add_device(*hdevice);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -236,6 +266,8 @@ extern int vx_dev_close(vx_device_h hdevice) {
|
|||||||
if (nullptr == hdevice)
|
if (nullptr == hdevice)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
gAutoDeviceCleanup.remove_device(hdevice);
|
||||||
|
|
||||||
vx_device *device = ((vx_device*)hdevice);
|
vx_device *device = ((vx_device*)hdevice);
|
||||||
|
|
||||||
#ifdef DUMP_PERF_STATS
|
#ifdef DUMP_PERF_STATS
|
||||||
|
|||||||
Reference in New Issue
Block a user