|
@@ -9,6 +9,7 @@
|
|
|
#include <string.h>
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "../Configuration.hpp"
|
|
#include "../Configuration.hpp"
|
|
|
|
|
+#include "../Data/Slot.hpp"
|
|
|
#include "../Polyfills/attributes.hpp"
|
|
#include "../Polyfills/attributes.hpp"
|
|
|
|
|
|
|
|
namespace ARDUINOJSON_NAMESPACE {
|
|
namespace ARDUINOJSON_NAMESPACE {
|
|
@@ -23,12 +24,36 @@ class MemoryPool {
|
|
|
|
|
|
|
|
virtual char *realloc(char *oldPtr, size_t oldSize, size_t newSize) = 0;
|
|
virtual char *realloc(char *oldPtr, size_t oldSize, size_t newSize) = 0;
|
|
|
|
|
|
|
|
|
|
+ Slot *allocSlot() {
|
|
|
|
|
+ if (_freeSlots) {
|
|
|
|
|
+ Slot *s = _freeSlots;
|
|
|
|
|
+ _freeSlots = s->next;
|
|
|
|
|
+ return s;
|
|
|
|
|
+ }
|
|
|
|
|
+ return reinterpret_cast<Slot *>(alloc(sizeof(Slot)));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ void freeSlot(Slot *slot) {
|
|
|
|
|
+ slot->next = _freeSlots;
|
|
|
|
|
+ _freeSlots = slot;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ size_t size() const {
|
|
|
|
|
+ size_t result = allocated_bytes();
|
|
|
|
|
+ for (Slot *s = _freeSlots; s; s = s->next) result -= sizeof(Slot);
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
protected:
|
|
protected:
|
|
|
|
|
+ MemoryPool() : _freeSlots(0) {}
|
|
|
|
|
+
|
|
|
// CAUTION: NO VIRTUAL DESTRUCTOR!
|
|
// CAUTION: NO VIRTUAL DESTRUCTOR!
|
|
|
// If we add a virtual constructor the Arduino compiler will add malloc()
|
|
// If we add a virtual constructor the Arduino compiler will add malloc()
|
|
|
// and free() to the binary, adding 706 useless bytes.
|
|
// and free() to the binary, adding 706 useless bytes.
|
|
|
~MemoryPool() {}
|
|
~MemoryPool() {}
|
|
|
|
|
|
|
|
|
|
+ virtual size_t allocated_bytes() const = 0;
|
|
|
|
|
+
|
|
|
// Preserve aligment if necessary
|
|
// Preserve aligment if necessary
|
|
|
static FORCE_INLINE size_t round_size_up(size_t bytes) {
|
|
static FORCE_INLINE size_t round_size_up(size_t bytes) {
|
|
|
#if ARDUINOJSON_ENABLE_ALIGNMENT
|
|
#if ARDUINOJSON_ENABLE_ALIGNMENT
|
|
@@ -38,5 +63,8 @@ class MemoryPool {
|
|
|
return bytes;
|
|
return bytes;
|
|
|
#endif
|
|
#endif
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ private:
|
|
|
|
|
+ Slot *_freeSlots;
|
|
|
};
|
|
};
|
|
|
} // namespace ARDUINOJSON_NAMESPACE
|
|
} // namespace ARDUINOJSON_NAMESPACE
|