diff --git a/mindspore/lite/src/runtime/allocator.cc b/mindspore/lite/src/runtime/allocator.cc index 2a423d9120..9819d6f912 100644 --- a/mindspore/lite/src/runtime/allocator.cc +++ b/mindspore/lite/src/runtime/allocator.cc @@ -44,6 +44,11 @@ void DefaultAllocator::UnLock() { } } +bool DefaultAllocator::ReuseMemory(size_t free_size, size_t size) { + return free_size >= size && + (free_size <= (size >= UINT32_MAX / (1ul << shiftFactor_) ? UINT32_MAX : size << shiftFactor_)); +} + void *DefaultAllocator::Malloc(size_t size) { if (size > MAX_MALLOC_SIZE) { MS_LOG(ERROR) << "MallocData out of max_size, size: " << size; @@ -55,7 +60,7 @@ void *DefaultAllocator::Malloc(size_t size) { } Lock(); auto iter = freeList_.lower_bound(size); - if (iter != freeList_.end() && (iter->second->size >= size) && (iter->second->size <= (size << shiftFactor_))) { + if (iter != freeList_.end() && ReuseMemory(iter->second->size, size)) { auto membuf = iter->second; freeList_.erase(iter); allocatedList_[membuf->buf] = membuf; diff --git a/mindspore/lite/src/runtime/allocator.h b/mindspore/lite/src/runtime/allocator.h index c4ac18e769..004d577840 100644 --- a/mindspore/lite/src/runtime/allocator.h +++ b/mindspore/lite/src/runtime/allocator.h @@ -58,6 +58,7 @@ class DefaultAllocator : public Allocator { private: void Lock(); void UnLock(); + bool ReuseMemory(size_t free_size, size_t size); struct MemBuf { size_t size; void *buf;