forked from bblanchon/ArduinoJson
Test: share SpyingAllocator
This commit is contained in:
@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
#include <ArduinoJson/Memory/Allocator.hpp>
|
#include <ArduinoJson/Memory/Allocator.hpp>
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
struct FailingAllocator : ArduinoJson::Allocator {
|
struct FailingAllocator : ArduinoJson::Allocator {
|
||||||
static FailingAllocator* instance() {
|
static FailingAllocator* instance() {
|
||||||
static FailingAllocator allocator;
|
static FailingAllocator allocator;
|
||||||
@ -26,3 +28,30 @@ struct FailingAllocator : ArduinoJson::Allocator {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class SpyingAllocator : public ArduinoJson::Allocator {
|
||||||
|
public:
|
||||||
|
virtual ~SpyingAllocator() {}
|
||||||
|
|
||||||
|
void* allocate(size_t n) override {
|
||||||
|
_log << "A" << n;
|
||||||
|
return malloc(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
void deallocate(void* p) override {
|
||||||
|
_log << "F";
|
||||||
|
free(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
void* reallocate(void* ptr, size_t n) override {
|
||||||
|
_log << "R" << n;
|
||||||
|
return realloc(ptr, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string log() const {
|
||||||
|
return _log.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::ostringstream _log;
|
||||||
|
};
|
||||||
|
@ -5,38 +5,12 @@
|
|||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <stdlib.h> // malloc, free
|
#include <stdlib.h> // malloc, free
|
||||||
#include <catch.hpp>
|
#include <catch.hpp>
|
||||||
#include <sstream>
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include "Allocators.hpp"
|
||||||
|
|
||||||
using ArduinoJson::detail::sizeofObject;
|
using ArduinoJson::detail::sizeofObject;
|
||||||
|
|
||||||
class SpyingAllocator : public Allocator {
|
|
||||||
public:
|
|
||||||
virtual ~SpyingAllocator() {}
|
|
||||||
|
|
||||||
void* allocate(size_t n) override {
|
|
||||||
_log << "A" << n;
|
|
||||||
return malloc(n);
|
|
||||||
}
|
|
||||||
|
|
||||||
void deallocate(void* p) override {
|
|
||||||
_log << "F";
|
|
||||||
free(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
void* reallocate(void* ptr, size_t n) override {
|
|
||||||
_log << "R" << n;
|
|
||||||
return realloc(ptr, n);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string log() const {
|
|
||||||
return _log.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::ostringstream _log;
|
|
||||||
};
|
|
||||||
|
|
||||||
class ControllableAllocator : public Allocator {
|
class ControllableAllocator : public Allocator {
|
||||||
public:
|
public:
|
||||||
ControllableAllocator() : _enabled(true) {}
|
ControllableAllocator() : _enabled(true) {}
|
||||||
|
Reference in New Issue
Block a user