Skip to content

Commit 6afa6b6

Browse files
committed
Test: Support failed allocations in SpyingAllocator
1 parent acd465b commit 6afa6b6

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

extras/tests/Helpers/Allocators.hpp

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,38 @@ struct FailingAllocator : ArduinoJson::Allocator {
3131

3232
class AllocatorLog {
3333
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+
}
3839

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+
}
4345

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;
4750
};
4851

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+
};
5357

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+
};
5863

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";
6166
return *this;
6267
}
6368

@@ -90,11 +95,16 @@ class SpyingAllocator : public ArduinoJson::Allocator {
9095
virtual ~SpyingAllocator() {}
9196

9297
void* allocate(size_t n) override {
93-
_log << AllocatorLog::Allocate(n);
9498
auto block = reinterpret_cast<AllocatedBlock*>(
9599
_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+
}
98108
}
99109

100110
void deallocate(void* p) override {
@@ -105,11 +115,17 @@ class SpyingAllocator : public ArduinoJson::Allocator {
105115

106116
void* reallocate(void* p, size_t n) override {
107117
auto block = AllocatedBlock::fromPayload(p);
108-
_log << AllocatorLog::Reallocate(block->size, n);
118+
auto oldSize = block->size;
109119
block = reinterpret_cast<AllocatedBlock*>(
110120
_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+
}
113129
}
114130

115131
const AllocatorLog& log() const {

0 commit comments

Comments
 (0)