From 568d75954e40d9cec156e4876d2d0730df099740 Mon Sep 17 00:00:00 2001 From: Niels Date: Sun, 15 Feb 2015 22:49:26 +0100 Subject: [PATCH] experiment with user-defined allocator --- src/json.hpp | 36 ++++++++++++++++++++++++++++-------- src/json.hpp.re2c | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 56 insertions(+), 16 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index bbffcfd3..2a738269 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -59,7 +59,8 @@ template < class StringType = std::string, class BooleanType = bool, class NumberIntegerType = int64_t, - class NumberFloatType = double + class NumberFloatType = double, + template class Allocator = std::allocator > class basic_json { @@ -199,7 +200,11 @@ class basic_json case (value_t::string): { - m_value.string = new string_t(""); + Allocator alloc; + m_value.string = alloc.allocate(1); + alloc.construct(m_value.string, ""); + + //m_value.string = new string_t(""); break; } @@ -270,13 +275,21 @@ class basic_json /// create a string (explicit) inline basic_json(const string_t& value) - : m_type(value_t::string), m_value(new string_t(value)) - {} + : m_type(value_t::string)//, m_value(new string_t(value)) + { + Allocator alloc; + m_value.string = alloc.allocate(1); + alloc.construct(m_value.string, value); + } /// create a string (explicit) inline basic_json(const typename string_t::value_type* value) - : m_type(value_t::string), m_value(new string_t(value)) - {} + : m_type(value_t::string)//, m_value(new string_t(value)) + { + Allocator alloc; + m_value.string = alloc.allocate(1); + alloc.construct(m_value.string, value); + } /// create a string (implicit) template alloc; + m_value.string = alloc.allocate(1); + alloc.construct(m_value.string, *other.m_value.string); break; } case (value_t::boolean): @@ -483,8 +499,12 @@ class basic_json case (value_t::string): { - delete m_value.string; + Allocator alloc; + alloc.deallocate(m_value.string, 1); m_value.string = nullptr; + + // delete m_value.string; + // m_value.string = nullptr; break; } diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 148884a3..dda0cfbd 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -59,7 +59,8 @@ template < class StringType = std::string, class BooleanType = bool, class NumberIntegerType = int64_t, - class NumberFloatType = double + class NumberFloatType = double, + template class Allocator = std::allocator > class basic_json { @@ -199,7 +200,11 @@ class basic_json case (value_t::string): { - m_value.string = new string_t(""); + Allocator alloc; + m_value.string = alloc.allocate(1); + alloc.construct(m_value.string, ""); + + //m_value.string = new string_t(""); break; } @@ -270,13 +275,21 @@ class basic_json /// create a string (explicit) inline basic_json(const string_t& value) - : m_type(value_t::string), m_value(new string_t(value)) - {} + : m_type(value_t::string)//, m_value(new string_t(value)) + { + Allocator alloc; + m_value.string = alloc.allocate(1); + alloc.construct(m_value.string, value); + } /// create a string (explicit) inline basic_json(const typename string_t::value_type* value) - : m_type(value_t::string), m_value(new string_t(value)) - {} + : m_type(value_t::string)//, m_value(new string_t(value)) + { + Allocator alloc; + m_value.string = alloc.allocate(1); + alloc.construct(m_value.string, value); + } /// create a string (implicit) template alloc; + m_value.string = alloc.allocate(1); + alloc.construct(m_value.string, *other.m_value.string); break; } case (value_t::boolean): @@ -483,8 +499,12 @@ class basic_json case (value_t::string): { - delete m_value.string; + Allocator alloc; + alloc.deallocate(m_value.string, 1); m_value.string = nullptr; + + // delete m_value.string; + // m_value.string = nullptr; break; }