chromium/third_party/tflite/src/tensorflow/lite/simple_memory_arena.cc

/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#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  // TF_LITE_TENSORFLOW_PROFILER

#if defined(__ANDROID__)
// Android has C11 aligned_alloc only with API 28 or newer, even with C++17 or
// C11 compilation (this is a non-standard behavior).
#define TF_LITE_HAS_ALIGNED_ALLOC
#elif defined(__APPLE__)
// Apple does not provide aligned_alloc, even with C++17 or C11 compilation
// (this is a non-standard behavior).
#define TF_LITE_HAS_ALIGNED_ALLOC
#elif defined(_WIN32)
// Windows does not provide aligned_alloc, even with C++17 or C11 compilation
// (this is a non-standard behavior). However, it provides _aligned_malloc,
// _aligned_realloc, and _aligned_free, with a slightly different behavior than
// the C11/C++17 standard functions (size requirement, and free function name.)
#define TF_LITE_HAS_ALIGNED_ALLOC
#elif __cplusplus >= 201703L || __STDC_VERSION__ >= 201112L
// C++17 or C11 has (std::)aligned_alloc
#define TF_LITE_HAS_ALIGNED_ALLOC
#endif

namespace {

template <typename T>
T AlignTo(size_t alignment, T offset) {}

// Allocates memory and aligns it to the specified size. Returns a pair of the
// allocation pointer and the aligned pointer.
tflite::PointerAlignedPointerPair AlignedAlloc(size_t size, size_t alignment);

// Frees up aligned memory.
void AlignedFree(const tflite::PointerAlignedPointerPair& buffer);

// Reallocates aligned memory
//
// The function either extends the memory allocation in-place, or if that is not
// possible a new allocation is created, the data is copied, and the old buffer
// is deallocated. It is an error to change the alignment during reallocation.
// If the previous allocation is null, this is equivalent to AlignedAlloc.
// Returns pointers to the new allocation.
tflite::PointerAlignedPointerPair AlignedRealloc(
    const tflite::PointerAlignedPointerPair& old_buffer, size_t old_size,
    size_t new_size, size_t alignment);

#if defined(_WIN32)
// On Windows <cstdlib> provides _aligned_malloc, _aligned_free, and
// _aligned_realloc, use them to implement the Aligned functions.

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
// Default implementation: Use malloc, allocating extra memory, and align the
// pointer in the allocated buffer.

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

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() {}

// Using weak symbols to create a pluggable debugging module.
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 {}

}  // namespace tflite