Skip to content

Raw byte allocations via GC versus Pointer #13589

@HertzDevil

Description

@HertzDevil

The standard library sometimes uses GC.malloc and GC.malloc_atomic to allocate raw bytes, when Slice isn't appropriate. Some examples are:

data = GC.malloc(data_size).as(self)

@buffer = GC.malloc_atomic(capacity.to_u32).as(UInt8*)

At other times it uses Pointer.malloc:

@temp_buf = Pointer(UInt8).malloc(len)

@emitter = Pointer(Void).malloc(LibYAML::EMITTER_SIZE).as(LibYAML::Emitter*)

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 IMO GC is an implementation detail and should be hidden away.

Note that Pointer(UInt8)#malloc is guaranteed to be atomic and Pointer(Void)#malloc is guaranteed to be non-atomic.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions