[libcxx-commits] [PATCH] D112168: [libc++]Use __libcpp_malloc/__libcpp_calloc in places of malloc/calloc

Xing Xue via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Oct 20 12:10:58 PDT 2021


xingxue created this revision.
xingxue added reviewers: ldionne, mstorsjo, daltenty, hubert.reinterpretcast, cebowleratibm.
xingxue added a project: LLVM.
xingxue requested review of this revision.
Herald added projects: libc++, libc++abi.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.
Herald added a reviewer: libc++abi.

`AIX` system header `<stdlib.h>` maps system calls `malloc` and `calloc` to `vec_malloc` and `vec_calloc` under macro `__VEC__` which is pre-defined by compiler when the target architecture supports Altivec. `vec_malloc`/`vec_calloc` give 16-byte aligned allocation and as a result, greatly increase the heap memory consumption if an application makes a large number of small heap allocations. This causes existing applications built with certain heap size to run out of memory unexpectedly when running against a newer version of `libc++`/`libc++abi` built with `__VEC__` defined (on `AIX`, linker option `-bmaxdata` is used to specify the maximum size of user data area). For example, an application usually `malloc()`s 1.5GB heap memory through the `new` operator out of 40M allocations but it allocates more than 2GB when running against the newer `libc++abi`.

To mitigate this issue, this patch defines `__libcpp_malloc()`/`__libcpp_calloc()` that are true `malloc()`/`calloc()` and use them in `libc++` and `libc++abi` implementations in places of the plain `malloc()`/`calloc(}` where a specific alignment is not required. For `AIX`, `__libcpp_malloc()`/`__libcpp_calloc()` map to assembly label `malloc`/`calloc`. For other platforms, they are function macros mapping to `malloc`/`calloc`. Functions `std::malloc()`/`std::calloc()` are not changed to maintain the original `AIX` semantic, i.e., they are mapped to `vec_malloc()`/`vec_calloc()` if macro `__VEC__` is defined.

Calls to `malloc()`/`calloc()` in `libunwind` are used for `DWARF` based unwinding.  Since `AIX` uses the traceback table based unwinding and the code for `DWARF` based unwinding is compiled out, the code in `libunwind` is not changed.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D112168

Files:
  libcxx/include/__support/ibm/xlocale.h
  libcxx/include/locale
  libcxx/include/new
  libcxx/include/ostream
  libcxx/src/debug.cpp
  libcxx/src/filesystem/posix_compat.h
  libcxx/src/ios.cpp
  libcxx/src/new.cpp
  libcxx/src/support/win32/support.cpp
  libcxxabi/src/cxa_demangle.cpp
  libcxxabi/src/cxa_thread_atexit.cpp
  libcxxabi/src/fallback_malloc.cpp
  libcxxabi/src/stdlib_new_delete.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112168.380939.patch
Type: text/x-patch
Size: 12739 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211020/fcb47d16/attachment-0001.bin>


More information about the libcxx-commits mailing list