You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Files.html 6.7KB

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