This is an archive of the discontinued LLVM Phabricator instance.

[libc] Add custom operator new to handle allocation failures gracefully.
ClosedPublic

Authored by sivachandra on Dec 7 2022, 2:41 PM.

Details

Summary

This patch adds the implementation of the custom operator new functions.
The implementation of the internal strdup has been updated to use
operator new for allocation.

We will make it a policy and document that all allocations have to go
through the libc's own operator new. A future change will also add
operator delete replacements and make it a policy that deallocations in
libc internal code have to go through those replacements.

Diff Detail

Event Timeline

sivachandra created this revision.Dec 7 2022, 2:41 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptDec 7 2022, 2:41 PM
sivachandra requested review of this revision.Dec 7 2022, 2:41 PM
lntue added inline comments.Dec 7 2022, 4:41 PM
libc/src/string/allocating_string_utils.h
27

Does it mean that our strdup will always return aligned strings?

tschuett added inline comments.Dec 7 2022, 5:02 PM
libc/src/string/allocating_string_utils.h
27

Would in this case a malloc returning a result or error type easier to understand and safer?

sivachandra added inline comments.Dec 8 2022, 12:14 AM
libc/src/string/allocating_string_utils.h
27

Does it mean that our strdup will always return aligned strings?

This is should call the non-aligning operator new. Why do you think it would always calls the aligning operator new?

27

Would in this case a malloc returning a result or error type easier to understand and safer?

Couple of points:

  1. We want the libc source code to look as much as possible like a normal modern C++ library.
  2. Using new/delete for allocation/deallocation has the added benefit that compilers will synthesize constructor/destructor calls appropriately. It is not relevant here I would think, but uniformity helps improve readability and keeps coding guidlines/principles simple.
tschuett added inline comments.Dec 8 2022, 11:31 PM
libc/src/string/allocating_string_utils.h
27

std::expected will be modern C++.

sivachandra added inline comments.Dec 8 2022, 11:43 PM
libc/src/string/allocating_string_utils.h
27

Sure, std::expected is a very good choice in a few places. What I am referring to here is using new/delete vs a custom allocator function which wraps malloc/free.

sivachandra added inline comments.Dec 8 2022, 11:58 PM
libc/src/string/allocating_string_utils.h
27

I have passed on your suggestion about std::expected here: https://reviews.llvm.org/D139576

lntue added inline comments.Dec 9 2022, 8:58 PM
libc/src/__support/CPP/new.h
55

What should happen if ac == nullptr? Does second parameter has to be a pointer according to the standard, or it can be a reference instead?

libc/src/string/allocating_string_utils.h
27

Does it mean that our strdup will always return aligned strings?

This is should call the non-aligning operator new. Why do you think it would always calls the aligning operator new?

Never mind, I was looking at wrong signatures.

Use reference argument.

sivachandra added inline comments.Dec 9 2022, 11:58 PM
libc/src/__support/CPP/new.h
55

Ah, good catch! I likely mixed up coding styles here. I have updated to use reference args throughout.

lntue accepted this revision.Dec 10 2022, 2:41 AM
This revision is now accepted and ready to land.Dec 10 2022, 2:41 AM