diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b334692..67d12e3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ Arduino JSON: change log ======================== +HEAD +---- + +* Removed global new operator overload (issue #40, #45 and #46) +* Added an example with EthernetServer + v4.1 ---- diff --git a/include/ArduinoJson/Internals/JsonBufferAllocated.hpp b/include/ArduinoJson/Internals/JsonBufferAllocated.hpp new file mode 100644 index 00000000..3e283889 --- /dev/null +++ b/include/ArduinoJson/Internals/JsonBufferAllocated.hpp @@ -0,0 +1,21 @@ +// Copyright Benoit Blanchon 2014 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson + +#pragma once + +#include "../JsonBuffer.hpp" + +namespace ArduinoJson { +namespace Internals { + +class JsonBufferAllocated { + public: + void *operator new(size_t n, JsonBuffer *jsonBuffer) throw() { + return jsonBuffer->alloc(n); + } +}; +} +} diff --git a/include/ArduinoJson/Internals/List.hpp b/include/ArduinoJson/Internals/List.hpp index be121b8b..8e64417c 100644 --- a/include/ArduinoJson/Internals/List.hpp +++ b/include/ArduinoJson/Internals/List.hpp @@ -9,7 +9,6 @@ #include "../JsonBuffer.hpp" #include "ListConstIterator.hpp" #include "ListIterator.hpp" -#include "PlacementNew.hpp" namespace ArduinoJson { namespace Internals { @@ -51,8 +50,7 @@ class List { protected: node_type *createNode() { if (!_buffer) return NULL; - void *ptr = _buffer->alloc(sizeof(node_type)); - return ptr ? new (ptr) node_type() : NULL; + return new (_buffer) node_type(); } void addNode(node_type *nodeToAdd) { diff --git a/include/ArduinoJson/Internals/ListNode.hpp b/include/ArduinoJson/Internals/ListNode.hpp index e5843d95..43c99f5f 100644 --- a/include/ArduinoJson/Internals/ListNode.hpp +++ b/include/ArduinoJson/Internals/ListNode.hpp @@ -8,16 +8,18 @@ #include // for NULL +#include "JsonBufferAllocated.hpp" + namespace ArduinoJson { namespace Internals { // A node for a singly-linked list. // Used by List and its iterators. template -struct ListNode { +struct ListNode : public Internals::JsonBufferAllocated { ListNode() : next(NULL) {} - ListNode* next; + ListNode *next; T content; }; } diff --git a/include/ArduinoJson/Internals/PlacementNew.hpp b/include/ArduinoJson/Internals/PlacementNew.hpp deleted file mode 100644 index 1095bb17..00000000 --- a/include/ArduinoJson/Internals/PlacementNew.hpp +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright Benoit Blanchon 2014 -// MIT License -// -// Arduino JSON library -// https://github.com/bblanchon/ArduinoJson - -#pragma once - -#ifdef ARDUINO - -// Declares the placement new as in . -// This is required for Arduino IDE because it doesn't include the header. -inline void *operator new(size_t, void *p) throw() { return p; } - -#else - -#include - -#endif diff --git a/include/ArduinoJson/JsonArray.hpp b/include/ArduinoJson/JsonArray.hpp index daff423a..62fa2188 100644 --- a/include/ArduinoJson/JsonArray.hpp +++ b/include/ArduinoJson/JsonArray.hpp @@ -6,6 +6,7 @@ #pragma once +#include "Internals/JsonBufferAllocated.hpp" #include "Internals/JsonPrintable.hpp" #include "Internals/List.hpp" #include "Internals/ReferenceType.hpp" @@ -30,7 +31,8 @@ class JsonBuffer; // It can also be deserialized from a JSON string via JsonBuffer::parseArray(). class JsonArray : public Internals::JsonPrintable, public Internals::ReferenceType, - public Internals::List { + public Internals::List, + public Internals::JsonBufferAllocated { // JsonBuffer is a friend because it needs to call the private constructor. friend class JsonBuffer; diff --git a/include/ArduinoJson/JsonObject.hpp b/include/ArduinoJson/JsonObject.hpp index bfea4bc6..bc210df0 100644 --- a/include/ArduinoJson/JsonObject.hpp +++ b/include/ArduinoJson/JsonObject.hpp @@ -6,6 +6,7 @@ #pragma once +#include "Internals/JsonBufferAllocated.hpp" #include "Internals/JsonPrintable.hpp" #include "Internals/List.hpp" #include "Internals/ReferenceType.hpp" @@ -30,7 +31,8 @@ class JsonBuffer; // It can also be deserialized from a JSON string via JsonBuffer::parseObject(). class JsonObject : public Internals::JsonPrintable, public Internals::ReferenceType, - public Internals::List { + public Internals::List, + public Internals::JsonBufferAllocated { // JsonBuffer is a friend because it needs to call the private constructor. friend class JsonBuffer; diff --git a/src/Internals/List.cpp b/src/Internals/List.cpp index f8d721da..09a41a4c 100644 --- a/src/Internals/List.cpp +++ b/src/Internals/List.cpp @@ -6,7 +6,6 @@ #include "../../include/ArduinoJson/Internals/List.hpp" -#include "../../include/ArduinoJson/Internals/PlacementNew.hpp" #include "../../include/ArduinoJson/JsonPair.hpp" #include "../../include/ArduinoJson/JsonVariant.hpp" diff --git a/src/JsonBuffer.cpp b/src/JsonBuffer.cpp index 49dcf3d9..52631707 100644 --- a/src/JsonBuffer.cpp +++ b/src/JsonBuffer.cpp @@ -7,7 +7,6 @@ #include "../include/ArduinoJson/JsonBuffer.hpp" #include "../include/ArduinoJson/Internals/JsonParser.hpp" -#include "../include/ArduinoJson/Internals/PlacementNew.hpp" #include "../include/ArduinoJson/JsonArray.hpp" #include "../include/ArduinoJson/JsonObject.hpp" @@ -15,15 +14,13 @@ using namespace ArduinoJson; using namespace ArduinoJson::Internals; JsonArray &JsonBuffer::createArray() { - void *ptr = alloc(sizeof(JsonArray)); - if (ptr) return *new (ptr) JsonArray(this); - return JsonArray::invalid(); + JsonArray *ptr = new (this) JsonArray(this); + return ptr ? *ptr : JsonArray::invalid(); } JsonObject &JsonBuffer::createObject() { - void *ptr = alloc(sizeof(JsonObject)); - if (ptr) return *new (ptr) JsonObject(this); - return JsonObject::invalid(); + JsonObject *ptr = new (this) JsonObject(this); + return ptr ? *ptr : JsonObject::invalid(); } JsonArray &JsonBuffer::parseArray(char *json, uint8_t nestingLimit) { diff --git a/src/JsonObject.cpp b/src/JsonObject.cpp index 355d2a04..71578d77 100644 --- a/src/JsonObject.cpp +++ b/src/JsonObject.cpp @@ -8,7 +8,6 @@ #include // for strcmp -#include "../include/ArduinoJson/Internals/PlacementNew.hpp" #include "../include/ArduinoJson/Internals/StringBuilder.hpp" #include "../include/ArduinoJson/JsonArray.hpp" #include "../include/ArduinoJson/JsonBuffer.hpp"