simple_memory_allocator.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/core/api/error_reporter.h"
  17. #include "tensorflow/lite/micro/compatibility.h"
  18. namespace tflite {
  19. // TODO(petewarden): This allocator never frees up or reuses any memory, even
  20. // though we have enough information about lifetimes of the tensors to do so.
  21. // This makes it pretty wasteful, so we should use a more intelligent method.
  22. class SimpleMemoryAllocator {
  23. public:
  24. // TODO(b/157615197): Cleanup constructors/destructor and use factory
  25. // functions.
  26. SimpleMemoryAllocator(ErrorReporter* error_reporter, uint8_t* buffer_head,
  27. uint8_t* buffer_tail);
  28. SimpleMemoryAllocator(ErrorReporter* error_reporter, uint8_t* buffer,
  29. size_t buffer_size);
  30. virtual ~SimpleMemoryAllocator();
  31. // Creates a new SimpleMemoryAllocator from a given buffer head and size.
  32. static SimpleMemoryAllocator* Create(ErrorReporter* error_reporter,
  33. uint8_t* buffer_head,
  34. size_t buffer_size);
  35. // Allocates memory starting at the head of the arena (lowest address and
  36. // moving upwards).
  37. virtual uint8_t* AllocateFromHead(size_t size, size_t alignment);
  38. // Allocates memory starting at the tail of the arena (highest address and
  39. // moving downwards).
  40. virtual uint8_t* AllocateFromTail(size_t size, size_t alignment);
  41. uint8_t* GetHead() const;
  42. uint8_t* GetTail() const;
  43. size_t GetHeadUsedBytes() const;
  44. size_t GetTailUsedBytes() const;
  45. size_t GetAvailableMemory() const;
  46. size_t GetUsedBytes() const;
  47. private:
  48. size_t GetBufferSize() const;
  49. ErrorReporter* error_reporter_;
  50. uint8_t* buffer_head_;
  51. uint8_t* buffer_tail_;
  52. uint8_t* head_;
  53. uint8_t* tail_;
  54. TF_LITE_REMOVE_VIRTUAL_DELETE
  55. };
  56. } // namespace tflite
  57. #endif // TENSORFLOW_LITE_MICRO_SIMPLE_MEMORY_ALLOCATOR_H_