-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Raw byte allocations via GC
versus Pointer
#13589
Comments
This however sounds quite a bit like a potential footgun. Would it make sense to make it more explicit? EDIT: (Looking at the code doing the allocation, perhaps not) |
I agree that all this duplicity seems unnecessary and confusing. Resolving this was previously proposed in #12391 (comment). For moving to Maybe adding just |
On the other hand, maybe |
It's possible to have |
If it is just about the interpreter, maybe we could expose struct Pointer
def self.malloc(size : UInt64)
{% if T.has_inner_pointers? %}
GC.malloc(size * sizeof(T)).as(T*)
{% else %}
GC.malloc_atomic(size * sizeof(T)).as(T*)
{% end %}
end
end |
I feel like I have either written or read that recently. But I don't recall. Maybe I had only intended to write about it...? 🤔 But yeah, there seems to be not much reason for |
I recall, the familiarity was from #13481. |
The standard library sometimes uses
GC.malloc
andGC.malloc_atomic
to allocate raw bytes, whenSlice
isn't appropriate. Some examples are:crystal/src/log/metadata.cr
Line 35 in 0e82118
crystal/src/string/builder.cr
Line 18 in 0e82118
At other times it uses
Pointer.malloc
:crystal/src/string/formatter.cr
Line 435 in 0e82118
crystal/src/yaml/builder.cr
Line 37 in 0e82118
The two may correspond to different allocators; an explanation is available in #12391. However, I believe this distinction is not that important because the two "different" allocators in this case are simply separate instances of the GC allocator. We should settle with one of those alternatives as a matter of consistency.
Personally I am in favor of the
Pointer
form, because IMOGC
is an implementation detail and should be hidden away.Note that
Pointer(UInt8)#malloc
is guaranteed to be atomic andPointer(Void)#malloc
is guaranteed to be non-atomic.The text was updated successfully, but these errors were encountered: