Skip to content

Reference counting and the token type in the C target

Edward A. Lee edited this page Nov 29, 2022 · 4 revisions

Define an output to be either an output port or the record on the self struct that records data sent to the input port of a contained reactor. If the output has a type of the form type* or type[], then it is said to have token type, and object of type lf_token_t will be created by the code generator to serve as a template for the data produced by this output. The struct for this output (a field of the self struct) will have a pointer to the template token at all times, though the actual object pointed to can change over time. The template contains the following critical pieces of information:

  • value: a pointer to the payload for the token, which is either an array or a scalar.
  • element_size: the size of each array element (or the total size if not an array). This is determined by the type of the output.
  • length: the length of the array of objects of size element_size carried by the token or 1 if it is not an array.
  • num_destinations: the number of destination input ports that refer to this output.
  • ref_count: a count of the number of times this token has been passed to the lf_schedule_token function, which puts the token into an event on the event queue.
  • destructor: a function that frees the memory of each element of size element_size.
  • copy_constructor: a function that copies an element of size element_size.

If no destructor is given, then free will be used. If no copy constructor is given, then elements will be copied using assignment =. A template token may become detached from an output (see below), at which time the num_destinations field will be set to 0.

When a reaction sets an output (or the input of a contained reactor) via lf_set(name, value), it attempts to just update the value (and maybe the length) of the template token.