MCPcopy Create free account
hub / github.com/awslabs/llrt / alloc_unsafe_slow

Function alloc_unsafe_slow

modules/llrt_buffer/src/buffer.rs:172–183  ·  view source on GitHub ↗
(ctx: Ctx<'_>, size: usize)

Source from the content-addressed store, hash-verified

170 let mut bytes: Vec<MaybeUninit<u8>> = Vec::with_capacity(size);
171 unsafe {
172 bytes.set_len(size);
173 }
174
175 Buffer(maybeuninit_to_u8(bytes)).into_js(&ctx)
176}
177
178fn maybeuninit_to_u8(vec: Vec<MaybeUninit<u8>>) -> Vec<u8> {
179 let len = vec.len();
180 let capacity = vec.capacity();
181 let ptr = vec.as_ptr() as *mut u8;
182
183 std::mem::forget(vec);
184
185 // This conversion is safe because MaybeUninit has the same memory layout as u8, meaning the underlying bytes are identical.
186 // Since Vec<MaybeUninit> and Vec share the same memory representation, a simple reinterpretation of the pointer is valid.

Callers

nothing calls this directly

Calls 4

allocFunction · 0.85
or_throwMethod · 0.80
BufferClass · 0.70
into_jsMethod · 0.45

Tested by

no test coverage detected