descriptors.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // descriptors.h -- manage file descriptors for gold -*- C++ -*-
  2. // Copyright (C) 2008-2015 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #ifndef GOLD_DESCRIPTORS_H
  18. #define GOLD_DESCRIPTORS_H
  19. #include <vector>
  20. #include "gold-threads.h"
  21. namespace gold
  22. {
  23. // This class manages file descriptors for gold.
  24. class Descriptors
  25. {
  26. public:
  27. Descriptors();
  28. // Get a file descriptor for a file. The DESCRIPTOR parameter is
  29. // the descriptor the last time the file was used; this will be -1
  30. // if this is the first time the file is being opened. The NAME,
  31. // FLAGS, and MODE parameters are as for ::open. NAME must be in
  32. // permanent storage. This returns the descriptor to use, which may
  33. // or may not be the same as DESCRIPTOR. If there is an error
  34. // opening the file, this will return -1 with errno set
  35. // appropriately.
  36. int
  37. open(int descriptor, const char* name, int flags, int mode = 0);
  38. // Release the file descriptor DESCRIPTOR. If PERMANENT is true, it
  39. // will be closed, and the caller may not reopen it. If PERMANENT
  40. // is false this doesn't necessarily close the descriptor, but it
  41. // makes it available to be closed; the descriptor must not be used
  42. // again except as an argument to Descriptor::open.
  43. void
  44. release(int descriptor, bool permanent);
  45. // Close all the descriptors open for reading.
  46. void
  47. close_all();
  48. private:
  49. // Information kept for a descriptor.
  50. struct Open_descriptor
  51. {
  52. // File name currently associated with descriptor. This is empty
  53. // if none.
  54. const char* name;
  55. // Index of next descriptor on stack of released descriptors.
  56. int stack_next;
  57. // Whether the descriptor is currently in use.
  58. bool inuse;
  59. // Whether this is a write descriptor.
  60. bool is_write;
  61. // Whether the descriptor is on the stack.
  62. bool is_on_stack;
  63. };
  64. bool
  65. close_some_descriptor();
  66. // We need to lock before accessing any fields.
  67. Lock* lock_;
  68. // Used to initialize the lock_ field exactly once.
  69. Initialize_lock initialize_lock_;
  70. // Information for descriptors.
  71. std::vector<Open_descriptor> open_descriptors_;
  72. // Top of stack.
  73. int stack_top_;
  74. // The current number of file descriptors open.
  75. int current_;
  76. // The maximum number of file descriptors we open.
  77. int limit_;
  78. };
  79. // File descriptors are a centralized data structure, and we use a
  80. // global variable rather than passing the data structure into every
  81. // routine that does file I/O.
  82. extern Descriptors descriptors;
  83. inline int
  84. open_descriptor(int descriptor, const char* name, int flags, int mode = 0)
  85. { return descriptors.open(descriptor, name, flags, mode); }
  86. inline void
  87. release_descriptor(int descriptor, bool permanent)
  88. { descriptors.release(descriptor, permanent); }
  89. inline void
  90. close_all_descriptors()
  91. { descriptors.close_all(); }
  92. } // End namespace gold.
  93. #endif // !defined(GOLD_DESCRIPTORS_H)