mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Added BasicJsonDocument::shrinkToFit()
				
					
				
			This commit is contained in:
		| @@ -10,13 +10,15 @@ | ||||
| #include <ArduinoJson/Polyfills/mpl/max.hpp> | ||||
| #include <ArduinoJson/Variant/VariantSlot.hpp> | ||||
|  | ||||
| #include <string.h>  // memmove | ||||
|  | ||||
| namespace ARDUINOJSON_NAMESPACE { | ||||
|  | ||||
| // _begin                                _end | ||||
| // v                                        v | ||||
| // +-------------+--------------+-----------+ | ||||
| // | strings...  |   (free)     |  ...slots | | ||||
| // +-------------+--------------+-----------+ | ||||
| // _begin                                   _end | ||||
| // v                                           v | ||||
| // +-------------+--------------+--------------+ | ||||
| // | strings...  |   (free)     |  ...variants | | ||||
| // +-------------+--------------+--------------+ | ||||
| //               ^              ^ | ||||
| //             _left          _right | ||||
|  | ||||
| @@ -101,6 +103,39 @@ class MemoryPool { | ||||
|     return p; | ||||
|   } | ||||
|  | ||||
|   // Squash the free space between strings and variants | ||||
|   // | ||||
|   // _begin                    _end | ||||
|   // v                            v | ||||
|   // +-------------+--------------+ | ||||
|   // | strings...  |  ...variants | | ||||
|   // +-------------+--------------+ | ||||
|   //               ^ | ||||
|   //          _left _right | ||||
|   // | ||||
|   // This funcion is called before a realloc. | ||||
|   ptrdiff_t squash() { | ||||
|     char* new_right = addPadding(_left); | ||||
|     if (new_right >= _right) return 0; | ||||
|  | ||||
|     size_t right_size = static_cast<size_t>(_end - _right); | ||||
|     memmove(new_right, _right, right_size); | ||||
|  | ||||
|     ptrdiff_t bytes_reclaimed = _right - new_right; | ||||
|     _right = new_right; | ||||
|     _end = new_right + right_size; | ||||
|     return bytes_reclaimed; | ||||
|   } | ||||
|  | ||||
|   // Move all pointers together | ||||
|   // This funcion is called after a realloc. | ||||
|   void movePointers(ptrdiff_t offset) { | ||||
|     _begin += offset; | ||||
|     _left += offset; | ||||
|     _right += offset; | ||||
|     _end += offset; | ||||
|   } | ||||
|  | ||||
|  private: | ||||
|   StringSlot* allocStringSlot() { | ||||
|     return allocRight<StringSlot>(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user