Simple-Operations.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <html lang="en">
  2. <head>
  3. <title>Simple Operations - 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="Lists.html#Lists" title="Lists">
  9. <link rel="prev" href="Lists.html#Lists" title="Lists">
  10. <link rel="next" href="Recoverable-Operations.html#Recoverable-Operations" title="Recoverable Operations">
  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="Simple-Operations"></a>
  28. <p>
  29. Next:&nbsp;<a rel="next" accesskey="n" href="Recoverable-Operations.html#Recoverable-Operations">Recoverable Operations</a>,
  30. Previous:&nbsp;<a rel="previous" accesskey="p" href="Lists.html#Lists">Lists</a>,
  31. Up:&nbsp;<a rel="up" accesskey="u" href="Lists.html#Lists">Lists</a>
  32. <hr>
  33. </div>
  34. <h4 class="subsection">3.1.1 Simple Operations</h4>
  35. <p>These functions are declared in the header file <code>lists.h</code>, which
  36. should be included in any C source file that uses them with a directive
  37. such as <code>#include&nbsp;&lt;avm/lists.h&gt;<!-- /@w --></code>. All of these functions except
  38. the first three have the potential cause a memory overflow. In that
  39. <a name="index-overflow-399"></a>event, a brief message is written to standard error and the process is
  40. killed rather than returning to the caller. It is possible for client
  41. programs requiring more robust behavior to do their own error handling
  42. by using the alternative versions of these operations described in the
  43. next section.
  44. <div class="defun">
  45. &mdash; Function: void <b>avm_initialize_lists</b> ()<var><a name="index-avm_005finitialize_005flists-400"></a></var><br>
  46. <blockquote><p>The function <code>avm_initialize_lists</code> should be called before any of
  47. the other ones in this section is called, because it sets up some
  48. internal data structures. Otherwise, the behavior of the other functions
  49. is undefined.
  50. </p></blockquote></div>
  51. <div class="defun">
  52. &mdash; Function: void <b>avm_dispose</b> (<var>list front</var>)<var><a name="index-avm_005fdispose-401"></a></var><br>
  53. <blockquote><p>This function deallocates the memory associated with a given list,
  54. either by consigning it to a cache maintained internally by the library,
  55. or by the standard <code>free</code> function if the cache is full. Shared
  56. lists are taken into account and handled properly according to a
  57. reference counting scheme. Lists should be freed only by this function,
  58. not by using <code>free</code> directly.
  59. </p></blockquote></div>
  60. <div class="defun">
  61. &mdash; Function: void <b>avm_count_lists</b> ()<var><a name="index-avm_005fcount_005flists-402"></a></var><br>
  62. <blockquote><p>If a client program aims to do its own storage reclamation, this
  63. function can be called optionally at the end of a run when it is
  64. believed that all lists have been freed. If any allocated lists remain
  65. at large, a warning will be printed to standard error. This function
  66. therefore provides a useful check for memory leaks. Overhead is small
  67. enough that it is not infeasible to leave this check in the production
  68. code.
  69. </p></blockquote></div>
  70. <div class="defun">
  71. &mdash; Function: list <b>avm_copied</b> (<var>list operand</var>)<var><a name="index-avm_005fcopied-403"></a></var><br>
  72. <blockquote><p>A copy of the argument list is returned by this function. The copy
  73. remains intact after the original is reclaimed. A typical use might be
  74. for retaining part of a list after the rest of it is no longer
  75. needed. In this example, a list <code>x</code> is traversed by a hypothetical
  76. <code>visit</code> function to each item, which is then immediately reclaimed.
  77. <pre class="example"> while(x){
  78. visit(x-&gt;head);
  79. old_x = x;
  80. x = avm_copied(x-&gt;tail); /* the right way */
  81. avm_dispose(old_x);
  82. }
  83. </pre>
  84. <p>This example allows each item in the list to be visited even as
  85. previously visited items are reclaimed, because <code>x</code> is copied at
  86. each iteration. This example contrasts with the next one, which will
  87. probably cause a segmentation fault.
  88. <a name="index-segmentation-fault-404"></a>
  89. <pre class="example"> while(x){
  90. visit(x-&gt;head);
  91. old_x = x;
  92. x = x-&gt;tail; /* the wrong way */
  93. avm_dispose(old_x);
  94. }
  95. </pre>
  96. <p>In the second example, a reference is made to a part of a list which no
  97. longer exists because it has been deallocated.
  98. <p>In fact, the <code>avm_copied</code> function does nothing but increment a
  99. reference count, so it is a fast, constant time operation that requires
  100. <a name="index-reference-count-405"></a>no additional memory allocation. Semantically this action is equivalent
  101. to creating a fresh copy of the list, because all list operations in the
  102. library deal with reference counts properly.
  103. </p></blockquote></div>
  104. <div class="defun">
  105. &mdash; Function: list <b>avm_join</b> (<var>list left, list right</var>)<var><a name="index-avm_005fjoin-406"></a></var><br>
  106. <blockquote><p>This function takes a pair of lists to a list in which the left is the
  107. head and the right is the tail. It may need to use <code>malloc</code> to
  108. allocate additional memory. If there is insufficient memory, an error
  109. message is written to standard error and the program exits.
  110. When the list returned by <code>avm_join</code> is eventually deallocated, the
  111. lists from which it was built are taken with it and must not be
  112. referenced again. For example, the following code is an error.
  113. <pre class="example"> z = avm_join(x,y);
  114. ...
  115. avm_dispose(z);
  116. avm_print_list(x); /* error here */
  117. </pre>
  118. <p>To accomplish something similar to this without an error, a copy of
  119. <code>x</code> should be made, as in the next example.
  120. <pre class="example"> z = avm_join(avm_copied(x),y);
  121. ...
  122. avm_dispose(z);
  123. avm_print_list(x); /* original x still intact */
  124. </pre>
  125. </blockquote></div>
  126. <div class="defun">
  127. &mdash; Function: void <b>avm_enqueue</b> (<var>list *front, list *back, list operand</var>)<var><a name="index-avm_005fenqueue-407"></a></var><br>
  128. <blockquote><p><a name="index-queues-408"></a>A fast simple way of building a list head first is provided by the
  129. <code>enqueue</code> function. The <code>front</code> is a pointer to the beginning
  130. of the list being built, and the <code>back</code> is a pointer to the last
  131. item. The recommended way to use it would be something like this.
  132. <pre class="example"> front = back = NULL;
  133. avm_enqueue(&amp;front,&amp;back,item);
  134. avm_enqueue(&amp;front,&amp;back,next_item);
  135. avm_enqueue(&amp;front,&amp;back,another_item);
  136. ...
  137. </pre>
  138. <p>It might be more typical for the calls to <code>avm_enqueue</code> to appear
  139. within a loop. In any case, after the above code is executed, the
  140. following postconditions will hold.
  141. <pre class="example"> front-&gt;head == item
  142. front-&gt;tail-&gt;head == next_item
  143. front-&gt;tail-&gt;tail-&gt;head == another_item
  144. back-&gt;head == another_item
  145. back-&gt;tail == NULL
  146. </pre>
  147. <p>The <code>avm_enqueue</code> function must never be used on a shared list, because
  148. it modifies its arguments in place. The only practical way to guarantee
  149. that a list is not shared is to initialize the <code>front</code> and <code>back</code> to
  150. <code>NULL</code> as shown before the first call to <code>avm_enqueue</code>, and to
  151. make no copies of <code>front</code> or <code>back</code> until after the last call
  152. to <code>avm_enqueue</code>.
  153. <p>Because a list built with <code>avm_enqueue</code> is not shared, it is one of the
  154. few instances of a list that can have something harmlessly appended to
  155. it in place. For example, if the next line of code were
  156. <pre class="example"> back-&gt;tail = rest_of_list;
  157. </pre>
  158. <p>that would be acceptable assuming <code>rest_of_list</code> is not shared and
  159. does not conceal a dangling or cyclic reference, and if nothing further
  160. were enqueued.
  161. <p>The items that are enqueued into a list are not copied and will be
  162. deallocated when the list is deallocated, so they must not be referenced
  163. thereafter. A non-obvious violation of this convention is implicit in
  164. the following code.
  165. <pre class="example"> ...
  166. avm_enqueue(&amp;front,&amp;back,x-&gt;head);
  167. ...
  168. avm_dispose(front);
  169. avm_print_list(x); /* error here */
  170. </pre>
  171. <p>This code might cause a segmentation fault because of the reference to
  172. <a name="index-segmentation-fault-409"></a><code>x</code> after its head has been deallocated. The following code is
  173. subject to the same problem,
  174. <pre class="example"> ...
  175. avm_enqueue(&amp;front,&amp;back,x-&gt;head);
  176. ...
  177. avm_dispose(x);
  178. avm_print_list(front); /* error here */
  179. </pre>
  180. <p>as is the following.
  181. <pre class="example"> ...
  182. avm_enqueue(&amp;front,&amp;back,x-&gt;head);
  183. ...
  184. avm_dispose(x); /* front is now impossible to reclaim */
  185. avm_dispose(front);
  186. </pre>
  187. <p>The problem with the last example is that it is not valid even to
  188. dispose of the same list more than once, albeit indirectly.
  189. <p>If part of a list is intended to be enqueued temporarily or
  190. independently of its parent, the list should be copied explicitly, as
  191. the following code demonstrates.
  192. <pre class="example"> ...
  193. avm_enqueue(&amp;front,&amp;back,avm_copied(x-&gt;head)); /* correct */
  194. ...
  195. avm_dispose(front);
  196. avm_print_list(x);
  197. </pre>
  198. </blockquote></div>
  199. <div class="defun">
  200. &mdash; Function: counter <b>avm_length</b> (<var>list operand</var>)<var><a name="index-avm_005flength-410"></a></var><br>
  201. <blockquote><p>A <code>counter</code> is meant to be the longest unsigned integer available
  202. <a name="index-g_t_0040code_007bcounter_007d-411"></a>on the host machine, and is defined in <code>common.h</code>, which is
  203. automatically included whenever <code>lists.h</code> is included. The
  204. <code>avm_length</code> function returns the number of items in a list. If a
  205. list is <code>NULL</code>, a value of zero is returned. There is a possibility
  206. of a counter overflow error from this function (<a href="Overflow-Errors.html#Overflow-Errors">Overflow Errors</a>),
  207. but only on a platform where the <code>counter</code> type is shorter than the
  208. address length.
  209. </p></blockquote></div>
  210. <div class="defun">
  211. &mdash; Function: counter <b>avm_area</b> (<var>list operand</var>)<var><a name="index-avm_005farea-412"></a></var><br>
  212. <blockquote><p>This function is similar to <code>avm_length</code>, but it treats its
  213. argument as a list of lists and returns the summation of their lengths.
  214. </p></blockquote></div>
  215. <div class="defun">
  216. &mdash; Function: list <b>avm_natural</b> (<var>counter number</var>)<var><a name="index-avm_005fnatural-413"></a></var><br>
  217. <blockquote><p><a name="index-naturals-414"></a>This function takes a <code>counter</code> to its representation as a list, as
  218. described in <a href="Representation-of-Numeric-and-Textual-Data.html#Representation-of-Numeric-and-Textual-Data">Representation of Numeric and Textual Data</a>. That is,
  219. the number is represented as a list of bits, least significant bit
  220. first, with each zero bit represented by <code>NULL</code> and each one bit
  221. represented by a list whose <code>head</code> and <code>tail</code> are <code>NULL</code>.
  222. </p></blockquote></div>
  223. <div class="defun">
  224. &mdash; Function: void <b>avm_print_list</b> (<var>list operand</var>)<var><a name="index-avm_005fprint_005flist-415"></a></var><br>
  225. <blockquote><p>The <code>avm_print_list</code> function is not used in any production code
  226. but retained in the library for debugging purposes. It prints a list to
  227. <a name="index-standard-output-416"></a>standard output using an expression involving only commas and parentheses,
  228. as per the <code>silly</code> syntax (<a href="A-Simple-Lisp-Like-Language.html#A-Simple-Lisp-Like-Language">A Simple Lisp Like Language</a>). The
  229. results quickly become unintelligible for lists of any significant size.
  230. The function is recursively defined and will crash in the event of a
  231. stack overflow, which will occur in the case of very large or cyclic
  232. lists.
  233. </p></blockquote></div>
  234. <div class="defun">
  235. &mdash; Function: list <b>avm_position</b> (<var>list key, list table, int *fault</var>)<var><a name="index-avm_005fposition-417"></a></var><br>
  236. <blockquote><p>This function searches for a <var>key</var> in a short <var>table</var> where
  237. each item is a possible key.
  238. <p>If it's not found, a <code>NULL</code> value is returned. If it's
  239. found, a list representing a character encoding according to
  240. <a href="Character-Table.html#Character-Table">Character Table</a> is returned.
  241. <p>The ascii code of the character corresponding to the returned list is
  242. the position of the <var>key</var> in the <var>table</var>, assuming position
  243. numbers start with 1.
  244. <p>The table should have a length of 255 or less. If it's longer and the
  245. <var>key</var> is found beyond that range, the higher order bits of the
  246. position number are ignored.
  247. <p>The integer referenced by <var>fault</var> is set to a non-zero value in
  248. the event of a memory overflow, which could happen in the course of
  249. the list comparisons necessary for the search.
  250. </p></blockquote></div>
  251. </body></html>