return error status if a memory allocation fails

This commit is contained in:
Attila Afra
2019-03-26 13:30:56 +02:00
parent 7ef75ed70e
commit 308ec2b17f
3 changed files with 28 additions and 11 deletions
+3
View File
@@ -30,6 +30,9 @@ struct mkldnn_memory: public mkldnn::impl::c_compatible {
: engine_(engine), md_(*md) {}
virtual ~mkldnn_memory() {}
/** allocates/initializes memory */
virtual mkldnn::impl::status_t init() = 0;
/** returns memory's engine */
mkldnn::impl::engine_t *engine() const { return engine_; }
/** returns memory's description */
+11 -2
View File
@@ -69,8 +69,17 @@ namespace cpu {
status_t cpu_engine_t::memory_create(memory_t **memory,
const memory_desc_t *md, void *handle) {
return safe_ptr_assign<memory_t>(*memory,
new cpu_memory_t(this, md, handle));
auto _memory = new cpu_memory_t(this, md, handle);
if (_memory == nullptr)
return status::out_of_memory;
status_t status = _memory->init();
if (status != status::success) {
delete _memory;
return status;
}
return safe_ptr_assign<memory_t>(*memory, _memory);
}
using pd_create_f = mkldnn::impl::engine_t::primitive_desc_create_f;
+14 -9
View File
@@ -33,21 +33,26 @@ struct cpu_memory_t: public memory_t {
cpu_memory_t(cpu_engine_t *engine, const memory_desc_t *md, void *handle)
: memory_t(engine, md)
, own_data_(handle == MKLDNN_NATIVE_HANDLE_ALLOCATE)
, data_((char *)handle)
{
if (own_data_) {
data_ = nullptr;
const size_t size = memory_desc_wrapper(this->md()).size();
if (size) data_ = (char *)malloc(size, 64);
}
zero_pad();
}
, data_((char *)handle) {}
cpu_memory_t(cpu_engine_t *engine, const memory_desc_t *md)
: cpu_memory_t(engine, md, nullptr) {}
~cpu_memory_t() { if (own_data_) free(data_); }
virtual status_t init() override {
if (own_data_) {
data_ = nullptr;
const size_t size = memory_desc_wrapper(this->md()).size();
if (size) {
data_ = (char *)malloc(size, 64);
if (data_ == nullptr)
return status::out_of_memory;
}
}
return zero_pad();
}
cpu_engine_t *engine() const { return (cpu_engine_t *)memory_t::engine(); }
virtual status_t get_data_handle(void **handle) const override {