Skip to content

Commit

Permalink
Removed global new operator overload (issue #40, #45 and #46)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Feb 1, 2015
1 parent dadd898 commit 8db338b
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 35 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
----

Expand Down
21 changes: 21 additions & 0 deletions include/ArduinoJson/Internals/JsonBufferAllocated.hpp
Original file line number Diff line number Diff line change
@@ -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);
}
};
}
}
4 changes: 1 addition & 3 deletions include/ArduinoJson/Internals/List.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "../JsonBuffer.hpp"
#include "ListConstIterator.hpp"
#include "ListIterator.hpp"
#include "PlacementNew.hpp"

namespace ArduinoJson {
namespace Internals {
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions include/ArduinoJson/Internals/ListNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

#include <stddef.h> // for NULL

#include "JsonBufferAllocated.hpp"

namespace ArduinoJson {
namespace Internals {

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

ListNode<T>* next;
ListNode<T> *next;
T content;
};
}
Expand Down
19 changes: 0 additions & 19 deletions include/ArduinoJson/Internals/PlacementNew.hpp

This file was deleted.

4 changes: 3 additions & 1 deletion include/ArduinoJson/JsonArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#pragma once

#include "Internals/JsonBufferAllocated.hpp"
#include "Internals/JsonPrintable.hpp"
#include "Internals/List.hpp"
#include "Internals/ReferenceType.hpp"
Expand All @@ -30,7 +31,8 @@ class JsonBuffer;
// It can also be deserialized from a JSON string via JsonBuffer::parseArray().
class JsonArray : public Internals::JsonPrintable<JsonArray>,
public Internals::ReferenceType,
public Internals::List<JsonVariant> {
public Internals::List<JsonVariant>,
public Internals::JsonBufferAllocated {
// JsonBuffer is a friend because it needs to call the private constructor.
friend class JsonBuffer;

Expand Down
4 changes: 3 additions & 1 deletion include/ArduinoJson/JsonObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#pragma once

#include "Internals/JsonBufferAllocated.hpp"
#include "Internals/JsonPrintable.hpp"
#include "Internals/List.hpp"
#include "Internals/ReferenceType.hpp"
Expand All @@ -30,7 +31,8 @@ class JsonBuffer;
// It can also be deserialized from a JSON string via JsonBuffer::parseObject().
class JsonObject : public Internals::JsonPrintable<JsonObject>,
public Internals::ReferenceType,
public Internals::List<JsonPair> {
public Internals::List<JsonPair>,
public Internals::JsonBufferAllocated {
// JsonBuffer is a friend because it needs to call the private constructor.
friend class JsonBuffer;

Expand Down
1 change: 0 additions & 1 deletion src/Internals/List.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
11 changes: 4 additions & 7 deletions src/JsonBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@
#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"

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) {
Expand Down
1 change: 0 additions & 1 deletion src/JsonObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <string.h> // for strcmp

#include "../include/ArduinoJson/Internals/PlacementNew.hpp"
#include "../include/ArduinoJson/Internals/StringBuilder.hpp"
#include "../include/ArduinoJson/JsonArray.hpp"
#include "../include/ArduinoJson/JsonBuffer.hpp"
Expand Down

0 comments on commit 8db338b

Please sign in to comment.