simple_memory_allocator.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifndef TENSORFLOW_LITE_MICRO_SIMPLE_MEMORY_ALLOCATOR_H_
  13. #define TENSORFLOW_LITE_MICRO_SIMPLE_MEMORY_ALLOCATOR_H_
  14. #include <cstddef>
  15. #include <cstdint>
  16. #include "tensorflow/lite/c/common.h"
  17. #include "tensorflow/lite/core/api/error_reporter.h"
  18. #include "tensorflow/lite/micro/compatibility.h"
  19. namespace tflite {
  20. // TODO(petewarden): This allocator never frees up or reuses any memory, even
  21. // though we have enough information about lifetimes of the tensors to do so.
  22. // This makes it pretty wasteful, so we should use a more intelligent method.
  23. class SimpleMemoryAllocator {
  24. public:
  25. // TODO(b/157615197): Cleanup constructors/destructor and use factory
  26. // functions.
  27. SimpleMemoryAllocator(ErrorReporter* error_reporter, uint8_t* buffer_head,
  28. uint8_t* buffer_tail);
  29. SimpleMemoryAllocator(ErrorReporter* error_reporter, uint8_t* buffer,
  30. size_t buffer_size);
  31. virtual ~SimpleMemoryAllocator();
  32. // Creates a new SimpleMemoryAllocator from a given buffer head and size.
  33. static SimpleMemoryAllocator* Create(ErrorReporter* error_reporter,
  34. uint8_t* buffer_head,
  35. size_t buffer_size);
  36. // Ensure that the head (lowest address and moving upwards) memory allocation
  37. // is at least a given size. This function will only increase the head size if
  38. // the passed in value is larger than the current head size. Calls to this
  39. // method will also invalidate all temporary allocation values. This call will
  40. // fail if a chain of allocations through AllocateTemp() have not been cleaned
  41. // up with a call to ResetTempAllocations().
  42. virtual TfLiteStatus EnsureHeadSize(size_t size, size_t alignment);
  43. // Allocates memory starting at the tail of the arena (highest address and
  44. // moving downwards).
  45. virtual uint8_t* AllocateFromTail(size_t size, size_t alignment);
  46. // Allocates a temporary buffer from the head of the arena (lowest address and
  47. // moving upwards) but does not update the actual head allocation size or
  48. // position. The returned buffer is guaranteed until either
  49. // ResetTempAllocations() is called or another call to AllocateFromHead().
  50. // Repeat calls to this function will create a chain of temp allocations. All
  51. // calls to AllocateTemp() must end with a call to ResetTempAllocations(). If
  52. // AllocateFromHead() is called before a call to ResetTempAllocations(), it
  53. // will fail with an error message.
  54. virtual uint8_t* AllocateTemp(size_t size, size_t alignment);
  55. // Resets a chain of temporary allocations back to the current head of the
  56. // arena (lowest address).
  57. virtual void ResetTempAllocations();
  58. uint8_t* GetHead() const;
  59. uint8_t* GetBufferHead() const;
  60. uint8_t* GetTail() const;
  61. size_t GetHeadUsedBytes() const;
  62. size_t GetTailUsedBytes() const;
  63. // Returns the number of bytes available with a given alignment.
  64. size_t GetAvailableMemory(size_t alignment) const;
  65. size_t GetUsedBytes() const;
  66. private:
  67. size_t GetBufferSize() const;
  68. ErrorReporter* error_reporter_;
  69. uint8_t* buffer_head_;
  70. uint8_t* buffer_tail_;
  71. uint8_t* head_;
  72. uint8_t* tail_;
  73. uint8_t* temp_;
  74. TF_LITE_REMOVE_VIRTUAL_DELETE
  75. };
  76. } // namespace tflite
  77. #endif // TENSORFLOW_LITE_MICRO_SIMPLE_MEMORY_ALLOCATOR_H_