@@ -31,33 +31,38 @@ struct FailingAllocator : ArduinoJson::Allocator {
31
31
32
32
class AllocatorLog {
33
33
public:
34
- struct Allocate {
35
- Allocate (size_t s) : size(s) {}
36
- size_t size;
37
- };
34
+ static std::string Allocate (size_t s) {
35
+ char buffer[32 ];
36
+ sprintf (buffer, " allocate(%zu)" , s);
37
+ return buffer;
38
+ }
38
39
39
- struct Reallocate {
40
- Reallocate (size_t s1, size_t s2) : oldSize(s1), newSize(s2) {}
41
- size_t oldSize, newSize;
42
- };
40
+ static std::string AllocateFail (size_t s) {
41
+ char buffer[32 ];
42
+ sprintf (buffer, " allocate(%zu) -> nullptr" , s);
43
+ return buffer;
44
+ }
43
45
44
- struct Deallocate {
45
- Deallocate (size_t s) : size(s) {}
46
- size_t size;
46
+ static std::string Reallocate (size_t s1, size_t s2) {
47
+ char buffer[32 ];
48
+ sprintf (buffer, " reallocate(%zu, %zu)" , s1, s2);
49
+ return buffer;
47
50
};
48
51
49
- AllocatorLog& operator <<(const Allocate& a) {
50
- _log << " allocate(" << a.size << " )\n " ;
51
- return *this ;
52
- }
52
+ static std::string ReallocateFail (size_t s1, size_t s2) {
53
+ char buffer[32 ];
54
+ sprintf (buffer, " reallocate(%zu, %zu) -> nullptr" , s1, s2);
55
+ return buffer;
56
+ };
53
57
54
- AllocatorLog& operator <<(const Deallocate& d) {
55
- _log << " deallocate(" << d.size << " )\n " ;
56
- return *this ;
57
- }
58
+ static std::string Deallocate (size_t s) {
59
+ char buffer[32 ];
60
+ sprintf (buffer, " deallocate(%zu)" , s);
61
+ return buffer;
62
+ };
58
63
59
- AllocatorLog& operator <<(const Reallocate& a ) {
60
- _log << " reallocate( " << a. oldSize << " , " << a. newSize << " ) \n " ;
64
+ AllocatorLog& operator <<(const std::string& s ) {
65
+ _log << s << " \n " ;
61
66
return *this ;
62
67
}
63
68
@@ -90,11 +95,16 @@ class SpyingAllocator : public ArduinoJson::Allocator {
90
95
virtual ~SpyingAllocator () {}
91
96
92
97
void * allocate (size_t n) override {
93
- _log << AllocatorLog::Allocate (n);
94
98
auto block = reinterpret_cast <AllocatedBlock*>(
95
99
_upstream->allocate (sizeof (AllocatedBlock) + n - 1 ));
96
- block->size = n;
97
- return block->payload ;
100
+ if (block) {
101
+ _log << AllocatorLog::Allocate (n);
102
+ block->size = n;
103
+ return block->payload ;
104
+ } else {
105
+ _log << AllocatorLog::AllocateFail (n);
106
+ return nullptr ;
107
+ }
98
108
}
99
109
100
110
void deallocate (void * p) override {
@@ -105,11 +115,17 @@ class SpyingAllocator : public ArduinoJson::Allocator {
105
115
106
116
void * reallocate (void * p, size_t n) override {
107
117
auto block = AllocatedBlock::fromPayload (p);
108
- _log << AllocatorLog::Reallocate ( block->size , n) ;
118
+ auto oldSize = block->size ;
109
119
block = reinterpret_cast <AllocatedBlock*>(
110
120
_upstream->reallocate (block, sizeof (AllocatedBlock) + n - 1 ));
111
- block->size = n;
112
- return block->payload ;
121
+ if (block) {
122
+ _log << AllocatorLog::Reallocate (oldSize, n);
123
+ block->size = n;
124
+ return block->payload ;
125
+ } else {
126
+ _log << AllocatorLog::ReallocateFail (oldSize, n);
127
+ return nullptr ;
128
+ }
113
129
}
114
130
115
131
const AllocatorLog& log () const {
0 commit comments