diff --git a/common/include/villas/cpuset.hpp b/common/include/villas/cpuset.hpp index e8f67c868..c738d1e29 100644 --- a/common/include/villas/cpuset.hpp +++ b/common/include/villas/cpuset.hpp @@ -50,7 +50,7 @@ public: setp = CPU_ALLOC(num_cpus); if (!setp) - throw villas::RuntimeError("Failed to allocated memory"); + throw MemoryAllocationError(); zero(); } diff --git a/common/include/villas/exceptions.hpp b/common/include/villas/exceptions.hpp index 632b2f20d..db5125254 100644 --- a/common/include/villas/exceptions.hpp +++ b/common/include/villas/exceptions.hpp @@ -62,6 +62,14 @@ public: { } }; +class MemoryAllocationError : public RuntimeError { + +public: + MemoryAllocationError() : + RuntimeError("Failed to allocate memory") + { } +}; + class JsonError : public std::runtime_error { protected: diff --git a/common/lib/advio.cpp b/common/lib/advio.cpp index df5382b1c..d3be3bb5c 100644 --- a/common/lib/advio.cpp +++ b/common/lib/advio.cpp @@ -211,7 +211,7 @@ AFILE * afopen(const char *uri, const char *mode) AFILE *af = new AFILE; if (!af) - throw RuntimeError("Failed to allocate memory!"); + throw MemoryAllocationError(); memset(af, 0, sizeof(AFILE)); diff --git a/common/lib/buffer.cpp b/common/lib/buffer.cpp index 564e9f10b..f2d5c6a07 100644 --- a/common/lib/buffer.cpp +++ b/common/lib/buffer.cpp @@ -35,7 +35,7 @@ Buffer::Buffer(size_t sz) : { buf = new char[size]; if (!buf) - throw RuntimeError("Failed to allocate memory"); + throw MemoryAllocationError(); memset(buf, 0, size); } diff --git a/common/lib/hist.cpp b/common/lib/hist.cpp index dc08fdc7c..8d7b1be2e 100644 --- a/common/lib/hist.cpp +++ b/common/lib/hist.cpp @@ -190,7 +190,7 @@ char * Hist::dump() const { char *buf = new char[128]; if (!buf) - throw RuntimeError("Failed to allocate memory!"); + throw MemoryAllocationError(); memset(buf, 0, 128); diff --git a/common/lib/kernel/vfio.cpp b/common/lib/kernel/vfio.cpp index f01491f1f..c29373c4a 100644 --- a/common/lib/kernel/vfio.cpp +++ b/common/lib/kernel/vfio.cpp @@ -144,7 +144,7 @@ Container::~Container() std::shared_ptr Container::create() { - std::shared_ptr container { new Container }; + std::shared_ptr container(new Container); return container; } @@ -549,6 +549,9 @@ Device::pciHotReset() sizeof(struct vfio_pci_dependent_device) * 64; auto *reset_info_buf = new char[reset_info_len]; + if (!reset_info_buf) + throw MemoryAllocationError(); + auto *reset_info = reinterpret_cast(reset_info_buf); reset_info->argsz = reset_info_len; @@ -577,6 +580,9 @@ Device::pciHotReset() const size_t reset_len = sizeof(struct vfio_pci_hot_reset) + sizeof(int32_t) * 1; auto *reset_buf = new char[reset_len]; + if (!reset_buf) + throw MemoryAllocationError(); + auto *reset = reinterpret_cast(reset_buf); reset->argsz = reset_len; @@ -609,9 +615,10 @@ Device::pciMsiInit(int efds[]) sizeof(int) * irqCount; auto *irqSetBuf = new char[irqSetSize]; + if (!irqSetBuf) + throw MemoryAllocationError(); + auto *irqSet = reinterpret_cast(irqSetBuf); - if (irqSet == nullptr) - return -1; irqSet->argsz = irqSetSize; irqSet->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER; @@ -653,9 +660,10 @@ Device::pciMsiDeinit(int efds[]) sizeof(int) * irqCount; auto *irqSetBuf = new char[irqSetSize]; + if (!irqSetBuf) + throw MemoryAllocationError(); + auto *irqSet = reinterpret_cast(irqSetBuf); - if (irqSet == nullptr) - return -1; irqSet->argsz = irqSetSize; irqSet->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER; diff --git a/common/lib/memory_manager.cpp b/common/lib/memory_manager.cpp index aff256b72..a45357379 100644 --- a/common/lib/memory_manager.cpp +++ b/common/lib/memory_manager.cpp @@ -24,9 +24,11 @@ #include #include +#include #include #include +using namespace villas; using namespace villas::utils; namespace villas { @@ -39,6 +41,8 @@ MemoryManager::get() { if (instance == nullptr) { instance = new MemoryManager; + if (!instance) + throw MemoryAllocationError(); } return *instance; diff --git a/common/lib/utils.cpp b/common/lib/utils.cpp index e8b8878e3..fa95a9a80 100644 --- a/common/lib/utils.cpp +++ b/common/lib/utils.cpp @@ -262,7 +262,7 @@ void * memdup(const void *src, size_t bytes) { void *dst = new char[bytes]; if (!dst) - throw RuntimeError("Failed to allocate memory!"); + throw MemoryAllocationError(); memcpy(dst, src, bytes); diff --git a/common/tests/unit/list.cpp b/common/tests/unit/list.cpp index 373dc3620..d51e0ede3 100644 --- a/common/tests/unit/list.cpp +++ b/common/tests/unit/list.cpp @@ -48,7 +48,7 @@ Test(list, vlist_lookup) for (unsigned i = 0; i < ARRAY_LEN(nouns); i++) { struct data *d = new struct data; if (!d) - throw RuntimeError("Failed to allocate memory!"); + throw MemoryAllocationError(); d->tag = nouns[i]; d->data = i;