JSONChildren.cpp 3.5 KB

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