Browse Source

fix_lite_allocator

pull/15878/head
sunsuodong 4 years ago
parent
commit
a1fc5800c0
2 changed files with 7 additions and 1 deletions
  1. +6
    -1
      mindspore/lite/src/runtime/allocator.cc
  2. +1
    -0
      mindspore/lite/src/runtime/allocator.h

+ 6
- 1
mindspore/lite/src/runtime/allocator.cc View File

@@ -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) { void *DefaultAllocator::Malloc(size_t size) {
if (size > MAX_MALLOC_SIZE) { if (size > MAX_MALLOC_SIZE) {
MS_LOG(ERROR) << "MallocData out of max_size, size: " << size; MS_LOG(ERROR) << "MallocData out of max_size, size: " << size;
@@ -55,7 +60,7 @@ void *DefaultAllocator::Malloc(size_t size) {
} }
Lock(); Lock();
auto iter = freeList_.lower_bound(size); 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; auto membuf = iter->second;
freeList_.erase(iter); freeList_.erase(iter);
allocatedList_[membuf->buf] = membuf; allocatedList_[membuf->buf] = membuf;


+ 1
- 0
mindspore/lite/src/runtime/allocator.h View File

@@ -58,6 +58,7 @@ class DefaultAllocator : public Allocator {
private: private:
void Lock(); void Lock();
void UnLock(); void UnLock();
bool ReuseMemory(size_t free_size, size_t size);
struct MemBuf { struct MemBuf {
size_t size; size_t size;
void *buf; void *buf;


Loading…
Cancel
Save