tflite-micro/tensorflow/lite/micro/tools/make/flatbuffers.patch
Esun Kim 41a80afaa4
[Dep] Upgraded Flatbuffer to 25.9.23 (#3296)
* flatbuffer-25.9.23

* Patch 1

* No dynamic allocation patch to Flatbuffer

* Individual ModelBuilder

* Fix

* Patch

* One more removal
2026-01-27 22:12:21 +00:00

133 lines
5 KiB
Diff

diff --git a/include/flatbuffers/base.h b/include/flatbuffers/base.h
index 987dcbc4..0b49748b 100644
--- a/include/flatbuffers/base.h
+++ b/include/flatbuffers/base.h
@@ -1,6 +1,16 @@
#ifndef FLATBUFFERS_BASE_H_
#define FLATBUFFERS_BASE_H_
+// For TFLM, we always want FLATBUFFERS_LOCALE_INDEPENDENT to be defined as 0.
+// We could achieve this by adding -DFLATBUFFERS_LOCALE_INDEPENDENT=0 to the
+// TFLM Makefile. However, for (at least) the Arduino, adding additional build
+// flags during the compilation can be a bit awkward. As such, we have instead
+// made a decision to change the default to be FLATBUFFERS_LOCALE_INDEPENDENT=0
+// for TFLM to make it easier for external IDE integration.
+#ifndef FLATBUFFERS_LOCALE_INDEPENDENT
+#define FLATBUFFERS_LOCALE_INDEPENDENT 0
+#endif
+
// clang-format off
// If activate should be declared and included first.
diff --git a/include/flatbuffers/default_allocator.h b/include/flatbuffers/default_allocator.h
index d1cab08d..b253b598 100644
--- a/include/flatbuffers/default_allocator.h
+++ b/include/flatbuffers/default_allocator.h
@@ -39,24 +39,18 @@ class DefaultAllocator : public Allocator {
// This is to avoid having a statically or dynamically allocated default
// allocator, or having to move it between the classes that may own it.
inline uint8_t* Allocate(Allocator* allocator, size_t size) {
- return allocator ? allocator->allocate(size)
- : DefaultAllocator().allocate(size);
+ return allocator->allocate(size);
}
inline void Deallocate(Allocator* allocator, uint8_t* p, size_t size) {
- if (allocator)
- allocator->deallocate(p, size);
- else
- DefaultAllocator().deallocate(p, size);
+ allocator->deallocate(p, size);
}
inline uint8_t* ReallocateDownward(Allocator* allocator, uint8_t* old_p,
size_t old_size, size_t new_size,
size_t in_use_back, size_t in_use_front) {
- return allocator ? allocator->reallocate_downward(old_p, old_size, new_size,
- in_use_back, in_use_front)
- : DefaultAllocator().reallocate_downward(
- old_p, old_size, new_size, in_use_back, in_use_front);
+ return allocator->reallocate_downward(old_p, old_size, new_size, in_use_back,
+ in_use_front);
}
} // namespace flatbuffers
diff --git a/include/flatbuffers/flatbuffer_builder.h b/include/flatbuffers/flatbuffer_builder.h
index 9eea6bab..843244d4 100644
--- a/include/flatbuffers/flatbuffer_builder.h
+++ b/include/flatbuffers/flatbuffer_builder.h
@@ -152,11 +152,13 @@ class FlatBufferBuilderImpl {
swap(minalign_, other.minalign_);
swap(force_defaults_, other.force_defaults_);
swap(dedup_vtables_, other.dedup_vtables_);
- swap(string_pool, other.string_pool);
+ // TFLM doesn't use dynamic memory allocation.
+ // swap(string_pool, other.string_pool);
}
~FlatBufferBuilderImpl() {
- if (string_pool) delete string_pool;
+ // TFLM doesn't use dynamic memory allocation.
+ // if (string_pool) delete string_pool;
}
void Reset() {
@@ -173,7 +175,8 @@ class FlatBufferBuilderImpl {
finished = false;
minalign_ = 1;
length_of_64_bit_region_ = 0;
- if (string_pool) string_pool->clear();
+ // TFLM doesn't use dynamic memory allocation.
+ // if (string_pool) string_pool->clear();
}
/// @brief The current size of the serialized buffer, counting from the end.
diff --git a/include/flatbuffers/flexbuffers.h b/include/flatbuffers/flexbuffers.h
index 2784dafb..a82fabde 100644
--- a/include/flatbuffers/flexbuffers.h
+++ b/include/flatbuffers/flexbuffers.h
@@ -542,9 +542,24 @@ class Reference {
case FBT_NULL:
return 0.0;
case FBT_STRING: {
+#if 1
+#if !defined( _MSC_VER)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wnull-dereference"
+#endif
+ // See b/173239141 for additional context. Patched via
+ // micro/tools/make/flexbuffers_download.sh
+ // Introduce a segfault for an unsupported code path for TFLM.
+ return *(static_cast<double*>(nullptr));
+#if !defined( _MSC_VER)
+#pragma GCC diagnostic pop
+#endif
+#else
+ // This is the original code
double d;
flatbuffers::StringToNumber(AsString().c_str(), &d);
return d;
+#endif
}
case FBT_VECTOR:
return static_cast<double>(AsVector().size());
diff --git a/include/flatbuffers/util.h b/include/flatbuffers/util.h
index 0313e74d..607b6a38 100644
--- a/include/flatbuffers/util.h
+++ b/include/flatbuffers/util.h
@@ -23,6 +23,12 @@
#include "flatbuffers/base.h"
#include "flatbuffers/stl_emulation.h"
+// For TFLM we always want to use FLATBUFFERS_PREFER_PRINTF=1. See
+// http://b/211811553 for more context.
+#ifndef FLATBUFFERS_PREFER_PRINTF
+#define FLATBUFFERS_PREFER_PRINTF 1
+#endif
+
#ifndef FLATBUFFERS_PREFER_PRINTF
#include <iomanip>
#include <sstream>
--
2.52.0.457.g6b5491de43-goog