Skip to content

Commit 41574cf

Browse files
authored
Merge pull request #3261 from BsAtHome/fix_clang-code-optimize-removal
Don't let clang's optimizer remove important initialization code.
2 parents 10fc808 + f7c5cb1 commit 41574cf

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/rtapi/uspace_rtapi_app.cc

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,22 @@ static void configure_memory()
704704
"mallopt(M_MMAP_MAX, -1) failed\n");
705705
}
706706
#endif
707-
char *buf = static_cast<char *>(malloc(PRE_ALLOC_SIZE));
707+
/*
708+
* The following code seems pointless, but there is a non-observable effect
709+
* in the allocation and loop.
710+
*
711+
* The malloc() is forced to set brk() because mmap() allocation is
712+
* disabled in a call to mallopt() above. All touched pages become resident
713+
* and locked in the loop because of above mlockall() call (see notes in
714+
* mlockall(2)). The mallopt() trim setting prevents the brk() from being
715+
* reduced after free(), effectively creating an open space for future
716+
* allocations that will not generate page faults.
717+
*
718+
* The qualifier 'volatile' on the buffer pointer is required because newer
719+
* clang would remove the malloc(), for()-loop and free() completely.
720+
* Marking 'buf' volatile ensures that the code will remain in place.
721+
*/
722+
volatile char *buf = static_cast<volatile char *>(malloc(PRE_ALLOC_SIZE));
708723
if (buf == NULL) {
709724
rtapi_print_msg(RTAPI_MSG_WARN, "malloc(PRE_ALLOC_SIZE) failed\n");
710725
return;
@@ -717,7 +732,7 @@ static void configure_memory()
717732
* memory and never given back to the system. */
718733
buf[i] = 0;
719734
}
720-
free(buf);
735+
free((void *)buf);
721736
}
722737

723738
static int harden_rt()

0 commit comments

Comments
 (0)