C/C++ API Reference
Loading...
Searching...
No Matches
fragmentation.h
1// Copyright 2024 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <cstddef>
17
18namespace pw::allocator {
19
21
22// inclusive-language: disable
23
48 // inclusive-language: enable
49
51 struct {
52 size_t hi = 0;
53 size_t lo = 0;
55
57 size_t sum = 0;
58
60 void AddFragment(size_t inner_size);
61
63 void SubtractFragment(size_t inner_size);
64
67
70
72 constexpr bool operator==(const Fragmentation& other) const {
73 return sum == other.sum && sum_of_squares.hi == other.sum_of_squares.hi &&
74 sum_of_squares.lo == other.sum_of_squares.lo;
75 }
76
78 constexpr bool operator!=(const Fragmentation& other) const {
79 return !(*this == other);
80 }
81
84 const Fragmentation& rhs) {
85 Fragmentation result = lhs;
86 result += rhs;
87 return result;
88 }
89
92 const Fragmentation& rhs) {
93 Fragmentation result = lhs;
94 result -= rhs;
95 return result;
96 }
97};
98
103float CalculateFragmentation(const Fragmentation& fragmentation);
104
106
107} // namespace pw::allocator
float CalculateFragmentation(const Fragmentation &fragmentation)
Definition: fragmentation.h:47
constexpr bool operator!=(const Fragmentation &other) const
Returns whether this fragmentation struct is different from another.
Definition: fragmentation.h:78
size_t sum
Sum of the inner sizes of free blocks.
Definition: fragmentation.h:57
Fragmentation & operator-=(const Fragmentation &other)
Subtracts another fragmentation struct from this one.
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.
friend Fragmentation operator+(const Fragmentation &lhs, const Fragmentation &rhs)
Combines two fragmentation structs.
Definition: fragmentation.h:83
void AddFragment(size_t inner_size)
Includes a region of free memory in the fragmentation calculation.
friend Fragmentation operator-(const Fragmentation &lhs, const Fragmentation &rhs)
Subtracts one fragmentation struct from another.
Definition: fragmentation.h:91
constexpr bool operator==(const Fragmentation &other) const
Returns whether this fragmentation struct is the same as another.
Definition: fragmentation.h:72
struct pw::allocator::Fragmentation::@1 sum_of_squares
Sum-of-squares of the inner sizes of free blocks.