Skip to main content

Function: allocStruct()

allocStruct(def, options?): AllocatedStruct

Allocate struct bytes for a C function to fill in, with a buffer per named array field wired up as if StructDef.pack had written a run of that many elements.

import { allocStruct, defineStruct } from 'tjs:ffi';

// struct int_list { unsigned count; int *items; };
const IntList = defineStruct([
['count', 'u32', { lengthOf: 'items' }],
['items', ['int']],
]);

const { bytes } = allocStruct(IntList, { lengths: { items: 4 } });

symbols.fill_int_list(bytes); // void fill_int_list(struct int_list *);
console.log(IntList.unpack(bytes));

Parameters

def

StructDef

The struct to allocate.

options?

AllocStructOptions

Which array fields to allocate room for.

Returns

AllocatedStruct