Skip to content

Commit fc8897a

Browse files
committed
Uses the appropriate allocator instead of malloc/free
Remove malloc/free and instead use unique_ptr with vector and char to represent the old character array.
1 parent 1ae3e95 commit fc8897a

File tree

2 files changed

+157
-69
lines changed

2 files changed

+157
-69
lines changed

include/json/value.h

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ class JSON_API Value {
181181
typedef Json::LargestInt LargestInt;
182182
typedef Json::LargestUInt LargestUInt;
183183
typedef Json::ArrayIndex ArrayIndex;
184+
typedef std::vector<char, _Alloc> StringData;
185+
#if __cplusplus >= 201103L || (defined(_CPPLIB_VER) && _CPPLIB_VER >= 520)
186+
typedef std::unique_ptr<StringData> StringDataPtr;
187+
#else
188+
typedef std::auto_ptr<StringData> StringDataPtr;
189+
#endif
184190

185191
static const Value& null; ///< We regret this reference to a global instance; prefer the simpler Value().
186192
static const Value& nullRef; ///< just a kludge for binary-compatibility; same as null
@@ -209,6 +215,23 @@ class JSON_API Value {
209215

210216
private:
211217
#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
218+
class StringValueHolder {
219+
public:
220+
StringValueHolder();
221+
StringValueHolder(StringDataPtr&& value);
222+
StringValueHolder(char* value);
223+
~StringValueHolder();
224+
char* GetString();
225+
const char* GetString() const;
226+
void SetString(StringDataPtr&& value);
227+
void SetString(char* value);
228+
bool IsRaw() const;
229+
230+
private:
231+
char* valueStringRaw_ = nullptr; // the value that was passed in, this does not belong to value
232+
StringDataPtr valueStringCopy_; // a copy of the value that was passed in
233+
bool raw_ = true;
234+
};
212235
class CZString {
213236
public:
214237
enum DuplicationPolicy {
@@ -240,7 +263,7 @@ class JSON_API Value {
240263
unsigned length_: 30; // 1GB max
241264
};
242265

243-
char const* cstr_; // actually, a prefixed string, unless policy is noDup
266+
StringValueHolder cstr_; // The string that's being stored
244267
union {
245268
ArrayIndex index_;
246269
StringStorage storage_;
@@ -578,7 +601,7 @@ Json::Value obj_value(Json::objectValue); // {}
578601

579602
void setComment(const char* text, size_t len);
580603

581-
char* comment_;
604+
StringValueHolder comment_;
582605
};
583606

584607
// struct MemberNamesTransform
@@ -595,9 +618,9 @@ Json::Value obj_value(Json::objectValue); // {}
595618
LargestUInt uint_;
596619
double real_;
597620
bool bool_;
598-
char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
599621
ObjectValues* map_;
600622
} value_;
623+
StringValueHolder stringValue_;
601624
ValueType type_ : 8;
602625
unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
603626
// If not allocated_, string_ must be null-terminated.

0 commit comments

Comments
 (0)