Skip to content

Implement ctl::unique_ptr #1216

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

Merged
merged 6 commits into from
Jun 16, 2024
Merged

Implement ctl::unique_ptr #1216

merged 6 commits into from
Jun 16, 2024

Conversation

mrdomino
Copy link
Collaborator

@mrdomino mrdomino commented Jun 15, 2024

The way unique_ptr is supposed to work is as a purely compile-time check that your raw pointers are getting deleted when they go out of scope. It should ideally emit the same exact machine code as if you were using raw pointers with manual deletes.

Part of what this means is that under normal circumstances, a unique_ptr shouldn’t take up more space than a raw pointer - in other words, sizeof unique_ptr<T> should == sizeof(T*).

The present PR doesn’t bother with the specialization for array types. I also left a couple other parts of the STL API unimplemented. I’d love to see someone else implement these, or I’ll get to them at some point.

The way unique_ptr is supposed to work is as a purely compile-time check
that your raw pointers are getting deleted when they go out of scope. It
should ideally emit the same exact machine code as if you were using raw
pointers with manual deletes.

Part of what this means is that under normal circumstances, a unique_ptr
shouldn’t take up more space than a raw pointer - in other words, sizeof
unique_ptr<T> should == sizeof(T*). The STL approach of having a deleter
as a second template parameter makes this slightly tricky but doable. It
works like this: normally each member of a struct needs to take at least
1 byte, but this doesn’t apply to inheriting from a base struct that has
no members. So practically speaking, what we do is, when it will save us
space, we inherit from our deleter instead of including it as a member.

The above is what our slightly complicated ctl::compressed_pair class is
doing. Honestly we could just elide compressed_pair and inherit from the
pair_elem for our deleter since a T* always takes up a word, but I wrote
the whole thing anyway - maybe it’ll come in handy somewhere else.

The present PR doesn’t bother with the specialization for array types. I
also left a couple other parts of the STL API unimplemented. I’d love to
see someone else implement these, or I’ll get to them at some point.
@mrdomino mrdomino removed the testing label Jun 16, 2024
@mrdomino mrdomino marked this pull request as ready for review June 16, 2024 00:08
mrdomino added 3 commits June 15, 2024 18:53
For that we need [[no_unique_address]].

Actually wait, I think maybe we have [[no_unique_address]]! Maybe we can
get rid of compressed_pair entirely...
This felt so good. Omg. Bye. You won’t be missed.
@mrdomino mrdomino merged commit f9dd568 into jart:master Jun 16, 2024
@mrdomino mrdomino deleted the ctl-unique branch June 16, 2024 02:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants