diff --git a/src/rtapi/uspace_rtapi_app.cc b/src/rtapi/uspace_rtapi_app.cc index c42c9f2a6e5..e79ff41b842 100644 --- a/src/rtapi/uspace_rtapi_app.cc +++ b/src/rtapi/uspace_rtapi_app.cc @@ -704,7 +704,22 @@ static void configure_memory() "mallopt(M_MMAP_MAX, -1) failed\n"); } #endif - char *buf = static_cast(malloc(PRE_ALLOC_SIZE)); + /* + * The following code seems pointless, but there is a non-observable effect + * in the allocation and loop. + * + * The malloc() is forced to set brk() because mmap() allocation is + * disabled in a call to mallopt() above. All touched pages become resident + * and locked in the loop because of above mlockall() call (see notes in + * mlockall(2)). The mallopt() trim setting prevents the brk() from being + * reduced after free(), effectively creating an open space for future + * allocations that will not generate page faults. + * + * The qualifier 'volatile' on the buffer pointer is required because newer + * clang would remove the malloc(), for()-loop and free() completely. + * Marking 'buf' volatile ensures that the code will remain in place. + */ + volatile char *buf = static_cast(malloc(PRE_ALLOC_SIZE)); if (buf == NULL) { rtapi_print_msg(RTAPI_MSG_WARN, "malloc(PRE_ALLOC_SIZE) failed\n"); return; @@ -717,7 +732,7 @@ static void configure_memory() * memory and never given back to the system. */ buf[i] = 0; } - free(buf); + free((void *)buf); } static int harden_rt()