You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

allocator.cc 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "src/runtime/allocator.h"
  17. #include "common/module_registry.h"
  18. #include "src/op_common.h"
  19. namespace mindspore {
  20. namespace predict {
  21. std::shared_ptr<Allocator> Allocator::Create() {
  22. auto alloc = GetRegistryInstance()->Create<Allocator>(MODULE_REG_NAME_ALLOCATOR);
  23. if (alloc != nullptr) {
  24. return alloc;
  25. }
  26. // default allocator
  27. return std::shared_ptr<Allocator>(new DefaultAllocator());
  28. }
  29. DefaultAllocator::DefaultAllocator() = default;
  30. DefaultAllocator::~DefaultAllocator() { Clear(); }
  31. void DefaultAllocator::SetContext(const AllocatorContext &ctx) {
  32. lockFlag = ctx.lockFlag;
  33. shiftFactor = ctx.shiftFactor;
  34. }
  35. void DefaultAllocator::Lock() {
  36. if (lockFlag) {
  37. lock.lock();
  38. }
  39. }
  40. void DefaultAllocator::UnLock() {
  41. if (lockFlag) {
  42. lock.unlock();
  43. }
  44. }
  45. void *DefaultAllocator::Malloc(size_t size) {
  46. if (size > MAX_MALLOC_SIZE) {
  47. return nullptr;
  48. }
  49. Lock();
  50. auto it = freeList.begin();
  51. for (; it != freeList.end(); it++) {
  52. auto membuf = *it;
  53. if ((membuf->size >= size) && (membuf->size < (size << shiftFactor))) {
  54. freeList.erase(it);
  55. allocatedList.push_back(membuf);
  56. UnLock();
  57. return membuf->buf;
  58. }
  59. }
  60. std::unique_ptr<MemBuf> membuf(reinterpret_cast<MemBuf *>(malloc(sizeof(MemBuf) + size)));
  61. if (membuf == nullptr) {
  62. UnLock();
  63. return nullptr;
  64. }
  65. membuf->size = size;
  66. membuf->buf = reinterpret_cast<char *>(membuf.get()) + sizeof(MemBuf);
  67. auto bufPtr = membuf->buf;
  68. allocatedList.push_back(membuf.release());
  69. UnLock();
  70. return bufPtr;
  71. }
  72. void DefaultAllocator::Free(void *buf) {
  73. if (buf == nullptr) {
  74. return;
  75. }
  76. Lock();
  77. auto it = allocatedList.begin();
  78. for (; it != allocatedList.end(); it++) {
  79. auto membuf = *it;
  80. if (membuf->buf == buf) {
  81. allocatedList.erase(it);
  82. freeList.push_back(membuf);
  83. UnLock();
  84. return;
  85. }
  86. }
  87. UnLock();
  88. free(buf);
  89. }
  90. size_t DefaultAllocator::GetTotalSize() {
  91. Lock();
  92. size_t totalSize = 0;
  93. auto it = allocatedList.begin();
  94. for (; it != allocatedList.end(); it++) {
  95. auto membuf = *it;
  96. totalSize += membuf->size;
  97. }
  98. it = freeList.begin();
  99. for (; it != freeList.end(); it++) {
  100. auto membuf = *it;
  101. totalSize += membuf->size;
  102. }
  103. UnLock();
  104. return totalSize;
  105. }
  106. void DefaultAllocator::Clear() {
  107. Lock();
  108. auto it = allocatedList.begin();
  109. for (; it != allocatedList.end(); it++) {
  110. free(*it);
  111. }
  112. allocatedList.clear();
  113. it = freeList.begin();
  114. for (; it != freeList.end(); it++) {
  115. free(*it);
  116. }
  117. freeList.clear();
  118. UnLock();
  119. }
  120. } // namespace predict
  121. } // namespace mindspore