Fixed segmentation fault in DynamicJsonBuffer when memory allocation fails (issue #92)

This commit is contained in:
Benoit Blanchon
2015-08-01 16:31:59 +02:00
parent f5b83f9314
commit 2524a00a96
8 changed files with 163 additions and 133 deletions

View File

@@ -6,41 +6,12 @@
#pragma once
#include "JsonBuffer.hpp"
#include <stdlib.h>
#include "Internals/BlockJsonBuffer.hpp"
namespace ArduinoJson {
// Forward declaration
namespace Internals {
struct DynamicJsonBufferBlock;
}
// Implements a JsonBuffer with dynamic memory allocation.
// You are strongly encouraged to consider using StaticJsonBuffer which is much
// more suitable for embedded systems.
class DynamicJsonBuffer : public JsonBuffer {
public:
DynamicJsonBuffer();
~DynamicJsonBuffer();
size_t size() const;
protected:
virtual void* alloc(size_t bytes);
private:
typedef Internals::DynamicJsonBufferBlock Block;
static const size_t FIRST_BLOCK_CAPACITY = 32;
static Block* createBlock(size_t capacity);
inline bool canAllocInHead(size_t bytes) const;
inline void* allocInHead(size_t bytes);
inline void addNewBlock();
Block* _head;
};
typedef Internals::BlockJsonBuffer<Internals::DefaultAllocator>
DynamicJsonBuffer;
}