fd_array.hh (12697:cd71b966be1e) fd_array.hh (13029:75abda747dc3)
1/*
2 * Copyright (c) 2016 Advanced Micro Devices, Inc.
3 * All rights reserved.
4 *
5 * For use for simulation and test purposes only
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:

--- 30 unchanged lines hidden (view full) ---

39#include <array>
40#include <memory>
41#include <string>
42
43#include "sim/fd_entry.hh"
44
45class FDArray
46{
1/*
2 * Copyright (c) 2016 Advanced Micro Devices, Inc.
3 * All rights reserved.
4 *
5 * For use for simulation and test purposes only
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:

--- 30 unchanged lines hidden (view full) ---

39#include <array>
40#include <memory>
41#include <string>
42
43#include "sim/fd_entry.hh"
44
45class FDArray
46{
47 private:
48 static const int NUM_FDS = 1024;
49
50 public:
51 /**
52 * Initialize the file descriptor array and set the standard file
53 * descriptors to defaults or values passed in with the process
54 * params.
55 * @param input Used to initialize the stdin file descriptor
56 * @param output Used to initialize the stdout file descriptor
57 * @param errout Used to initialize the stderr file descriptor
58 */
59 FDArray(std::string const& input, std::string const& output,
60 std::string const& errout);
61
47 public:
48 /**
49 * Initialize the file descriptor array and set the standard file
50 * descriptors to defaults or values passed in with the process
51 * params.
52 * @param input Used to initialize the stdin file descriptor
53 * @param output Used to initialize the stdout file descriptor
54 * @param errout Used to initialize the stderr file descriptor
55 */
56 FDArray(std::string const& input, std::string const& output,
57 std::string const& errout);
58
62 std::string _input;
63 std::string _output;
64 std::string _errout;
65
66 /**
67 * Figure out the file offsets for all currently open files and save them
68 * the offsets during the calls to drain by the owning process.
69 */
70 void updateFileOffsets();
71
72 /**
73 * Restore all offsets for currently open files during the unserialize
74 * phase for the owning process class.
75 */
76 void restoreFileOffsets();
77
78 /**
59 /**
60 * Figure out the file offsets for all currently open files and save them
61 * the offsets during the calls to drain by the owning process.
62 */
63 void updateFileOffsets();
64
65 /**
66 * Restore all offsets for currently open files during the unserialize
67 * phase for the owning process class.
68 */
69 void restoreFileOffsets();
70
71 /**
72 * Put the pointer specified by fdep into the _fdArray entry indexed
73 * by tgt_fd.
74 * @param tgt_fd Use target file descriptors to index the array.
75 * @param fdep Incoming pointer used to set the entry pointed to by tgt_fd.
76 */
77 void setFDEntry(int tgt_fd, std::shared_ptr<FDEntry> fdep);
78
79 /**
79 * Treat this object like a normal array in using the subscript operator
80 * to pull entries out of it.
81 * @param tgt_fd Use target file descriptors to index the array.
82 */
80 * Treat this object like a normal array in using the subscript operator
81 * to pull entries out of it.
82 * @param tgt_fd Use target file descriptors to index the array.
83 */
83 inline std::shared_ptr<FDEntry>
84 std::shared_ptr
84 operator[](int tgt_fd)
85 {
86 return getFDEntry(tgt_fd);
87 }
88
89 /**
85 operator[](int tgt_fd)
86 {
87 return getFDEntry(tgt_fd);
88 }
89
90 /**
91 * Return the size of the _fdArray field
92 */
93 int getSize() const { return _fdArray.size(); }
94
95 /**
90 * Step through the file descriptor array and find the first available
91 * entry which is denoted as being free by being a 'nullptr'. That file
92 * descriptor entry is the new target file descriptor entry that we
93 * return as the return parameter.
94 * @param fdp Allocated beforehand and passed into this method;
95 * the fdp is meant to be a generic pointer capable of pointing to
96 * different types of file descriptors. Must cast the pointer to the
97 * correct type before dereferencing to access the needed fields.
98 */
99 int allocFD(std::shared_ptr<FDEntry> fdp);
100
101 /**
96 * Step through the file descriptor array and find the first available
97 * entry which is denoted as being free by being a 'nullptr'. That file
98 * descriptor entry is the new target file descriptor entry that we
99 * return as the return parameter.
100 * @param fdp Allocated beforehand and passed into this method;
101 * the fdp is meant to be a generic pointer capable of pointing to
102 * different types of file descriptors. Must cast the pointer to the
103 * correct type before dereferencing to access the needed fields.
104 */
105 int allocFD(std::shared_ptr<FDEntry> fdp);
106
107 /**
102 * Return the size of the _fdArray field
103 */
104 int getSize() const { return _fdArray.size(); }
105
106 /**
107 * Put the pointer specified by fdep into the _fdArray entry indexed
108 * by tgt_fd.
109 * @param tgt_fd Use target file descriptors to index the array.
110 * @param fdep Incoming pointer used to set the entry pointed to by tgt_fd.
111 */
112 void setFDEntry(int tgt_fd, std::shared_ptr<FDEntry> fdep);
113
114 /**
115 * Try to close the host file descriptor. If successful, set the
116 * specified file descriptor entry object pointer to nullptr.
117 * Used to "close" the target file descriptor.
118 * @param tgt_fd Use target file descriptors to index the array.
119 */
120 int closeFDEntry(int tgt_fd);
121
122 private:

--- 13 unchanged lines hidden (view full) ---

136 * @param tgt_fd Use target file descriptors to index the array.
137 */
138 std::shared_ptr<FDEntry> getFDEntry(int tgt_fd);
139
140 /**
141 * Hold pointers to the file descriptor entries. The array size is
142 * statically defined by the operating system.
143 */
108 * Try to close the host file descriptor. If successful, set the
109 * specified file descriptor entry object pointer to nullptr.
110 * Used to "close" the target file descriptor.
111 * @param tgt_fd Use target file descriptors to index the array.
112 */
113 int closeFDEntry(int tgt_fd);
114
115 private:

--- 13 unchanged lines hidden (view full) ---

129 * @param tgt_fd Use target file descriptors to index the array.
130 */
131 std::shared_ptr<FDEntry> getFDEntry(int tgt_fd);
132
133 /**
134 * Hold pointers to the file descriptor entries. The array size is
135 * statically defined by the operating system.
136 */
144 std::array<std::shared_ptr<FDEntry>, NUM_FDS> _fdArray;
137 static constexpr size_t _numFDs {1024};
138 std::array<std::shared_ptr<FDEntry>, _numFDs> _fdArray;
145
146 /**
139
140 /**
141 * Hold param strings passed from the Process class which indicate
142 * the filename for each of the corresponding files or some keyword
143 * indicating the use of standard file descriptors.
144 */
145 std::string _input;
146 std::string _output;
147 std::string _errout;
148
149 /**
147 * Hold strings which represent the default values which are checked
148 * against to initialize the standard file descriptors. If the string
149 * provided doesn't hit against these maps, then a file is opened on the
150 * host instead of using the host's standard file descriptors.
151 */
150 * Hold strings which represent the default values which are checked
151 * against to initialize the standard file descriptors. If the string
152 * provided doesn't hit against these maps, then a file is opened on the
153 * host instead of using the host's standard file descriptors.
154 */
152 std::map imap;
153 std::map oemap;
155 std::map<std::string, int> _imap;
156 std::map<std::string, int> _oemap;
154};
155
156#endif // __FD_ARRAY_HH__
157};
158
159#endif // __FD_ARRAY_HH__