interplane.Get Omega
Read in your language

interplane-mv

INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority.

Brought by drakestapleton

branch main Branches Tags History

interplane-mv / docs / external / nightdriver / 565 / harness / stubs / esp_heap_caps.h

54 lines · 3160 bytes Raw Blame History
1// Host MODEL of ESP-IDF heap capability semantics. Written for this harness, contains no project code.
2// Sources (ESP-IDF v5.5.2, the version pinned by NightDriverStrip main via pioarduino 55.03.37):
3// caps bit values: components/heap/include/esp_heap_caps.h:29-41
4// region capabilities: components/heap/port/esp32/memory_layout.c:49-54 (DRAM, D/IRAM, IRAM rows)
5// free size / largest: components/heap/heap_caps.c:277-286 (sum over matching heaps), :367-389 (max over matching heaps)
6// eligibility rule: components/heap/heap_caps_base.c heap_caps_aligned_alloc_base: (get_all_caps(heap) & caps) == caps
7// NOT modelled: TLSF size classes and per-block overhead (a real allocation can fail where this model succeeds,
8// never the other way round for the cases below), block-owner bytes, EXEC special cases, PSRAM.
9#pragma once
10#include <cstddef>
11#include <cstdint>
12#include <vector>
13#include <algorithm>
14
15#define MALLOC_CAP_EXEC (1u<<0)
16#define MALLOC_CAP_32BIT (1u<<1)
17#define MALLOC_CAP_8BIT (1u<<2)
18#define MALLOC_CAP_DMA (1u<<3)
19#define MALLOC_CAP_INTERNAL (1u<<11)
20#define MALLOC_CAP_DEFAULT (1u<<12)
21
22namespace heapmodel {
23struct Region { const char* name; uint32_t caps[3]; std::vector<size_t> freeBlocks; };
24inline std::vector<Region>& heaps() { static std::vector<Region> h; return h; }
25inline uint32_t all_caps(const Region& r) { return r.caps[0] | r.caps[1] | r.caps[2]; }
26// memory_layout.c:49-54
27inline Region DRAM (std::vector<size_t> b) { return {"DRAM", {MALLOC_CAP_8BIT|MALLOC_CAP_DEFAULT, MALLOC_CAP_INTERNAL|MALLOC_CAP_DMA|MALLOC_CAP_32BIT, 0}, b}; }
28inline Region DIRAM(std::vector<size_t> b) { return {"D/IRAM", {0, MALLOC_CAP_DMA|MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL|MALLOC_CAP_DEFAULT, MALLOC_CAP_32BIT|MALLOC_CAP_EXEC}, b}; }
29inline Region IRAM (std::vector<size_t> b) { return {"IRAM", {MALLOC_CAP_INTERNAL|MALLOC_CAP_EXEC|MALLOC_CAP_32BIT, 0, 0}, b}; }
30inline void reset() { heaps().clear(); }
31inline size_t sum(const Region& r) { size_t s = 0; for (auto b : r.freeBlocks) s += b; return s; }
32inline size_t largest(const Region& r) { size_t m = 0; for (auto b : r.freeBlocks) m = std::max(m, b); return m; }
33// heap_caps_base.c eligibility, first fit over priorities then regions; returns true if a block was found
34inline bool try_alloc(size_t size, uint32_t caps) {
35 for (int prio = 0; prio < 3; prio++)
36 for (auto& r : heaps())
37 if ((r.caps[prio] & caps) != 0 && (all_caps(r) & caps) == caps)
38 for (auto& b : r.freeBlocks)
39 if (b >= size) { b -= size; return true; }
40 return false;
41}
42}
43
44inline size_t heap_caps_get_free_size(uint32_t caps) {
45 size_t t = 0;
46 for (auto& r : heapmodel::heaps()) if ((heapmodel::all_caps(r) & caps) == caps) t += heapmodel::sum(r);
47 return t;
48}
49inline size_t heap_caps_get_largest_free_block(uint32_t caps) {
50 size_t m = 0;
51 for (auto& r : heapmodel::heaps()) if ((heapmodel::all_caps(r) & caps) == caps) m = std::max(m, heapmodel::largest(r));
52 return m;
53}
54inline void* heap_caps_malloc(size_t size, uint32_t caps) { return heapmodel::try_alloc(size, caps) ? (void*)1 : nullptr; }