Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/sys/posix/posix_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,16 @@ Posix_EarlyInit
===============
*/
void Posix_EarlyInit( void ) {
#if defined( __GLIBC__ )
// glibc gives every thread that allocates a malloc arena of its own. The
// job workers allocate during level loads and the main thread frees, so
// each arena keeps its pages and RSS climbs from map to map even after
// Sys_ReleaseFreeHeapMemory. Two arenas keep that in check; set
// MALLOC_ARENA_MAX to choose another number.
if ( getenv( "MALLOC_ARENA_MAX" ) == NULL ) {
mallopt( M_ARENA_MAX, 2 );
}
#endif
posix_mainThread = pthread_self();
posix_mainThreadRecorded = true;
memset( &asyncThread, 0, sizeof( asyncThread ) );
Expand Down
13 changes: 13 additions & 0 deletions tools/tests/posix_memory_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,23 @@ def validate_level_load_heap_release() -> None:
raise AssertionError("level load must release free heap memory after the settle frames")


def validate_malloc_arena_cap() -> None:
posix = read("src/sys/posix/posix_main.cpp")
body = function_body(posix, "void Posix_EarlyInit( void ) {")
require(body, "#if defined( __GLIBC__ )", "glibc malloc arena cap")
require(body, 'if ( getenv( "MALLOC_ARENA_MAX" ) == NULL ) {', "user MALLOC_ARENA_MAX wins")
require(body, "mallopt( M_ARENA_MAX, 2 );", "glibc malloc arena cap")
cap = body.find("mallopt( M_ARENA_MAX, 2 );")
thread = body.find("posix_mainThread = pthread_self();")
if cap == -1 or thread == -1 or cap > thread:
raise AssertionError("the malloc arena cap must be set before Posix_EarlyInit does anything else")


def main() -> None:
validate_posix_page_locking()
validate_ram_total_helpers()
validate_level_load_heap_release()
validate_malloc_arena_cap()
print("posix_memory_management: ok")


Expand Down
Loading