DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
wearlevel.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file wearlevel.cpp
6 * @brief Flash wear-leveling slot selector core (see wearlevel.h).
7 */
8
10
11#if DETWS_ENABLE_WEARLEVEL
12
13size_t detws_wearlevel_pick(const uint32_t *counts, size_t n)
14{
15 if (!counts || n == 0)
16 return 0;
17 size_t best = 0;
18 uint32_t lowest = counts[0];
19 for (size_t i = 1; i < n; i++)
20 {
21 if (counts[i] < lowest) // strict <, so ties keep the lowest index
22 {
23 lowest = counts[i];
24 best = i;
25 }
26 }
27 return best;
28}
29
30void detws_wearlevel_mark(uint32_t *counts, size_t n, size_t idx)
31{
32 if (!counts || idx >= n)
33 return;
34 if (counts[idx] != 0xFFFFFFFFu) // saturate: never wrap a wear count back to 0
35 counts[idx]++;
36}
37
38uint32_t detws_wearlevel_spread(const uint32_t *counts, size_t n)
39{
40 if (!counts || n == 0)
41 return 0;
42 uint32_t lo = counts[0];
43 uint32_t hi = counts[0];
44 for (size_t i = 1; i < n; i++)
45 {
46 if (counts[i] < lo)
47 lo = counts[i];
48 if (counts[i] > hi)
49 hi = counts[i];
50 }
51 return hi - lo;
52}
53
54#endif // DETWS_ENABLE_WEARLEVEL
Flash wear-leveling slot selector (DETWS_ENABLE_WEARLEVEL).