The R Project SVN R-packages

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7747 ripley 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
 */
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
 
21
/*
22
 clang objects to testing pointers to be non-null:
23
 warning: 'this' pointer cannot be null in well-defined C++ code ...
24
*/
25
void jsonChildren::inc(void) json_nothrow {
26
//    JSON_ASSERT(this != 0, JSON_TEXT("Children is null inc"));
27
    if (json_unlikely(mysize == mycapacity)){  //it's full
28
	   if (json_unlikely(mycapacity == 0)){  //the array hasn't been created yet
29
//		  JSON_ASSERT(!array, JSON_TEXT("Expanding a 0 capacity array, but not null"));
30
		  #ifdef JSON_LESS_MEMORY
31
			 array = json_malloc<JSONNode*>(1);
32
			 mycapacity = 1;
33
		  #else
34
			 array = json_malloc<JSONNode*>(8);  //8 seems average for JSON, and it's only 64 bytes
35
			 mycapacity = 8;
36
		  #endif
37
	   } else {
38
		  #ifdef JSON_LESS_MEMORY
39
			 mycapacity += 1;  //increment the size of the array
40
		  #else
41
			 mycapacity <<= 1;  //double the size of the array
42
		  #endif
43
		  array = json_realloc<JSONNode*>(array, mycapacity);
44
	   }
45
    }
46
}
47
 
48
 
49
void jsonChildren::inc(json_index_t amount) json_nothrow {
50
//    JSON_ASSERT(this != 0, JSON_TEXT("Children is null inc(amount)"));
51
    if (json_unlikely(amount == 0)) return;
52
    if (json_likely(mysize + amount >= mycapacity)){  //it's full
53
	   if (json_unlikely(mycapacity == 0)){  //the array hasn't been created yet
54
//		  JSON_ASSERT(!array, JSON_TEXT("Expanding a 0 capacity array, but not null"));
55
		  #ifdef JSON_LESS_MEMORY
56
			 array = json_malloc<JSONNode*>(amount);
57
			 mycapacity = amount;
58
		  #else
59
			 array = json_malloc<JSONNode*>(amount > 8 ? amount : 8);  //8 seems average for JSON, and it's only 64 bytes
60
			 mycapacity = amount > 8 ? amount : 8;
61
		  #endif
62
	   } else {
63
		  #ifdef JSON_LESS_MEMORY
64
			 mycapacity = mysize + amount;  //increment the size of the array
65
		  #else
66
			 while(mysize + amount > mycapacity){
67
				mycapacity <<= 1;  //double the size of the array
68
			 }
69
		  #endif
70
		  array = json_realloc<JSONNode*>(array, mycapacity);
71
	   }
72
    }
73
}
74
 
75
//actually deletes everything within the vector, this is safe to do on an empty or even a null array
76
void jsonChildren::deleteAll(void) json_nothrow {
77
//    JSON_ASSERT(this != 0, JSON_TEXT("Children is null deleteAll"));
78
    json_foreach(this, runner){
79
//        JSON_ASSERT(*runner != 0, JSON_TEXT("a null pointer within the children"));
80
	   JSONNode::deleteJSONNode(*runner);  //this is why I can't do forward declaration
81
    }
82
}
83
 
84
void jsonChildren::doerase(JSONNode ** position, json_index_t number) json_nothrow {
85
//    JSON_ASSERT(this != 0, JSON_TEXT("Children is null doerase"));
86
//    JSON_ASSERT(array != 0, JSON_TEXT("erasing something from a null array 2"));
87
//    JSON_ASSERT(position >= array, JSON_TEXT("position is beneath the start of the array 2"));
88
//    JSON_ASSERT(position + number <= array + mysize, JSON_TEXT("erasing out of bounds 2"));
89
    if (position + number >= array + mysize){
90
	   mysize = (json_index_t)(position - array);
91
	   #ifndef JSON_ISO_STRICT
92
//		  JSON_ASSERT((long long)position - (long long)array >= 0, JSON_TEXT("doing negative allocation"));
93
	   #endif
94
    } else {
95
	   std::memmove(position, position + number, (mysize - (position - array) - number) * sizeof(JSONNode *));
96
	   mysize -= number;
97
    }
98
}