/* struct capn is a common structure shared between segments in the same * session/context so that far pointers between segments will be created. * * lookup is used to lookup segments by id when derefencing a far pointer * * create is used to create or lookup an alternate segment that has at least * sz available (ie returned seg->len + sz <= seg->cap) * * create_local is used to create a segment for the copy tree and should be * allocated in the local memory space. * * Allocated segments must be zero initialized. * * create and lookup can be NULL if you don't need multiple segments and don't * want to support copying * * seglist and copylist are linked lists which can be used to free up segments * on cleanup, but should not be modified by the user. * * lookup, create, create_local, and user can be set by the user. Other values * should be zero initialized. */ structcapn { /* user settable */ structcapn_segment *(*lookup)(void* /*user*/, uint32_t /*id */); structcapn_segment *(*create)(void* /*user*/, uint32_t /*id */, int /*sz*/); structcapn_segment *(*create_local)(void* /*user*/, int /*sz*/); void *user; /* zero initialized, user should not modify */ uint32_t segnum; structcapn_tree *copy; structcapn_tree *segtree; structcapn_segment *seglist, *lastseg; structcapn_segment *copylist; };
/* struct capn_segment contains the information about a single segment. * * capn points to a struct capn that is shared between segments in the * same session * * id specifies the segment id, used for far pointers * * data specifies the segment data. This should not move after creation. * * len specifies the current segment length. This is 0 for a blank * segment. * * cap specifies the segment capacity. * * When creating new structures len will be incremented until it reaches cap, * at which point a new segment will be requested via capn->create. The * create callback can either create a new segment or expand an existing * one by incrementing cap and returning the expanded segment. * * data, len, and cap must all be 8 byte aligned, hence the ALIGNED_(8) macro * on the struct definition. * * data, len, cap, and user should all be set by the user. Other values * should be zero initialized. */ truct ALIGNED_(8) capn_segment { structcapn_treehdr; structcapn_segment *next; structcapn *capn; uint32_t id; /* user settable */ char *data; size_t len, cap; void *user; };
/* capn_getp|setp functions get/set ptrs in list/structs * off is the list index or pointer index in a struct * capn_setp will copy the data, create far pointers, etc if the target * is in a different segment/context. * Both of these will use/return inner pointers for composite lists. */ capn_ptr capn_getp(capn_ptr p, int off, int resolve); intcapn_setp(capn_ptr p, int off, capn_ptr tgt);