Simple-Operations.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
  2. <html>
  3. <!-- Created on December 10, 2012 by texi2html 1.82
  4. texi2html was written by:
  5. Lionel Cons <[email protected]> (original author)
  6. Karl Berry <[email protected]>
  7. Olaf Bachmann <[email protected]>
  8. and many others.
  9. Maintained by: Many creative people.
  10. Send bugs and suggestions to <[email protected]>
  11. -->
  12. <head>
  13. <title>avram - a virtual machine code interpreter: 3.1.1 Simple Operations</title>
  14. <meta name="description" content="avram - a virtual machine code interpreter: 3.1.1 Simple Operations">
  15. <meta name="keywords" content="avram - a virtual machine code interpreter: 3.1.1 Simple Operations">
  16. <meta name="resource-type" content="document">
  17. <meta name="distribution" content="global">
  18. <meta name="Generator" content="texi2html 1.82">
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <style type="text/css">
  21. <!--
  22. a.summary-letter {text-decoration: none}
  23. blockquote.smallquotation {font-size: smaller}
  24. pre.display {font-family: serif}
  25. pre.format {font-family: serif}
  26. pre.menu-comment {font-family: serif}
  27. pre.menu-preformatted {font-family: serif}
  28. pre.smalldisplay {font-family: serif; font-size: smaller}
  29. pre.smallexample {font-size: smaller}
  30. pre.smallformat {font-family: serif; font-size: smaller}
  31. pre.smalllisp {font-size: smaller}
  32. span.roman {font-family:serif; font-weight:normal;}
  33. span.sansserif {font-family:sans-serif; font-weight:normal;}
  34. ul.toc {list-style: none}
  35. -->
  36. </style>
  37. </head>
  38. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  39. <a name="Simple-Operations"></a>
  40. <table cellpadding="1" cellspacing="1" border="0">
  41. <tr><td valign="middle" align="left">[<a href="Lists.html#Lists" title="Previous section in reading order"> &lt; </a>]</td>
  42. <td valign="middle" align="left">[<a href="Recoverable-Operations.html#Recoverable-Operations" title="Next section in reading order"> &gt; </a>]</td>
  43. <td valign="middle" align="left"> &nbsp; </td>
  44. <td valign="middle" align="left">[<a href="Library-Reference.html#Library-Reference" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
  45. <td valign="middle" align="left">[<a href="Lists.html#Lists" title="Up section"> Up </a>]</td>
  46. <td valign="middle" align="left">[<a href="Character-Table.html#Character-Table" title="Next chapter"> &gt;&gt; </a>]</td>
  47. <td valign="middle" align="left"> &nbsp; </td>
  48. <td valign="middle" align="left"> &nbsp; </td>
  49. <td valign="middle" align="left"> &nbsp; </td>
  50. <td valign="middle" align="left"> &nbsp; </td>
  51. <td valign="middle" align="left">[<a href="avram.html#Top" title="Cover (top) of document">Top</a>]</td>
  52. <td valign="middle" align="left">[<a href="avram_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
  53. <td valign="middle" align="left">[<a href="Function-Index.html#Function-Index" title="Index">Index</a>]</td>
  54. <td valign="middle" align="left">[<a href="avram_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
  55. </tr></table>
  56. <hr size="1">
  57. <a name="Simple-Operations-1"></a>
  58. <h3 class="subsection">3.1.1 Simple Operations</h3>
  59. <p>These functions are declared in the header file <code>lists.h</code>, which
  60. should be included in any C source file that uses them with a directive
  61. such as <code>#include &lt;avm/lists.h&gt;</code>. All of these functions except
  62. the first three have the potential cause a memory overflow. In that
  63. <a name="index-overflow-2"></a>
  64. event, a brief message is written to standard error and the process is
  65. killed rather than returning to the caller. It is possible for client
  66. programs requiring more robust behavior to do their own error handling
  67. by using the alternative versions of these operations described in the
  68. next section.
  69. </p>
  70. <dl>
  71. <dt><a name="index-avm_005finitialize_005flists"></a><u>Function:</u> void <b>avm_initialize_lists</b><i> ()</i></dt>
  72. <dd><p>The function <code>avm_initialize_lists</code> should be called before any of
  73. the other ones in this section is called, because it sets up some
  74. internal data structures. Otherwise, the behavior of the other functions
  75. is undefined.
  76. </p></dd></dl>
  77. <dl>
  78. <dt><a name="index-avm_005fdispose"></a><u>Function:</u> void <b>avm_dispose</b><i> (list <var>front</var>)</i></dt>
  79. <dd><p>This function deallocates the memory associated with a given list,
  80. either by consigning it to a cache maintained internally by the library,
  81. or by the standard <code>free</code> function if the cache is full. Shared
  82. lists are taken into account and handled properly according to a
  83. reference counting scheme. Lists should be freed only by this function,
  84. not by using <code>free</code> directly.
  85. </p></dd></dl>
  86. <dl>
  87. <dt><a name="index-avm_005fcount_005flists"></a><u>Function:</u> void <b>avm_count_lists</b><i> ()</i></dt>
  88. <dd><p>If a client program aims to do its own storage reclamation, this
  89. function can be called optionally at the end of a run when it is
  90. believed that all lists have been freed. If any allocated lists remain
  91. at large, a warning will be printed to standard error. This function
  92. therefore provides a useful check for memory leaks. Overhead is small
  93. enough that it is not infeasible to leave this check in the production
  94. code.
  95. </p></dd></dl>
  96. <dl>
  97. <dt><a name="index-avm_005fcopied"></a><u>Function:</u> list <b>avm_copied</b><i> (list <var>operand</var>)</i></dt>
  98. <dd><p>A copy of the argument list is returned by this function. The copy
  99. remains intact after the original is reclaimed. A typical use might be
  100. for retaining part of a list after the rest of it is no longer
  101. needed. In this example, a list <code>x</code> is traversed by a hypothetical
  102. <code>visit</code> function to each item, which is then immediately reclaimed.
  103. </p>
  104. <table><tr><td>&nbsp;</td><td><pre class="example">while(x){
  105. visit(x-&gt;head);
  106. old_x = x;
  107. x = avm_copied(x-&gt;tail); /* the right way */
  108. avm_dispose(old_x);
  109. }
  110. </pre></td></tr></table>
  111. <p>This example allows each item in the list to be visited even as
  112. previously visited items are reclaimed, because <code>x</code> is copied at
  113. each iteration. This example contrasts with the next one, which will
  114. probably cause a segmentation fault.
  115. <a name="index-segmentation-fault"></a>
  116. </p>
  117. <table><tr><td>&nbsp;</td><td><pre class="example">while(x){
  118. visit(x-&gt;head);
  119. old_x = x;
  120. x = x-&gt;tail; /* the wrong way */
  121. avm_dispose(old_x);
  122. }
  123. </pre></td></tr></table>
  124. <p>In the second example, a reference is made to a part of a list which no
  125. longer exists because it has been deallocated.
  126. </p>
  127. <p>In fact, the <code>avm_copied</code> function does nothing but increment a
  128. reference count, so it is a fast, constant time operation that requires
  129. <a name="index-reference-count-1"></a>
  130. no additional memory allocation. Semantically this action is equivalent
  131. to creating a fresh copy of the list, because all list operations in the
  132. library deal with reference counts properly.
  133. </p></dd></dl>
  134. <dl>
  135. <dt><a name="index-avm_005fjoin"></a><u>Function:</u> list <b>avm_join</b><i> (list <var>left</var>, list <var>right</var>)</i></dt>
  136. <dd><p>This function takes a pair of lists to a list in which the left is the
  137. head and the right is the tail. It may need to use <code>malloc</code> to
  138. allocate additional memory. If there is insufficient memory, an error
  139. message is written to standard error and the program exits.
  140. When the list returned by <code>avm_join</code> is eventually deallocated, the
  141. lists from which it was built are taken with it and must not be
  142. referenced again. For example, the following code is an error.
  143. </p>
  144. <table><tr><td>&nbsp;</td><td><pre class="example">z = avm_join(x,y);
  145. &hellip;
  146. avm_dispose(z);
  147. avm_print_list(x); /* error here */
  148. </pre></td></tr></table>
  149. <p>To accomplish something similar to this without an error, a copy of
  150. <code>x</code> should be made, as in the next example.
  151. </p>
  152. <table><tr><td>&nbsp;</td><td><pre class="example">z = avm_join(avm_copied(x),y);
  153. &hellip;
  154. avm_dispose(z);
  155. avm_print_list(x); /* original x still intact */
  156. </pre></td></tr></table>
  157. </dd></dl>
  158. <dl>
  159. <dt><a name="index-avm_005fenqueue"></a><u>Function:</u> void <b>avm_enqueue</b><i> (list *<var>front</var>, list *<var>back</var>, list <var>operand</var>)</i></dt>
  160. <dd><a name="index-queues-1"></a>
  161. <p>A fast simple way of building a list head first is provided by the
  162. <code>enqueue</code> function. The <code>front</code> is a pointer to the beginning
  163. of the list being built, and the <code>back</code> is a pointer to the last
  164. item. The recommended way to use it would be something like this.
  165. </p>
  166. <table><tr><td>&nbsp;</td><td><pre class="example">front = back = NULL;
  167. avm_enqueue(&amp;front,&amp;back,item);
  168. avm_enqueue(&amp;front,&amp;back,next_item);
  169. avm_enqueue(&amp;front,&amp;back,another_item);
  170. &hellip;
  171. </pre></td></tr></table>
  172. <p>It might be more typical for the calls to <code>avm_enqueue</code> to appear
  173. within a loop. In any case, after the above code is executed, the
  174. following postconditions will hold.
  175. </p>
  176. <table><tr><td>&nbsp;</td><td><pre class="example">front-&gt;head == item
  177. front-&gt;tail-&gt;head == next_item
  178. front-&gt;tail-&gt;tail-&gt;head == another_item
  179. back-&gt;head == another_item
  180. back-&gt;tail == NULL
  181. </pre></td></tr></table>
  182. <p>The <code>avm_enqueue</code> function must never be used on a shared list, because
  183. it modifies its arguments in place. The only practical way to guarantee
  184. that a list is not shared is to initialize the <code>front</code> and <code>back</code> to
  185. <code>NULL</code> as shown before the first call to <code>avm_enqueue</code>, and to
  186. make no copies of <code>front</code> or <code>back</code> until after the last call
  187. to <code>avm_enqueue</code>.
  188. </p>
  189. <p>Because a list built with <code>avm_enqueue</code> is not shared, it is one of the
  190. few instances of a list that can have something harmlessly appended to
  191. it in place. For example, if the next line of code were
  192. </p>
  193. <table><tr><td>&nbsp;</td><td><pre class="example">back-&gt;tail = rest_of_list;
  194. </pre></td></tr></table>
  195. <p>that would be acceptable assuming <code>rest_of_list</code> is not shared and
  196. does not conceal a dangling or cyclic reference, and if nothing further
  197. were enqueued.
  198. </p>
  199. <p>The items that are enqueued into a list are not copied and will be
  200. deallocated when the list is deallocated, so they must not be referenced
  201. thereafter. A non-obvious violation of this convention is implicit in
  202. the following code.
  203. </p>
  204. <table><tr><td>&nbsp;</td><td><pre class="example">&hellip;
  205. avm_enqueue(&amp;front,&amp;back,x-&gt;head);
  206. &hellip;
  207. avm_dispose(front);
  208. avm_print_list(x); /* error here */
  209. </pre></td></tr></table>
  210. <p>This code might cause a segmentation fault because of the reference to
  211. <a name="index-segmentation-fault-1"></a>
  212. <code>x</code> after its head has been deallocated. The following code is
  213. subject to the same problem,
  214. </p>
  215. <table><tr><td>&nbsp;</td><td><pre class="example">&hellip;
  216. avm_enqueue(&amp;front,&amp;back,x-&gt;head);
  217. &hellip;
  218. avm_dispose(x);
  219. avm_print_list(front); /* error here */
  220. </pre></td></tr></table>
  221. <p>as is the following.
  222. </p>
  223. <table><tr><td>&nbsp;</td><td><pre class="example">&hellip;
  224. avm_enqueue(&amp;front,&amp;back,x-&gt;head);
  225. &hellip;
  226. avm_dispose(x); /* front is now impossible to reclaim */
  227. avm_dispose(front);
  228. </pre></td></tr></table>
  229. <p>The problem with the last example is that it is not valid even to
  230. dispose of the same list more than once, albeit indirectly.
  231. </p>
  232. <p>If part of a list is intended to be enqueued temporarily or
  233. independently of its parent, the list should be copied explicitly, as
  234. the following code demonstrates.
  235. </p>
  236. <table><tr><td>&nbsp;</td><td><pre class="example">&hellip;
  237. avm_enqueue(&amp;front,&amp;back,avm_copied(x-&gt;head)); /* correct */
  238. &hellip;
  239. avm_dispose(front);
  240. avm_print_list(x);
  241. </pre></td></tr></table>
  242. </dd></dl>
  243. <dl>
  244. <dt><a name="index-avm_005flength"></a><u>Function:</u> counter <b>avm_length</b><i> (list <var>operand</var>)</i></dt>
  245. <dd><p>A <code>counter</code> is meant to be the longest unsigned integer available
  246. <a name="index-counter"></a>
  247. on the host machine, and is defined in <code>common.h</code>, which is
  248. automatically included whenever <code>lists.h</code> is included. The
  249. <code>avm_length</code> function returns the number of items in a list. If a
  250. list is <code>NULL</code>, a value of zero is returned. There is a possibility
  251. of a counter overflow error from this function (<a href="Overflow-Errors.html#Overflow-Errors">Overflow Errors</a>),
  252. but only on a platform where the <code>counter</code> type is shorter than the
  253. address length.
  254. </p></dd></dl>
  255. <dl>
  256. <dt><a name="index-avm_005farea"></a><u>Function:</u> counter <b>avm_area</b><i> (list <var>operand</var>)</i></dt>
  257. <dd><p>This function is similar to <code>avm_length</code>, but it treats its
  258. argument as a list of lists and returns the summation of their lengths.
  259. </p></dd></dl>
  260. <dl>
  261. <dt><a name="index-avm_005fnatural"></a><u>Function:</u> list <b>avm_natural</b><i> (counter <var>number</var>)</i></dt>
  262. <dd><a name="index-naturals-2"></a>
  263. <p>This function takes a <code>counter</code> to its representation as a list, as
  264. 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,
  265. the number is represented as a list of bits, least significant bit
  266. first, with each zero bit represented by <code>NULL</code> and each one bit
  267. represented by a list whose <code>head</code> and <code>tail</code> are <code>NULL</code>.
  268. </p></dd></dl>
  269. <dl>
  270. <dt><a name="index-avm_005fprint_005flist"></a><u>Function:</u> void <b>avm_print_list</b><i> (list <var>operand</var>)</i></dt>
  271. <dd><p>The <code>avm_print_list</code> function is not used in any production code
  272. but retained in the library for debugging purposes. It prints a list to
  273. <a name="index-standard-output-3"></a>
  274. standard output using an expression involving only commas and parentheses,
  275. 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
  276. results quickly become unintelligible for lists of any significant size.
  277. The function is recursively defined and will crash in the event of a
  278. stack overflow, which will occur in the case of very large or cyclic
  279. lists.
  280. </p></dd></dl>
  281. <dl>
  282. <dt><a name="index-avm_005fposition"></a><u>Function:</u> list <b>avm_position</b><i> (list <var>key</var>, list <var>table</var>, int *<var>fault</var>)</i></dt>
  283. <dd><p>This function searches for a <var>key</var> in a short <var>table</var> where
  284. each item is a possible key.
  285. </p>
  286. <p>If it&rsquo;s not found, a <code>NULL</code> value is returned. If it&rsquo;s
  287. found, a list representing a character encoding according to
  288. <a href="Character-Table.html#Character-Table">Character Table</a> is returned.
  289. </p>
  290. <p>The ascii code of the character corresponding to the returned list is
  291. the position of the <var>key</var> in the <var>table</var>, assuming position
  292. numbers start with 1.
  293. </p>
  294. <p>The table should have a length of 255 or less. If it&rsquo;s longer and the
  295. <var>key</var> is found beyond that range, the higher order bits of the
  296. position number are ignored.
  297. </p>
  298. <p>The integer referenced by <var>fault</var> is set to a non-zero value in
  299. the event of a memory overflow, which could happen in the course of
  300. the list comparisons necessary for the search.
  301. </p></dd></dl>
  302. <hr size="1">
  303. <table cellpadding="1" cellspacing="1" border="0">
  304. <tr><td valign="middle" align="left">[<a href="Lists.html#Lists" title="Previous section in reading order"> &lt; </a>]</td>
  305. <td valign="middle" align="left">[<a href="Recoverable-Operations.html#Recoverable-Operations" title="Next section in reading order"> &gt; </a>]</td>
  306. <td valign="middle" align="left"> &nbsp; </td>
  307. <td valign="middle" align="left">[<a href="Library-Reference.html#Library-Reference" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
  308. <td valign="middle" align="left">[<a href="Lists.html#Lists" title="Up section"> Up </a>]</td>
  309. <td valign="middle" align="left">[<a href="Character-Table.html#Character-Table" title="Next chapter"> &gt;&gt; </a>]</td>
  310. <td valign="middle" align="left"> &nbsp; </td>
  311. <td valign="middle" align="left"> &nbsp; </td>
  312. <td valign="middle" align="left"> &nbsp; </td>
  313. <td valign="middle" align="left"> &nbsp; </td>
  314. <td valign="middle" align="left">[<a href="avram.html#Top" title="Cover (top) of document">Top</a>]</td>
  315. <td valign="middle" align="left">[<a href="avram_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
  316. <td valign="middle" align="left">[<a href="Function-Index.html#Function-Index" title="Index">Index</a>]</td>
  317. <td valign="middle" align="left">[<a href="avram_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
  318. </tr></table>
  319. <p>
  320. <font size="-1">
  321. This document was generated on <i>December 10, 2012</i> using <a href="http://www.nongnu.org/texi2html/"><i>texi2html 1.82</i></a>.
  322. </font>
  323. <br>
  324. </p>
  325. </body>
  326. </html>