reentrancy.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <html>
  2. <head>
  3. <title>reentrancy.html</title>
  4. <link rel="stylesheet" type="text/css" href="../styles.css">
  5. </head>
  6. <body>
  7. <h4>
  8. Reentrancy
  9. </h4>
  10. <div>
  11. Macro expansion in the preprocessor is entirely functional.&nbsp; Therefore,
  12. there is no iteration.&nbsp; Unfortunately, the preprocessor also disallows
  13. recursion.&nbsp; This means that the library must fake iteration or recursion
  14. by defining sets of macros that are implemented similarly.&nbsp;
  15. </div>
  16. <div>
  17. To illustrate, here is a simple concatenation macro:
  18. </div>
  19. <div class="code">
  20. <pre>
  21. #define CONCAT(a, b) CONCAT_D(a, b)
  22. #define CONCAT_D(a, b) a ## b
  23. CONCAT(a, CONCAT(b, c)) // abc
  24. </pre>
  25. </div>
  26. <div>
  27. This is fine for a simple case like the above, but what happens in a scenario
  28. like the following:
  29. </div>
  30. <div class="code">
  31. <pre>
  32. #define AB(x, y) CONCAT(x, y)
  33. CONCAT(A, B(p, q)) // CONCAT(p, q)
  34. </pre>
  35. </div>
  36. <div>
  37. Because there is no recursion, the example above expands to <code>CONCAT(p, q)</code>
  38. rather than <code>pq</code>.
  39. </div>
  40. <div>
  41. There are only two ways to "fix" the above.&nbsp; First, it can be documented
  42. that <code>AB</code> uses <code>CONCAT</code> and disallow usage similar to the
  43. above.&nbsp; Second, multiple concatenation macros can be provided....
  44. </div>
  45. <div class="code">
  46. <pre>
  47. #define CONCAT_1(a, b) CONCAT_1_D(a, b)
  48. #define CONCAT_1_D(a, b) a ## b
  49. #define CONCAT_2(a, b) CONCAT_2_D(a, b)
  50. #define CONCAT_2_D(a, b) a ## b
  51. #define AB(x, y) CONCAT_2(x, y)
  52. CONCAT_1(A, B(p, q)) // pq
  53. </pre>
  54. </div>
  55. <div>
  56. This solves the problem.&nbsp; However, it is now necessary to know that <code>AB</code>
  57. uses, not only <i>a</i> concatenation macro, but <code>CONCAT_2</code> specifically.
  58. </div>
  59. <div>
  60. A better solution is to abstract <i>which</i> concatenation macro is used....
  61. </div>
  62. <div class="code">
  63. <pre>
  64. #define AB(c, x, y) CONCAT_ ## c(x, y)
  65. CONCAT_1(A, B(2, p, q)) // pq
  66. </pre>
  67. </div>
  68. <div>
  69. This is an example of <i>generic reentrance</i>, in this case, into a fictional
  70. set of concatenation macros.&nbsp; The <code>c</code> parameter represents the
  71. "state" of the concatenation construct, and as long as the user keeps track of
  72. this state, <code>AB</code> can be used inside of a concatenation macro.
  73. </div>
  74. <div>
  75. The library has the same choices.&nbsp; It either has to disallow a construct
  76. being inside itself or provide multiple, equivalent definitions of a construct
  77. and provide a uniform way to <i>reenter</i> that construct.&nbsp; There are
  78. several contructs that <i>require</i> recursion (such as <b>BOOST_PP_WHILE</b>).&nbsp;
  79. Consequently, the library chooses to provide several sets of macros with
  80. mechanisms to reenter the set at a macro that has not already been used.
  81. </div>
  82. <div>
  83. In particular, the library must provide reentrance for <b>BOOST_PP_FOR</b>, <b>BOOST_PP_REPEAT</b>,
  84. and <b>BOOST_PP_WHILE</b>.&nbsp; There are two mechanisms that are used to
  85. accomplish this:&nbsp; state parameters (like the above concatenation example)
  86. and <i>automatic recursion</i>.
  87. </div>
  88. <h4>
  89. State Parameters
  90. </h4>
  91. <div>
  92. Each of the above constructs (<b>BOOST_PP_FOR</b>, <b>BOOST_PP_REPEAT</b>, and <b>BOOST_PP_WHILE</b>)
  93. has an associated state.&nbsp; This state provides the means to reenter the
  94. respective construct.
  95. </div>
  96. <div>
  97. Several user-defined macros are passed to each of these constructs (for use as
  98. predicates, operations, etc.).&nbsp; Every time a user-defined macro is
  99. invoked, it is passed the current state of the construct that invoked it so
  100. that the macro can reenter the respective set if necessary.
  101. </div>
  102. <div>
  103. These states are used in one of two ways--either by concatenating to or passing
  104. to another macro.
  105. </div>
  106. <div>
  107. There are three types of macros that use these state parameters.&nbsp; First,
  108. the set itself which is reentered through concatenation.&nbsp; Second,
  109. corresponding sets that act like they are a part of the the primary set.&nbsp;
  110. These are also reentered through concatenation.&nbsp; And third, macros that
  111. internally use the first or second type of macro.&nbsp; These macros take the
  112. state as an additional argument.
  113. </div>
  114. <div>
  115. The state of <b>BOOST_PP_WHILE</b> is symbolized by the letter <i>D</i>.&nbsp;
  116. Two user-defined macros are passed to <b>BOOST_PP_WHILE</b>--a predicate and an
  117. operation.&nbsp; When <b>BOOST_PP_WHILE</b> expands these macros, it passes
  118. along its state so that these macros can reenter the <b>BOOST_PP_WHILE</b> set.&nbsp;
  119. </div>
  120. <div>
  121. Consider the following multiplication implementation that illustrates this
  122. technique:
  123. </div>
  124. <div class="code">
  125. <pre>
  126. // The addition interface macro.
  127. // The _D signifies that it reenters
  128. // BOOST_PP_WHILE with concatenation.
  129. #define ADD_D(d, x, y) \
  130. BOOST_PP_TUPLE_ELEM( \
  131. 2, 0, \
  132. BOOST_PP_WHILE_ ## d(ADD_P, ADD_O, (x, y)) \
  133. ) \
  134. /**/
  135. // The predicate that is passed to BOOST_PP_WHILE.
  136. // It returns "true" until "y" becomes zero.
  137. #define ADD_P(d, xy) BOOST_PP_TUPLE_ELEM(2, 1, xy)
  138. // The operation that is passed to BOOST_PP_WHILE.
  139. // It increments "x" and decrements "y" which will
  140. // eventually cause "y" to equal zero and therefore
  141. // cause the predicate to return "false."
  142. #define ADD_O(d, xy) \
  143. ( \
  144. BOOST_PP_INC( \
  145. BOOST_PP_TUPLE_ELEM(2, 0, xy) \
  146. ), \
  147. BOOST_PP_DEC( \
  148. BOOST_PP_TUPLE_ELEM(2, 1, xy) \
  149. ) \
  150. ) \
  151. /**/
  152. // The multiplication interface macro.
  153. #define MUL(x, y) \
  154. BOOST_PP_TUPLE_ELEM( \
  155. 3, 0, \
  156. BOOST_PP_WHILE(MUL_P, MUL_O, (0, x, y)) \
  157. ) \
  158. /**/
  159. // The predicate that is passed to BOOST_PP_WHILE.
  160. // It returns "true" until "y" becomes zero.
  161. #define MUL_P(d, rxy) BOOST_PP_TUPLE_ELEM(3, 2, rxy)
  162. // The operation that is passed to BOOST_PP_WHILE.
  163. // It adds "x" to "r" and decrements "y" which will
  164. // eventually cause "y" to equal zero and therefore
  165. // cause the predicate to return "false."
  166. #define MUL_O(d, rxy) \
  167. ( \
  168. ADD_D( \
  169. d, /* pass the state on to ADD_D */ \
  170. BOOST_PP_TUPLE_ELEM(3, 0, rxy), \
  171. BOOST_PP_TUPLE_ELEM(3, 1, rxy) \
  172. ), \
  173. BOOST_PP_TUPLE_ELEM(3, 1, rxy), \
  174. BOOST_PP_DEC( \
  175. BOOST_PP_TUPLE_ELEM(3, 2, rxy) \
  176. ) \
  177. ) \
  178. /**/
  179. MUL(3, 2) // expands to 6
  180. </pre>
  181. </div>
  182. <div>
  183. There are a couple things to note in the above implementation.&nbsp; First,
  184. note how <code>ADD_D</code> reenters <b>BOOST_PP_WHILE</b> using the <i>d</i> state
  185. parameter.&nbsp; Second, note how <code>MUL</code>'s operation, which is
  186. expanded by <b>BOOST_PP_WHILE</b>, passes the state on to <code>ADD_D</code>.&nbsp;
  187. This illustrates state reentrance by both argument and concatenation.
  188. </div>
  189. <div>
  190. For every macro in the library that uses <b>BOOST_PP_WHILE</b>, there is a
  191. state reentrant variant.&nbsp; If that variant uses an argument rather than
  192. concatenation, it is suffixed by <code>_D</code> to symbolize its method of
  193. reentrance.&nbsp; Examples or this include the library's own <b>BOOST_PP_ADD_D</b>
  194. and <b>BOOST_PP_MUL_D</b>.&nbsp; If the variant uses concatenation, it is
  195. suffixed by an underscore.&nbsp; It is completed by concatenation of the
  196. state.&nbsp; This includes <b>BOOST_PP_WHILE</b> itself with <b>BOOST_PP_WHILE_</b>
  197. ## <i>d</i> and, for example, <b>BOOST_PP_LIST_FOLD_LEFT</b> with <b>BOOST_PP_LIST_FOLD_LEFT_</b>
  198. ## <i>d</i>.
  199. </div>
  200. <div>
  201. The same set of conventions are used for <b>BOOST_PP_FOR</b> and <b>BOOST_PP_REPEAT</b>,
  202. but with the letters <i>R</i> and <i>Z</i>, respectively, to symbolize their
  203. states.
  204. </div>
  205. <div>
  206. Also note that the above <code>MUL</code> implementation, though not
  207. immediately obvious, is using <i>all three</i> types of reentrance.&nbsp; Not
  208. only is it using both types of <i>state</i> reentrance, it is also using <i>automatic
  209. recursion</i>....
  210. </div>
  211. <h4>
  212. Automatic Recursion
  213. </h4>
  214. <div>
  215. Automatic recursion is a technique that vastly simplifies the use of reentrant
  216. constructs.&nbsp; It is used by simply <i>not</i> using any state parameters at
  217. all.
  218. </div>
  219. <div>
  220. The <code>MUL</code> example above uses automatic recursion when it uses <b>BOOST_PP_WHILE</b>
  221. by itself.&nbsp; In other words, <code>MUL</code> can <i>still</i> be used
  222. inside <b>BOOST_PP_WHILE</b> even though it doesn't reenter <b>BOOST_PP_WHILE</b>
  223. by concatenating the state to <b>BOOST_PP_WHILE_</b>.
  224. </div>
  225. <div>
  226. To accomplish this, the library uses a "trick."&nbsp; Despite what it looks
  227. like, the macro <b>BOOST_PP_WHILE</b> does not take three arguments.&nbsp; In
  228. fact, it takes no arguments at all.&nbsp; Instead, the <b>BOOST_PP_WHILE</b> macro
  229. expands <i>to</i> a macro that takes three arguments.&nbsp; It simply detects
  230. what the next available <b>BOOST_PP_WHILE_</b> ## <i>d</i> macro is and returns
  231. it.&nbsp; This detection process is somewhat involved, so I won't go into <i>how</i>
  232. it works here, but suffice to say it <i>does</i> work.
  233. </div>
  234. <div>
  235. Using automatic recursion to reenter various sets of macros is obviously much
  236. simpler.&nbsp; It completely hides the underlying implementation details.&nbsp;
  237. So, if it is so much easier to use, why do the state parameters still
  238. exist?&nbsp; The reason is simple as well.&nbsp; When state parameters are
  239. used, the state is <i>known</i> at all times.&nbsp; This is not the case when
  240. automatic recursion is used.&nbsp; The automatic recursion mechanism has to <i>deduce</i>
  241. the state at each point that it is used.&nbsp; This implies a cost in macro
  242. complexity that in some situations--notably at deep macro depths--will slow
  243. some preprocessors to a crawl.
  244. </div>
  245. <h4>
  246. Conclusion
  247. </h4>
  248. <div>
  249. It is really a tradeoff whether to use state parameters or automatic recursion
  250. for reentrancy.&nbsp; The strengths of automatic recursion are ease of use and
  251. implementation encapsulation.&nbsp; These come at a performance cost on some
  252. preprocessors in some situations.&nbsp; The primary strength of state
  253. parameters, on the other hand, is efficiency.&nbsp; Use of the state parameters
  254. is the only way to achieve <i>maximum</i> efficiency.&nbsp; This efficiency
  255. comes at the cost of both code complexity and exposition of implementation.
  256. </div>
  257. <h4>
  258. See Also
  259. </h4>
  260. <ul>
  261. <li><a href="../ref/for.html">BOOST_PP_FOR</a></li>
  262. <li><a href="../ref/repeat.html">BOOST_PP_REPEAT</a></li>
  263. <li><a href="../ref/while.html">BOOST_PP_WHILE</a></li>
  264. </ul>
  265. <div class="sig">
  266. - Paul Mensonides
  267. </div>
  268. <hr size="1">
  269. <div style="margin-left: 0px;">
  270. <i>© Copyright <a href="http://www.housemarque.com" target="_top">Housemarque Oy</a> 2002</i>
  271. </br><i>© Copyright Paul Mensonides 2002</i>
  272. </div>
  273. <div style="margin-left: 0px;">
  274. <p><small>Distributed under the Boost Software License, Version 1.0. (See
  275. accompanying file <a href="../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
  276. copy at <a href=
  277. "http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>)</small></p>
  278. </div>
  279. </body>
  280. </html>