Ports-and-Packets.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <html lang="en">
  2. <head>
  3. <title>Ports and Packets - avram - a virtual machine code interpreter</title>
  4. <meta http-equiv="Content-Type" content="text/html">
  5. <meta name="description" content="avram - a virtual machine code interpreter">
  6. <meta name="generator" content="makeinfo 4.13">
  7. <link title="Top" rel="start" href="index.html#Top">
  8. <link rel="up" href="Emulation-Primitives.html#Emulation-Primitives" title="Emulation Primitives">
  9. <link rel="prev" href="Lists-of-Pairs-of-Ports.html#Lists-of-Pairs-of-Ports" title="Lists of Pairs of Ports">
  10. <link rel="next" href="Instruction-Stacks.html#Instruction-Stacks" title="Instruction Stacks">
  11. <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
  12. <meta http-equiv="Content-Style-Type" content="text/css">
  13. <style type="text/css"><!--
  14. pre.display { font-family:inherit }
  15. pre.format { font-family:inherit }
  16. pre.smalldisplay { font-family:inherit; font-size:smaller }
  17. pre.smallformat { font-family:inherit; font-size:smaller }
  18. pre.smallexample { font-size:smaller }
  19. pre.smalllisp { font-size:smaller }
  20. span.sc { font-variant:small-caps }
  21. span.roman { font-family:serif; font-weight:normal; }
  22. span.sansserif { font-family:sans-serif; font-weight:normal; }
  23. --></style>
  24. </head>
  25. <body>
  26. <div class="node">
  27. <a name="Ports-and-Packets"></a>
  28. <p>
  29. Next:&nbsp;<a rel="next" accesskey="n" href="Instruction-Stacks.html#Instruction-Stacks">Instruction Stacks</a>,
  30. Previous:&nbsp;<a rel="previous" accesskey="p" href="Lists-of-Pairs-of-Ports.html#Lists-of-Pairs-of-Ports">Lists of Pairs of Ports</a>,
  31. Up:&nbsp;<a rel="up" accesskey="u" href="Emulation-Primitives.html#Emulation-Primitives">Emulation Primitives</a>
  32. <hr>
  33. </div>
  34. <h4 class="subsection">3.8.2 Ports and Packets</h4>
  35. <p>A pointer type declared as a <code>port</code> points to a structure in the
  36. following form, where a <code>flag</code> is an unsigned short integer type,
  37. and a <code>counter</code> is an unsigned long integer.
  38. <a name="index-g_t_0040code_007bcounter_007d-635"></a><a name="index-g_t_0040code_007bflag_007d-636"></a><a name="index-g_t_0040code_007bavm_005fpacket_007d-637"></a>
  39. <pre class="example"> struct avm_packet
  40. {
  41. port parent;
  42. counter errors;
  43. portal descendents;
  44. list impetus, contents;
  45. flag predicating;
  46. };
  47. </pre>
  48. <p class="noindent">For reasons that make sense to C, the <code>avm_packet</code> and <code>port</code>
  49. types are declared in <code>lists.h</code>, but a few memory management
  50. operations on them are available by way of functions declared in
  51. <samp><span class="file">ports.h</span></samp>. The intended meaning of this structure is described
  52. presently, but first the memory management functions are as follows.
  53. <div class="defun">
  54. &mdash; Function: port <b>avm_newport</b> (<var>counter errors, port parent, int predicating</var>)<var><a name="index-avm_005fnewport-638"></a></var><br>
  55. <blockquote><p>This function attempts to allocate storage for a new packet structure
  56. and returns its address if successful. If storage can not be allocated,
  57. a <code>NULL</code> pointer is returned. The <code>errors</code>, <code>parent</code>, and
  58. <code>predicating</code> fields are initialized with the parameters supplied
  59. by the caller. The rest of the structure is filled with zeros. A local
  60. memory cache is used for improved performance.
  61. </p></blockquote></div>
  62. <div class="defun">
  63. &mdash; Function: void <b>avm_sever</b> (<var>port appendage</var>)<var><a name="index-avm_005fsever-639"></a></var><br>
  64. <blockquote><p>This function reclaims the storage associated with a <code>port</code>, either
  65. freeing it entirely or holding it in a local cache. None of the
  66. entities that may be referenced by pointers within the structure are
  67. affected. Only this function should be used by client programs for
  68. disposing of ports, not the <code>free</code> function directly, or some
  69. internal bookkeeping will be disrupted. An internal error results if the
  70. argument is a <code>NULL</code> pointer.
  71. </p></blockquote></div>
  72. <div class="defun">
  73. &mdash; Function: void <b>avm_initialize_ports</b> ()<var><a name="index-avm_005finitialize_005fports-640"></a></var><br>
  74. <blockquote><p>This function must be called prior to calling either of the two above,
  75. in order to initialize some static variables.
  76. </p></blockquote></div>
  77. <div class="defun">
  78. &mdash; Function: void <b>avm_count_ports</b> ()<var><a name="index-avm_005fcount_005fports-641"></a></var><br>
  79. <blockquote><p>This function may be called after the last call to any of the other
  80. functions in this section in order to detect and report unreclaimed
  81. storage associated with ports. A non-fatal warning will be written to
  82. standard error if any is detected, but otherwise there is no effect.
  83. </p></blockquote></div>
  84. <p>The interesting aspect of this data structure is the role it plays in
  85. capturing the state of a computation. For this purpose, it corresponds
  86. to a single node in a partially computed result to be represented by a
  87. <code>list</code> when it's finished. The nodes should be envisioned as a
  88. doubly-linked binary tree, except that the pair of <code>descendents</code>
  89. for each node is not yet known with certainty, so a list of alternatives
  90. must be maintained.
  91. <p>Because the computation is not completed while this data structure
  92. exists, there are always some empty fields in it. For example, the
  93. <code>descendents</code> and the <code>contents</code> fields embody the same
  94. information, the latter doing so in a compact as opposed to a more
  95. expanded form. Hence, it would be redundant for both fields to be
  96. non-empty at the same time. The data structure is built initially with
  97. <code>descendents</code> and no <code>contents</code>, only to be transformed into
  98. one with <code>contents</code> and no <code>descendents</code>.
  99. <p>The significance of each field in the structure can be summarized as
  100. follows.
  101. <dl>
  102. <dt><code>contents</code><dd>If the computational result destined for the <code>port</code> pointing to this packet
  103. is not complete, then this field is <code>NULL</code> and the
  104. <code>descendents</code> are being computed. Otherwise, it contains the result
  105. of the computation.
  106. <br><dt><code>descendents</code><dd>This field points to a list of pairs of ports serving as the
  107. destinations for an ensemble of concurrent computations.<a rel="footnote" href="#fn-1" name="fnd-1"><sup>1</sup></a> The <code>head</code>
  108. and <code>tail</code> of the <code>contents</code> are to be identified respectively
  109. with the <code>contents</code> of the <code>left</code> and <code>right</code> <code>port</code>
  110. in the first pair to finish being computed.
  111. <br><dt><code>parent</code><dd>If this packet is addressed by the <code>left</code> or the <code>right</code> of
  112. <code>port</code> in one of the <code>descendents</code> of some other packet, then
  113. this field points to that packet.
  114. <br><dt><code>errors</code><dd>A non-zero value in this field indicates that the result destined for
  115. the <code>contents</code> of this packet is expected to be an error
  116. message. If the exact level of error severity incurred in the
  117. computation of the <code>contents</code> matches this number, then the
  118. contents can be assigned the result, but otherwise the result should
  119. propagate to the <code>contents</code> of the <code>parent</code>.
  120. <br><dt><code>predicating</code><dd>A non-zero value in this field implies that the result destined for the
  121. <code>contents</code> of this packet is being computed in order to decide
  122. which arm of a conditional function should be chosen. I.e., a
  123. <code>NULL</code> result calls for the one that is invoked when the predicate
  124. is false.
  125. <br><dt><code>impetus</code><dd>If the result destined for the <code>contents</code> of this packet is being
  126. computed in order to transform a virtual code fragment from its original
  127. form to an equivalent representation capable of being evaluated more
  128. directly, this field points to a <code>list</code> node at the root of the
  129. virtual code in its original form.
  130. </dl>
  131. <p>One of the hitherto undocumented fields in a <code>list</code> node structure
  132. <a name="index-g_t_0040code_007binterpretation_007d-642"></a><a name="index-g_t_0040code_007bimpetus_007d-643"></a>declared in <samp><span class="file">lists.h</span></samp> is called the <code>interpretation</code>, and is
  133. of type <code>list</code>. A client program delving into sufficient depth of
  134. detail to be concerned with ports and packets may reasonably assign the
  135. <code>interpretation</code> field of the <code>list</code> referenced by the
  136. <code>impetus</code> field in a packet to be a copy of the <code>contents</code> of
  137. the packet when they are eventually obtained. Doing so will save some
  138. time by eliminating the need for it to be recomputed if the same virtual
  139. code should be executed again.
  140. <p>If this course is taken, the <code>facilitator</code> field in a <code>list</code>
  141. <a name="index-g_t_0040code_007bfacilitator_007d-644"></a>node, also hitherto undocumented, should contain the address of the
  142. packet referring to the list node as its <code>impetus</code>. The reason for
  143. this additional link is so that it can be followed when the
  144. <code>impetus</code> of the packet is be cleared by <code>avm_dispose</code> in the
  145. event that the <code>list</code> node is freed before the computation
  146. completes. This action is performed in order to preclude a dangling
  147. pointer in the <code>impetus</code> field.
  148. <div class="footnote">
  149. <hr>
  150. <h4>Footnotes</h4><p class="footnote"><small>[<a name="fn-1" href="#fnd-1">1</a>]</small> Earlier
  151. versions of <code>avram</code> included a bottom avoiding choice combinator
  152. that required this feature, but which has been withdrawn. A single pair
  153. of descendent ports would now suffice.</p>
  154. <hr></div>
  155. </body></html>