Function tryAdoptMallocResource

Adopts one malloc/free-compatible allocation.

bool tryAdoptMallocResource(
  void* base,
  ulong byteLength,
  ref OwnedByteResource owned
) nothrow @nogc;

On success ownership of base transfers to owned.

owned must be empty. Adoption never replaces an existing release obligation.

If owned already owns a resource, the function returns false, leaves owned unchanged, and ownership of base remains with the caller.

The caller must not free or otherwise release base after a successful call.

Successful adoption records the physical resource as read-write.

The caller therefore also asserts that the complete adopted byte range is valid writable storage for the lifetime of the ownership obligation.

A null base is rejected and no ownership transfer occurs.

This API deliberately uses a ref output target rather than out. Resetting an already-live ownership token to .init would bypass its release transition.

The function is @system because the library cannot prove that:

- base really denotes a free()-compatible allocation; - byteLength correctly describes that allocation; - the complete adopted byte range is writable; - the caller truly owns the allocation being transferred.

After successful adoption normal OwnedByteResource lifetime management does not require raw callback/context handling.

Example

Example adopting a malloc-compatible byte allocation.

import core.stdc.stdlib : malloc;
import raster;

void* memory = malloc(16);
assert(memory !is null);

OwnedByteResource resource;
assert(tryAdoptMallocResource(memory, 16, resource));
assert(resource.ownsResource);
assert(resource.byteLength == 16);