Information that can be used to represent the fragmentation of the block allocator's memory region.
Fragmentation can be measured as 1 minus the normalized root-sum-square of the free block inner sizes, i.e.
1 - (sqrt(sum(U[i]^2)) / sum(U[i])
where U[i] is the inner size of the i-th free block.
This metric has been described by Adam Sawicki (https://asawicki.info/news_1757_a_metric_for_memory_fragmentation), and used in esp8266/Arduino (https://github.com/esp8266/Arduino/blob/master/cores/esp8266/Esp-frag.cpp), among others.
This struct provides the sum-of-squares and the sum of the inner sizes of free blocks. This approach allows accumulating Fragmentation by summing each part. It is left to the caller to perform the square root and division steps, perhaps on a host, AP, or other device with better floating point support.
The sum-of-squares is stored as a pair of sizes, since it can overflow.
Public Member Functions | |
| void | AddFragment (size_t inner_size) |
| Includes a region of free memory in the fragmentation calculation. | |
| void | SubtractFragment (size_t inner_size) |
| Excludes a region of free memory from the fragmentation calculation. | |
| Fragmentation & | operator+= (const Fragmentation &other) |
| Combines this fragmentation struct with another. | |
| Fragmentation & | operator-= (const Fragmentation &other) |
| Subtracts another fragmentation struct from this one. | |
| constexpr bool | operator== (const Fragmentation &other) const |
| Returns whether this fragmentation struct is the same as another. | |
| constexpr bool | operator!= (const Fragmentation &other) const |
| Returns whether this fragmentation struct is different from another. | |
Public Attributes | |
| struct { | |
| size_t hi = 0 | |
| size_t lo = 0 | |
| } | sum_of_squares |
| Sum-of-squares of the inner sizes of free blocks. | |
| size_t | sum = 0 |
| Sum of the inner sizes of free blocks. | |
Friends | |
| Fragmentation | operator+ (const Fragmentation &lhs, const Fragmentation &rhs) |
| Combines two fragmentation structs. | |
| Fragmentation | operator- (const Fragmentation &lhs, const Fragmentation &rhs) |
| Subtracts one fragmentation struct from another. | |