1 #include "JSONChildren.h"
2 #include "JSONNode.h"
3
4 /*
5 * reserves a certain number of bytes, in memory saving mode it creates a special
6 * type of child container that will not autoshrink
7 */
reserve2(jsonChildren * & mine,json_index_t amount)8 void jsonChildren::reserve2(jsonChildren *& mine, json_index_t amount) json_nothrow {
9 if (mine -> array != 0){
10 if (mine -> mycapacity < amount){
11 mine -> inc(amount - mine -> mycapacity);
12 #ifdef JSON_LESS_MEMORY
13 mine = jsonChildren_Reserved::newChildren_Reserved(mine, amount);
14 #endif
15 }
16 } else {
17 mine -> reserve(amount);
18 }
19 }
20
inc(void)21 void jsonChildren::inc(void) json_nothrow {
22 JSON_ASSERT(this != 0, JSON_TEXT("Children is null inc"));
23 if (json_unlikely(mysize == mycapacity)){ //it's full
24 if (json_unlikely(mycapacity == 0)){ //the array hasn't been created yet
25 JSON_ASSERT(!array, JSON_TEXT("Expanding a 0 capacity array, but not null"));
26 #ifdef JSON_LESS_MEMORY
27 array = json_malloc<JSONNode*>(1);
28 mycapacity = 1;
29 #else
30 array = json_malloc<JSONNode*>(8); //8 seems average for JSON, and it's only 64 bytes
31 mycapacity = 8;
32 #endif
33 } else {
34 #ifdef JSON_LESS_MEMORY
35 mycapacity += 1; //increment the size of the array
36 #else
37 mycapacity <<= 1; //double the size of the array
38 #endif
39 array = json_realloc<JSONNode*>(array, mycapacity);
40 }
41 }
42 }
43
44
inc(json_index_t amount)45 void jsonChildren::inc(json_index_t amount) json_nothrow {
46 JSON_ASSERT(this != 0, JSON_TEXT("Children is null inc(amount)"));
47 if (json_unlikely(amount == 0)) return;
48 if (json_likely(mysize + amount >= mycapacity)){ //it's full
49 if (json_unlikely(mycapacity == 0)){ //the array hasn't been created yet
50 JSON_ASSERT(!array, JSON_TEXT("Expanding a 0 capacity array, but not null"));
51 #ifdef JSON_LESS_MEMORY
52 array = json_malloc<JSONNode*>(amount);
53 mycapacity = amount;
54 #else
55 array = json_malloc<JSONNode*>(amount > 8 ? amount : 8); //8 seems average for JSON, and it's only 64 bytes
56 mycapacity = amount > 8 ? amount : 8;
57 #endif
58 } else {
59 #ifdef JSON_LESS_MEMORY
60 mycapacity = mysize + amount; //increment the size of the array
61 #else
62 while(mysize + amount > mycapacity){
63 mycapacity <<= 1; //double the size of the array
64 }
65 #endif
66 array = json_realloc<JSONNode*>(array, mycapacity);
67 }
68 }
69 }
70
71 //actually deletes everything within the vector, this is safe to do on an empty or even a null array
deleteAll(void)72 void jsonChildren::deleteAll(void) json_nothrow {
73 JSON_ASSERT(this != 0, JSON_TEXT("Children is null deleteAll"));
74 json_foreach(this, runner){
75 JSON_ASSERT(*runner != JSON_TEXT('\0'), JSON_TEXT("a null pointer within the children"));
76 JSONNode::deleteJSONNode(*runner); //this is why I can't do forward declaration
77 }
78 }
79
doerase(JSONNode ** position,json_index_t number)80 void jsonChildren::doerase(JSONNode ** position, json_index_t number) json_nothrow {
81 JSON_ASSERT(this != 0, JSON_TEXT("Children is null doerase"));
82 JSON_ASSERT(array != 0, JSON_TEXT("erasing something from a null array 2"));
83 JSON_ASSERT(position >= array, JSON_TEXT("position is beneath the start of the array 2"));
84 JSON_ASSERT(position + number <= array + mysize, JSON_TEXT("erasing out of bounds 2"));
85 if (position + number >= array + mysize){
86 mysize = (json_index_t)(position - array);
87 #ifndef JSON_ISO_STRICT
88 JSON_ASSERT((long long)position - (long long)array >= 0, JSON_TEXT("doing negative allocation"));
89 #endif
90 } else {
91 std::memmove(position, position + number, (mysize - (position - array) - number) * sizeof(JSONNode *));
92 mysize -= number;
93 }
94 }
95