Files.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  4. <head>
  5. <title>The GNU C Preprocessor Internals: Files</title>
  6. <meta name="description" content="The GNU C Preprocessor Internals: Files">
  7. <meta name="keywords" content="The GNU C Preprocessor Internals: Files">
  8. <meta name="resource-type" content="document">
  9. <meta name="distribution" content="global">
  10. <meta name="Generator" content="makeinfo">
  11. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  12. <link href="index.html#Top" rel="start" title="Top">
  13. <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
  14. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  15. <link href="index.html#Top" rel="up" title="Top">
  16. <link href="Concept-Index.html#Concept-Index" rel="next" title="Concept Index">
  17. <link href="Guard-Macros.html#Guard-Macros" rel="prev" title="Guard Macros">
  18. <style type="text/css">
  19. <!--
  20. a.summary-letter {text-decoration: none}
  21. blockquote.smallquotation {font-size: smaller}
  22. div.display {margin-left: 3.2em}
  23. div.example {margin-left: 3.2em}
  24. div.indentedblock {margin-left: 3.2em}
  25. div.lisp {margin-left: 3.2em}
  26. div.smalldisplay {margin-left: 3.2em}
  27. div.smallexample {margin-left: 3.2em}
  28. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  29. div.smalllisp {margin-left: 3.2em}
  30. kbd {font-style:oblique}
  31. pre.display {font-family: inherit}
  32. pre.format {font-family: inherit}
  33. pre.menu-comment {font-family: serif}
  34. pre.menu-preformatted {font-family: serif}
  35. pre.smalldisplay {font-family: inherit; font-size: smaller}
  36. pre.smallexample {font-size: smaller}
  37. pre.smallformat {font-family: inherit; font-size: smaller}
  38. pre.smalllisp {font-size: smaller}
  39. span.nocodebreak {white-space:nowrap}
  40. span.nolinebreak {white-space:nowrap}
  41. span.roman {font-family:serif; font-weight:normal}
  42. span.sansserif {font-family:sans-serif; font-weight:normal}
  43. ul.no-bullet {list-style: none}
  44. -->
  45. </style>
  46. </head>
  47. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  48. <a name="Files"></a>
  49. <div class="header">
  50. <p>
  51. Next: <a href="Concept-Index.html#Concept-Index" accesskey="n" rel="next">Concept Index</a>, Previous: <a href="Guard-Macros.html#Guard-Macros" accesskey="p" rel="prev">Guard Macros</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  52. </div>
  53. <hr>
  54. <a name="File-Handling"></a>
  55. <h2 class="unnumbered">File Handling</h2>
  56. <a name="index-files"></a>
  57. <p>Fairly obviously, the file handling code of cpplib resides in the file
  58. <samp>files.c</samp>. It takes care of the details of file searching,
  59. opening, reading and caching, for both the main source file and all the
  60. headers it recursively includes.
  61. </p>
  62. <p>The basic strategy is to minimize the number of system calls. On many
  63. systems, the basic <code>open ()</code> and <code>fstat ()</code> system calls can
  64. be quite expensive. For every <code>#include</code>-d file, we need to try
  65. all the directories in the search path until we find a match. Some
  66. projects, such as glibc, pass twenty or thirty include paths on the
  67. command line, so this can rapidly become time consuming.
  68. </p>
  69. <p>For a header file we have not encountered before we have little choice
  70. but to do this. However, it is often the case that the same headers are
  71. repeatedly included, and in these cases we try to avoid repeating the
  72. filesystem queries whilst searching for the correct file.
  73. </p>
  74. <p>For each file we try to open, we store the constructed path in a splay
  75. tree. This path first undergoes simplification by the function
  76. <code>_cpp_simplify_pathname</code>. For example,
  77. <samp>/usr/include/bits/../foo.h</samp> is simplified to
  78. <samp>/usr/include/foo.h</samp> before we enter it in the splay tree and try
  79. to <code>open ()</code> the file. CPP will then find subsequent uses of
  80. <samp>foo.h</samp>, even as <samp>/usr/include/foo.h</samp>, in the splay tree and
  81. save system calls.
  82. </p>
  83. <p>Further, it is likely the file contents have also been cached, saving a
  84. <code>read ()</code> system call. We don&rsquo;t bother caching the contents of
  85. header files that are re-inclusion protected, and whose re-inclusion
  86. macro is defined when we leave the header file for the first time. If
  87. the host supports it, we try to map suitably large files into memory,
  88. rather than reading them in directly.
  89. </p>
  90. <p>The include paths are internally stored on a null-terminated
  91. singly-linked list, starting with the <code>&quot;header.h&quot;</code> directory search
  92. chain, which then links into the <code>&lt;header.h&gt;</code> directory chain.
  93. </p>
  94. <p>Files included with the <code>&lt;foo.h&gt;</code> syntax start the lookup directly
  95. in the second half of this chain. However, files included with the
  96. <code>&quot;foo.h&quot;</code> syntax start at the beginning of the chain, but with one
  97. extra directory prepended. This is the directory of the current file;
  98. the one containing the <code>#include</code> directive. Prepending this
  99. directory on a per-file basis is handled by the function
  100. <code>search_from</code>.
  101. </p>
  102. <p>Note that a header included with a directory component, such as
  103. <code>#include &quot;mydir/foo.h&quot;</code> and opened as
  104. <samp>/usr/local/include/mydir/foo.h</samp>, will have the complete path minus
  105. the basename &lsquo;<samp>foo.h</samp>&rsquo; as the current directory.
  106. </p>
  107. <p>Enough information is stored in the splay tree that CPP can immediately
  108. tell whether it can skip the header file because of the multiple include
  109. optimization, whether the file didn&rsquo;t exist or couldn&rsquo;t be opened for
  110. some reason, or whether the header was flagged not to be re-used, as it
  111. is with the obsolete <code>#import</code> directive.
  112. </p>
  113. <p>For the benefit of MS-DOS filesystems with an 8.3 filename limitation,
  114. CPP offers the ability to treat various include file names as aliases
  115. for the real header files with shorter names. The map from one to the
  116. other is found in a special file called &lsquo;<samp>header.gcc</samp>&rsquo;, stored in the
  117. command line (or system) include directories to which the mapping
  118. applies. This may be higher up the directory tree than the full path to
  119. the file minus the base name.
  120. </p>
  121. <hr>
  122. <div class="header">
  123. <p>
  124. Next: <a href="Concept-Index.html#Concept-Index" accesskey="n" rel="next">Concept Index</a>, Previous: <a href="Guard-Macros.html#Guard-Macros" accesskey="p" rel="prev">Guard Macros</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  125. </div>
  126. </body>
  127. </html>