ProtoCore v0.0.2
Deterministic, zero-heap network stack for embedded targets
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 PC_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
22pc_place pc_psram_place(size_t size, bool dma_required, size_t free_dram, size_t free_psram, size_t psram_threshold,
23 size_t dram_reserve)
24{
25 if (size == 0)
26 {
27 return pc_place::PLACE_FAIL;
28 }
29
30 bool d_fits = dram_fits(size, free_dram, dram_reserve);
31 bool p_fits = size <= free_psram;
32
33 if (dma_required) // PSRAM is not DMA-capable: DRAM or bust.
34 {
35 return d_fits ? pc_place::PLACE_DRAM : pc_place::PLACE_FAIL;
36 }
37
38 if (size >= psram_threshold) // large / cold: prefer PSRAM.
39 {
40 if (p_fits)
41 {
42 return pc_place::PLACE_PSRAM;
43 }
44 if (d_fits)
45 {
46 return pc_place::PLACE_DRAM;
47 }
48 return pc_place::PLACE_FAIL;
49 }
50
51 // small / hot: prefer DRAM.
52 if (d_fits)
53 {
54 return pc_place::PLACE_DRAM;
55 }
56 if (p_fits)
57 {
58 return pc_place::PLACE_PSRAM;
59 }
60 return pc_place::PLACE_FAIL;
61}
62
63void pc_pingpong_init(PingPong *pp)
64{
65 if (pp)
66 {
67 pp->fill_idx = 0;
68 }
69}
70
71uint8_t pc_pingpong_fill_index(const PingPong *pp)
72{
73 return pp ? pp->fill_idx : 0;
74}
75
76uint8_t pc_pingpong_drain_index(const PingPong *pp)
77{
78 return pp ? (uint8_t)(pp->fill_idx ^ 1u) : 1;
79}
80
81uint8_t pc_pingpong_swap(PingPong *pp)
82{
83 if (!pp)
84 {
85 return 0;
86 }
87 pp->fill_idx ^= 1u;
88 return pp->fill_idx;
89}
90
91#endif // PC_ENABLE_PSRAM_POOL
Buffer placement policy (DRAM vs PSRAM) + SPI DMA ping-pong index manager (PC_ENABLE_PSRAM_POOL).