simple_memory_allocator.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #include "tensorflow/lite/micro/simple_memory_allocator.h"
  13. #include <cstddef>
  14. #include <cstdint>
  15. #include <new>
  16. #include "tensorflow/lite/c/common.h"
  17. #include "tensorflow/lite/core/api/error_reporter.h"
  18. #include "tensorflow/lite/kernels/internal/compatibility.h"
  19. #include "tensorflow/lite/micro/memory_helpers.h"
  20. namespace tflite {
  21. SimpleMemoryAllocator::SimpleMemoryAllocator(ErrorReporter* error_reporter,
  22. uint8_t* buffer_head,
  23. uint8_t* buffer_tail)
  24. : error_reporter_(error_reporter),
  25. buffer_head_(buffer_head),
  26. buffer_tail_(buffer_tail),
  27. head_(buffer_head),
  28. tail_(buffer_tail),
  29. temp_(buffer_head_) {}
  30. SimpleMemoryAllocator::SimpleMemoryAllocator(ErrorReporter* error_reporter,
  31. uint8_t* buffer,
  32. size_t buffer_size)
  33. : SimpleMemoryAllocator(error_reporter, buffer, buffer + buffer_size) {}
  34. /* static */
  35. SimpleMemoryAllocator* SimpleMemoryAllocator::Create(
  36. ErrorReporter* error_reporter, uint8_t* buffer_head, size_t buffer_size) {
  37. TFLITE_DCHECK(error_reporter != nullptr);
  38. TFLITE_DCHECK(buffer_head != nullptr);
  39. SimpleMemoryAllocator tmp =
  40. SimpleMemoryAllocator(error_reporter, buffer_head, buffer_size);
  41. // Allocate enough bytes from the buffer to create a SimpleMemoryAllocator.
  42. // The new instance will use the current adjusted tail buffer from the tmp
  43. // allocator instance.
  44. uint8_t* allocator_buffer = tmp.AllocateFromTail(
  45. sizeof(SimpleMemoryAllocator), alignof(SimpleMemoryAllocator));
  46. // Use the default copy constructor to populate internal states.
  47. return new (allocator_buffer) SimpleMemoryAllocator(tmp);
  48. }
  49. SimpleMemoryAllocator::~SimpleMemoryAllocator() {}
  50. TfLiteStatus SimpleMemoryAllocator::EnsureHeadSize(size_t size,
  51. size_t alignment) {
  52. if (head_ != temp_) {
  53. TF_LITE_REPORT_ERROR(
  54. error_reporter_,
  55. "Internal error: EnsureHeadSize() needs to be called after"
  56. "ResetTempAllocations().");
  57. return kTfLiteError;
  58. }
  59. uint8_t* const aligned_result = AlignPointerUp(buffer_head_, alignment);
  60. if (aligned_result + size < head_) {
  61. // Size is below the current head size, just return.
  62. return kTfLiteOk;
  63. }
  64. const size_t available_memory = tail_ - aligned_result;
  65. if (available_memory < size) {
  66. TF_LITE_REPORT_ERROR(
  67. error_reporter_,
  68. "Failed to adjust head size. Requested: %u, available %u, missing: %u",
  69. size, available_memory, size - available_memory);
  70. return kTfLiteError;
  71. }
  72. head_ = aligned_result + size;
  73. temp_ = head_;
  74. return kTfLiteOk;
  75. }
  76. uint8_t* SimpleMemoryAllocator::AllocateFromTail(size_t size,
  77. size_t alignment) {
  78. uint8_t* const aligned_result = AlignPointerDown(tail_ - size, alignment);
  79. if (aligned_result < head_) {
  80. #ifndef TF_LITE_STRIP_ERROR_STRINGS
  81. const size_t missing_memory = head_ - aligned_result;
  82. TF_LITE_REPORT_ERROR(error_reporter_,
  83. "Failed to allocate tail memory. Requested: %u, "
  84. "available %u, missing: %u",
  85. size, size - missing_memory, missing_memory);
  86. #endif
  87. return nullptr;
  88. }
  89. tail_ = aligned_result;
  90. return aligned_result;
  91. }
  92. uint8_t* SimpleMemoryAllocator::AllocateTemp(size_t size, size_t alignment) {
  93. uint8_t* const aligned_result = AlignPointerUp(temp_, alignment);
  94. const size_t available_memory = tail_ - aligned_result;
  95. if (available_memory < size) {
  96. TF_LITE_REPORT_ERROR(error_reporter_,
  97. "Failed to allocate temp memory. Requested: %u, "
  98. "available %u, missing: %u",
  99. size, available_memory, size - available_memory);
  100. return nullptr;
  101. }
  102. temp_ = aligned_result + size;
  103. return aligned_result;
  104. }
  105. void SimpleMemoryAllocator::ResetTempAllocations() { temp_ = head_; }
  106. uint8_t* SimpleMemoryAllocator::GetHead() const { return head_; }
  107. uint8_t* SimpleMemoryAllocator::GetBufferHead() const { return buffer_head_; }
  108. uint8_t* SimpleMemoryAllocator::GetTail() const { return tail_; }
  109. size_t SimpleMemoryAllocator::GetHeadUsedBytes() const {
  110. return head_ - buffer_head_;
  111. }
  112. size_t SimpleMemoryAllocator::GetTailUsedBytes() const {
  113. return buffer_tail_ - tail_;
  114. }
  115. size_t SimpleMemoryAllocator::GetAvailableMemory(size_t alignment) const {
  116. uint8_t* const aligned_head = AlignPointerUp(head_, alignment);
  117. uint8_t* const aligned_tail = AlignPointerDown(tail_, alignment);
  118. return aligned_tail - aligned_head;
  119. }
  120. size_t SimpleMemoryAllocator::GetUsedBytes() const {
  121. return GetBufferSize() - (tail_ - head_);
  122. }
  123. size_t SimpleMemoryAllocator::GetBufferSize() const {
  124. return buffer_tail_ - buffer_head_;
  125. }
  126. } // namespace tflite