DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
psram_pool.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 psram_pool.cpp
6 * @brief Buffer placement policy + SPI DMA ping-pong index manager (see psram_pool.h).
7 */
8
10
11#if DETWS_ENABLE_PSRAM_POOL
12
13namespace
14{
15// Does `size` fit in DRAM while still leaving `reserve` free? Overflow-safe (64-bit sum).
16bool dram_fits(size_t size, size_t free_dram, size_t reserve)
17{
18 return (uint64_t)size + reserve <= (uint64_t)free_dram;
19}
20} // namespace
21
22DetwsPlace detws_psram_place(size_t size, bool dma_required, size_t free_dram, size_t free_psram,
23 size_t psram_threshold, size_t dram_reserve)
24{
25 if (size == 0)
26 return DetwsPlace::PLACE_FAIL;
27
28 bool d_fits = dram_fits(size, free_dram, dram_reserve);
29 bool p_fits = size <= free_psram;
30
31 if (dma_required) // PSRAM is not DMA-capable: DRAM or bust.
32 return d_fits ? DetwsPlace::PLACE_DRAM : DetwsPlace::PLACE_FAIL;
33
34 if (size >= psram_threshold) // large / cold: prefer PSRAM.
35 {
36 if (p_fits)
37 return DetwsPlace::PLACE_PSRAM;
38 if (d_fits)
39 return DetwsPlace::PLACE_DRAM;
40 return DetwsPlace::PLACE_FAIL;
41 }
42
43 // small / hot: prefer DRAM.
44 if (d_fits)
45 return DetwsPlace::PLACE_DRAM;
46 if (p_fits)
47 return DetwsPlace::PLACE_PSRAM;
48 return DetwsPlace::PLACE_FAIL;
49}
50
51void detws_pingpong_init(PingPong *pp)
52{
53 if (pp)
54 pp->fill_idx = 0;
55}
56
57uint8_t detws_pingpong_fill_index(const PingPong *pp)
58{
59 return pp ? pp->fill_idx : 0;
60}
61
62uint8_t detws_pingpong_drain_index(const PingPong *pp)
63{
64 return pp ? (uint8_t)(pp->fill_idx ^ 1u) : 1;
65}
66
67uint8_t detws_pingpong_swap(PingPong *pp)
68{
69 if (!pp)
70 return 0;
71 pp->fill_idx ^= 1u;
72 return pp->fill_idx;
73}
74
75#endif // DETWS_ENABLE_PSRAM_POOL
Buffer placement policy (DRAM vs PSRAM) + SPI DMA ping-pong index manager (DETWS_ENABLE_PSRAM_POOL).