#include "tensorflow/lite/simple_memory_arena.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <string>
#include <vector>
#include "tensorflow/lite/core/c/common.h"
#include "tensorflow/lite/core/macros.h"
#ifdef TF_LITE_TENSORFLOW_PROFILER
#include "tensorflow/lite/tensorflow_profiler_logger.h"
#endif
#if defined(__ANDROID__)
#define TF_LITE_HAS_ALIGNED_ALLOC …
#elif defined(__APPLE__)
#define TF_LITE_HAS_ALIGNED_ALLOC …
#elif defined(_WIN32)
#define TF_LITE_HAS_ALIGNED_ALLOC …
#elif __cplusplus >= 201703L || __STDC_VERSION__ >= 201112L
#define TF_LITE_HAS_ALIGNED_ALLOC …
#endif
namespace {
template <typename T>
T AlignTo(size_t alignment, T offset) { … }
tflite::PointerAlignedPointerPair AlignedAlloc(size_t size, size_t alignment);
void AlignedFree(const tflite::PointerAlignedPointerPair& buffer);
tflite::PointerAlignedPointerPair AlignedRealloc(
const tflite::PointerAlignedPointerPair& old_buffer, size_t old_size,
size_t new_size, size_t alignment);
#if defined(_WIN32)
tflite::PointerAlignedPointerPair AlignedAlloc(size_t size, size_t alignment) {
char* pointer = reinterpret_cast<char*>(_aligned_malloc(size, alignment));
char* aligned_ptr = pointer;
return {pointer, aligned_ptr};
}
void AlignedFree(const tflite::PointerAlignedPointerPair& buffer) {
_aligned_free(buffer.pointer);
}
tflite::PointerAlignedPointerPair AlignedRealloc(
const tflite::PointerAlignedPointerPair& old_buffer, size_t old_size,
size_t new_size, size_t alignment) {
char* pointer = reinterpret_cast<char*>(
_aligned_realloc(old_buffer.pointer, new_size, alignment));
char* aligned_ptr = pointer;
return {pointer, aligned_ptr};
}
#else
tflite::PointerAlignedPointerPair AlignedAlloc(size_t size, size_t alignment) { … }
void AlignedFree(const tflite::PointerAlignedPointerPair& buffer) { … }
tflite::PointerAlignedPointerPair AlignedRealloc(
const tflite::PointerAlignedPointerPair& old_buffer, size_t old_size,
size_t new_size, size_t alignment) { … }
#endif
}
namespace tflite {
bool ResizableAlignedBuffer::Resize(size_t new_size) { … }
void ResizableAlignedBuffer::Release() { … }
void SimpleMemoryArena::PurgeAfter(int32_t node) { … }
void SimpleMemoryArena::PurgeActiveAllocs(int32_t node) { … }
void SimpleMemoryArena::CalculateActiveAllocs(
const std::vector<ArenaAllocWithUsageInterval>& allocs, int32_t node) { … }
void SimpleMemoryArena::ResetAllocs() { … }
TfLiteStatus SimpleMemoryArena::Allocate(
TfLiteContext* context, size_t alignment, size_t size, int32_t tensor,
int32_t first_node, int32_t last_node,
ArenaAllocWithUsageInterval* new_alloc) { … }
TfLiteStatus SimpleMemoryArena::Commit(bool* arena_reallocated) { … }
TfLiteStatus SimpleMemoryArena::ResolveAlloc(
TfLiteContext* context, const ArenaAllocWithUsageInterval& alloc,
char** output_ptr) { … }
TfLiteStatus SimpleMemoryArena::ClearPlan() { … }
TfLiteStatus SimpleMemoryArena::ReleaseBuffer() { … }
TFLITE_ATTRIBUTE_WEAK void DumpArenaInfo(
const std::string& name, const std::vector<int>& execution_plan,
size_t arena_size, const std::vector<ArenaAllocWithUsageInterval>& allocs) { … }
void SimpleMemoryArena::DumpDebugInfo(
const std::string& name, const std::vector<int>& execution_plan) const { … }
}