8000 Removed global new operator overload (issue #40, #45 and #46) · littlestrange/ArduinoJson@8db338b · GitHub
[go: up one dir, main page]

Skip to content

Commit 8db338b

Browse files
committed
Removed global new operator overload (issue bblanchon#40, bblanchon#45 and bblanchon#46)
1 parent dadd898 commit 8db338b

File tree

10 files changed

+42
-35
lines changed

10 files changed

+42
-35
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Arduino JSON: change log
22
========================
33

4+
HEAD
5+
----
6+
7+
* Removed global new operator overload (issue #40, #45 and #46)
8+
* Added an example with EthernetServer
9+
410
v4.1
511
----
612

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright Benoit Blanchon 2014
2+
// MIT License
3+
//
4+
// Arduino JSON library
5+
// https://github.com/bblanchon/ArduinoJson
6+
7+
#pragma once
8+
9+
#include "../JsonBuffer.hpp"
10+
11+
namespace ArduinoJson {
12+
namespace Internals {
13+
14+
class JsonBufferAllocated {
15+
public:
16+
void *operator new(size_t n, JsonBuffer *jsonBuffer) throw() {
17+
return jsonBuffer->alloc(n);
18+
}
19+
};
20+
}
21+
}

include/ArduinoJson/Internals/List.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "../JsonBuffer.hpp"
1010
#include "ListConstIterator.hpp"
1111
#include "ListIterator.hpp"
12-
#include "PlacementNew.hpp"
1312

1413
namespace ArduinoJson {
1514
namespace Internals {
@@ -51,8 +50,7 @@ class List {
5150
protected:
5251
node_type *createNode() {
5352
if (!_buffer) return NULL;
54-
void *ptr = _buffer->alloc(sizeof(node_type));
55-
return ptr ? new (ptr) node_type() : NULL;
53+
return new (_buffer) node_type();
5654
}
5755

5856
void addNode(node_type *nodeToAdd) {

include/ArduinoJson/Internals/ListNode.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88

99
#include <stddef.h> // for NULL
1010

11+
#include "JsonBufferAllocated.hpp"
12+
1113
namespace ArduinoJson {
1214
namespace Internals {
1315

1416
// A node for a singly-linked list.
1517
// Used by List<T> and its iterators.
1618
template <typename T>
17-
struct ListNode {
19+
struct ListNode : public Internals::JsonBufferAllocated {
1820
ListNode() : next(NULL) {}
1921

20-
ListNode<T>* next;
22+
ListNode<T> *next;
2123
T content;
2224
};
2325
}

include/ArduinoJson/Internals/PlacementNew.hpp

Lines changed: 0 additions & 19 deletions
This file was deleted.

include/ArduinoJson/JsonArray.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#pragma once
88

9+
#include "Internals/JsonBufferAllocated.hpp"
910
#include "Internals/JsonPrintable.hpp"
1011
#include "Internals/List.hpp"
1112
#include "Internals/ReferenceType.hpp"
@@ -30,7 +31,8 @@ class JsonBuffer;
3031
// It can also be deserialized from a JSON string via JsonBuffer::parseArray().
3132
class JsonArray : public Internals::JsonPrintable<JsonArray>,
3233
public Internals::ReferenceType,
33-
public Internals::List<JsonVariant> {
34+
public Internals::List<JsonVariant>,
35+
public Internals::JsonBufferAllocated {
3436
// JsonBuffer is a friend because it needs to call the private constructor.
3537
friend class JsonBuffer;
3638

include/ArduinoJson/JsonObject.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#pragma once
88

9+
#include "Internals/JsonBufferAllocated.hpp"
910
#include "Internals/JsonPrintable.hpp"
1011
#include "Internals/List.hpp"
1112
#include "Internals/ReferenceType.hpp"
@@ -30,7 +31,8 @@ class JsonBuffer;
3031
// It can also be deserialized from a JSON string via JsonBuffer::parseObject().
3132
class JsonObject : public Internals::JsonPrintable<JsonObject>,
3233
public Internals::ReferenceType,
33-
public Internals::List<JsonPair> {
34+
public Internals::List<JsonPair>,
35+
public Internals::JsonBufferAllocated {
3436
// JsonBuffer is a friend because it needs to call the private constructor.
3537
friend class JsonBuffer;
3638

src/Internals/List.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#include "../../include/ArduinoJson/Internals/List.hpp"
88

9-
#include "../../include/ArduinoJson/Internals/PlacementNew.hpp"
109
#include "../../include/ArduinoJson/JsonPair.hpp"
1110
#include "../../include/ArduinoJson/JsonVariant.hpp"
1211

src/JsonBuffer.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,20 @@
77
#include "../include/ArduinoJson/JsonBuffer.hpp"
88

99
#include "../include/ArduinoJson/Internals/JsonParser.hpp"
10-
#include "../include/ArduinoJson/Internals/PlacementNew.hpp"
1110
#include "../include/ArduinoJson/JsonArray.hpp"
1211
#include "../include/ArduinoJson/JsonObject.hpp"
1312

1413
using namespace ArduinoJson;
1514
using namespace ArduinoJson::Internals;
1615

1716
JsonArray &JsonBuffer::createArray() {
18-
void *ptr = alloc(sizeof(JsonArray));
19-
if (ptr) return *new (ptr) JsonArray(this);
20-
return JsonArray::invalid();
17+
JsonArray *ptr = new (this) JsonArray(this);
18+
return ptr ? *ptr : JsonArray::invalid();
2119
}
2220

2321
JsonObject &JsonBuffer::createObject() {
24-
void *ptr = alloc(sizeof(JsonObject));
25-
if (ptr) return *new (ptr) JsonObject(this);
26-
return JsonObject::invalid();
22+
JsonObject *ptr = new (this) JsonObject(this);
23+
return ptr ? *ptr : JsonObject::invalid();
2724
}
2825

2926
JsonArray &JsonBuffer::parseArray(char *json, uint8_t nestingLimit) {

src/JsonObject.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include <string.h> // for strcmp
1010

11-
#include "../include/ArduinoJson/Internals/PlacementNew.hpp"
1211
#include "../include/ArduinoJson/Internals/StringBuilder.hpp"
1312
#include "../include/ArduinoJson/JsonArray.hpp"
1413
#include "../include/ArduinoJson/JsonBuffer.hpp"

0 commit comments

Comments
 (0)
0