Commit Graph
5 Commits
Author SHA1 Message Date
LibretroAdmin d02f74f74a libretro-common: add mempool_t, a fixed-size block allocator
Serves one block size out of chunks taken from malloc() in bulk, with
released blocks going on an intrusive free list. A caller that builds
and tears down N same-sized objects pays a logarithmic number of calls
into libc instead of N, and a rebuild of the same size touches libc not
at all.

Blocks keep their address while held, so a container that relocates the
pointer -- qsort() in file_list_sort_on_alt(), memmove() in
file_list_insert() -- moves the pointer and never the block.

Covered by test/memory/test_mempool.c: block distinctness under a
whole-block write, alignment for an unaligned block size, free-list
reuse, a rebuild taking no new chunk, deinit with blocks outstanding,
and interleaved churn across chunk boundaries. ASan+UBSan+LSan and TSan
both clean, 10/10.
2026-08-29 23:38:45 +02:00
libretroadmin 1d03cec9da (mem_stats) Remove the frontend memory vtable entirely
The two function pointers are gone from frontend_ctx_driver_t, along
with every implementation behind them and the entries assigning them in
seventeen drivers.  Nothing in frontend/ mentions system memory now.

Finishing it turned up four platforms the earlier passes had missed, all
for the same reason - they were searched for by the names the struct
uses, and these did not use them:

  - PS3 and Orbis spell theirs get_mem_total and get_mem_used, and
    return size_t, so every grep for get_total_mem or uint64_t went
    straight past.  Both were assigning into the two slots being
    removed;
  - the null driver in frontend_driver.c labels its entries
    get_mem_total and get_mem_free, a third spelling again.

Orbis was also wrong, and had been: get_mem_used returns the memory
taken, and it was wired into the free slot, so that platform answered
every "how much is spare" with its opposite.  In mem_stats it is the
ceiling less the taken.  PS3's get_mem_used, despite the name, read
mem_info.avail and was right; it is carried across as free unchanged,
with the syscall shape it needs, which neither SDK declares and the
frontend spelled out for itself.

Emscripten no longer needs the provider hook that was added for it, so
that is gone too.  The runtime already knows what the frontend was
tracking: the heap ceiling is emscripten_get_heap_max, and what is spare
is that less the allocator's own tally.  No RetroArch state, so nothing
to register.

That is thirteen platforms answering from one file, and the shifted-
field hazard is closed: the entries and the fields are removed together
rather than one before the other, which for a while in the making of
this left every field after them in every driver silently moved up two.
2026-07-25 08:10:57 +02:00
libretroadmin 92826f5777 (mem_stats) Retire the frontend memory wrappers and call it directly
Once the platform answers moved to libretro-common, asking the frontend
for them was a layer that added nothing: frontend_driver_get_free_memory
looked up a driver to find a function pointer that was, for eight
platforms out of nine, going to be absent so that the call could fall
through to the place the answer now lives.  Worse, it was a layer the
code most in need of the answer cannot use - libretro-common cannot call
into the frontend, which is why the answers had to move in the first
place.

The ninth platform is what kept the layer alive: emscripten answers from
a budget it keeps itself, which no general query can see.  So move that
mechanism too.  mem_stats takes a provider the platform can register,
emscripten registers its pair where it allocates the state they read,
and every caller - the memory overlay, the thumbnail admission tests,
the overlay guard, the content scanner and the menu - asks mem_stats and
gets one answer whichever side of the fence it is on.  Before this, a
libretro-common caller and a RetroArch caller asking the same question
on the web build would have got different numbers, which is a worse
failure than the indirection.

frontend_driver_get_free_memory and its total counterpart are gone, with
their declarations.  The database sample stubbed the former to satisfy
its link; it builds the real translation unit now instead, which is four
lines of Makefile and no stub.

What is deliberately still there: the two vtable slots and the nine
per-platform implementations, now unread.  Deleting those means editing
a positional, comment-labelled initializer in seventeen platform
drivers, where dropping the wrong two entries shifts every field after
them and still compiles - and eight of those platforms have no toolchain
here.  That is the change to make with CI watching.
2026-07-25 07:51:11 +02:00
libretroadmin 152a0ac592 (mem_stats) One place to ask the machine how much memory it has
"How much memory is there, and how much is spare" is a question about
the platform, not about the frontend, but the answers lived in the
frontend drivers - nine copies, one per platform, reachable only from
inside RetroArch and not at all from libretro-common, where several of
the callers that want them actually are.  Gather them into
libretro-common/memory/mem_stats.c, beside mem2_manager.c, whose GX
helpers one of those answers already depends on.

frontend_driver_get_free_memory and its total counterpart now fall
through to it when the frontend has no override.  That leaves the
platforms whose vtable slots were already NULL - DOS, PS3, QNX, Wii U,
Xbox, Xenon - answering for the first time instead of returning 0, and
it leaves the ones with overrides exactly as they were.

One override is not going away: the emscripten port answers from a
budget it maintains itself, which no general query can see.

The unix answer is ported faithfully rather than tidied: free memory
still prefers the kernel's MemAvailable and falls back to the older
MemFree + Buffers + Cached - Shmem sum, and total still comes from
sysconf everywhere except Dingux, which reads it out of the file
because sysconf there cannot be trusted.  Both were checked against an
independently written reference reading /proc/meminfo on the same
machine and agree to the byte.

This is the additive half.  The nine per-platform copies are still
there and still take precedence; removing them is a separate change,
and one that wants CI, because eight of those platforms cannot be
compiled here - the first attempt at removing even the unix pair cut a
function in half at an inner brace.
2026-07-25 07:44:51 +02:00
libretroadmin ce5544fdb0 Move the GX MEM2 allocator into libretro-common
frontend_gx_get_free_mem() is the only platform memory query that
cannot be answered by the platform alone: MEM1 comes from
SYS_GetArena1Size(), but the MEM2 half is only knowable from a heap
RetroArch itself creates over Arena2. That heap lived in
memory/wii/mem2_manager.c, above libretro-common, so anything lower
that wanted a memory figure could not have one.

Since every GameCube and Wii build links libretro-common - they are
statically linked, there is no configuration where it is absent - the
allocator belongs below rather than above. Moved as a rename, so
history follows it.

It is more than a getter: an --wrap interposer over malloc, calloc,
realloc, free, memalign, strdup, strndup and malloc_usable_size, so an
allocation MEM1 cannot satisfy falls through to MEM2 instead of
failing, plus an override of libogc's weak MALLOC_MEM2. The wrap flags
stay in Makefile.wii and Makefile.griffin, since they are link-time
rather than source. Nothing about that behaviour changes.

The header needed fixing on the way: it declared bool without
including boolean.h and only ever compiled because callers happened to
include something else first. It is now self-contained and carries the
usual libretro-common preamble.

Both callers move with it - platform_gx.c and gx_gfx.c, the second of
which I would have missed had I only followed the include in the
first.

No devkitPPC here, so this is verified by compiling the real source
against stub libogc headers rather than by building for the console.
At -m32, which is PowerPC's word size, it is clean under -Wall
-Werror; at 64-bit the file's uint32_t pointer casts warn, which is an
artefact of the host and not of the move. The symbol surface is
byte-identical before and after: all eight __wrap_* entries,
MALLOC_MEM2, and the three public functions, which is what the linker
flags depend on. Makefile.wii names the new object path (literally,
since LIBRETRO_COMM_DIR is not defined yet at that line) and
griffin.c the new source path; no build file provides it twice, and no
reference to memory/wii remains anywhere in the tree.
2026-07-24 12:46:27 +02:00