Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Placement New #17057

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

WalterBright
Copy link
Member

This implements Placement New, described in https://github.com/WalterBright/documents/blob/master/placementnew.md

@WalterBright WalterBright added Enhancement WIP Work In Progress - not ready for review or pulling labels Nov 10, 2024
@dlang-bot
Copy link
Contributor

Thanks for your pull request, @WalterBright!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#17057"

@WalterBright WalterBright force-pushed the placementNew branch 3 times, most recently from 29a5064 to 4db86a4 Compare November 10, 2024 07:46
@thewilsonator thewilsonator added Needs Changelog A changelog entry needs to be added to /changelog Needs Spec PR A PR updating the language specification needs to be submitted to dlang.org Needs Tests New Language Feature labels Nov 10, 2024
@WalterBright WalterBright force-pushed the placementNew branch 6 times, most recently from 311f2cb to fd23a02 Compare November 12, 2024 08:55
@WalterBright
Copy link
Member Author

I got it to work for some basic cases. Next comes extending it to more complex ones.

@WalterBright WalterBright force-pushed the placementNew branch 3 times, most recently from 6456c9a to c1d3813 Compare November 13, 2024 09:23
@WalterBright
Copy link
Member Author

Placement new for structs seem to be working now!

@WalterBright WalterBright force-pushed the placementNew branch 4 times, most recently from 5f00e37 to 49f4a4c Compare November 14, 2024 08:17
@Connor-GH
Copy link

Connor-GH commented Nov 15, 2024

If one desires to use classes without the GC, such as in BetterC, it's just awkward to use emplace

If i'm reading this right, this will allow classes in betterC, using a kind of "allocator argument" like in $OTHER_LANGUAGES? Once upon a time I made my own internal fork of dmd that forced betterC on all D code but of course that didn't last long because it couldn't compile Phobos.

(I should clarify, my usecase is making a kernel with betterC and so far I have had to use a hacky mixin and alias this in order to use inheritance. Also, will this unlock the door for interfaces?)

@WalterBright WalterBright force-pushed the placementNew branch 2 times, most recently from 16cdbd6 to 448abed Compare November 15, 2024 04:51
@WalterBright
Copy link
Member Author

Placement works for class objects now!

@TurkeyMan
Copy link
Contributor

Did you tweak it to receive pointers as arguments? I've sent several emails recently and they've all gone silent; I'm wondering if they're getting lost. I also tried commenting on the code in GitHub.

@WalterBright
Copy link
Member Author

I haven't received emails about this. I will check my spam filter. I don't know why you cannot comment on github? I thought it was you who had gone silent! Glad that's not the case.

I stewed about the placement expression as pointer for a while, and figured that making it essentially a ref parameter is more the D way of eschewing pointers in favor of ref. A ref also includes bounds on the size, making that check something the language can do.

Of course, placement new will always be unsafe, but at least we can prevent memory overruns with ref parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Needs Changelog A changelog entry needs to be added to /changelog Needs Spec PR A PR updating the language specification needs to be submitted to dlang.org Needs Tests New Language Feature WIP Work In Progress - not ready for review or pulling
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants